Skip to main content
summaryrefslogtreecommitdiffstats
blob: cb04dd96e5441b1ca0468f566a665ba17621a2ce (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
/*******************************************************************************
 *  Copyright (c) 2009 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.p2.tests;

import java.io.File;
import java.io.FileOutputStream;
import java.net.URI;
import java.util.*;
import org.eclipse.ant.core.AntRunner;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.URIUtil;
import org.eclipse.equinox.internal.p2.persistence.XMLWriter;
import org.eclipse.equinox.internal.provisional.p2.core.ProvisionException;
import org.eclipse.equinox.internal.provisional.p2.metadata.query.IQueryable;
import org.eclipse.equinox.p2.metadata.IArtifactKey;
import org.eclipse.equinox.p2.metadata.IInstallableUnit;
import org.eclipse.equinox.p2.metadata.query.IQueryResult;
import org.eclipse.equinox.p2.repository.artifact.ArtifactKeyQuery;
import org.eclipse.equinox.p2.repository.artifact.IArtifactRepository;

public class AbstractAntProvisioningTest extends AbstractProvisioningTest {
	protected static final String TYPE_ARTIFACT = "A";
	protected static final String TYPE_METADATA = "M";
	protected static final String TYPE_BOTH = null;

	private static final String TARGET = "target";
	private static final String ROOT = "project";
	private static final String NAME = "name";
	private static final String DEFAULT_TARGET = "default";
	private static final String DEFAULT_NAME = "default";

	AntTaskElement root, target;
	File buildScript, logLocation;

	public void setUp() throws Exception {
		super.setUp();
		buildScript = new File(getTempFolder(), getUniqueString());
		logLocation = new File(getTempFolder(), getUniqueString());
		createBuildScript();
	}

	public void tearDown() throws Exception {
		// Delete the build script
		delete(buildScript.getParentFile());
		delete(logLocation.getParentFile());
		super.tearDown();
	}

	/*
	 * Run the specified buildscript
	 */
	protected void runAntTask(File buildFile) {
		try {
			runAntTaskWithExceptions(buildFile);
		} catch (CoreException e) {
			fail(rootCause(e));
		}
	}

	private void runAntTaskWithExceptions(File buildFile) throws CoreException {
		AntRunner ant = new AntRunner();
		ant.setArguments("-logfile \"" + logLocation + "\"");
		ant.setBuildFileLocation(buildFile.getAbsolutePath());
		ant.addBuildLogger("org.apache.tools.ant.XmlLogger");
		ant.run();
	}

	/*
	 * Run the build script described programmatically
	 */
	protected void runAntTask() {
		try {
			runAntTaskWithExceptions();
		} catch (CoreException e) {
			fail(rootCause(e));
		}
	}

	protected void runAntTaskWithExceptions() throws CoreException {
		try {
			writeBuildScript();
		} catch (Exception e) {
			fail("Error writing build script", e);
		}
		runAntTaskWithExceptions(buildScript);
	}

	/*
	 * Adds an Ant Task to the build script
	 */
	protected void addTask(AntTaskElement task) {
		target.addElement(task);
	}

	/*
	 * Create and return an repository element for this address and type
	 */
	protected AntTaskElement getRepositoryElement(URI address, String kind) {
		return getRepositoryElement(address, kind, null, null, null, null);
	}

	protected AntTaskElement getIUElement(String name, String version) {
		AntTaskElement iuElement = new AntTaskElement("iu");
		iuElement.addAttribute("id", name);
		if (version != null)
			iuElement.addAttribute("version", version);
		return iuElement;
	}

	protected AntTaskElement getRepositoryElement(URI address, String kind, String name, String format, Boolean compressed, Boolean append) {
		AntTaskElement repo = new AntTaskElement("repository");
		repo.addAttributes(new String[] {"location", URIUtil.toUnencodedString(address)});
		if (kind != null)
			repo.addAttributes(new String[] {"kind", kind});
		if (name != null)
			repo.addAttributes(new String[] {"name", name});
		if (format != null)
			repo.addAttributes(new String[] {"format", format});
		if (compressed != null)
			repo.addAttributes(new String[] {"compressed", compressed.toString()});
		if (append != null)
			repo.addAttributes(new String[] {"append", append.toString()});
		return repo;
	}

	/*
	 * Create an element from the specified information
	 */
	protected AntTaskElement createIUElement(IInstallableUnit iu) {
		return createIUElement(iu.getId(), iu.getVersion().toString());
	}

	/*
	 * Create an element from the specified information
	 */
	protected AntTaskElement createIUElement(String id, String version) {
		AntTaskElement iuElement = new AntTaskElement("iu");
		iuElement.addAttribute("id", id);
		iuElement.addAttribute("version", version);
		return iuElement;
	}

	/*
	 * Create the base elements of the build script
	 */
	private void createBuildScript() {
		root = new AntTaskElement(ROOT);
		root.addAttributes(new String[] {NAME, ROOT, DEFAULT_TARGET, DEFAULT_NAME});
		target = new AntTaskElement(TARGET);
		target.addAttributes(new String[] {NAME, DEFAULT_NAME});
		root.addElement(target);
	}

	/*
	 * Write the build script to disk
	 */
	private void writeBuildScript() throws Exception {
		FileOutputStream outputStream = null;
		try {
			outputStream = new FileOutputStream(buildScript);
			XMLWriter writer = new XMLWriter(outputStream, null);
			writeElement(writer, root);
			writer.flush();
		} finally {
			if (outputStream != null)
				outputStream.close();
		}
	}

	/*
	 * Write an element to the buildscript
	 */
	private void writeElement(XMLWriter writer, AntTaskElement task) {
		// Properties ought to occur in key-value pairs 
		assertTrue("Task " + task + " should have an even number of properties", (task.attributes.size() % 2) == 0);

		// Start tag
		writer.start(task.name);

		// write properties
		for (Iterator iter = task.attributes.iterator(); iter.hasNext();)
			writer.attribute((String) iter.next(), (String) iter.next());

		// write sub elements if applicable
		for (Iterator iter = task.elements.iterator(); iter.hasNext();)
			writeElement(writer, (AntTaskElement) iter.next());

		// close tag
		writer.end();
	}

	// Class which can be used to represent elements in a task
	protected class AntTaskElement {
		public String name;
		public List attributes = new ArrayList();
		public List elements = new ArrayList();

		public AntTaskElement(String name) {
			this.name = name;
		}

		public void addAttribute(String attribute, String value) {
			attributes.add(attribute);
			attributes.add(value);
		}

		public void addAttributes(String[] propertyArray) {
			attributes.addAll(Arrays.asList(propertyArray));
		}

		public void addElement(AntTaskElement element) {
			elements.add(element);
		}

		public String toString() {
			return name;
		}
	}

	protected static Throwable rootCause(Throwable e) {
		if (e.getCause() != null)
			return rootCause(e.getCause());
		return e;
	}

	protected static void fail(Throwable e) {
		fail("An exception occurred while running the task", e);
	}

	protected void assertLogContains(String content) {
		try {
			assertLogContainsLine(logLocation, content);
		} catch (Exception e) {
			fail("Error asserting log contents.", e);
		}
	}

	protected static void assertIUContentEquals(String message, IQueryable source, IQueryable destination) {
		assertContains(message, source, destination);
		assertContains(message, destination, source);
	}

	protected void assertArtifactKeyContentEquals(String message, IQueryResult ius, URI artifactRepositoryLocation) {
		try {
			IArtifactRepository repo = getArtifactRepositoryManager().loadRepository(artifactRepositoryLocation, null);
			List fromIUs = getArtifactKeys(ius);
			Iterator fromRepo = repo.query(ArtifactKeyQuery.ALL_KEYS, null).iterator();
			assertContains(message, fromIUs, fromRepo);
			assertContains(message, fromRepo, fromIUs);
		} catch (ProvisionException e) {
			fail("Failed to load repository", e);
		}

	}

	protected static List getArtifactKeys(IQueryResult<IInstallableUnit> ius) {
		List<IArtifactKey> keys = new ArrayList<IArtifactKey>();

		for (Iterator<IInstallableUnit> iter = ius.iterator(); iter.hasNext();)
			keys.addAll(iter.next().getArtifacts());
		return keys;
	}
}

Back to the top