|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2006 - 2006 Mylar eclipse.org project and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* Mylar project committers - initial API and implementation |
| 10 |
*******************************************************************************/ |
| 11 |
|
| 12 |
package org.eclipse.mylar.trac.tests.support; |
| 13 |
|
| 14 |
import java.io.IOException; |
| 15 |
import java.net.MalformedURLException; |
| 16 |
import java.net.URL; |
| 17 |
import java.util.ArrayList; |
| 18 |
import java.util.Arrays; |
| 19 |
import java.util.Hashtable; |
| 20 |
import java.util.List; |
| 21 |
import java.util.Vector; |
| 22 |
|
| 23 |
import org.apache.xmlrpc.XmlRpcClient; |
| 24 |
import org.apache.xmlrpc.XmlRpcException; |
| 25 |
import org.eclipse.mylar.internal.trac.core.TracXmlRpcRepository; |
| 26 |
import org.eclipse.mylar.internal.trac.core.ITracRepository.Version; |
| 27 |
|
| 28 |
/** |
| 29 |
* @author Steffen Pingel |
| 30 |
*/ |
| 31 |
public class XmlRpcServer { |
| 32 |
|
| 33 |
public abstract class AbstractTracItem { |
| 34 |
|
| 35 |
public abstract void delete() throws Exception; |
| 36 |
|
| 37 |
protected void itemCreated() { |
| 38 |
fixture.items.add(this); |
| 39 |
} |
| 40 |
|
| 41 |
protected void itemDeleted() { |
| 42 |
fixture.items.remove(this); |
| 43 |
} |
| 44 |
|
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Represents a Trac type with multiple attributes such as a milestone. |
| 49 |
*/ |
| 50 |
public class ModelEnum extends AbstractTracItem { |
| 51 |
|
| 52 |
private String[] attributes; |
| 53 |
|
| 54 |
private String id; |
| 55 |
|
| 56 |
private String module; |
| 57 |
|
| 58 |
public ModelEnum(String module, String id, String... attributes) { |
| 59 |
this.module = module; |
| 60 |
this.id = id; |
| 61 |
this.attributes = attributes; |
| 62 |
} |
| 63 |
|
| 64 |
public ModelEnum create(Object... params) throws Exception { |
| 65 |
call(module + ".create", id, toMap(params)); |
| 66 |
itemCreated(); |
| 67 |
return this; |
| 68 |
} |
| 69 |
|
| 70 |
public void delete() throws Exception { |
| 71 |
call(module + ".delete", id); |
| 72 |
itemDeleted(); |
| 73 |
} |
| 74 |
|
| 75 |
public void deleteAll() throws Exception { |
| 76 |
String[] ids = getAll(); |
| 77 |
for (String id : ids) { |
| 78 |
call(module + ".delete", id); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
public ModelEnum deleteAndCreate(Object... params) throws Exception { |
| 83 |
try { |
| 84 |
if (Arrays.asList(getAll()).contains(id)) { |
| 85 |
delete(); |
| 86 |
} |
| 87 |
} catch (Exception e) { |
| 88 |
// ignore |
| 89 |
} |
| 90 |
|
| 91 |
return create(params); |
| 92 |
} |
| 93 |
|
| 94 |
public Object[] get() throws Exception { |
| 95 |
Hashtable values = (Hashtable) call(module + ".get", id); |
| 96 |
Object[] result = new Object[values.size()]; |
| 97 |
for (int i = 0; i < result.length && i < attributes.length; i++) { |
| 98 |
result[i] = values.get(attributes[i]); |
| 99 |
} |
| 100 |
return result; |
| 101 |
} |
| 102 |
|
| 103 |
@SuppressWarnings("unchecked") |
| 104 |
public String[] getAll() throws Exception { |
| 105 |
return (String[]) ((Vector) call(module + ".getAll")).toArray(new String[0]); |
| 106 |
} |
| 107 |
|
| 108 |
private Hashtable<String, Object> toMap(Object... params) { |
| 109 |
Hashtable<String, Object> attrs = new Hashtable<String, Object>(); |
| 110 |
for (int i = 0; i < attributes.length && i < params.length; i++) { |
| 111 |
attrs.put((String) attributes[i], params[i]); |
| 112 |
} |
| 113 |
return attrs; |
| 114 |
} |
| 115 |
|
| 116 |
public ModelEnum update(Object... params) throws Exception { |
| 117 |
call(module + ".update", id, toMap(params)); |
| 118 |
return this; |
| 119 |
} |
| 120 |
|
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Records changes to the repository. |
| 125 |
*/ |
| 126 |
public class TestFixture { |
| 127 |
|
| 128 |
List<AbstractTracItem> items = new ArrayList<AbstractTracItem>(); |
| 129 |
|
| 130 |
/** |
| 131 |
* Undo all changes. |
| 132 |
*/ |
| 133 |
public void cleanup() throws Exception { |
| 134 |
while (!items.isEmpty()) { |
| 135 |
items.get(0).delete(); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Represents a Trac ticket. |
| 143 |
*/ |
| 144 |
public class Ticket extends AbstractTracItem { |
| 145 |
|
| 146 |
private Integer id; |
| 147 |
|
| 148 |
public Ticket(Integer id) { |
| 149 |
this.id = id; |
| 150 |
} |
| 151 |
|
| 152 |
public Ticket create(String summary, String description) throws Exception { |
| 153 |
this.id = (Integer) call("ticket.create", summary, description, new Hashtable()); |
| 154 |
if (id == null) { |
| 155 |
throw new RuntimeException("Could not create ticket: " + summary); |
| 156 |
} |
| 157 |
itemCreated(); |
| 158 |
return this; |
| 159 |
} |
| 160 |
|
| 161 |
public void delete() throws Exception { |
| 162 |
call("ticket.delete", id); |
| 163 |
itemDeleted(); |
| 164 |
} |
| 165 |
|
| 166 |
public void deleteAll() throws Exception { |
| 167 |
Integer[] ids = getAll(); |
| 168 |
for (Integer id : ids) { |
| 169 |
call("ticket.delete", id); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
public Object getValue(String key) throws Exception { |
| 174 |
return getValues().get(key); |
| 175 |
} |
| 176 |
|
| 177 |
public Hashtable getValues() throws Exception { |
| 178 |
return (Hashtable) ((Vector) call("ticket.get", id)).get(3); |
| 179 |
} |
| 180 |
|
| 181 |
@SuppressWarnings("unchecked") |
| 182 |
public Integer[] getAll() throws Exception { |
| 183 |
return (Integer[]) ((Vector) call("ticket.query", "order=id")).toArray(new Integer[0]); |
| 184 |
} |
| 185 |
|
| 186 |
public int getId() { |
| 187 |
return id; |
| 188 |
} |
| 189 |
|
| 190 |
public Ticket update(String comment, String key, String value) throws Exception { |
| 191 |
Hashtable<String, Object> attrs = new Hashtable<String, Object>(); |
| 192 |
attrs.put(key, value); |
| 193 |
call("ticket.update", id, comment, attrs); |
| 194 |
return this; |
| 195 |
} |
| 196 |
|
| 197 |
} |
| 198 |
|
| 199 |
/** |
| 200 |
* Represents a Trac type that has a single attribute such as a priority. |
| 201 |
*/ |
| 202 |
public class TicketEnum extends AbstractTracItem { |
| 203 |
|
| 204 |
private String id; |
| 205 |
|
| 206 |
private String module; |
| 207 |
|
| 208 |
public TicketEnum(String module, String id) { |
| 209 |
this.module = module; |
| 210 |
this.id = id; |
| 211 |
} |
| 212 |
|
| 213 |
public TicketEnum create(String param) throws Exception { |
| 214 |
call(module + ".create", id, param); |
| 215 |
itemCreated(); |
| 216 |
return this; |
| 217 |
} |
| 218 |
|
| 219 |
public void delete() throws Exception { |
| 220 |
call(module + ".delete", id); |
| 221 |
itemDeleted(); |
| 222 |
} |
| 223 |
|
| 224 |
public void deleteAll() throws Exception { |
| 225 |
String[] ids = getAll(); |
| 226 |
for (String id : ids) { |
| 227 |
call(module + ".delete", id); |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
public TicketEnum deleteAndCreate(String param) throws Exception { |
| 232 |
try { |
| 233 |
if (Arrays.asList(getAll()).contains(id)) { |
| 234 |
delete(); |
| 235 |
} |
| 236 |
} catch (Exception e) { |
| 237 |
// ignore |
| 238 |
} |
| 239 |
|
| 240 |
return create(param); |
| 241 |
} |
| 242 |
|
| 243 |
public String get() throws Exception { |
| 244 |
return (String) call(module + ".get", id); |
| 245 |
} |
| 246 |
|
| 247 |
@SuppressWarnings("unchecked") |
| 248 |
public String[] getAll() throws Exception { |
| 249 |
return (String[]) ((Vector) call(module + ".getAll")).toArray(new String[0]); |
| 250 |
} |
| 251 |
|
| 252 |
public TicketEnum update(String param) throws Exception { |
| 253 |
call(module + ".update", id, param); |
| 254 |
return this; |
| 255 |
} |
| 256 |
|
| 257 |
} |
| 258 |
|
| 259 |
private XmlRpcClient client; |
| 260 |
|
| 261 |
private TestFixture fixture; |
| 262 |
|
| 263 |
private String password; |
| 264 |
|
| 265 |
private TracXmlRpcRepository repository; |
| 266 |
|
| 267 |
private String url; |
| 268 |
|
| 269 |
private String username; |
| 270 |
|
| 271 |
public XmlRpcServer(String url, String username, String password) throws Exception { |
| 272 |
this.url = url; |
| 273 |
this.username = username; |
| 274 |
this.password = password; |
| 275 |
|
| 276 |
this.fixture = new TestFixture(); |
| 277 |
|
| 278 |
this.repository = new TracXmlRpcRepository(new URL(url), Version.XML_RPC, username, password); |
| 279 |
this.client = repository.getClient(); |
| 280 |
} |
| 281 |
|
| 282 |
private Object call(String method, Object... parameters) throws XmlRpcException, IOException { |
| 283 |
Vector<Object> params = new Vector<Object>(parameters.length); |
| 284 |
for (Object parameter : parameters) { |
| 285 |
params.add(parameter); |
| 286 |
} |
| 287 |
|
| 288 |
Object result = client.execute(method, params); |
| 289 |
if (result instanceof XmlRpcException) { |
| 290 |
throw (XmlRpcException) result; |
| 291 |
} |
| 292 |
return result; |
| 293 |
} |
| 294 |
|
| 295 |
public TestFixture getFixture() { |
| 296 |
return fixture; |
| 297 |
} |
| 298 |
|
| 299 |
public String getPassword() { |
| 300 |
return password; |
| 301 |
} |
| 302 |
|
| 303 |
public TracXmlRpcRepository getRepository() throws MalformedURLException { |
| 304 |
return repository; |
| 305 |
} |
| 306 |
|
| 307 |
public String getUrl() { |
| 308 |
return url; |
| 309 |
} |
| 310 |
|
| 311 |
public String getUsername() { |
| 312 |
return username; |
| 313 |
} |
| 314 |
|
| 315 |
public Ticket ticket() throws Exception { |
| 316 |
return new Ticket(null); |
| 317 |
} |
| 318 |
|
| 319 |
public Ticket ticket(int id) throws Exception { |
| 320 |
return new Ticket(id); |
| 321 |
} |
| 322 |
|
| 323 |
public ModelEnum ticketComponent(String id) throws Exception { |
| 324 |
return new ModelEnum("ticket.component", id, "owner", "description"); |
| 325 |
} |
| 326 |
|
| 327 |
public ModelEnum ticketMilestone(String id) throws Exception { |
| 328 |
return new ModelEnum("ticket.milestone", id, "due", "completed", "description"); |
| 329 |
} |
| 330 |
|
| 331 |
public TicketEnum ticketPriority(String id) throws Exception { |
| 332 |
return new TicketEnum("ticket.priority", id); |
| 333 |
} |
| 334 |
|
| 335 |
public TicketEnum ticketSeverity(String id) throws Exception { |
| 336 |
return new TicketEnum("ticket.severity", id); |
| 337 |
} |
| 338 |
|
| 339 |
public TicketEnum ticketStatus(String id) throws Exception { |
| 340 |
return new TicketEnum("ticket.status", id); |
| 341 |
} |
| 342 |
|
| 343 |
public TicketEnum ticketType(String id) throws Exception { |
| 344 |
return new TicketEnum("ticket.type", id); |
| 345 |
} |
| 346 |
|
| 347 |
public ModelEnum ticketVersion(String id) throws Exception { |
| 348 |
return new ModelEnum("ticket.version", id, "time", "description"); |
| 349 |
} |
| 350 |
|
| 351 |
} |