Skip to main content
summaryrefslogtreecommitdiffstats
blob: 8ed9f516bc16b089e6e7a5722b97b8676e9b413f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*******************************************************************************
 * 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.Map;
import java.util.Vector;

import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.client.XmlRpcClient;
import org.eclipse.mylar.internal.trac.core.TracXmlRpcClient;
import org.eclipse.mylar.internal.trac.core.ITracClient.Version;

/**
 * @author Steffen Pingel
 */
public class XmlRpcServer {

	public abstract class AbstractTracItem {

		public abstract void delete() throws Exception;

		void itemCreated() {
			data.items.add(this);
		}

		void itemDeleted() {
			data.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 {
			if (Arrays.asList(getAll()).contains(id)) {
				delete();
			}

			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 Arrays.asList((Object[]) 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 TestData {

		// all created items
		List<AbstractTracItem> items = new ArrayList<AbstractTracItem>();
		
		// all created tickets
		public List<Ticket> tickets = new ArrayList<Ticket>();
		
		public int attachmentTicketId = 5;
		
		public int htmlEntitiesTicketId = 6;
		
		/**
		 * 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 Map getValues() throws Exception {
			return (Map) ((Object[]) call("ticket.get", id))[3];
		}

		@SuppressWarnings("unchecked")
		public Integer[] getAll() throws Exception {
			return Arrays.asList((Object[]) call("ticket.query", "order=id")).toArray(new Integer[0]);
		}

		public int getId() {
			return id;
		}

		@Override
		protected void itemCreated() {
			super.itemCreated();
			data.tickets.add(this);
		}
		
		@Override
		protected void itemDeleted() {
			super.itemDeleted();
			data.tickets.remove(this);
		}
		
		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 {
			if (Arrays.asList(getAll()).contains(id)) {
				delete();
			}

			return create(param);
		}

		public String get() throws Exception {
			return (String) call(module + ".get", id);
		}

		@SuppressWarnings("unchecked")
		public String[] getAll() throws Exception {
			return Arrays.asList((Object[]) 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 TestData data;

	private String password;

	private TracXmlRpcClient 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.data = new TestData();

		this.repository = new TracXmlRpcClient(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 TestData getData() {
		return data;
	}

	public String getPassword() {
		return password;
	}

	public TracXmlRpcClient 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");
	}

}

Back to the top