Download
Getting Started
Members
Projects
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
More
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
Toggle navigation
Bugzilla – Attachment 45029 Details for
Bug 148089
Trac connector: implement XML-RPC core
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
Log In
[x]
|
Terms of Use
|
Copyright Agent
Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read
this important communication.
[patch]
XML-RPC core classes and test classes
mylar-trac-xmlrpc-core-patch.txt (text/plain), 33.57 KB, created by
Steffen Pingel
on 2006-06-21 15:35:47 EDT
(
hide
)
Description:
XML-RPC core classes and test classes
Filename:
MIME Type:
Creator:
Steffen Pingel
Created:
2006-06-21 15:35:47 EDT
Size:
33.57 KB
patch
obsolete
>### Eclipse Workspace Patch 1.0 >#P org.eclipse.mylar.trac.tests >Index: src/org/eclipse/mylar/trac/tests/TracXmlRpcRepositoryTest.java >=================================================================== >RCS file: src/org/eclipse/mylar/trac/tests/TracXmlRpcRepositoryTest.java >diff -N src/org/eclipse/mylar/trac/tests/TracXmlRpcRepositoryTest.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/mylar/trac/tests/TracXmlRpcRepositoryTest.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,54 @@ >+/******************************************************************************* >+ * Copyright (c) 2006 - 2006 Mylar eclipse.org project and others. >+ * All rights reserved. This program and the accompanying materials >+ * are made available under the terms of the Eclipse Public License v1.0 >+ * which accompanies this distribution, and is available at >+ * http://www.eclipse.org/legal/epl-v10.html >+ * >+ * Contributors: >+ * Mylar project committers - initial API and implementation >+ *******************************************************************************/ >+ >+package org.eclipse.mylar.trac.tests; >+ >+import java.net.URL; >+ >+import org.eclipse.mylar.internal.trac.core.ITracRepository; >+import org.eclipse.mylar.internal.trac.core.TracLoginException; >+import org.eclipse.mylar.internal.trac.core.TracRemoteException; >+import org.eclipse.mylar.internal.trac.core.TracXmlRpcRepository; >+import org.eclipse.mylar.internal.trac.core.ITracRepository.Version; >+import org.eclipse.mylar.trac.tests.support.AbstractTracRepositoryFactory; >+ >+/** >+ * @author Steffen Pingel >+ */ >+public class TracXmlRpcRepositoryTest extends AbstractTracRepositoryTest { >+ >+ public TracXmlRpcRepositoryTest() { >+ super(new AbstractTracRepositoryFactory() { >+ protected ITracRepository createRepository(String url, String username, String password) throws Exception { >+ return new TracXmlRpcRepository(new URL(url), Version.XML_RPC, username, password); >+ } >+ }); >+ } >+ >+ public void testValidateFailNoAuth() throws Exception { >+ factory.connect(Constants.TEST_REPOSITORY1_URL, "", ""); >+ try { >+ factory.repository.validate(); >+ fail("Expected TracLoginException"); >+ } catch (TracLoginException e) { >+ } >+ } >+ >+ public void testMulticallExceptions() throws Exception { >+ factory.connectRepository1(); >+ try { >+ ((TracXmlRpcRepository) factory.repository).getTickets(new int[] { 1, Integer.MAX_VALUE }); >+ fail("Expected TracRemoteException"); >+ } catch (TracRemoteException e) { >+ } >+ } >+ >+} >Index: src/org/eclipse/mylar/trac/tests/AbstractTracRepositorySearchTest.java >=================================================================== >RCS file: src/org/eclipse/mylar/trac/tests/AbstractTracRepositorySearchTest.java >diff -N src/org/eclipse/mylar/trac/tests/AbstractTracRepositorySearchTest.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/mylar/trac/tests/AbstractTracRepositorySearchTest.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,164 @@ >+/******************************************************************************* >+ * Copyright (c) 2006 - 2006 Mylar eclipse.org project and others. >+ * All rights reserved. This program and the accompanying materials >+ * are made available under the terms of the Eclipse Public License v1.0 >+ * which accompanies this distribution, and is available at >+ * http://www.eclipse.org/legal/epl-v10.html >+ * >+ * Contributors: >+ * Mylar project committers - initial API and implementation >+ *******************************************************************************/ >+ >+package org.eclipse.mylar.trac.tests; >+ >+import java.util.ArrayList; >+import java.util.Hashtable; >+import java.util.List; >+import java.util.Map; >+ >+import junit.framework.TestCase; >+ >+import org.eclipse.mylar.internal.trac.core.TracException; >+import org.eclipse.mylar.internal.trac.model.TracSearch; >+import org.eclipse.mylar.internal.trac.model.TracTicket; >+import org.eclipse.mylar.internal.trac.model.TracTicket.Key; >+import org.eclipse.mylar.trac.tests.support.AbstractTracRepositoryFactory; >+import org.eclipse.mylar.trac.tests.support.XmlRpcServer; >+import org.eclipse.mylar.trac.tests.support.XmlRpcServer.Ticket; >+ >+/** >+ * Test cases that validate search results for classes that implement >+ * {@link ITracRepositor}. >+ * >+ * @author Steffen Pingel >+ */ >+public abstract class AbstractTracRepositorySearchTest extends TestCase { >+ >+ protected XmlRpcServer server; >+ >+ protected AbstractTracRepositoryFactory factory; >+ >+ protected static List<Ticket> tickets; >+ >+ private static boolean setupRun = false; >+ >+ public AbstractTracRepositorySearchTest(AbstractTracRepositoryFactory factory) { >+ this.factory = factory; >+ } >+ >+ protected void setUp() throws Exception { >+ super.setUp(); >+ >+ server = new XmlRpcServer(Constants.TEST_REPOSITORY1_URL, Constants.TEST_REPOSITORY1_ADMIN_USERNAME, >+ Constants.TEST_REPOSITORY1_ADMIN_PASSWORD); >+ >+ if (!setupRun) { >+ tickets = new ArrayList<Ticket>(); >+ >+ server.ticket().deleteAll(); >+ >+ server.ticketMilestone("m1").deleteAndCreate(); >+ Ticket ticket = add(server.ticket().create("summary1", "description1")); >+ ticket.update("comment", "milestone", "m1"); >+ >+ server.ticketMilestone("m2").deleteAndCreate(); >+ ticket = add(server.ticket().create("summary2", "description2")); >+ ticket.update("comment", "milestone", "m2"); >+ ticket = add(server.ticket().create("summary3", "description3")); >+ ticket.update("comment", "milestone", "m2"); >+ >+ ticket = add(server.ticket().create("summary4", "description4")); >+ >+ setupRun = true; >+ } >+ >+ factory.connectRepository1(); >+ } >+ >+ protected void tearDown() throws Exception { >+ super.tearDown(); >+ >+ // server.getFixture().cleanup(); >+ } >+ >+ protected Ticket add(Ticket ticket) { >+ tickets.add(ticket); >+ return ticket; >+ } >+ >+ protected void assertTicketEquals(Ticket ticket, TracTicket tracTicket) throws Exception { >+ assertTrue(tracTicket.isValid()); >+ >+ Hashtable expectedValues = ticket.getValues(); >+ Map<String, String> values = tracTicket.getValues(); >+ for (String key : values.keySet()) { >+ assertEquals("Values for key '" + key + "' did not match", expectedValues.get(key), values.get(key)); >+ } >+ } >+ >+ public void testGetTicket() throws Exception { >+ TracTicket ticket = factory.repository.getTicket(tickets.get(0).getId()); >+ assertTicketEquals(tickets.get(0), ticket); >+ >+ ticket = factory.repository.getTicket(tickets.get(1).getId()); >+ assertTicketEquals(tickets.get(1), ticket); >+ } >+ >+ public void testGetTicketInvalidId() throws Exception { >+ try { >+ factory.repository.getTicket(Integer.MAX_VALUE); >+ fail("Expected TracException"); >+ } catch (TracException e) { >+ } >+ } >+ >+ public void testSearchAll() throws Exception { >+ TracSearch search = new TracSearch(); >+ List<TracTicket> result = new ArrayList<TracTicket>(); >+ factory.repository.search(search, result); >+ assertEquals(tickets.size(), result.size()); >+ } >+ >+ public void testSearchEmpty() throws Exception { >+ TracSearch search = new TracSearch(); >+ search.addFilter("milestone", "does not exist"); >+ List<TracTicket> result = new ArrayList<TracTicket>(); >+ factory.repository.search(search, result); >+ assertEquals(0, result.size()); >+ } >+ >+ public void testSearchMilestone1() throws Exception { >+ TracSearch search = new TracSearch(); >+ search.addFilter("milestone", "m1"); >+ List<TracTicket> result = new ArrayList<TracTicket>(); >+ factory.repository.search(search, result); >+ assertEquals(1, result.size()); >+ assertTicketEquals(tickets.get(0), result.get(0)); >+ } >+ >+ public void testSearchMilestone2() throws Exception { >+ TracSearch search = new TracSearch(); >+ search.addFilter("milestone", "m1"); >+ search.addFilter("milestone", "m2"); >+ search.setOrderBy("id"); >+ List<TracTicket> result = new ArrayList<TracTicket>(); >+ factory.repository.search(search, result); >+ assertEquals(3, result.size()); >+ assertTicketEquals(tickets.get(0), result.get(0)); >+ assertTicketEquals(tickets.get(1), result.get(1)); >+ assertTicketEquals(tickets.get(2), result.get(2)); >+ } >+ >+ public void testSearchExactMatch() throws Exception { >+ TracSearch search = new TracSearch(); >+ search.addFilter("milestone", "m1"); >+ search.addFilter("summary", "summary1"); >+ List<TracTicket> result = new ArrayList<TracTicket>(); >+ factory.repository.search(search, result); >+ assertEquals(1, result.size()); >+ assertTicketEquals(tickets.get(0), result.get(0)); >+ assertEquals("m1", result.get(0).getValue(Key.MILESTONE)); >+ assertEquals("summary1", result.get(0).getValue(Key.SUMMARY)); >+ } >+ >+} >Index: src/org/eclipse/mylar/trac/tests/TracXmlRpcRepositorySearchTest.java >=================================================================== >RCS file: src/org/eclipse/mylar/trac/tests/TracXmlRpcRepositorySearchTest.java >diff -N src/org/eclipse/mylar/trac/tests/TracXmlRpcRepositorySearchTest.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/mylar/trac/tests/TracXmlRpcRepositorySearchTest.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,54 @@ >+/******************************************************************************* >+ * Copyright (c) 2006 - 2006 Mylar eclipse.org project and others. >+ * All rights reserved. This program and the accompanying materials >+ * are made available under the terms of the Eclipse Public License v1.0 >+ * which accompanies this distribution, and is available at >+ * http://www.eclipse.org/legal/epl-v10.html >+ * >+ * Contributors: >+ * Mylar project committers - initial API and implementation >+ *******************************************************************************/ >+ >+package org.eclipse.mylar.trac.tests; >+ >+import java.net.URL; >+import java.util.ArrayList; >+import java.util.List; >+ >+import org.eclipse.mylar.internal.trac.core.ITracRepository; >+import org.eclipse.mylar.internal.trac.core.TracXmlRpcRepository; >+import org.eclipse.mylar.internal.trac.core.ITracRepository.Version; >+import org.eclipse.mylar.internal.trac.model.TracSearch; >+import org.eclipse.mylar.internal.trac.model.TracTicket; >+import org.eclipse.mylar.internal.trac.model.TracTicket.Key; >+import org.eclipse.mylar.trac.tests.support.AbstractTracRepositoryFactory; >+ >+/** >+ * @author Steffen Pingel >+ */ >+public class TracXmlRpcRepositorySearchTest extends AbstractTracRepositorySearchTest { >+ >+ public TracXmlRpcRepositorySearchTest() { >+ super(new AbstractTracRepositoryFactory() { >+ protected ITracRepository createRepository(String url, String username, String password) throws Exception { >+ return new TracXmlRpcRepository(new URL(url), Version.XML_RPC, username, password); >+ } >+ }); >+ } >+ >+ public void testSearchValidateTicket() throws Exception { >+ TracSearch search = new TracSearch(); >+ search.addFilter("summary", "summary1"); >+ List<TracTicket> result = new ArrayList<TracTicket>(); >+ factory.repository.search(search, result); >+ assertEquals(1, result.size()); >+ assertTicketEquals(tickets.get(0), result.get(0)); >+ assertEquals("component1", result.get(0).getValue(Key.COMPONENT)); >+ assertEquals("description1", result.get(0).getValue(Key.DESCRIPTION)); >+ assertEquals("m1", result.get(0).getValue(Key.MILESTONE)); >+ assertEquals(factory.username, result.get(0).getValue(Key.REPORTER)); >+ assertEquals("summary1", result.get(0).getValue(Key.SUMMARY)); >+ assertEquals("", result.get(0).getValue(Key.VERSION)); >+ } >+ >+} >Index: src/org/eclipse/mylar/trac/tests/support/XmlRpcServer.java >=================================================================== >RCS file: src/org/eclipse/mylar/trac/tests/support/XmlRpcServer.java >diff -N src/org/eclipse/mylar/trac/tests/support/XmlRpcServer.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/mylar/trac/tests/support/XmlRpcServer.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,351 @@ >+/******************************************************************************* >+ * Copyright (c) 2006 - 2006 Mylar eclipse.org project and others. >+ * All rights reserved. This program and the accompanying materials >+ * are made available under the terms of the Eclipse Public License v1.0 >+ * which accompanies this distribution, and is available at >+ * http://www.eclipse.org/legal/epl-v10.html >+ * >+ * Contributors: >+ * Mylar project committers - initial API and implementation >+ *******************************************************************************/ >+ >+package org.eclipse.mylar.trac.tests.support; >+ >+import java.io.IOException; >+import java.net.MalformedURLException; >+import java.net.URL; >+import java.util.ArrayList; >+import java.util.Arrays; >+import java.util.Hashtable; >+import java.util.List; >+import java.util.Vector; >+ >+import org.apache.xmlrpc.XmlRpcClient; >+import org.apache.xmlrpc.XmlRpcException; >+import org.eclipse.mylar.internal.trac.core.TracXmlRpcRepository; >+import org.eclipse.mylar.internal.trac.core.ITracRepository.Version; >+ >+/** >+ * @author Steffen Pingel >+ */ >+public class XmlRpcServer { >+ >+ public abstract class AbstractTracItem { >+ >+ public abstract void delete() throws Exception; >+ >+ protected void itemCreated() { >+ fixture.items.add(this); >+ } >+ >+ protected void itemDeleted() { >+ fixture.items.remove(this); >+ } >+ >+ } >+ >+ /** >+ * Represents a Trac type with multiple attributes such as a milestone. >+ */ >+ public class ModelEnum extends AbstractTracItem { >+ >+ private String[] attributes; >+ >+ private String id; >+ >+ private String module; >+ >+ public ModelEnum(String module, String id, String... attributes) { >+ this.module = module; >+ this.id = id; >+ this.attributes = attributes; >+ } >+ >+ public ModelEnum create(Object... params) throws Exception { >+ call(module + ".create", id, toMap(params)); >+ itemCreated(); >+ return this; >+ } >+ >+ public void delete() throws Exception { >+ call(module + ".delete", id); >+ itemDeleted(); >+ } >+ >+ public void deleteAll() throws Exception { >+ String[] ids = getAll(); >+ for (String id : ids) { >+ call(module + ".delete", id); >+ } >+ } >+ >+ public ModelEnum deleteAndCreate(Object... params) throws Exception { >+ try { >+ if (Arrays.asList(getAll()).contains(id)) { >+ delete(); >+ } >+ } catch (Exception e) { >+ // ignore >+ } >+ >+ return create(params); >+ } >+ >+ public Object[] get() throws Exception { >+ Hashtable values = (Hashtable) call(module + ".get", id); >+ Object[] result = new Object[values.size()]; >+ for (int i = 0; i < result.length && i < attributes.length; i++) { >+ result[i] = values.get(attributes[i]); >+ } >+ return result; >+ } >+ >+ @SuppressWarnings("unchecked") >+ public String[] getAll() throws Exception { >+ return (String[]) ((Vector) call(module + ".getAll")).toArray(new String[0]); >+ } >+ >+ private Hashtable<String, Object> toMap(Object... params) { >+ Hashtable<String, Object> attrs = new Hashtable<String, Object>(); >+ for (int i = 0; i < attributes.length && i < params.length; i++) { >+ attrs.put((String) attributes[i], params[i]); >+ } >+ return attrs; >+ } >+ >+ public ModelEnum update(Object... params) throws Exception { >+ call(module + ".update", id, toMap(params)); >+ return this; >+ } >+ >+ } >+ >+ /** >+ * Records changes to the repository. >+ */ >+ public class TestFixture { >+ >+ List<AbstractTracItem> items = new ArrayList<AbstractTracItem>(); >+ >+ /** >+ * Undo all changes. >+ */ >+ public void cleanup() throws Exception { >+ while (!items.isEmpty()) { >+ items.get(0).delete(); >+ } >+ } >+ >+ } >+ >+ /** >+ * Represents a Trac ticket. >+ */ >+ public class Ticket extends AbstractTracItem { >+ >+ private Integer id; >+ >+ public Ticket(Integer id) { >+ this.id = id; >+ } >+ >+ public Ticket create(String summary, String description) throws Exception { >+ this.id = (Integer) call("ticket.create", summary, description, new Hashtable()); >+ if (id == null) { >+ throw new RuntimeException("Could not create ticket: " + summary); >+ } >+ itemCreated(); >+ return this; >+ } >+ >+ public void delete() throws Exception { >+ call("ticket.delete", id); >+ itemDeleted(); >+ } >+ >+ public void deleteAll() throws Exception { >+ Integer[] ids = getAll(); >+ for (Integer id : ids) { >+ call("ticket.delete", id); >+ } >+ } >+ >+ public Object getValue(String key) throws Exception { >+ return getValues().get(key); >+ } >+ >+ public Hashtable getValues() throws Exception { >+ return (Hashtable) ((Vector) call("ticket.get", id)).get(3); >+ } >+ >+ @SuppressWarnings("unchecked") >+ public Integer[] getAll() throws Exception { >+ return (Integer[]) ((Vector) call("ticket.query", "order=id")).toArray(new Integer[0]); >+ } >+ >+ public int getId() { >+ return id; >+ } >+ >+ public Ticket update(String comment, String key, String value) throws Exception { >+ Hashtable<String, Object> attrs = new Hashtable<String, Object>(); >+ attrs.put(key, value); >+ call("ticket.update", id, comment, attrs); >+ return this; >+ } >+ >+ } >+ >+ /** >+ * Represents a Trac type that has a single attribute such as a priority. >+ */ >+ public class TicketEnum extends AbstractTracItem { >+ >+ private String id; >+ >+ private String module; >+ >+ public TicketEnum(String module, String id) { >+ this.module = module; >+ this.id = id; >+ } >+ >+ public TicketEnum create(String param) throws Exception { >+ call(module + ".create", id, param); >+ itemCreated(); >+ return this; >+ } >+ >+ public void delete() throws Exception { >+ call(module + ".delete", id); >+ itemDeleted(); >+ } >+ >+ public void deleteAll() throws Exception { >+ String[] ids = getAll(); >+ for (String id : ids) { >+ call(module + ".delete", id); >+ } >+ } >+ >+ public TicketEnum deleteAndCreate(String param) throws Exception { >+ try { >+ if (Arrays.asList(getAll()).contains(id)) { >+ delete(); >+ } >+ } catch (Exception e) { >+ // ignore >+ } >+ >+ return create(param); >+ } >+ >+ public String get() throws Exception { >+ return (String) call(module + ".get", id); >+ } >+ >+ @SuppressWarnings("unchecked") >+ public String[] getAll() throws Exception { >+ return (String[]) ((Vector) call(module + ".getAll")).toArray(new String[0]); >+ } >+ >+ public TicketEnum update(String param) throws Exception { >+ call(module + ".update", id, param); >+ return this; >+ } >+ >+ } >+ >+ private XmlRpcClient client; >+ >+ private TestFixture fixture; >+ >+ private String password; >+ >+ private TracXmlRpcRepository repository; >+ >+ private String url; >+ >+ private String username; >+ >+ public XmlRpcServer(String url, String username, String password) throws Exception { >+ this.url = url; >+ this.username = username; >+ this.password = password; >+ >+ this.fixture = new TestFixture(); >+ >+ this.repository = new TracXmlRpcRepository(new URL(url), Version.XML_RPC, username, password); >+ this.client = repository.getClient(); >+ } >+ >+ private Object call(String method, Object... parameters) throws XmlRpcException, IOException { >+ Vector<Object> params = new Vector<Object>(parameters.length); >+ for (Object parameter : parameters) { >+ params.add(parameter); >+ } >+ >+ Object result = client.execute(method, params); >+ if (result instanceof XmlRpcException) { >+ throw (XmlRpcException) result; >+ } >+ return result; >+ } >+ >+ public TestFixture getFixture() { >+ return fixture; >+ } >+ >+ public String getPassword() { >+ return password; >+ } >+ >+ public TracXmlRpcRepository getRepository() throws MalformedURLException { >+ return repository; >+ } >+ >+ public String getUrl() { >+ return url; >+ } >+ >+ public String getUsername() { >+ return username; >+ } >+ >+ public Ticket ticket() throws Exception { >+ return new Ticket(null); >+ } >+ >+ public Ticket ticket(int id) throws Exception { >+ return new Ticket(id); >+ } >+ >+ public ModelEnum ticketComponent(String id) throws Exception { >+ return new ModelEnum("ticket.component", id, "owner", "description"); >+ } >+ >+ public ModelEnum ticketMilestone(String id) throws Exception { >+ return new ModelEnum("ticket.milestone", id, "due", "completed", "description"); >+ } >+ >+ public TicketEnum ticketPriority(String id) throws Exception { >+ return new TicketEnum("ticket.priority", id); >+ } >+ >+ public TicketEnum ticketSeverity(String id) throws Exception { >+ return new TicketEnum("ticket.severity", id); >+ } >+ >+ public TicketEnum ticketStatus(String id) throws Exception { >+ return new TicketEnum("ticket.status", id); >+ } >+ >+ public TicketEnum ticketType(String id) throws Exception { >+ return new TicketEnum("ticket.type", id); >+ } >+ >+ public ModelEnum ticketVersion(String id) throws Exception { >+ return new ModelEnum("ticket.version", id, "time", "description"); >+ } >+ >+} >Index: src/org/eclipse/mylar/trac/tests/support/AbstractTracRepositoryFactory.java >=================================================================== >RCS file: src/org/eclipse/mylar/trac/tests/support/AbstractTracRepositoryFactory.java >diff -N src/org/eclipse/mylar/trac/tests/support/AbstractTracRepositoryFactory.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/mylar/trac/tests/support/AbstractTracRepositoryFactory.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,52 @@ >+/******************************************************************************* >+ * Copyright (c) 2006 - 2006 Mylar eclipse.org project and others. >+ * All rights reserved. This program and the accompanying materials >+ * are made available under the terms of the Eclipse Public License v1.0 >+ * which accompanies this distribution, and is available at >+ * http://www.eclipse.org/legal/epl-v10.html >+ * >+ * Contributors: >+ * Mylar project committers - initial API and implementation >+ *******************************************************************************/ >+ >+package org.eclipse.mylar.trac.tests.support; >+ >+import java.net.Authenticator; >+ >+import org.eclipse.mylar.internal.trac.core.ITracRepository; >+import org.eclipse.mylar.trac.tests.Constants; >+ >+/** >+ * @author Steffen Pingel >+ * >+ */ >+public abstract class AbstractTracRepositoryFactory { >+ >+ public String repositoryUrl; >+ >+ public ITracRepository repository; >+ >+ public String username; >+ >+ public String password; >+ >+ public ITracRepository connectRepository1() throws Exception { >+ return connect(Constants.TEST_REPOSITORY1_URL, Constants.TEST_REPOSITORY1_ADMIN_USERNAME, >+ Constants.TEST_REPOSITORY1_ADMIN_PASSWORD); >+ } >+ >+ public ITracRepository connect(String url, String username, String password) throws Exception { >+ this.repositoryUrl = url; >+ this.username = username; >+ this.password = password; >+ this.repository = createRepository(url, username, password); >+ >+ // make sure no dialog pops up to prompt for a password >+ Authenticator.setDefault(null); >+ >+ return this.repository; >+ } >+ >+ protected abstract ITracRepository createRepository(String url, String username, String password) throws Exception; >+ >+} >Index: src/org/eclipse/mylar/trac/tests/AbstractTracRepositoryTest.java >=================================================================== >RCS file: src/org/eclipse/mylar/trac/tests/AbstractTracRepositoryTest.java >diff -N src/org/eclipse/mylar/trac/tests/AbstractTracRepositoryTest.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/mylar/trac/tests/AbstractTracRepositoryTest.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,55 @@ >+/******************************************************************************* >+ * Copyright (c) 2006 - 2006 Mylar eclipse.org project and others. >+ * All rights reserved. This program and the accompanying materials >+ * are made available under the terms of the Eclipse Public License v1.0 >+ * which accompanies this distribution, and is available at >+ * http://www.eclipse.org/legal/epl-v10.html >+ * >+ * Contributors: >+ * Mylar project committers - initial API and implementation >+ *******************************************************************************/ >+ >+package org.eclipse.mylar.trac.tests; >+ >+import junit.framework.TestCase; >+ >+import org.eclipse.mylar.internal.trac.core.TracLoginException; >+import org.eclipse.mylar.trac.tests.support.AbstractTracRepositoryFactory; >+ >+/** >+ * Test cases for classes that implement {@link ITracRepositor}. >+ * >+ * @author Steffen Pingel >+ */ >+public abstract class AbstractTracRepositoryTest extends TestCase { >+ >+ protected AbstractTracRepositoryFactory factory; >+ >+ public AbstractTracRepositoryTest(AbstractTracRepositoryFactory factory) { >+ this.factory = factory; >+ } >+ >+ public void testValidate() throws Exception { >+ factory.connectRepository1(); >+ factory.repository.validate(); >+ } >+ >+ public void testValidateFailAuthWrongPassword() throws Exception { >+ factory.connect(Constants.TEST_REPOSITORY1_URL, Constants.TEST_REPOSITORY1_ADMIN_USERNAME, "wrongpassword"); >+ try { >+ factory.repository.validate(); >+ fail("Expected TracLoginException"); >+ } catch (TracLoginException e) { >+ } >+ } >+ >+ public void testValidateFailAuthWrongUsername() throws Exception { >+ factory.connect(Constants.TEST_REPOSITORY1_URL, "wrongusername", Constants.TEST_REPOSITORY1_ADMIN_PASSWORD); >+ try { >+ factory.repository.validate(); >+ fail("Expected TracLoginException"); >+ } catch (TracLoginException e) { >+ } >+ } >+ >+} >#P org.eclipse.mylar.trac >Index: META-INF/MANIFEST.MF >=================================================================== >RCS file: /cvsroot/technology/org.eclipse.mylar/sandbox/org.eclipse.mylar.trac/META-INF/MANIFEST.MF,v >retrieving revision 1.4 >diff -u -r1.4 MANIFEST.MF >--- META-INF/MANIFEST.MF 21 Jun 2006 03:40:32 -0000 1.4 >+++ META-INF/MANIFEST.MF 21 Jun 2006 19:25:16 -0000 >@@ -8,7 +8,9 @@ > Bundle-Localization: plugin > Require-Bundle: org.eclipse.ui, > org.eclipse.core.runtime, >- org.eclipse.mylar.core >+ org.eclipse.mylar.core, >+ org.eclipse.mylar.tasklist, >+ org.apache.xmlrpc > Eclipse-LazyStart: true > Export-Package: org.eclipse.mylar.internal.trac, > org.eclipse.mylar.internal.trac.core, >Index: src/org/eclipse/mylar/internal/trac/core/TracXmlRpcRepository.java >=================================================================== >RCS file: src/org/eclipse/mylar/internal/trac/core/TracXmlRpcRepository.java >diff -N src/org/eclipse/mylar/internal/trac/core/TracXmlRpcRepository.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/mylar/internal/trac/core/TracXmlRpcRepository.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,279 @@ >+package org.eclipse.mylar.internal.trac.core; >+ >+import java.io.IOException; >+import java.io.InputStream; >+import java.io.OutputStream; >+import java.net.HttpURLConnection; >+import java.net.URL; >+import java.util.ArrayList; >+import java.util.Hashtable; >+import java.util.Iterator; >+import java.util.List; >+import java.util.Vector; >+ >+import org.apache.xmlrpc.XmlRpc; >+import org.apache.xmlrpc.XmlRpcClient; >+import org.apache.xmlrpc.XmlRpcClientException; >+import org.apache.xmlrpc.XmlRpcException; >+import org.apache.xmlrpc.XmlRpcTransport; >+import org.apache.xmlrpc.XmlRpcTransportFactory; >+import org.eclipse.mylar.internal.trac.MylarTracPlugin; >+import org.eclipse.mylar.internal.trac.model.TracSearch; >+import org.eclipse.mylar.internal.trac.model.TracTicket; >+ >+/** >+ * Represents a Trac repository that is accessed through the Trac XmlRpcPlugin. >+ * >+ * @author Steffen Pingel >+ */ >+public class TracXmlRpcRepository extends AbstractTracRepository { >+ >+ public static final String XMLRPC_URL = "/xmlrpc"; >+ >+ private XmlRpcClient xmlrpc; >+ >+ public TracXmlRpcRepository(URL url, Version version, String username, String password) { >+ super(url, version, username, password); >+ } >+ >+ public XmlRpcClient getClient() throws TracException { >+ if (xmlrpc != null) { >+ return xmlrpc; >+ } >+ >+ // initialize XML-RPC library >+ XmlRpc.setDefaultInputEncoding(ITracRepository.CHARSET); >+ >+ try { >+ String location = repositoryUrl.toString(); >+ if (hasAuthenticationCredentials()) { >+ location += LOGIN_URL; >+ } >+ location += XMLRPC_URL; >+ >+ URL url = new URL(location); >+ TransportFactory transport = new TransportFactory(url); >+ xmlrpc = new XmlRpcClient(url, transport); >+ } catch (Exception e) { >+ throw new TracException(e); >+ } >+ >+ return xmlrpc; >+ } >+ >+ private Object call(String method, Object... parameters) throws TracException { >+ getClient(); >+ >+ Vector<Object> params = new Vector<Object>(); >+ for (Object parameter : parameters) { >+ params.add(parameter); >+ } >+ >+ try { >+ return xmlrpc.execute(method, params); >+ } catch (HttpException e) { >+ if (e.responseCode == HttpURLConnection.HTTP_FORBIDDEN >+ || e.responseCode == HttpURLConnection.HTTP_UNAUTHORIZED) { >+ throw new TracLoginException(); >+ } else { >+ throw new TracException(e); >+ } >+ } catch (XmlRpcException e) { >+ throw new TracRemoteException(e); >+ } catch (Exception e) { >+ throw new TracException(e); >+ } >+ } >+ >+ private Vector multicall(Hashtable<String, Object>... calls) throws TracException { >+ Vector result = (Vector) call("system.multicall", new Object[] { calls }); >+ for (Object item : result) { >+ try { >+ checkForException(item); >+ } >+ catch (XmlRpcException e) { >+ throw new TracRemoteException(e); >+ } >+ catch (Exception e) { >+ throw new TracException(e); >+ } >+ } >+ return result; >+ } >+ >+ private void checkForException(Object result) throws NumberFormatException, XmlRpcException { >+ if (result instanceof Hashtable) { >+ Hashtable exceptionData = (Hashtable) result; >+ if (exceptionData.containsKey("faultCode") && exceptionData.containsKey("faultString")) { >+ throw new XmlRpcException( >+ Integer.parseInt(exceptionData.get("faultCode").toString()), >+ (String) exceptionData.get("faultString")); >+ } >+ } >+ } >+ >+ private Hashtable<String, Object> createMultiCall(String methodName, Object... parameters) throws TracException { >+ Hashtable<String, Object> table = new Hashtable<String, Object>(); >+ table.put("methodName", methodName); >+ table.put("params", parameters); >+ return table; >+ } >+ >+ public void validate() throws TracException { >+ Vector result = (Vector) call("system.listMethods"); >+ boolean hasGetTicket = false, hasQuery = false; >+ for (Object methodName : result) { >+ if ("ticket.get".equals(methodName)) { >+ hasGetTicket = true; >+ } >+ if ("ticket.query".equals(methodName)) { >+ hasQuery = true; >+ } >+ >+ if (hasGetTicket && hasQuery) { >+ return; >+ } >+ } >+ >+ throw new TracException("XML-RPC is missing required API calls"); >+ } >+ >+ public TracTicket getTicket(int id) throws TracException { >+ Vector result = (Vector) call("ticket.get", id); >+ return parseTicket(result); >+ } >+ >+ @SuppressWarnings("unchecked") >+ public List<TracTicket> getTickets(int[] ids) throws TracException { >+ Hashtable<String, Object>[] calls = new Hashtable[ids.length]; >+ for (int i = 0; i < calls.length; i++) { >+ calls[i] = createMultiCall("ticket.get", ids[i]); >+ } >+ >+ Vector result = multicall(calls); >+ assert result.size() == ids.length; >+ >+ List<TracTicket> tickets = new ArrayList<TracTicket>(result.size()); >+ for (Iterator it = result.iterator(); it.hasNext();) { >+ Vector ticketResult = (Vector) it.next(); >+ tickets.add(parseTicket(ticketResult)); >+ } >+ >+ return tickets; >+ } >+ >+ @SuppressWarnings("unchecked") >+ public void search(TracSearch query, List<TracTicket> tickets) throws TracException { >+ // an empty query string is not valid, therefore prepend order >+ Vector result = (Vector) call("ticket.query", "order=id" + query.toQuery()); >+ >+ Hashtable<String, Object>[] calls = new Hashtable[result.size()]; >+ for (int i = 0; i < calls.length; i++) { >+ calls[i] = createMultiCall("ticket.get", result.get(i)); >+ } >+ result = multicall(calls); >+ >+ for (Iterator it = result.iterator(); it.hasNext();) { >+ Vector ticketResult = (Vector) it.next(); >+ tickets.add(parseTicket(ticketResult)); >+ } >+ } >+ >+ private TracTicket parseTicket(Vector result) throws InvalidTicketException { >+ TracTicket ticket = new TracTicket((Integer) result.get(0)); >+ ticket.setCreated((Integer) result.get(1)); >+ ticket.setLastChanged((Integer) result.get(2)); >+ Hashtable attributes = (Hashtable) result.get(3); >+ for (Object key : attributes.keySet()) { >+ ticket.putTracValue(key.toString(), attributes.get(key).toString()); >+ } >+ return ticket; >+ } >+ >+ /** >+ * A custom transport factory used to establish XML-RPC connections. Uses >+ * the Eclipse proxy settings. >+ * >+ * @author Steffen Pingel >+ */ >+ private class TransportFactory implements XmlRpcTransportFactory { >+ >+ private URL url; >+ >+ public TransportFactory(URL url) { >+ assert url != null; >+ >+ this.url = url; >+ } >+ >+ public XmlRpcTransport createTransport() throws XmlRpcClientException { >+ return new XmlRpcTransport() { >+ >+ private HttpURLConnection connection; >+ >+ public void endClientRequest() throws XmlRpcClientException { >+ assert connection != null; >+ >+ try { >+ connection.getInputStream().close(); >+ } catch (Exception e) { >+ throw new XmlRpcClientException("Exception closing connection", e); >+ } >+ } >+ >+ public InputStream sendXmlRpc(byte[] request) throws IOException, XmlRpcClientException { >+ try { >+ connection = MylarTracPlugin.getHttpConnection(url); >+ } catch (Exception e) { >+ throw new IOException(e.toString()); >+ } >+ >+ connection.setDoInput(true); >+ connection.setDoOutput(true); >+ connection.setUseCaches(false); >+ connection.setAllowUserInteraction(false); >+ >+ connection.setRequestProperty("Content-Length", request.length + ""); >+ connection.setRequestProperty("Content-Type", "text/xml"); >+ if (hasAuthenticationCredentials()) { >+ MylarTracPlugin.setAuthCredentials(connection, username, password); >+ } >+ >+ OutputStream out = connection.getOutputStream(); >+ out.write(request); >+ out.flush(); >+ out.close(); >+ >+ connection.connect(); >+ int responseCode = connection.getResponseCode(); >+ if (responseCode != HttpURLConnection.HTTP_OK) { >+ throw new HttpException(responseCode); >+ } >+ >+ return connection.getInputStream(); >+ } >+ >+ }; >+ } >+ >+ public void setProperty(String key, Object value) { >+ // ignore, this is never called by the library >+ } >+ >+ } >+ >+ private class HttpException extends IOException { >+ >+ private static final long serialVersionUID = 7083228933121822248L; >+ >+ final int responseCode; >+ >+ public HttpException(int responseCode) { >+ super("HTTP Error " + responseCode); >+ >+ this.responseCode = responseCode; >+ } >+ >+ } >+ >+}
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 148089
: 45029 |
45030