Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 265550 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/equinox/p2/tests/mirror/AllTests.java (+2 lines)
Lines 23-28 Link Here
23
		suite.addTestSuite(MetadataMirrorApplicationTest.class);
23
		suite.addTestSuite(MetadataMirrorApplicationTest.class);
24
		suite.addTestSuite(ArtifactRepositoryCleanupTest.class);
24
		suite.addTestSuite(ArtifactRepositoryCleanupTest.class);
25
		suite.addTestSuite(MetadataRepositoryCleanupTest.class);
25
		suite.addTestSuite(MetadataRepositoryCleanupTest.class);
26
		suite.addTestSuite(NewMirrorApplicationArtifactTest.class);
27
		suite.addTestSuite(NewMirrorApplicationMetadataTest.class);
26
		return suite;
28
		return suite;
27
	}
29
	}
28
30
(-)META-INF/MANIFEST.MF (+2 lines)
Lines 6-11 Link Here
6
Bundle-Localization: plugin
6
Bundle-Localization: plugin
7
Bundle-Version: 1.1.0.qualifier
7
Bundle-Version: 1.1.0.qualifier
8
Import-Package: javax.xml.parsers,
8
Import-Package: javax.xml.parsers,
9
 org.eclipse.ant.core,
9
 org.eclipse.ecf.filetransfer,
10
 org.eclipse.ecf.filetransfer,
10
 org.eclipse.equinox.internal.p2.artifact.mirror,
11
 org.eclipse.equinox.internal.p2.artifact.mirror,
11
 org.eclipse.equinox.internal.p2.artifact.processors.md5,
12
 org.eclipse.equinox.internal.p2.artifact.processors.md5,
Lines 50-55 Link Here
50
 org.eclipse.equinox.internal.provisional.spi.p2.artifact.repository,
51
 org.eclipse.equinox.internal.provisional.spi.p2.artifact.repository,
51
 org.eclipse.equinox.internal.provisional.spi.p2.metadata.repository,
52
 org.eclipse.equinox.internal.provisional.spi.p2.metadata.repository,
52
 org.eclipse.equinox.internal.provisional.spi.p2.repository,
53
 org.eclipse.equinox.internal.provisional.spi.p2.repository,
54
 org.eclipse.equinox.p2.internal.repository.tools,
53
 org.eclipse.equinox.spi.p2.publisher,
55
 org.eclipse.equinox.spi.p2.publisher,
54
 org.eclipse.internal.provisional.equinox.p2.jarprocessor,
56
 org.eclipse.internal.provisional.equinox.p2.jarprocessor,
55
 org.eclipse.osgi.service.datalocation,
57
 org.eclipse.osgi.service.datalocation,
(-)src/org/eclipse/equinox/p2/tests/mirror/NewMirrorApplicationMetadataTest.java (+967 lines)
Added Link Here
1
/*******************************************************************************
2
 *  Copyright (c) 2008, 2009 IBM Corporation 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
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.equinox.p2.tests.mirror;
12
13
import java.io.File;
14
import java.net.*;
15
import java.util.HashMap;
16
import java.util.Map;
17
import org.eclipse.equinox.internal.p2.metadata.repository.CompositeMetadataRepository;
18
import org.eclipse.equinox.internal.provisional.p2.core.ProvisionException;
19
import org.eclipse.equinox.internal.provisional.p2.metadata.IInstallableUnit;
20
import org.eclipse.equinox.internal.provisional.p2.metadata.query.InstallableUnitQuery;
21
import org.eclipse.equinox.internal.provisional.p2.metadata.repository.IMetadataRepository;
22
import org.eclipse.equinox.internal.provisional.p2.metadata.repository.IMetadataRepositoryManager;
23
import org.eclipse.equinox.internal.provisional.p2.query.Collector;
24
import org.eclipse.equinox.internal.provisional.p2.repository.IRepository;
25
import org.eclipse.equinox.internal.simpleconfigurator.utils.URIUtil;
26
import org.eclipse.equinox.p2.internal.repository.tools.MirrorApplication;
27
import org.eclipse.equinox.p2.internal.repository.tools.RepositoryDescriptor;
28
import org.eclipse.equinox.p2.tests.AbstractProvisioningTest;
29
import org.eclipse.osgi.util.NLS;
30
31
/*
32
 * Modified from MetadataMirrorApplicationTest
33
 */
34
public class NewMirrorApplicationMetadataTest extends AbstractProvisioningTest {
35
	protected File destRepoLocation;
36
	protected File sourceRepoLocation; //helloworldfeature
37
	protected File sourceRepo2Location; //anotherfeature
38
	protected File sourceRepo3Location; //helloworldfeature + yetanotherfeature
39
	protected File sourceRepo4Location; //helloworldfeature v1.0.1
40
41
	protected Exception exception = null;
42
43
	/* (non-Javadoc)
44
	 * @see org.eclipse.equinox.p2.tests.AbstractProvisioningTest#setUp()
45
	 */
46
	protected void setUp() throws Exception {
47
		super.setUp();
48
		//load all the repositories
49
		sourceRepoLocation = getTestData("0.0", "/testData/mirror/mirrorSourceRepo1 with space");
50
		sourceRepo2Location = getTestData("0.1", "/testData/mirror/mirrorSourceRepo2");
51
		sourceRepo3Location = getTestData("0.2", "/testData/mirror/mirrorSourceRepo3");
52
		sourceRepo4Location = getTestData("0.3", "/testData/mirror/mirrorSourceRepo4");
53
54
		//create destination location
55
		destRepoLocation = new File(getTempFolder(), "BasicMirrorApplicationTest");
56
		AbstractProvisioningTest.delete(destRepoLocation);
57
	}
58
59
	/* (non-Javadoc)
60
	 * @see org.eclipse.equinox.p2.tests.AbstractProvisioningTest#tearDown()
61
	 */
62
	protected void tearDown() throws Exception {
63
		//remove all the repositories
64
		getMetadataRepositoryManager().removeRepository(destRepoLocation.toURI());
65
		getMetadataRepositoryManager().removeRepository(sourceRepoLocation.toURI());
66
		getMetadataRepositoryManager().removeRepository(sourceRepo2Location.toURI());
67
		getMetadataRepositoryManager().removeRepository(sourceRepo3Location.toURI());
68
		getMetadataRepositoryManager().removeRepository(sourceRepo4Location.toURI());
69
		exception = null;
70
		//delete the destination location (no left over files for the next test)
71
		delete(destRepoLocation);
72
		super.tearDown();
73
	}
74
75
	/**
76
	 * Runs mirror application with default arguments. source is the source repo, 
77
	 * destination is the destination repo, append is if the "-writeMode clean" argument should be excluded
78
	 * 
79
	 * Note: We use URL here because command line applications traffic in unencoded URLs,
80
	 * so we can't use java.net.URI which will always use the encoded form
81
	 */
82
	private void basicRunMirrorApplication(String message, URL source, URL destination, boolean append) throws Exception {
83
		MirrorApplication app = new MirrorApplication();
84
85
		if (destination != null) {
86
			RepositoryDescriptor dest = new RepositoryDescriptor();
87
			dest.setLocation(URIUtil.fromString(destination.toExternalForm()));
88
			dest.setAppend(append);
89
			dest.setKind("metadata");
90
			app.addDestination(dest);
91
		}
92
93
		if (source != null) {
94
			RepositoryDescriptor src = new RepositoryDescriptor();
95
			src.setLocation(URIUtil.fromString(source.toExternalForm()));
96
			src.setKind("metadata");
97
			app.addSource(src);
98
		}
99
		app.run(null);
100
	}
101
102
	private void basicRunMirrorApplication(String message, URL source, URL destination, boolean append, String name) throws Exception {
103
		MirrorApplication app = new MirrorApplication();
104
105
		if (destination != null) {
106
			RepositoryDescriptor dest = new RepositoryDescriptor();
107
			dest.setLocation(URIUtil.fromString(destination.toExternalForm()));
108
			dest.setAppend(append);
109
			dest.setKind("metadata");
110
			dest.setName(name);
111
			app.addDestination(dest);
112
		}
113
114
		if (source != null) {
115
			RepositoryDescriptor src = new RepositoryDescriptor();
116
			src.setLocation(URIUtil.fromString(source.toExternalForm()));
117
			src.setKind("metadata");
118
			app.addSource(src);
119
		}
120
		app.run(null);
121
	}
122
123
	/**
124
	 * just a wrapper method for compatibility
125
	 */
126
	private void runMirrorApplication(String message, File source, File destination, boolean append) {
127
		try {
128
			basicRunMirrorApplication(message, source.toURL(), destination.toURL(), append);
129
		} catch (Exception e) {
130
			fail(message, e);
131
		}
132
	}
133
134
	/**
135
	 * Takes 2 collectors, compares them, and returns the number of unique keys
136
	 * Needed to verify that only the appropriate number of files have been transfered by the mirror application
137
	 */
138
	private int getNumUnique(Collector c1, Collector c2) {
139
		Object[] repo1 = c1.toCollection().toArray();
140
		Object[] repo2 = c2.toCollection().toArray();
141
142
		//initialize to the size of both collectors
143
		int numKeys = repo1.length + repo2.length;
144
145
		for (int i = 0; i < repo1.length; i++) {
146
			for (int j = 0; j < repo2.length; j++) {
147
				if (isEqual((IInstallableUnit) repo1[i], (IInstallableUnit) repo2[j]))
148
					numKeys--;
149
				//identical keys has bee found, therefore the number of unique keys is one less than previously thought
150
			}
151
		}
152
		return numKeys;
153
	}
154
155
	/**
156
	 * Tests mirroring all metadata in a repository to an empty repository
157
	 * Source contains A, B
158
	 * Target contains
159
	 */
160
	private void metadataMirrorToEmpty(String message, boolean append) {
161
		//destination repo is created blank
162
		runMirrorApplication(message + ".0", sourceRepoLocation, destRepoLocation, append); //do not append
163
	}
164
165
	/**
166
	 * Tests mirroring all metadata in a repository to a repository populated with non-duplicate entries
167
	 * Source contains A, B
168
	 * Target contains C, D
169
	 */
170
	private void metadataMirrorToPopulated(String message, boolean append) {
171
		//Setup: populate destination with non-duplicate metadata
172
		runMirrorApplication(message + ".0", sourceRepo2Location, destRepoLocation, false); //value of append does not matter
173
174
		try {
175
			//Setup: ensure setup completed successfully
176
			assertContentEquals(message + ".1", getMetadataRepositoryManager().loadRepository(sourceRepo2Location.toURI(), null), getMetadataRepositoryManager().loadRepository(destRepoLocation.toURI(), null));
177
		} catch (ProvisionException e) {
178
			fail(message + ".2", e);
179
		}
180
181
		//mirror test data
182
		runMirrorApplication(message + ".4", sourceRepoLocation, destRepoLocation, append);
183
	}
184
185
	/**
186
	 * Tests mirroring all metadata in a repository to a repository populated with exact duplicate data
187
	 * @throws Exception
188
	 * Source contains A, B
189
	 * Target contains A, B
190
	 */
191
	private void metadataMirrorToFullDuplicate(String message, boolean append) {
192
		//Setup: populate destination with duplicate metadata
193
		runMirrorApplication(message + ".0", sourceRepoLocation, destRepoLocation, false); //value of append does not matter
194
195
		try {
196
			//Setup: verify contents
197
			assertContentEquals(message + ".1", getMetadataRepositoryManager().loadRepository(sourceRepoLocation.toURI(), null), getMetadataRepositoryManager().loadRepository(destRepoLocation.toURI(), null));
198
		} catch (ProvisionException e) {
199
			fail(message + ".2", e);
200
		}
201
202
		//mirror test data
203
		runMirrorApplication(message + ".4", sourceRepoLocation, destRepoLocation, append);
204
	}
205
206
	/**
207
	 * Tests mirroring all metadata in a repository to a repository populated with partially duplicate data
208
	 * Source contains A, B, C, D
209
	 * Target contains  A, B
210
	 */
211
	private void metadataMirrorToPartialDuplicate(String message, boolean append) {
212
		//Setup: populate destination with duplicate metadata
213
		runMirrorApplication(message + ".0", sourceRepoLocation, destRepoLocation, false); //value of append does not matter
214
215
		try {
216
			//Setup: verify contents
217
			assertContentEquals(message + ".1", getMetadataRepositoryManager().loadRepository(sourceRepoLocation.toURI(), null), getMetadataRepositoryManager().loadRepository(destRepoLocation.toURI(), null));
218
		} catch (ProvisionException e) {
219
			fail(message + ".2", e);
220
		}
221
222
		//mirror test data
223
		runMirrorApplication(message + ".4", sourceRepo3Location, destRepoLocation, append);
224
	}
225
226
	/**
227
	 * Tests mirroring all metadata in a repository to a repository populated with both full duplicate and non-duplicate data
228
	 * Source contains A, B
229
	 * Target contains A, B, C, D
230
	 */
231
	private void metadataMirrorToPopulatedWithFullDuplicate(String message, boolean append) {
232
		//Setup: populate destination with non-duplicate metadata
233
		runMirrorApplication(message + ".0", sourceRepo3Location, destRepoLocation, false); //value of append does not matter
234
235
		try {
236
			//Setup: verify
237
			assertContentEquals(message + ".1", getMetadataRepositoryManager().loadRepository(sourceRepo3Location.toURI(), null), getMetadataRepositoryManager().loadRepository(destRepoLocation.toURI(), null));
238
		} catch (ProvisionException e) {
239
			fail(message + ".2", e);
240
		}
241
242
		//mirror duplicate data
243
		runMirrorApplication(message + ".4", sourceRepoLocation, destRepoLocation, append);
244
	}
245
246
	/**
247
	 * Tests mirroring all metadata in a repository to a repository populated with both partial duplicate and non-duplicate data
248
	 * Source contains A, B, C, D
249
	 * Target contains A, B, E, F
250
	 */
251
	private void metadataMirrorToPopulatedWithPartialDuplicate(String message, boolean append) {
252
		//Setup: populate destination with non-duplicate metadata
253
		runMirrorApplication(message + ".0", sourceRepo2Location, destRepoLocation, false); //value of append does not matter
254
255
		try {
256
			//Setup: verify
257
			assertContentEquals(message + ".1", getMetadataRepositoryManager().loadRepository(sourceRepo2Location.toURI(), null), getMetadataRepositoryManager().loadRepository(destRepoLocation.toURI(), null));
258
		} catch (ProvisionException e) {
259
			fail(message + ".2", e);
260
		}
261
262
		//Setup: populate destination with duplicate metadata
263
		runMirrorApplication(message + ".4", sourceRepoLocation, destRepoLocation, true);
264
265
		try {
266
			//Setup: verify
267
			assertContains(message + ".5", getMetadataRepositoryManager().loadRepository(sourceRepoLocation.toURI(), null), getMetadataRepositoryManager().loadRepository(destRepoLocation.toURI(), null));
268
			assertContains(message + ".6", getMetadataRepositoryManager().loadRepository(sourceRepo2Location.toURI(), null), getMetadataRepositoryManager().loadRepository(destRepoLocation.toURI(), null));
269
		} catch (ProvisionException e) {
270
			fail(message + ".7", e);
271
		}
272
273
		//mirror duplicate data
274
		runMirrorApplication(message + ".7", sourceRepo3Location, destRepoLocation, append);
275
	}
276
277
	/**
278
	 * Tests mirroring all artifacts in a repository to an empty repository
279
	 * Source contains A, B
280
	 * Target contains
281
	 */
282
	private File metadataMirrorEmpty(String message, boolean append) {
283
		//Setup: Create an empty repository
284
		File emptyRepository = new File(getTempFolder(), getUniqueString());
285
		//Setup: remove repository if it exists
286
		getMetadataRepositoryManager().removeRepository(emptyRepository.toURI());
287
		//Setup: delete any data that may be in the folder
288
		AbstractProvisioningTest.delete(emptyRepository);
289
		try {
290
			getMetadataRepositoryManager().createRepository(emptyRepository.toURI(), "Empty Repository", IMetadataRepositoryManager.TYPE_SIMPLE_REPOSITORY, null);
291
		} catch (ProvisionException e) {
292
			fail(message + ".1", e);
293
		}
294
295
		try {
296
			basicRunMirrorApplication(message + ".0", emptyRepository.toURL(), destRepoLocation.toURL(), append);
297
		} catch (Exception e) {
298
			exception = e;
299
		}
300
301
		return emptyRepository; //return the repository for use in verification
302
	}
303
304
	/**
305
	 * Tests mirroring all metadata from an empty repository
306
	 * Source contains
307
	 */
308
	private File metadataMirrorEmptyToPopulated(String message, boolean append) {
309
		//Setup: Populate the repository
310
		runMirrorApplication(message + ".0", sourceRepoLocation, destRepoLocation, false);
311
312
		return metadataMirrorEmpty(message + ".1", append); //create the empty repository, perform the mirror, pass the result back
313
	}
314
315
	/**
316
	 * Tests mirroring all metadata in a repository to an empty repository 
317
	 * Source contains A, B
318
	 * Target contains
319
	 * Expected is A, B
320
	 */
321
	public void testMetadataMirrorToEmpty() {
322
		metadataMirrorToEmpty("1.0", true); //run the test with append set to true
323
324
		try {
325
			//verify destination's content
326
			assertContentEquals("1.1", getMetadataRepositoryManager().loadRepository(sourceRepoLocation.toURI(), null), getMetadataRepositoryManager().loadRepository(destRepoLocation.toURI(), null));
327
		} catch (ProvisionException e) {
328
			fail("1.2", e);
329
		}
330
	}
331
332
	/**
333
	 * Tests mirroring all metadata in a repository to an empty repository with "-writeMode clean"
334
	 * Source contains A, B
335
	 * Target contains
336
	 * Expected is A, B
337
	 */
338
	public void testMetadataMirrorToEmptyWithClean() {
339
		metadataMirrorToEmpty("2.0", false); //run the test with append set to false
340
341
		try {
342
			//verify destination's content
343
			assertContentEquals("2.1", getMetadataRepositoryManager().loadRepository(sourceRepoLocation.toURI(), null), getMetadataRepositoryManager().loadRepository(destRepoLocation.toURI(), null));
344
		} catch (ProvisionException e) {
345
			fail("2.2", e);
346
		}
347
	}
348
349
	/**
350
	 * Tests mirroring all metadata in a repository to a repository populated with exact duplicate data
351
	 * Source contains A, B
352
	 * Target contains A, B
353
	 * Expected is A, B
354
	 */
355
	public void testMetadataMirrorToFullDuplicate() {
356
		metadataMirrorToFullDuplicate("3.0", true); //run the test with append set to true
357
358
		try {
359
			//verify destination's content
360
			assertContentEquals("3.1", getMetadataRepositoryManager().loadRepository(sourceRepoLocation.toURI(), null), getMetadataRepositoryManager().loadRepository(destRepoLocation.toURI(), null));
361
		} catch (ProvisionException e) {
362
			fail("3.2", e);
363
		}
364
	}
365
366
	/**
367
	 * Tests mirroring all metadata in a repository to a repository populated with exact duplicate data with "-writeMode clean"
368
	 * Source contains A, B
369
	 * Target contains A, B
370
	 * Expected is A, B
371
	 */
372
	public void testMetadataMirrorToFullDuplicateWithClean() {
373
		metadataMirrorToFullDuplicate("4.0", false); //run the test with append set to false
374
375
		try {
376
			//verify destination's content
377
			assertContentEquals("4.1", getMetadataRepositoryManager().loadRepository(sourceRepoLocation.toURI(), null), getMetadataRepositoryManager().loadRepository(destRepoLocation.toURI(), null));
378
		} catch (ProvisionException e) {
379
			fail("4.2", e);
380
		}
381
	}
382
383
	/**
384
	 * Tests mirroring all metadata in a repository to a repository populated with non-duplicate entries
385
	 * Source contains A, B
386
	 * Target contains C, D
387
	 * Expected is A, B, C, D
388
	 */
389
	public void testMetadataMirrorToPopulated() {
390
		metadataMirrorToPopulated("5.0", true); //run the test with append set to true
391
392
		try {
393
			//verify destination's content
394
			assertContains("5.1", getMetadataRepositoryManager().loadRepository(sourceRepoLocation.toURI(), null), getMetadataRepositoryManager().loadRepository(destRepoLocation.toURI(), null));
395
			assertContains("5.2", getMetadataRepositoryManager().loadRepository(sourceRepo2Location.toURI(), null), getMetadataRepositoryManager().loadRepository(destRepoLocation.toURI(), null));
396
			//checks that the destination has the correct number of keys (no extras)
397
			assertEquals("5.3", getNumUnique(getMetadataRepositoryManager().loadRepository(sourceRepoLocation.toURI(), null).query(InstallableUnitQuery.ANY, new Collector(), null), getMetadataRepositoryManager().loadRepository(sourceRepo2Location.toURI(), null).query(InstallableUnitQuery.ANY, new Collector(), null)), getMetadataRepositoryManager().loadRepository(destRepoLocation.toURI(), null).query(InstallableUnitQuery.ANY, new Collector(), null).size());
398
		} catch (ProvisionException e) {
399
			fail("5.4", e);
400
		}
401
	}
402
403
	/**
404
	 * Tests mirroring all metadata in a repository to a repository populated with non-duplicate entries with "-writeMode clean"
405
	 * Source contains A, B
406
	 * Target contains C, D
407
	 * Expected is A, B
408
	 */
409
	public void testMetadataMirrorToPopulatedWithClean() {
410
		metadataMirrorToPopulated("6.0", false); //run the test with append set to false
411
412
		try {
413
			//verify destination's content
414
			assertContentEquals("6.1", getMetadataRepositoryManager().loadRepository(sourceRepoLocation.toURI(), null), getMetadataRepositoryManager().loadRepository(destRepoLocation.toURI(), null));
415
		} catch (ProvisionException e) {
416
			fail("6.2", e);
417
		}
418
	}
419
420
	/**
421
	 * Tests mirroring all metadata in a repository to a repository populated with partially duplicate data
422
	 * Source contains A, B, C, D
423
	 * Target contains  A, B
424
	 * Expected is A, B, C, D
425
	 */
426
	public void testMetadataMirrorToPartialDuplicate() {
427
		metadataMirrorToPartialDuplicate("7.0", true); //run the test with append set to true
428
429
		try {
430
			//verify destination's content
431
			assertContentEquals("7.1", getMetadataRepositoryManager().loadRepository(sourceRepo3Location.toURI(), null), getMetadataRepositoryManager().loadRepository(destRepoLocation.toURI(), null));
432
		} catch (ProvisionException e) {
433
			fail("7.2", e);
434
		}
435
	}
436
437
	/**
438
	 * Tests mirroring all metadata in a repository to a repository populated with partially duplicate data with "-writeMode clean"
439
	 * Source contains A, B, C, D
440
	 * Target contains  A, B
441
	 * Expected is A, B, C, D
442
	 */
443
	public void testMetadataMirrorToPartialDuplicateWithClean() {
444
		metadataMirrorToPartialDuplicate("8.0", false); //run the test with append set to false
445
446
		try {
447
			//verify destination's content
448
			assertContentEquals("8.1", getMetadataRepositoryManager().loadRepository(sourceRepo3Location.toURI(), null), getMetadataRepositoryManager().loadRepository(destRepoLocation.toURI(), null));
449
		} catch (ProvisionException e) {
450
			fail("8.2", e);
451
		}
452
	}
453
454
	/**
455
	 * Tests mirroring all metadata in a repository to a repository populated with both full duplicate and non-duplicate data
456
	 * Source contains A, B
457
	 * Target contains A, B, C, D
458
	 * Expected is A, B, C, D
459
	 */
460
	public void testMetadataMirrorToPopulatedWithFullDuplicate() {
461
		metadataMirrorToPopulatedWithFullDuplicate("9.0", true); //run the test with append set to true
462
463
		try {
464
			//verify destination's content
465
			assertContentEquals("9.1", getMetadataRepositoryManager().loadRepository(sourceRepo3Location.toURI(), null), getMetadataRepositoryManager().loadRepository(destRepoLocation.toURI(), null));
466
		} catch (ProvisionException e) {
467
			fail("9.2", e);
468
		}
469
	}
470
471
	/**
472
	 * Tests mirroring all metadata in a repository to a repository populated with both full duplicate and non-duplicate data with "-writeMode clean"
473
	 * Source contains A, B
474
	 * Target contains A, B, C, D
475
	 * Expected is A, B
476
	 */
477
	public void testMetadataMirrorToPopulatedWithFullDuplicateWithClean() {
478
		metadataMirrorToPopulatedWithFullDuplicate("10.0", false); //run the test with append set to false
479
480
		try {
481
			//verify destination's content
482
			assertContentEquals("10.1", getMetadataRepositoryManager().loadRepository(sourceRepoLocation.toURI(), null), getMetadataRepositoryManager().loadRepository(destRepoLocation.toURI(), null));
483
		} catch (ProvisionException e) {
484
			fail("10.2", e);
485
		}
486
	}
487
488
	/**
489
	 * Tests mirroring all metadata in a repository to a repository populated with both partial duplicate and non-duplicate data
490
	 * Source contains A, B, C, D
491
	 * Target contains A, B, E, F
492
	 * Expected is A, B, C, D, E, F
493
	 */
494
	public void testMetadataMirrorToPopulatedWithPartialDuplicate() {
495
		metadataMirrorToPopulatedWithPartialDuplicate("11.0", true); //run the test with append set to true
496
497
		try {
498
			//verify destination's content
499
			assertContains("11.1", getMetadataRepositoryManager().loadRepository(sourceRepo3Location.toURI(), null), getMetadataRepositoryManager().loadRepository(destRepoLocation.toURI(), null));
500
			assertContains("11.2", getMetadataRepositoryManager().loadRepository(sourceRepo2Location.toURI(), null), getMetadataRepositoryManager().loadRepository(destRepoLocation.toURI(), null));
501
			//checks that the destination has the correct number of keys (no extras)
502
			assertEquals("11.3", getNumUnique(getMetadataRepositoryManager().loadRepository(sourceRepo2Location.toURI(), null).query(InstallableUnitQuery.ANY, new Collector(), null), getMetadataRepositoryManager().loadRepository(sourceRepo3Location.toURI(), null).query(InstallableUnitQuery.ANY, new Collector(), null)), getMetadataRepositoryManager().loadRepository(destRepoLocation.toURI(), null).query(InstallableUnitQuery.ANY, new Collector(), null).size());
503
		} catch (ProvisionException e) {
504
			fail("11.4", e);
505
		}
506
	}
507
508
	/**
509
	 * Tests mirroring all metadata in a repository to a repository populated with both partial duplicate and non-duplicate data with "-writeMode clean"
510
	 * Source contains A, B, C, D
511
	 * Target contains A, B, E, F
512
	 * Expected is A, B, C, D
513
	 */
514
	public void testMetadataMirrorToPopulatedWithPartialDuplicateWithClean() {
515
		metadataMirrorToPopulatedWithPartialDuplicate("12.0", false); //run the test with append set to false
516
517
		try {
518
			//verify destination's content
519
			assertContentEquals("12.1", getMetadataRepositoryManager().loadRepository(sourceRepo3Location.toURI(), null), getMetadataRepositoryManager().loadRepository(destRepoLocation.toURI(), null));
520
		} catch (ProvisionException e) {
521
			fail("12.2", e);
522
		}
523
	}
524
525
	/**
526
	 * Tests MirrorApplication's behaviour when given an invalid source repository
527
	 */
528
	public void testMetadataMirrorFromInvalid() {
529
		//get a temp folder
530
		File invalidRepository = new File(getTempFolder(), getUniqueString());
531
		//delete any data that may exist in that temp folder
532
		delete(invalidRepository);
533
534
		try {
535
			basicRunMirrorApplication("13.1", invalidRepository.toURL(), destRepoLocation.toURL(), true);
536
			//we expect a provisioning exception to be thrown and should never get here
537
			fail("13.0 ProvisionExpection not thrown");
538
		} catch (ProvisionException e) {
539
			return; //correct type of exception has been received
540
		} catch (Exception e) {
541
			fail("13.2", e);
542
		}
543
	}
544
545
	/**
546
	 * Tests MirrorApplication's behaviour when given an invalid destination repository
547
	 */
548
	public void testMetadataMirrorToInvalid() {
549
		URI invalidDestRepository = null;
550
		try {
551
			invalidDestRepository = new URI("http://foobar.com/abcdefg");
552
			basicRunMirrorApplication("14.1", sourceRepoLocation.toURL(), invalidDestRepository.toURL(), true);
553
			//we expect an illegal state exception to be thrown and should never get here
554
			fail("14.0 IllegalStateExpection not thrown");
555
		} catch (ProvisionException e) {
556
			assertEquals("Unexpected error message", NLS.bind(org.eclipse.equinox.p2.internal.repository.tools.Messages.exception_invalidDestination, URIUtil.toUnencodedString(invalidDestRepository)), e.getMessage());
557
			return; //correct type of exception has been thrown
558
		} catch (Exception e) {
559
			fail("14.1", e);
560
		} finally {
561
			if (invalidDestRepository != null)
562
				getArtifactRepositoryManager().removeRepository(invalidDestRepository);
563
		}
564
	}
565
566
	/**
567
	 * Tests MirrorApplication's behaviour when given both an invalid source and an invalid destination repository
568
	 */
569
	public void testMetadataMirrorBothInvalid() {
570
		File invalidRepository = new File(getTempFolder(), getUniqueString());
571
		delete(invalidRepository);
572
573
		try {
574
			URI invalidDestRepository = new URI("http://foobar.com/abcdefg");
575
			basicRunMirrorApplication("15.1", invalidRepository.toURL(), invalidDestRepository.toURL(), true);
576
			//we expect a provisioning exception to be thrown and should never get here
577
			fail("15.0 ProvisionExpection not thrown");
578
		} catch (ProvisionException e) {
579
			return; //correct type of exception has been thrown
580
		} catch (Exception e) {
581
			fail("15.2", e);
582
		}
583
	}
584
585
	/**
586
	 * Tests mirroring an empty repository to another empty repository
587
	 * Source contains
588
	 * Target contains
589
	 * Expected is
590
	 */
591
	public void testMetadataMirrorEmptyToEmpty() {
592
		File emptyRepository = null;
593
		try {
594
			emptyRepository = metadataMirrorEmptyToPopulated("19.0", false);
595
			assertTrue("Unexpected exception type", exception instanceof ProvisionException);
596
597
			try {
598
				//verify destination's content
599
				assertContentEquals("16.1", getMetadataRepositoryManager().loadRepository(emptyRepository.toURI(), null), getMetadataRepositoryManager().loadRepository(destRepoLocation.toURI(), null));
600
			} catch (ProvisionException e) {
601
				fail("16.2", e);
602
			}
603
		} finally {
604
			if (emptyRepository != null) {
605
				//remove the empty repository
606
				getMetadataRepositoryManager().removeRepository(emptyRepository.toURI());
607
				//delete any leftover data
608
				delete(emptyRepository);
609
			}
610
		}
611
	}
612
613
	/**
614
	 * Tests mirroring an empty repository to a populated repository
615
	 * Source contains
616
	 * Target contains A, B
617
	 * Expected is A, B
618
	 */
619
	public void testArtifactMirrorEmptyToPopulated() {
620
		File emptyRepository = null;
621
		try {
622
			emptyRepository = metadataMirrorEmptyToPopulated("17.0", true);
623
			assertTrue("Unexpected exception type", exception instanceof ProvisionException);
624
625
			try {
626
				//verify destination's content
627
				assertContains("17.1", getMetadataRepositoryManager().loadRepository(emptyRepository.toURI(), null), getMetadataRepositoryManager().loadRepository(destRepoLocation.toURI(), null));
628
				assertContentEquals("17.2", getMetadataRepositoryManager().loadRepository(sourceRepoLocation.toURI(), null), getMetadataRepositoryManager().loadRepository(destRepoLocation.toURI(), null));
629
			} catch (ProvisionException e) {
630
				fail("17.3", e);
631
			}
632
		} finally {
633
			if (emptyRepository != null) {
634
				//remove the empty repository
635
				getMetadataRepositoryManager().removeRepository(emptyRepository.toURI());
636
				//delete any leftover data
637
				delete(emptyRepository);
638
			}
639
		}
640
	}
641
642
	/**
643
	 * Tests mirroring an empty repository to a populated repository with "-writeMode clean"
644
	 * Source contains
645
	 * Target contains A, B
646
	 * Expected is
647
	 */
648
	public void testArtifactMirrorEmptyToPopulatedWithClean() {
649
		File emptyRepository = null;
650
		try {
651
			emptyRepository = metadataMirrorEmptyToPopulated("18.0", false);
652
			assertTrue("Unexpected exception type", exception instanceof ProvisionException);
653
654
			try {
655
				//verify destination's content
656
				assertContentEquals("18.1", getMetadataRepositoryManager().loadRepository(emptyRepository.toURI(), null), getMetadataRepositoryManager().loadRepository(destRepoLocation.toURI(), null));
657
			} catch (ProvisionException e) {
658
				fail("18.2", e);
659
			}
660
		} finally {
661
			//remove the empty repository
662
			getMetadataRepositoryManager().removeRepository(emptyRepository.toURI());
663
			//delete any leftover data
664
			delete(emptyRepository);
665
		}
666
	}
667
668
	/**
669
	 * Tests mirroring a repository to itself
670
	 * Source contains A, B
671
	 * Target contains A, B
672
	 * Expected is A, B
673
	 */
674
	public void testArtifactMirrorSourceIsDestination() {
675
		//Setup: Populate the repository
676
		runMirrorApplication("19.0", sourceRepoLocation, destRepoLocation, false);
677
678
		//run the mirror application with the source being the same as the destination
679
		runMirrorApplication("19.1", destRepoLocation, destRepoLocation, true);
680
681
		try {
682
			//verify destination's content
683
			assertContentEquals("19.2", getMetadataRepositoryManager().loadRepository(sourceRepoLocation.toURI(), null), getMetadataRepositoryManager().loadRepository(destRepoLocation.toURI(), null));
684
		} catch (ProvisionException e) {
685
			fail("19.3", e);
686
		}
687
	}
688
689
	/**
690
	 * Tests mirroring a repository with a different version of the same package
691
	 * Source contains A, B (v1.0.1)
692
	 * Target contains A, B (v1.0.0)
693
	 * Expected is A, B (v1.0.0) and A, B (v1.0.1)
694
	 */
695
	public void testArtifactMirrorDifferentVersions() {
696
		//Setup: Populate the repository
697
		runMirrorApplication("20.0", sourceRepoLocation, destRepoLocation, false);
698
699
		//start a mirror application where the source contains the same artifacts but with a different version compared to the destination
700
		runMirrorApplication("20.1", sourceRepo4Location, destRepoLocation, true);
701
702
		try {
703
			//verify destination's content
704
			assertContains("20.2", getMetadataRepositoryManager().loadRepository(sourceRepoLocation.toURI(), null), getMetadataRepositoryManager().loadRepository(destRepoLocation.toURI(), null));
705
			assertContains("20.3", getMetadataRepositoryManager().loadRepository(sourceRepo4Location.toURI(), null), getMetadataRepositoryManager().loadRepository(destRepoLocation.toURI(), null));
706
			//checks that the destination has the correct number of keys (no extras)
707
			assertEquals("20.4", getNumUnique(getMetadataRepositoryManager().loadRepository(sourceRepoLocation.toURI(), null).query(InstallableUnitQuery.ANY, new Collector(), null), getMetadataRepositoryManager().loadRepository(sourceRepo4Location.toURI(), null).query(InstallableUnitQuery.ANY, new Collector(), null)), getMetadataRepositoryManager().loadRepository(destRepoLocation.toURI(), null).query(InstallableUnitQuery.ANY, new Collector(), null).size());
708
		} catch (ProvisionException e) {
709
			fail("20.5", e);
710
		}
711
	}
712
713
	/**
714
	 * Tests how mirror application handles an unspecified source
715
	 */
716
	public void testArtifactMirrorNullSource() {
717
		try {
718
			basicRunMirrorApplication("21.1", null, destRepoLocation.toURL(), true);
719
			//We expect the IllegalStateException to be thrown
720
			fail("21.3 IllegalStateException not thrown");
721
		} catch (ProvisionException e) {
722
			return; //expected type of exception has been thrown
723
		} catch (Exception e) {
724
			fail("21.2", e);
725
		}
726
	}
727
728
	/**
729
	 * Tests how mirror application handles an unspecified destination
730
	 */
731
	public void testArtifactMirrorNullDestination() {
732
		try {
733
			basicRunMirrorApplication("21.1", sourceRepoLocation.toURL(), null, true);
734
			//We expect the IllegalStateException to be thrown
735
			fail("22.3 IllegalStateException not thrown");
736
		} catch (ProvisionException e) {
737
			return; //expected type of exception has been thrown
738
		} catch (Exception e) {
739
			fail("22.2", e);
740
		}
741
	}
742
743
	/**
744
	 * Tests how mirror application handles both an unspecified source and an unspecified destination
745
	 */
746
	public void testArtifactMirrorNullBoth() {
747
		try {
748
			basicRunMirrorApplication("23.0", null, null, true);
749
			//We expect the IllegalStateException to be thrown
750
			fail("23.2 IllegalStateException not thrown");
751
		} catch (ProvisionException e) {
752
			return; //expected type of exception has been thrown
753
		} catch (Exception e) {
754
			fail("23.1", e);
755
		}
756
	}
757
758
	/**
759
	 * Ensures that a repository created before the mirror application is run does not have its properties changed
760
	 */
761
	public void testExistingArtifactRepoProperties() {
762
		//Setup: create the destination
763
		String name = "Destination Name";
764
		Map properties = null; //default properties
765
		try {
766
			//create the repository and get the resulting properties
767
			properties = getMetadataRepositoryManager().createRepository(destRepoLocation.toURI(), name, IMetadataRepositoryManager.TYPE_SIMPLE_REPOSITORY, properties).getProperties();
768
		} catch (ProvisionException e) {
769
			fail("25.0", e);
770
		}
771
772
		//run the mirror application
773
		metadataMirrorToEmpty("25.2", true);
774
775
		try {
776
			IMetadataRepository repository = getMetadataRepositoryManager().loadRepository(destRepoLocation.toURI(), null);
777
			assertEquals("25.3", name, repository.getName());
778
			assertRepositoryProperties("25.4", properties, repository.getProperties());
779
		} catch (ProvisionException e) {
780
			fail("25.5", e);
781
		}
782
	}
783
784
	/**
785
	 * Ensures that a repository created by the mirror application has specified name
786
	 * For Bug 256909
787
	 */
788
	public void testNewArtifactRepoWithNewName() {
789
		String name = "Bug 256909 test - new";
790
		try {
791
			//set the arguments
792
			//run the mirror application
793
			basicRunMirrorApplication("Bug 256909 Test", sourceRepoLocation.toURL(), destRepoLocation.toURL(), false, name);
794
		} catch (MalformedURLException e) {
795
			fail("Error creating URLs for Source/Detination", e);
796
		} catch (Exception e) {
797
			fail("Error running mirror application", e);
798
		}
799
800
		try {
801
			assertEquals("Assert name was set correct", name, getMetadataRepositoryManager().loadRepository(destRepoLocation.toURI(), null).getName());
802
		} catch (ProvisionException e) {
803
			fail("Cannot obtain destination", e);
804
		}
805
	}
806
807
	/**
808
	 * Ensures that an existing destination used by the mirror application is given specified name
809
	 * For Bug 256909
810
	 */
811
	public void testExistingArtifactRepoWithNewName() {
812
		String oldName = "The original naem for Bug 256909 test - existing";
813
		String newName = "Bug 256909 test - existing";
814
		//Setup create the repository
815
		IMetadataRepository destinationRepo = null;
816
		try {
817
			destinationRepo = getMetadataRepositoryManager().createRepository(destRepoLocation.toURI(), oldName, IMetadataRepositoryManager.TYPE_SIMPLE_REPOSITORY, null);
818
			assertTrue(destinationRepo.isModifiable());
819
		} catch (ProvisionException e) {
820
			fail("Error creating repo at destination", e);
821
		}
822
		assertEquals("Assert name is set correctly before mirror", oldName, destinationRepo.getName());
823
824
		try {
825
			basicRunMirrorApplication("Bug 256809 Test", sourceRepoLocation.toURL(), destRepoLocation.toURL(), true, newName);
826
		} catch (MalformedURLException e) {
827
			fail("Error creating URLs for Source/Detination", e);
828
		} catch (Exception e) {
829
			fail("Error running mirror application", e);
830
		}
831
832
		try {
833
			assertEquals("Assert name is set correctly after mirror", newName, getMetadataRepositoryManager().loadRepository(destRepoLocation.toURI(), null).getName());
834
		} catch (ProvisionException e) {
835
			fail("Error loading destination", e);
836
		}
837
	}
838
839
	//for Bug 235683
840
	public void testMirrorCompressedSource() {
841
		File compressedSource = getTestData("0", "/testData/mirror/mirrorCompressedRepo");
842
843
		//Setup: get the content.jar file
844
		File compressedMetadataXML = new File(compressedSource.getAbsoluteFile() + "/content.jar");
845
		//Setup: make sure content.jar exists
846
		assertTrue("1", compressedMetadataXML.exists());
847
848
		try {
849
			basicRunMirrorApplication("2", compressedSource.toURL(), destRepoLocation.toURL(), false);
850
		} catch (MalformedURLException e) {
851
			fail("3", e);
852
		} catch (Exception e) {
853
			fail("4", e);
854
		}
855
856
		//get the content.jar file
857
		File destMetadataXML = new File(destRepoLocation.getAbsolutePath() + "/content.jar");
858
		//make sure content.jar exists
859
		assertTrue("5", destMetadataXML.exists());
860
	}
861
862
	//for Bug 235683
863
	public void testMirrorCompressedSourcetoUncompressedDestination() {
864
		File compressedSource = getTestData("0", "/testData/mirror/mirrorCompressedRepo");
865
866
		//Setup: get the content.jar file
867
		File compressedMetadataXML = new File(compressedSource.getAbsoluteFile() + "/content.jar");
868
		//Setup: make sure content.jar exists
869
		assertTrue("1", compressedMetadataXML.exists());
870
871
		//Setup: create the destination
872
		try {
873
			String name = "Destination Name " + destRepoLocation;
874
			getMetadataRepositoryManager().createRepository(destRepoLocation.toURI(), name, IMetadataRepositoryManager.TYPE_SIMPLE_REPOSITORY, null);
875
		} catch (ProvisionException e) {
876
			fail("2", e);
877
		}
878
879
		try {
880
			basicRunMirrorApplication("3", compressedSource.toURL(), destRepoLocation.toURL(), false);
881
		} catch (MalformedURLException e) {
882
			fail("4", e);
883
		} catch (Exception e) {
884
			fail("5", e);
885
		}
886
887
		//get the content.jar file
888
		File destMetadataXML = new File(destRepoLocation.getAbsolutePath() + "/content.jar");
889
		//make sure content.jar does not exist
890
		assertFalse("6", destMetadataXML.exists());
891
		//get the content.xml file
892
		destMetadataXML = new File(destRepoLocation.getAbsolutePath() + "/content.xml");
893
		//make sure content.xml exists
894
		assertTrue("7", destMetadataXML.exists());
895
	}
896
897
	public void testMirrorUncompressedSourceToCompressedDestination() {
898
		File uncompressedSource = getTestData("0", "/testData/mirror/mirrorSourceRepo3");
899
900
		//Setup: get the content.xml file
901
		File uncompressedContentXML = new File(uncompressedSource.getAbsoluteFile() + "/content.xml");
902
		//Setup: make sure content.xml exists
903
		assertTrue("1", uncompressedContentXML.exists());
904
905
		//Setup: create the destination
906
		try {
907
			String name = "Destination Name " + destRepoLocation;
908
			Map property = new HashMap();
909
			property.put(IRepository.PROP_COMPRESSED, "true");
910
			getMetadataRepositoryManager().createRepository(destRepoLocation.toURI(), name, IMetadataRepositoryManager.TYPE_SIMPLE_REPOSITORY, property);
911
		} catch (ProvisionException e) {
912
			fail("2", e);
913
		}
914
915
		assertTrue("2.1", new File(destRepoLocation, "content.jar").exists());
916
		try {
917
			basicRunMirrorApplication("3", uncompressedSource.toURL(), destRepoLocation.toURL(), false);
918
		} catch (MalformedURLException e) {
919
			fail("4", e);
920
		} catch (Exception e) {
921
			fail("5", e);
922
		}
923
924
		//get the content.jar file
925
		File destMetadataXML = new File(destRepoLocation.getAbsolutePath() + "/content.jar");
926
		//make sure content.jar does exist
927
		assertTrue("6", destMetadataXML.exists());
928
		//get the content.xml file
929
		destMetadataXML = new File(destRepoLocation.getAbsolutePath() + "/content.xml");
930
		//make sure content.xml exists
931
		assertFalse("7", destMetadataXML.exists());
932
	}
933
934
	public void testMirrorApplicationWithCompositeSource() {
935
		//Setup Make composite repository
936
		File repoLocation = new File(getTempFolder(), "CompositeMetadataMirrorTest");
937
		AbstractProvisioningTest.delete(repoLocation);
938
		IMetadataRepository repo = null;
939
		try {
940
			repo = getMetadataRepositoryManager().createRepository(repoLocation.toURI(), "metadata name", IMetadataRepositoryManager.TYPE_COMPOSITE_REPOSITORY, null);
941
		} catch (ProvisionException e) {
942
			fail("Could not create repository");
943
		}
944
		//ensure proper type of repository has been created
945
		if (!(repo instanceof CompositeMetadataRepository))
946
			fail("Repository is not a CompositeMetadataRepository");
947
		//Populate source
948
		File child1 = getTestData("1", "/testData/mirror/mirrorSourceRepo1 with space");
949
		File child2 = getTestData("2", "/testData/mirror/mirrorSourceRepo2");
950
		((CompositeMetadataRepository) repo).addChild(child1.toURI());
951
		((CompositeMetadataRepository) repo).addChild(child2.toURI());
952
953
		runMirrorApplication("Mirroring from Composite Source", repoLocation, destRepoLocation, false);
954
955
		try {
956
			assertContentEquals("Verifying contents", repo, getMetadataRepositoryManager().loadRepository(destRepoLocation.toURI(), null));
957
958
			//Verify that result is the same as mirroring from the 2 repositories seperately
959
			assertContains("3", getMetadataRepositoryManager().loadRepository(sourceRepoLocation.toURI(), null), getMetadataRepositoryManager().loadRepository(destRepoLocation.toURI(), null));
960
			assertContains("4", getMetadataRepositoryManager().loadRepository(sourceRepo2Location.toURI(), null), getMetadataRepositoryManager().loadRepository(destRepoLocation.toURI(), null));
961
			//checks that the destination has the correct number of keys (no extras)
962
			assertEquals("5", getNumUnique(getMetadataRepositoryManager().loadRepository(sourceRepoLocation.toURI(), null).query(InstallableUnitQuery.ANY, new Collector(), null), getMetadataRepositoryManager().loadRepository(sourceRepo2Location.toURI(), null).query(InstallableUnitQuery.ANY, new Collector(), null)), getMetadataRepositoryManager().loadRepository(destRepoLocation.toURI(), null).query(InstallableUnitQuery.ANY, new Collector(), null).size());
963
		} catch (ProvisionException e) {
964
			fail("Could not load destination", e);
965
		}
966
	}
967
}
(-)src/org/eclipse/equinox/p2/tests/ant/MirrorTaskTest.java (+656 lines)
Added Link Here
1
/*******************************************************************************
2
 *  Copyright (c) 2009 IBM Corporation 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
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.equinox.p2.tests.ant;
12
13
import java.io.*;
14
import java.net.URI;
15
import java.net.URISyntaxException;
16
import java.util.Iterator;
17
import java.util.Properties;
18
import org.eclipse.core.runtime.*;
19
import org.eclipse.equinox.internal.p2.artifact.processors.md5.Messages;
20
import org.eclipse.equinox.internal.p2.director.PermissiveSlicer;
21
import org.eclipse.equinox.internal.p2.metadata.InstallableUnit;
22
import org.eclipse.equinox.internal.provisional.p2.artifact.repository.*;
23
import org.eclipse.equinox.internal.provisional.p2.core.ProvisionException;
24
import org.eclipse.equinox.internal.provisional.p2.core.Version;
25
import org.eclipse.equinox.internal.provisional.p2.metadata.IArtifactKey;
26
import org.eclipse.equinox.internal.provisional.p2.metadata.IInstallableUnit;
27
import org.eclipse.equinox.internal.provisional.p2.metadata.query.InstallableUnitQuery;
28
import org.eclipse.equinox.internal.provisional.p2.metadata.repository.IMetadataRepository;
29
import org.eclipse.equinox.internal.provisional.p2.metadata.repository.IMetadataRepositoryManager;
30
import org.eclipse.equinox.internal.provisional.p2.query.Collector;
31
import org.eclipse.equinox.internal.provisional.p2.query.IQueryable;
32
import org.eclipse.equinox.p2.tests.AbstractAntProvisioningTest;
33
import org.eclipse.equinox.p2.tests.AbstractProvisioningTest;
34
import org.eclipse.equinox.spi.p2.publisher.PublisherHelper;
35
import org.eclipse.osgi.util.NLS;
36
37
public class MirrorTaskTest extends AbstractAntProvisioningTest {
38
	private static final String MIRROR_TASK = "p2.mirror";
39
	private URI destinationRepo;
40
	private URI artifactRepo, sliceArtifactRepo, sliceRepo, sourceRepo2, zipRepo;
41
42
	public void setUp() throws Exception {
43
		super.setUp();
44
		// Get a random location to create a repository
45
		destinationRepo = (new File(getTempFolder(), getUniqueString())).toURI();
46
		artifactRepo = getTestData("error loading data", "testData/mirror/mirrorPackedRepo").toURI();
47
		sourceRepo2 = getTestData("error loading data", "testData/mirror/mirrorSourceRepo2").toURI();
48
		sliceRepo = getTestData("error loading data", "testData/permissiveSlicer").toURI();
49
		sliceArtifactRepo = getTestData("error loading data", "testData/testRepos/updateSite").toURI();
50
		zipRepo = getTestData("error loading data", "/testData/mirror/zippedRepo.zip").toURI();
51
	}
52
53
	public void tearDown() throws Exception {
54
		// Remove repository manager references
55
		getArtifactRepositoryManager().removeRepository(destinationRepo);
56
		getMetadataRepositoryManager().removeRepository(destinationRepo);
57
		getArtifactRepositoryManager().removeRepository(sliceRepo);
58
		getMetadataRepositoryManager().removeRepository(sliceRepo);
59
		getArtifactRepositoryManager().removeRepository(sourceRepo2);
60
		getMetadataRepositoryManager().removeRepository(sourceRepo2);
61
		getArtifactRepositoryManager().removeRepository(zipRepo);
62
		getMetadataRepositoryManager().removeRepository(zipRepo);
63
		// Cleanup disk
64
		delete(new File(destinationRepo).getParentFile());
65
		super.tearDown();
66
	}
67
68
	/*
69
	 * Test that it is possible to mirror by only specifying an Artifact repository
70
	 */
71
	public void testMirrorArtifactOnly() {
72
		AntTaskElement mirror = createMirrorTask(TYPE_ARTIFACT);
73
		mirror.addElement(createSourceElement(artifactRepo, null));
74
		runAntTask();
75
76
		assertEquals("Different number of Artifact Keys", getArtifactKeyCount(artifactRepo), getArtifactKeyCount(destinationRepo));
77
		assertEquals("Different number of ArtifactDescriptors", getArtifactDescriptorCount(artifactRepo), getArtifactDescriptorCount(destinationRepo));
78
	}
79
80
	/*
81
	 * Test that it is possible to mirror when only specifying a Metadata repository 
82
	 */
83
	public void testMirrorMetadataOnly() {
84
		AntTaskElement mirror = createMirrorTask(TYPE_METADATA);
85
		mirror.addElement(createSourceElement(null, sourceRepo2));
86
		runAntTask();
87
88
		assertEquals("Different number of IUs", getIUCount(sourceRepo2), getIUCount(destinationRepo));
89
	}
90
91
	/*
92
	 * Test we can mirror from a zipped repository
93
	 */
94
	public void testMirrorFromZip() {
95
		URI localAddress = null;
96
		try {
97
			localAddress = URIUtil.fromString(new File(zipRepo).toString());
98
		} catch (URISyntaxException e) {
99
			fail("failed to convert zip repo location");
100
		}
101
		AntTaskElement mirror = createMirrorTask(TYPE_BOTH);
102
		mirror.addElement(createSourceElement(localAddress, localAddress));
103
		runAntTask();
104
105
		assertEquals("Wrong number of ArtifactKeys", getArtifactKeyCount(URIUtil.toJarURI(zipRepo, null)), getArtifactKeyCount(destinationRepo));
106
		assertEquals("Wrong number of ArtifactDescriptors", getArtifactDescriptorCount(URIUtil.toJarURI(zipRepo, null)), getArtifactDescriptorCount(destinationRepo));
107
		assertEquals("Different number of IUs", getIUCount(sourceRepo2), getIUCount(destinationRepo));
108
	}
109
110
	/*
111
	 * Test that all IUs can be mirrored
112
	 */
113
	public void testMirrorAllIUSpecified() throws ProvisionException {
114
		AntTaskElement mirror = createMirrorTask(TYPE_BOTH);
115
		mirror.addElement(createSourceElement(sourceRepo2, sourceRepo2));
116
		addAllIUs(mirror, getMetadataRepositoryManager().loadRepository(sourceRepo2, null));
117
		runAntTask();
118
119
		assertEquals("Different number of Artifact Keys", getArtifactKeyCount(sourceRepo2), getArtifactKeyCount(destinationRepo));
120
		assertContentEquals("IUs differ", getMetadataRepositoryManager().loadRepository(sourceRepo2, null), getMetadataRepositoryManager().loadRepository(destinationRepo, null));
121
		assertEquals("Different number of IUs", getIUCount(sourceRepo2), getIUCount(destinationRepo));
122
		assertContentEquals("Artifacts differ", getArtifactRepositoryManager().loadRepository(sourceRepo2, null), getArtifactRepositoryManager().loadRepository(destinationRepo, null));
123
	}
124
125
	/*
126
	 * Test that we only mirror specified IUs & Artifacts
127
	 */
128
	public void testMirrorSomeIUSpecified() {
129
		AntTaskElement mirror = createMirrorTask(TYPE_BOTH);
130
		mirror.addElement(createSourceElement(sourceRepo2, sourceRepo2));
131
		mirror.addElement(createIUElement("anotherplugin", "1.0.0"));
132
133
		runAntTask();
134
135
		assertEquals("Wrong number of ArtifactKeys", 1, getArtifactKeyCount(destinationRepo));
136
		assertEquals("Wrong number of IUs", 1, getIUCount(destinationRepo));
137
	}
138
139
	/*
140
	 * Test that the proper exception is thrown when no IU is provided
141
	 */
142
	public void testMirrorNoIUNoRepo() {
143
		AntTaskElement mirror = createMirrorTask(TYPE_BOTH);
144
		mirror.addElement(createSourceElement(sourceRepo2, null));
145
146
		Exception exception = null;
147
		try {
148
			runAntTaskWithExceptions();
149
		} catch (CoreException e) {
150
			exception = e;
151
		}
152
		if (exception == null)
153
			fail("No exception thrown");
154
		if (!(rootCause(exception) instanceof ProvisionException) && !rootCause(exception).getMessage().contains("No IUs"))
155
			fail("Exception is of an unexpected type or message", rootCause(exception));
156
157
	}
158
159
	/*
160
	 * Test that all IUs are mirrored when none are specified
161
	 */
162
	public void testMirrorNoIUSpecified() {
163
		AntTaskElement mirror = createMirrorTask(TYPE_BOTH);
164
		mirror.addElement(createSourceElement(sourceRepo2, sourceRepo2));
165
166
		runAntTask();
167
168
		try {
169
			assertEquals("Different number of Artifact Keys", getArtifactKeyCount(sourceRepo2), getArtifactKeyCount(destinationRepo));
170
			assertContentEquals("Artifacts differ", getArtifactRepositoryManager().loadRepository(sourceRepo2, null), getArtifactRepositoryManager().loadRepository(destinationRepo, null));
171
			assertEquals("Different number of IUs", getIUCount(sourceRepo2), getIUCount(destinationRepo));
172
			assertContentEquals("IUs differ", getMetadataRepositoryManager().loadRepository(sourceRepo2, null), getMetadataRepositoryManager().loadRepository(destinationRepo, null));
173
		} catch (ProvisionException e) {
174
			fail("Failed to compare results", e);
175
		}
176
	}
177
178
	/*
179
	 * Test the handling of invalid destinations with the mirror task
180
	 */
181
	public void testMirrorWithInvalidDestination() throws URISyntaxException {
182
		URI location = new URI("invalid:/scheme");
183
184
		AntTaskElement mirror = new AntTaskElement(MIRROR_TASK);
185
		mirror.addElement(getRepositoryElement(location, TYPE_BOTH));
186
		mirror.addElement(createSourceElement(sourceRepo2, sourceRepo2));
187
		addTask(mirror);
188
189
		Throwable exception = null;
190
		try {
191
			runAntTaskWithExceptions();
192
		} catch (Exception e) {
193
			exception = e;
194
		}
195
		if (exception == null)
196
			fail("No Exception thrown");
197
198
		while (exception.getCause() != null && !(exception instanceof ProvisionException))
199
			exception = exception.getCause();
200
		assertTrue("Unexpected error", NLS.bind(org.eclipse.equinox.p2.internal.repository.tools.Messages.exception_invalidDestination, location).equals(exception.getMessage()));
201
	}
202
203
	/*
204
	 * Test the handling of invalid destinations with the mirror task
205
	 */
206
	public void testMirrorWithInvalidSource() throws URISyntaxException {
207
		URI location = new URI("invalid:/scheme2");
208
209
		AntTaskElement mirror = createMirrorTask(TYPE_BOTH);
210
		mirror.addElement(createSourceElement(location, location));
211
		addTask(mirror);
212
213
		Throwable exception = null;
214
		try {
215
			runAntTaskWithExceptions();
216
		} catch (Exception e) {
217
			exception = e;
218
		}
219
		if (exception == null)
220
			fail("No Exception thrown");
221
222
		while (exception.getCause() != null && !(exception instanceof ProvisionException))
223
			exception = exception.getCause();
224
		assertTrue("Unexpected error", NLS.bind(org.eclipse.equinox.p2.internal.repository.tools.Messages.exception_invalidSource, location).equals(exception.getMessage()));
225
	}
226
227
	/*
228
	 * Test slicing options
229
	 */
230
	public void testSlicingFollowStrict() {
231
		IMetadataRepository repo = null;
232
		try {
233
			repo = loadMetadataRepository(sliceRepo);
234
		} catch (ProvisionException e) {
235
			fail("Loading repository failed", e);
236
		}
237
		Collector c = repo.query(new InstallableUnitQuery("org.eclipse.rcp.feature.group"), new Collector(), new NullProgressMonitor());
238
		IInstallableUnit iu = (IInstallableUnit) c.iterator().next();
239
240
		AntTaskElement mirror = createMirrorTask(TYPE_METADATA);
241
		mirror.addElement(createSourceElement(null, sliceRepo));
242
		mirror.addElement(createSlicingOption(null, null, true, null, "win32,win32,x86"));
243
		mirror.addElement(createIUElement(iu.getId(), iu.getVersion().toString()));
244
245
		runAntTask();
246
247
		Properties p = getSliceProperties();
248
		p.setProperty("org.eclipse.update.install.features", String.valueOf(true));
249
		PermissiveSlicer slicer = new PermissiveSlicer(repo, p, true, true, true, true);
250
		IQueryable result = slicer.slice(new IInstallableUnit[] {iu}, new NullProgressMonitor());
251
		assertEquals("Different number of IUs", result.query(InstallableUnitQuery.ANY, new Collector(), new NullProgressMonitor()).size(), getIUCount(destinationRepo));
252
253
		try {
254
			assertIUContentEquals("IUs differ", result, getMetadataRepositoryManager().loadRepository(destinationRepo, null));
255
		} catch (ProvisionException e) {
256
			fail("Failed to compare contents", e);
257
		}
258
	}
259
260
	public void testSlicingIncludeNonGreedy() {
261
		IMetadataRepository repo = null;
262
		try {
263
			repo = loadMetadataRepository(sliceRepo);
264
		} catch (ProvisionException e) {
265
			fail("Loading repository failed", e);
266
		}
267
		Collector c = repo.query(new InstallableUnitQuery("org.eclipse.rcp.feature.group"), new Collector(), new NullProgressMonitor());
268
		IInstallableUnit iu = (IInstallableUnit) c.iterator().next();
269
270
		AntTaskElement mirror = createMirrorTask(TYPE_METADATA);
271
		mirror.addElement(createSourceElement(null, sliceRepo));
272
		mirror.addElement(createSlicingOption(null, false, null, null, null));
273
		mirror.addElement(createIUElement(iu.getId(), iu.getVersion().toString()));
274
275
		runAntTask();
276
277
		PermissiveSlicer slicer = new PermissiveSlicer(repo, new Properties(), true, false, true, false);
278
		IQueryable result = slicer.slice(new IInstallableUnit[] {iu}, new NullProgressMonitor());
279
280
		assertEquals("Different number of IUs", result.query(InstallableUnitQuery.ANY, new Collector(), new NullProgressMonitor()).size(), getIUCount(destinationRepo));
281
		try {
282
			assertIUContentEquals("IUs differ", result, getMetadataRepositoryManager().loadRepository(destinationRepo, null));
283
		} catch (ProvisionException e) {
284
			fail("Failed to compare contents", e);
285
		}
286
	}
287
288
	public void testSlicingIncludeOptionalDependencies() {
289
		IMetadataRepository repo = null;
290
		try {
291
			repo = loadMetadataRepository(sliceRepo);
292
		} catch (ProvisionException e) {
293
			fail("Loading repository failed", e);
294
		}
295
		Collector c = repo.query(new InstallableUnitQuery("org.eclipse.rcp.feature.group"), new Collector(), new NullProgressMonitor());
296
		IInstallableUnit iu = (IInstallableUnit) c.iterator().next();
297
298
		AntTaskElement mirror = createMirrorTask(TYPE_METADATA);
299
		mirror.addElement(createSourceElement(null, sliceRepo));
300
		mirror.addElement(createSlicingOption(false, null, null, null, "win32,win32,x86"));
301
		mirror.addElement(createIUElement(iu.getId(), iu.getVersion().toString()));
302
303
		runAntTask();
304
305
		Properties p = getSliceProperties();
306
		p.setProperty("org.eclipse.update.install.features", String.valueOf(true));
307
		PermissiveSlicer slicer = new PermissiveSlicer(repo, p, false, true, true, false);
308
		IQueryable result = slicer.slice(new IInstallableUnit[] {iu}, new NullProgressMonitor());
309
		assertEquals("Different number of IUs", result.query(InstallableUnitQuery.ANY, new Collector(), new NullProgressMonitor()).size(), getIUCount(destinationRepo));
310
		try {
311
			assertIUContentEquals("IUs differ", result, getMetadataRepositoryManager().loadRepository(destinationRepo, null));
312
		} catch (ProvisionException e) {
313
			fail("Failed to compare contents", e);
314
		}
315
	}
316
317
	/*
318
	 * Test the platform filter
319
	 */
320
	public void testSlicingPlatformFilter() {
321
		IMetadataRepository repo = null;
322
		try {
323
			repo = loadMetadataRepository(sliceRepo);
324
		} catch (ProvisionException e) {
325
			fail("Loading repository failed", e);
326
		}
327
		Collector c = repo.query(new InstallableUnitQuery("org.eclipse.rcp.feature.group"), new Collector(), new NullProgressMonitor());
328
		IInstallableUnit iu = (IInstallableUnit) c.iterator().next();
329
330
		AntTaskElement mirror = createMirrorTask(TYPE_METADATA);
331
		mirror.addElement(createSourceElement(null, sliceRepo));
332
		mirror.addElement(createSlicingOption(null, null, null, null, "win32,win32,x86"));
333
		mirror.addElement(createIUElement(iu.getId(), iu.getVersion().toString()));
334
335
		runAntTask();
336
337
		Properties p = getSliceProperties();
338
		p.setProperty("org.eclipse.update.install.features", String.valueOf(true));
339
		PermissiveSlicer slicer = new PermissiveSlicer(repo, p, true, true, true, false);
340
		IQueryable result = slicer.slice(new IInstallableUnit[] {iu}, new NullProgressMonitor());
341
		assertEquals("Different number of IUs", result.query(InstallableUnitQuery.ANY, new Collector(), new NullProgressMonitor()).size(), getIUCount(destinationRepo));
342
		try {
343
			assertIUContentEquals("IUs differ", result, getMetadataRepositoryManager().loadRepository(destinationRepo, null));
344
		} catch (ProvisionException e) {
345
			fail("Failed to compare contents", e);
346
		}
347
	}
348
349
	/*
350
	 * Test disabling includeFeatures for SlicingOptions
351
	 */
352
	public void testSlicingIncludeFeaturesFalse() {
353
		IMetadataRepository repo = null;
354
		try {
355
			repo = loadMetadataRepository(sliceRepo);
356
		} catch (ProvisionException e) {
357
			fail("Loading repository failed", e);
358
		}
359
		Collector c = repo.query(new InstallableUnitQuery("org.eclipse.rcp.feature.group"), new Collector(), new NullProgressMonitor());
360
		IInstallableUnit iu = (IInstallableUnit) c.iterator().next();
361
362
		// Create task
363
		AntTaskElement mirror = createMirrorTask(TYPE_METADATA);
364
		mirror.addElement(createSourceElement(null, sliceRepo));
365
		mirror.addElement(createSlicingOption(null, null, null, false, "win32,win32,x86"));
366
		mirror.addElement(createIUElement(iu.getId(), iu.getVersion().toString()));
367
368
		runAntTask();
369
370
		Properties p = getSliceProperties();
371
		PermissiveSlicer slicer = new PermissiveSlicer(repo, p, true, true, true, false);
372
		IQueryable result = slicer.slice(new IInstallableUnit[] {iu}, new NullProgressMonitor());
373
		assertEquals("Different number of IUs", result.query(InstallableUnitQuery.ANY, new Collector(), new NullProgressMonitor()).size(), getIUCount(destinationRepo));
374
		try {
375
			assertIUContentEquals("IUs differ", result, getMetadataRepositoryManager().loadRepository(destinationRepo, null));
376
		} catch (ProvisionException e) {
377
			fail("Failed to compare contents", e);
378
		}
379
	}
380
381
	/*
382
	 * Tests the results of a slice are used to mirror artifacts
383
	 */
384
	public void testSlicingArtifactsMirrored() {
385
		IMetadataRepository repo = null;
386
		try {
387
			repo = loadMetadataRepository(sliceArtifactRepo);
388
		} catch (ProvisionException e) {
389
			fail("Loading repository failed", e);
390
		}
391
		Collector c = repo.query(new InstallableUnitQuery("test.feature.feature.group"), new Collector(), new NullProgressMonitor());
392
		IInstallableUnit iu = (IInstallableUnit) c.iterator().next();
393
394
		// Create task
395
		AntTaskElement mirror = createMirrorTask(TYPE_BOTH);
396
		mirror.addElement(createSourceElement(sliceArtifactRepo, sliceArtifactRepo));
397
		mirror.addElement(createSlicingOption(null, null, null, false, "win32,win32,x86"));
398
		mirror.addElement(createIUElement(iu.getId(), iu.getVersion().toString()));
399
400
		runAntTask();
401
402
		Properties p = getSliceProperties();
403
		PermissiveSlicer slicer = new PermissiveSlicer(repo, p, true, true, true, false);
404
		IQueryable result = slicer.slice(new IInstallableUnit[] {iu}, new NullProgressMonitor());
405
406
		assertEquals("Different number of IUs", result.query(InstallableUnitQuery.ANY, new Collector(), new NullProgressMonitor()).size(), getIUCount(destinationRepo));
407
		assertEquals("Different number of ArtifactKeys", getArtifactKeyCount(result.query(InstallableUnitQuery.ANY, new Collector(), new NullProgressMonitor())), getArtifactKeyCount(destinationRepo));
408
		try {
409
			assertArtifactKeyContentEquals("Different ArtifactKeys", result.query(InstallableUnitQuery.ANY, new Collector(), new NullProgressMonitor()), destinationRepo);
410
			assertIUContentEquals("IUs differ", result, getMetadataRepositoryManager().loadRepository(destinationRepo, null));
411
		} catch (ProvisionException e) {
412
			fail("Failed to compare contents", e);
413
		}
414
415
	}
416
417
	/*
418
	 * Test the result of a slice which results in no IUs
419
	 */
420
	public void testSlicingInvalid() {
421
		AntTaskElement mirror = createMirrorTask(TYPE_METADATA);
422
		mirror.addElement(createSourceElement(null, sliceRepo));
423
		mirror.addElement(createSlicingOption(null, null, null, null, "win32,win32,x86"));
424
425
		Exception exception = null;
426
		try {
427
			runAntTaskWithExceptions();
428
		} catch (Exception e) {
429
			exception = e;
430
		}
431
432
		if (exception == null || !(rootCause(exception) instanceof ProvisionException)) {
433
			fail("Unexpected exception type", exception);
434
		}
435
	}
436
437
	/*
438
	 * Modified from org.eclipse.equinox.p2.tests.mirror.ArtifactMirrorApplicationTest
439
	 */
440
	public void testBaselineCompareUsingMD5Comparator() {
441
		//Setup create descriptors with different md5 values
442
		IArtifactKey dupKey = PublisherHelper.createBinaryArtifactKey("testKeyId", new Version("1.2.3"));
443
		File artifact1 = getTestData("0.0", "/testData/mirror/mirrorSourceRepo1 with space/content.xml");
444
		File artifact2 = getTestData("0.0", "/testData/mirror/mirrorSourceRepo2/content.xml");
445
446
		//Setup Copy the file to the baseline
447
		File repoLocation = getTestFolder(getUniqueString());
448
		File baselineLocation = getTestFolder(getUniqueString());
449
		File baselineBinaryDirectory = new File(baselineLocation, "binary");
450
		baselineBinaryDirectory.mkdir();
451
		File baselineContentLocation = new File(baselineBinaryDirectory, "testKeyId_1.2.3");
452
		AbstractProvisioningTest.copy("Copying File to baseline", artifact2, baselineContentLocation);
453
454
		IArtifactDescriptor descriptor1 = PublisherHelper.createArtifactDescriptor(dupKey, artifact1);
455
		IArtifactDescriptor descriptor2 = PublisherHelper.createArtifactDescriptor(dupKey, baselineContentLocation);
456
457
		assertEquals("Ensuring Descriptors are the same", descriptor1, descriptor2);
458
		assertNotSame("Ensuring MD5 values are different", descriptor1.getProperty(IArtifactDescriptor.DOWNLOAD_MD5), descriptor2.getProperty(IArtifactDescriptor.DOWNLOAD_MD5));
459
460
		//Setup make repositories
461
		IArtifactRepository repo = null;
462
		IArtifactRepository baseline = null;
463
		try {
464
			repo = createRepositoryWithIU(repoLocation.toURI(), descriptor1);
465
			baseline = createRepositoryWithIU(baselineLocation.toURI(), descriptor2);
466
		} catch (ProvisionException e) {
467
			fail("Error creating repositories", e);
468
		}
469
470
		//Comparator prints to stderr, redirect that to a file
471
		PrintStream oldErr = System.err;
472
		PrintStream newErr = null;
473
		PrintStream oldOut = System.out;
474
		PrintStream newOut = null;
475
		try {
476
			(new File(destinationRepo)).mkdir();
477
			newErr = new PrintStream(new FileOutputStream(new File(new File(destinationRepo), "sys.err")));
478
			newOut = new PrintStream(new FileOutputStream(new File(new File(destinationRepo), "sys.out")));
479
		} catch (FileNotFoundException e) {
480
			fail("Error redirecting outputs", e);
481
		}
482
483
		try {
484
			System.setErr(newErr);
485
			System.setOut(newOut);
486
487
			// Create task
488
			AntTaskElement mirror = createMirrorTask(TYPE_BOTH);
489
			// Add source
490
			mirror.addElement(createSourceElement(repoLocation.toURI(), repoLocation.toURI()));
491
			// set verbose
492
			mirror.addAttribute("verbose", String.valueOf(true));
493
494
			// Create a comparator element 
495
			AntTaskElement comparator = new AntTaskElement("comparator");
496
			comparator.addElement(getRepositoryElement(baselineLocation.toURI(), null));
497
			mirror.addElement(comparator);
498
499
			runAntTaskWithExceptions();
500
		} catch (Exception e) {
501
			fail("Running mirror application with baseline compare", rootCause(e));
502
		} finally {
503
			System.setErr(oldErr);
504
			newErr.close();
505
			System.setOut(oldOut);
506
			newOut.close();
507
		}
508
509
		IArtifactRepository destination = null;
510
		try {
511
			destination = getArtifactRepositoryManager().loadRepository(destinationRepo, null);
512
		} catch (ProvisionException e) {
513
			fail("Error loading destination", e);
514
		}
515
516
		IArtifactDescriptor[] destDescriptors = destination.getArtifactDescriptors(descriptor2.getArtifactKey());
517
		assertEquals("Ensuring destination has correct number of descriptors", 1, destDescriptors.length);
518
		assertEquals("Ensuring destination contains the descriptor from the baseline", descriptor2.getProperty(IArtifactDescriptor.DOWNLOAD_MD5), destDescriptors[0].getProperty(IArtifactDescriptor.DOWNLOAD_MD5));
519
		String msg = NLS.bind(Messages.warning_differentMD5, new Object[] {URIUtil.toUnencodedString(baseline.getLocation()), URIUtil.toUnencodedString(repo.getLocation()), descriptor1});
520
521
		assertLogContains(msg);
522
	}
523
524
	private Properties getSliceProperties() {
525
		Properties p = new Properties();
526
		p.setProperty("osgi.os", "win32");
527
		p.setProperty("osgi.ws", "win32");
528
		p.setProperty("osgi.arch", "x86");
529
		return p;
530
	}
531
532
	protected AntTaskElement createSlicingOption(Boolean includeOptional, Boolean includeNonGreedy, Boolean followStrict, Boolean includeFeatures, String platformFilter) {
533
		AntTaskElement slicing = new AntTaskElement("slicingoptions");
534
		if (followStrict != null)
535
			slicing.addAttribute("followstrict", followStrict.toString());
536
		if (includeFeatures != null)
537
			slicing.addAttribute("includefeatures", includeFeatures.toString());
538
		if (includeNonGreedy != null)
539
			slicing.addAttribute("includenongreedy", includeNonGreedy.toString());
540
		if (includeOptional != null)
541
			slicing.addAttribute("includeoptional", includeOptional.toString());
542
		if (platformFilter != null)
543
			slicing.addAttribute("platformfilter", platformFilter);
544
		return slicing;
545
	}
546
547
	/*
548
	 * Create an IU for a descriptor and the IU+descriptor to the specified repo
549
	 */
550
	protected IArtifactRepository createRepositoryWithIU(URI repoLocation, IArtifactDescriptor descriptor) throws ProvisionException {
551
		IArtifactRepository artifactRepository = getArtifactRepositoryManager().createRepository(repoLocation, "Repo 1", IArtifactRepositoryManager.TYPE_SIMPLE_REPOSITORY, null);
552
		artifactRepository.addDescriptor(descriptor);
553
554
		IMetadataRepository metaRepo = getMetadataRepositoryManager().createRepository(repoLocation, "Repo", IMetadataRepositoryManager.TYPE_SIMPLE_REPOSITORY, null);
555
		InstallableUnit iu = new InstallableUnit();
556
		iu.setId(descriptor.getArtifactKey().getId() + "IU");
557
		iu.setVersion(descriptor.getArtifactKey().getVersion());
558
		iu.setArtifacts(new IArtifactKey[] {descriptor.getArtifactKey()});
559
		metaRepo.addInstallableUnits(new IInstallableUnit[] {iu});
560
561
		return artifactRepository;
562
	}
563
564
	/*
565
	 * Get the number of ArtifactKeys in a repository
566
	 */
567
	protected int getArtifactKeyCount(URI location) {
568
		try {
569
			return getArtifactRepositoryManager().loadRepository(location, null).getArtifactKeys().length;
570
		} catch (ProvisionException e) {
571
			fail("Failed to load repository " + URIUtil.toUnencodedString(location) + " for ArtifactDescriptor count");
572
			return -1;
573
		}
574
	}
575
576
	/*
577
	 * Get the number of ArtifactKeys in a repository
578
	 */
579
	protected int getArtifactKeyCount(Collector ius) {
580
		int count = 0;
581
		for (Iterator iter = ius.iterator(); iter.hasNext();)
582
			count += ((InstallableUnit) iter.next()).getArtifacts().length;
583
		return count;
584
	}
585
586
	/*
587
	 * Get the number of ArtifactDescriptors in a repository
588
	 */
589
	protected int getArtifactDescriptorCount(URI location) {
590
		int count = 0;
591
		try {
592
			IArtifactRepository repo = getArtifactRepositoryManager().loadRepository(location, null);
593
			IArtifactKey[] keys = repo.getArtifactKeys();
594
			for (int i = 0; i < keys.length; i++)
595
				count += repo.getArtifactDescriptors(keys[i]).length;
596
		} catch (ProvisionException e) {
597
			fail("Failed to load repository " + URIUtil.toUnencodedString(location) + " for ArtifactDescriptor count");
598
		}
599
		return count;
600
	}
601
602
	/*
603
	 * Get the number of IUs in a repository
604
	 */
605
	protected int getIUCount(URI location) {
606
		try {
607
			return getMetadataRepositoryManager().loadRepository(location, null).query(InstallableUnitQuery.ANY, new Collector(), null).size();
608
		} catch (ProvisionException e) {
609
			fail("Failed to load repository " + URIUtil.toUnencodedString(location) + " for ArtifactDescriptor count");
610
			return -1;
611
		}
612
	}
613
614
	/*
615
	 * Add all IUs to the parent element
616
	 */
617
	protected void addAllIUs(AntTaskElement parent, IMetadataRepository repo) {
618
		Collector collector = repo.query(InstallableUnitQuery.ANY, new Collector(), null);
619
620
		for (Iterator iter = collector.iterator(); iter.hasNext();) {
621
			IInstallableUnit iu = (IInstallableUnit) iter.next();
622
			parent.addElement(createIUElement(iu.getId(), iu.getVersion().toString()));
623
		}
624
	}
625
626
	/*
627
	 * Create an element from the specified information
628
	 */
629
	protected AntTaskElement createIUElement(String id, String version) {
630
		AntTaskElement iu = new AntTaskElement("iu");
631
		iu.addAttributes(new String[] {"id", id, "version", version});
632
		return iu;
633
	}
634
635
	/*
636
	 * Create the base mirror task & add it to the script
637
	 */
638
	protected AntTaskElement createMirrorTask(String type) {
639
		AntTaskElement mirror = new AntTaskElement(MIRROR_TASK);
640
		mirror.addElement(getRepositoryElement(destinationRepo, type));
641
		addTask(mirror);
642
		return mirror;
643
	}
644
645
	/*
646
	 * Create a source element with the specified repositories
647
	 */
648
	protected AntTaskElement createSourceElement(URI artifact, URI metadata) {
649
		AntTaskElement source = new AntTaskElement("source");
650
		if (artifact != null)
651
			source.addElement(getRepositoryElement(artifact, AbstractAntProvisioningTest.TYPE_ARTIFACT));
652
		if (metadata != null)
653
			source.addElement(getRepositoryElement(metadata, AbstractAntProvisioningTest.TYPE_METADATA));
654
		return source;
655
	}
656
}
(-)src/org/eclipse/equinox/p2/tests/ant/CompositeRepositoryTaskTest.java (+455 lines)
Added Link Here
1
/*******************************************************************************
2
 *  Copyright (c) 2009 IBM Corporation 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
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.equinox.p2.tests.ant;
12
13
import java.io.File;
14
import java.net.MalformedURLException;
15
import java.net.URI;
16
import org.eclipse.core.runtime.CoreException;
17
import org.eclipse.core.runtime.URIUtil;
18
import org.eclipse.equinox.internal.p2.artifact.repository.CompositeArtifactRepository;
19
import org.eclipse.equinox.internal.p2.metadata.repository.CompositeMetadataRepository;
20
import org.eclipse.equinox.internal.p2.repository.helpers.RepositoryHelper;
21
import org.eclipse.equinox.internal.provisional.p2.artifact.repository.IArtifactRepository;
22
import org.eclipse.equinox.internal.provisional.p2.artifact.repository.IArtifactRepositoryManager;
23
import org.eclipse.equinox.internal.provisional.p2.core.ProvisionException;
24
import org.eclipse.equinox.internal.provisional.p2.metadata.repository.IMetadataRepositoryManager;
25
import org.eclipse.equinox.internal.provisional.p2.repository.ICompositeRepository;
26
import org.eclipse.equinox.internal.provisional.p2.repository.IRepository;
27
import org.eclipse.equinox.p2.tests.AbstractAntProvisioningTest;
28
29
public class CompositeRepositoryTaskTest extends AbstractAntProvisioningTest {
30
	private static final String ADD_ELEMENT = "add";
31
	private static final String REMOVE_ELEMENT = "remove";
32
	private URI compositeSite;
33
	private URI childSite, childSite2;
34
35
	public void setUp() throws Exception {
36
		super.setUp();
37
		// Get a random location to create a repository
38
		compositeSite = (new File(getTempFolder(), getUniqueString())).toURI();
39
		childSite = getTestData("Loading test data", "testData/testRepos/simple.1").toURI();
40
		childSite2 = new URI("memory:/in/memory");
41
	}
42
43
	public void tearDown() throws Exception {
44
		// Remove repository manager references
45
		getArtifactRepositoryManager().removeRepository(compositeSite);
46
		getMetadataRepositoryManager().removeRepository(compositeSite);
47
48
		getArtifactRepositoryManager().removeRepository(childSite);
49
		getMetadataRepositoryManager().removeRepository(childSite);
50
		getArtifactRepositoryManager().removeRepository(childSite2);
51
		getMetadataRepositoryManager().removeRepository(childSite2);
52
53
		// Cleanup disk
54
		delete(new File(compositeSite));
55
		super.tearDown();
56
	}
57
58
	/*
59
	 * Test adding a child to an existing artifact repository
60
	 */
61
	public void testAddChildToExistingArtifactRepository() throws Exception {
62
		// Create repository
63
		createCompositeRepository(TYPE_ARTIFACT);
64
		// Create the modify repository task
65
		AntTaskElement modify = createCompositeRepositoryTaskElement(TYPE_ARTIFACT);
66
		addTask(modify);
67
68
		// Create the Add element
69
		AntTaskElement add = new AntTaskElement(ADD_ELEMENT);
70
		add.addElement(getRepositoryElement(childSite, TYPE_ARTIFACT));
71
		modify.addElement(add);
72
73
		// Run the task
74
		runAntTask();
75
76
		CompositeArtifactRepository repo = (CompositeArtifactRepository) getCompositeRepository(TYPE_ARTIFACT);
77
		assertTrue("Repository does not contain child", repo.getChildren().contains(childSite));
78
	}
79
80
	/*
81
	 * Test what occurs when no children are added to the newly created Composite repository
82
	 */
83
	public void testCreateNoChidlren() {
84
		AntTaskElement modify = createCompositeRepositoryTaskElement(TYPE_BOTH);
85
		addTask(modify);
86
		runAntTask();
87
88
		if (getArtifactRepositoryManager().contains(compositeSite))
89
			getArtifactRepositoryManager().removeRepository(compositeSite);
90
		if (getMetadataRepositoryManager().contains(compositeSite))
91
			getMetadataRepositoryManager().removeRepository(compositeSite);
92
93
		ICompositeRepository artifact = null;
94
		ICompositeRepository metadata = null;
95
		try {
96
			artifact = (ICompositeRepository) getArtifactRepositoryManager().loadRepository(compositeSite, null);
97
			metadata = (ICompositeRepository) getMetadataRepositoryManager().loadRepository(compositeSite, null);
98
		} catch (ProvisionException e) {
99
			fail("Failed to load repositories", e);
100
		}
101
		assertTrue("Artifact Repository contains children", artifact.getChildren().isEmpty());
102
		assertTrue("Metadata Repository contains children", metadata.getChildren().isEmpty());
103
	}
104
105
	/*
106
	 * Test adding a child to an existing metadata repository 
107
	 */
108
	public void testAddChildToExistingMetadataRepository() {
109
		// Create repository
110
		createCompositeRepository(TYPE_METADATA);
111
		// Create the modify repository task
112
		AntTaskElement modify = createCompositeRepositoryTaskElement(TYPE_METADATA);
113
		addTask(modify);
114
115
		// Create the Add element
116
		AntTaskElement add = new AntTaskElement(ADD_ELEMENT);
117
		add.addElement(getRepositoryElement(childSite, TYPE_METADATA));
118
		modify.addElement(add);
119
120
		// Run the task
121
		runAntTask();
122
123
		CompositeMetadataRepository repo = (CompositeMetadataRepository) getCompositeRepository(TYPE_METADATA);
124
		assertTrue("Repository does not contain child", repo.getChildren().contains(childSite));
125
	}
126
127
	/*
128
	 * Test adding a child to both types of repositories (which already exist)
129
	 */
130
	public void testAddChildToExistingRepositories() {
131
		// Create repository
132
		createCompositeRepository(null);
133
		// Create the modify repository task
134
		AntTaskElement modify = createCompositeRepositoryTaskElement(null);
135
		addTask(modify);
136
137
		// Create the Add element
138
		modify.addElement(createAddElement(null, new URI[] {childSite}));
139
140
		// Run the task
141
		runAntTask();
142
143
		CompositeArtifactRepository artifactRepo = (CompositeArtifactRepository) getCompositeRepository(TYPE_ARTIFACT);
144
		assertTrue("Repository does not contain child", artifactRepo.getChildren().contains(childSite));
145
146
		CompositeMetadataRepository metadataRepo = (CompositeMetadataRepository) getCompositeRepository(TYPE_METADATA);
147
		assertTrue("Repository does not contain child", metadataRepo.getChildren().contains(childSite));
148
	}
149
150
	/*
151
	 * Test the ability to remove all children
152
	 */
153
	public void testRemoveAllChildren() {
154
		// Create repository
155
		ICompositeRepository parent = createCompositeRepository(TYPE_ARTIFACT);
156
		parent.addChild(childSite);
157
158
		// Create the modify repository task
159
		AntTaskElement modify = new AntTaskElement("p2.composite.repository");
160
		AntTaskElement destination = getRepositoryElement(compositeSite, TYPE_ARTIFACT);
161
		destination.addAttribute("append", String.valueOf(false));
162
		modify.addElement(destination);
163
		addTask(modify);
164
165
		// Run the task
166
		runAntTask();
167
168
		CompositeArtifactRepository artifactRepo = (CompositeArtifactRepository) getCompositeRepository(TYPE_ARTIFACT);
169
		assertTrue("Children not removed", artifactRepo.getChildren().isEmpty());
170
171
	}
172
173
	/*
174
	 * Test the removal of specified children
175
	 */
176
	public void testRemoveChild() {
177
		ICompositeRepository repo = createCompositeRepository(TYPE_ARTIFACT);
178
		try {
179
			getArtifactRepositoryManager().loadRepository(childSite, null);
180
			getArtifactRepositoryManager().createRepository(childSite2, "Child site", IArtifactRepositoryManager.TYPE_COMPOSITE_REPOSITORY, null);
181
182
			repo.addChild(childSite);
183
			repo.addChild(childSite2);
184
		} catch (ProvisionException e) {
185
			fail("Failed to create child repositories");
186
		}
187
		getArtifactRepositoryManager().removeRepository(compositeSite);
188
189
		AntTaskElement modify = createCompositeRepositoryTaskElement(TYPE_ARTIFACT);
190
		modify.addElement(createRemoveElement(TYPE_ARTIFACT, new URI[] {childSite}));
191
		addTask(modify);
192
193
		runAntTask();
194
195
		repo = getCompositeRepository(TYPE_ARTIFACT);
196
197
		assertFalse(repo.getChildren().contains(childSite));
198
		assertTrue(repo.getChildren().contains(childSite2));
199
	}
200
201
	/*
202
	 * Test creating a CompositeArtifactRepository
203
	 */
204
	public void testCreateCompositeArtifactRepository() throws Exception {
205
		// Create Composite Repository Task
206
		AntTaskElement createCompositeTask = createCompositeRepositoryTaskElement(TYPE_ARTIFACT);
207
		addTask(createCompositeTask);
208
209
		runAntTask();
210
211
		assertTrue(getArtifactRepositoryManager().contains(compositeSite));
212
		assertTrue(getArtifactRepositoryManager().loadRepository(compositeSite, null) instanceof CompositeArtifactRepository);
213
		assertFalse("Metadata repository does not exists", getMetadataRepositoryManager().contains(compositeSite));
214
	}
215
216
	/*
217
	 * Test creating a CompositeMetadataRepository
218
	 */
219
	public void testCreateCompositeMetadataRepository() throws Exception {
220
		// Create Composite Repository Task
221
		AntTaskElement createCompositeTask = createCompositeRepositoryTaskElement(TYPE_METADATA);
222
		addTask(createCompositeTask);
223
224
		runAntTask();
225
226
		assertTrue("Metadata repository does not exists", getMetadataRepositoryManager().contains(compositeSite));
227
		assertTrue("Metadata repository is not a CompositeRepository", getMetadataRepositoryManager().loadRepository(compositeSite, null) instanceof CompositeMetadataRepository);
228
		assertFalse("Artifact repository also exists", getArtifactRepositoryManager().contains(compositeSite));
229
	}
230
231
	/*
232
	 * Tests the ability to create both Artifact & Metadata repositories at once.
233
	 */
234
	public void testCreateCombinedCompositeRepository() throws Exception {
235
		// Create Composite Repository Task
236
		AntTaskElement createCompositeTask = createCompositeRepositoryTaskElement(null);
237
		addTask(createCompositeTask);
238
239
		runAntTask();
240
241
		assertTrue("Metadata repository does not exists", getMetadataRepositoryManager().contains(compositeSite));
242
		assertTrue("Artifact repository does not exists", getArtifactRepositoryManager().contains(compositeSite));
243
		assertTrue("Metadata repository is not a CompositeRepository", getMetadataRepositoryManager().loadRepository(compositeSite, null) instanceof CompositeMetadataRepository);
244
		assertTrue("Artifact repository is not a CompositeRepository", getArtifactRepositoryManager().loadRepository(compositeSite, null) instanceof CompositeArtifactRepository);
245
	}
246
247
	/*
248
	 * Test that failOnExists attribute is honoured
249
	 */
250
	public void testFailOnExists() throws Exception {
251
		// Create Composite Repository Task
252
		AntTaskElement createCompositeTask = createCompositeRepositoryTaskElement(TYPE_ARTIFACT);
253
		addTask(createCompositeTask);
254
		runAntTask();
255
256
		// Set failOnExists
257
		createCompositeTask.addAttributes(new String[] {"failOnExists", String.valueOf(true)});
258
259
		Throwable exception = null;
260
		try {
261
			runAntTaskWithExceptions();
262
		} catch (CoreException e) {
263
			exception = rootCause(e);
264
		}
265
		if (!exception.getMessage().contains("exists"))
266
			fail("Unexpected exception: ", exception);
267
	}
268
269
	/*
270
	 * Test that not-compressed attribute is honoured
271
	 */
272
	public void testNotCompressed() throws Exception {
273
		// Create Composite Repository Task
274
		AntTaskElement createCompositeTask = createCompositeRepositoryTaskElement(TYPE_ARTIFACT);
275
		addTask(createCompositeTask);
276
		// Set the compressed attribute to false
277
		((AntTaskElement) createCompositeTask.elements.get(0)).addAttributes(new String[] {"compressed", String.valueOf(false)});
278
		runAntTask();
279
280
		ICompositeRepository repo = getCompositeRepository(TYPE_ARTIFACT);
281
		assertTrue(repo instanceof CompositeArtifactRepository);
282
		assertFalse("The repository is compressed", Boolean.valueOf((String) repo.getProperties().get(IRepository.PROP_COMPRESSED)));
283
	}
284
285
	/*
286
	 * Test that the name is properly set on a newly created repository
287
	 */
288
	public void testName() {
289
		String repoName = "My Test Repository";
290
		// Create Composite Repository Task
291
		AntTaskElement createCompositeTask = createCompositeRepositoryTaskElement(TYPE_ARTIFACT);
292
		addTask(createCompositeTask);
293
		// Set the repository name
294
		((AntTaskElement) createCompositeTask.elements.get(0)).addAttributes(new String[] {"name", repoName});
295
296
		runAntTask();
297
298
		try {
299
			IArtifactRepository repo = getArtifactRepositoryManager().loadRepository(compositeSite, null);
300
			assertTrue(repo instanceof CompositeArtifactRepository);
301
			assertEquals(repoName, repo.getName());
302
		} catch (ProvisionException e) {
303
			fail("Failed to load repository", e);
304
		}
305
	}
306
307
	/*
308
	 * Test adding a child to a new artifact repository
309
	 */
310
	public void testAddChildToNewArtifactRepository() {
311
		// Create Composite Repository Task
312
		AntTaskElement createCompositeTask = createCompositeRepositoryTaskElement(TYPE_ARTIFACT);
313
		addTask(createCompositeTask);
314
315
		// Create add element
316
		AntTaskElement addElement = new AntTaskElement("add");
317
		// Add a repository
318
		addElement.addElement(getRepositoryElement(childSite, TYPE_ARTIFACT));
319
		createCompositeTask.addElement(addElement);
320
321
		runAntTask();
322
323
		try {
324
			CompositeArtifactRepository repo = (CompositeArtifactRepository) getArtifactRepositoryManager().loadRepository(compositeSite, null);
325
			assertTrue(repo.getChildren().contains(childSite));
326
			assertEquals("More than one child present", 1, repo.getChildren().size());
327
		} catch (ProvisionException e) {
328
			fail("Failed to load repository", e);
329
		}
330
	}
331
332
	/*
333
	 * Test adding a child to a new metadata repository
334
	 */
335
	public void testAddChildToNewMetadataRepository() {
336
		// Create Composite Repository Task
337
		AntTaskElement createCompositeTask = createCompositeRepositoryTaskElement(TYPE_METADATA);
338
		addTask(createCompositeTask);
339
340
		// Create add element
341
		AntTaskElement addElement = new AntTaskElement("add");
342
		// Add a repository
343
		addElement.addElement(getRepositoryElement(childSite, TYPE_METADATA));
344
		createCompositeTask.addElement(addElement);
345
346
		runAntTask();
347
348
		try {
349
			ICompositeRepository repo = (ICompositeRepository) getMetadataRepositoryManager().loadRepository(compositeSite, null);
350
			assertTrue(repo.getChildren().contains(childSite));
351
			assertEquals("More than one child present", 1, repo.getChildren().size());
352
		} catch (ProvisionException e) {
353
			fail("Failed to load repository", e);
354
		}
355
	}
356
357
	/*
358
	 * Test how the task behaves with an invalid location
359
	 */
360
	public void testInvalidLocation() throws Exception {
361
		URI location = URIUtil.fromString("scheme:/location");
362
		AntTaskElement createCompositeTask = new AntTaskElement("p2.composite.repository");
363
		createCompositeTask.addElement(getRepositoryElement(location, TYPE_ARTIFACT));
364
		addTask(createCompositeTask);
365
366
		Exception exception = null;
367
		try {
368
			runAntTaskWithExceptions();
369
		} catch (CoreException e) {
370
			exception = e;
371
			if (!(rootCause(e) instanceof MalformedURLException))
372
				fail("Expected MalformedURLException.", e);
373
			else {
374
				try {
375
					getArtifactRepositoryManager().loadRepository(location, null);
376
					fail("Repository with invalid location loaded.");
377
				} catch (ProvisionException e2) {
378
					// This is a success
379
				}
380
			}
381
		}
382
		if (exception == null)
383
			fail("No exception thrown");
384
	}
385
386
	/*
387
	 * Get the composite repository at the default location
388
	 */
389
	protected ICompositeRepository getCompositeRepository(String type) {
390
		try {
391
			if (type == TYPE_ARTIFACT) {
392
				return (ICompositeRepository) getArtifactRepositoryManager().loadRepository(compositeSite, null);
393
			} else if (type == TYPE_METADATA)
394
				return (ICompositeRepository) getMetadataRepositoryManager().loadRepository(compositeSite, null);
395
			else
396
				fail("No type specified");
397
		} catch (ProvisionException e) {
398
			fail("Failed to load repository", e);
399
		} catch (ClassCastException e) {
400
			fail("Repository is not composite", e);
401
		}
402
		// Will not occur
403
		return null;
404
	}
405
406
	/*
407
	 *  Create an "remove" AntTaskElement for the specified addresses
408
	 */
409
	protected AntTaskElement createRemoveElement(String type, URI[] addresses) {
410
		AntTaskElement add = new AntTaskElement(REMOVE_ELEMENT);
411
		for (int i = 0; i < addresses.length; i++)
412
			add.addElement(getRepositoryElement(addresses[i], type));
413
		return add;
414
	}
415
416
	/*
417
	 * Create an "add" AntTaskElement for the specified addresses
418
	 */
419
	protected AntTaskElement createAddElement(String type, URI[] addresses) {
420
		AntTaskElement add = new AntTaskElement(ADD_ELEMENT);
421
		for (int i = 0; i < addresses.length; i++)
422
			add.addElement(getRepositoryElement(addresses[i], type));
423
		return add;
424
	}
425
426
	/*
427
	 * Create an AntTaskElement representing a p2 composite repository task with the default repo location specified
428
	 */
429
	protected AntTaskElement createCompositeRepositoryTaskElement(String type) {
430
		AntTaskElement compositeTask = new AntTaskElement("p2.composite.repository");
431
		compositeTask.addElement(getRepositoryElement(compositeSite, type));
432
433
		return compositeTask;
434
	}
435
436
	/*
437
	 * Create a composite repository at the default location of the specified type(s)
438
	 */
439
	protected ICompositeRepository createCompositeRepository(String type) {
440
		ICompositeRepository repo = null;
441
		try {
442
			if (TYPE_ARTIFACT.equals(type) || type == null) {
443
				repo = (ICompositeRepository) RepositoryHelper.validDestinationRepository(getArtifactRepositoryManager().createRepository(compositeSite, "Test Composite Repo", IArtifactRepositoryManager.TYPE_COMPOSITE_REPOSITORY, null));
444
			}
445
			if (TYPE_METADATA.equals(type) || type == null) {
446
				repo = (ICompositeRepository) RepositoryHelper.validDestinationRepository(getMetadataRepositoryManager().createRepository(compositeSite, "Test Composite Repo", IMetadataRepositoryManager.TYPE_COMPOSITE_REPOSITORY, null));
447
			}
448
		} catch (ProvisionException e) {
449
			fail("Failed to create composite repository", e);
450
		} catch (IllegalStateException e) {
451
			fail("failed to create writeable composite repository", e);
452
		}
453
		return repo;
454
	}
455
}
(-)src/org/eclipse/equinox/p2/tests/ant/AllTests.java (+27 lines)
Added Link Here
1
/*******************************************************************************
2
 *  Copyright (c) 2009 IBM Corporation 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
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.equinox.p2.tests.ant;
12
13
import junit.framework.*;
14
15
/**
16
 * Performs all automated artifact repository tests.
17
 */
18
public class AllTests extends TestCase {
19
20
	public static Test suite() {
21
		TestSuite suite = new TestSuite(AllTests.class.getName());
22
		suite.addTestSuite(CompositeRepositoryTaskTest.class);
23
		suite.addTestSuite(MirrorTaskTest.class);
24
		return suite;
25
	}
26
27
}
(-)src/org/eclipse/equinox/p2/tests/mirror/NewMirrorApplicationArtifactTest.java (+1467 lines)
Added Link Here
1
/*******************************************************************************
2
 *  Copyright (c) 2008, 2009 IBM Corporation 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
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.equinox.p2.tests.mirror;
12
13
import java.io.*;
14
import java.net.MalformedURLException;
15
import java.net.URI;
16
import java.util.*;
17
import org.eclipse.core.runtime.*;
18
import org.eclipse.equinox.internal.p2.artifact.processors.md5.Messages;
19
import org.eclipse.equinox.internal.p2.artifact.repository.*;
20
import org.eclipse.equinox.internal.p2.artifact.repository.simple.SimpleArtifactRepository;
21
import org.eclipse.equinox.internal.p2.core.helpers.OrderedProperties;
22
import org.eclipse.equinox.internal.p2.core.helpers.ServiceHelper;
23
import org.eclipse.equinox.internal.provisional.p2.artifact.repository.*;
24
import org.eclipse.equinox.internal.provisional.p2.core.ProvisionException;
25
import org.eclipse.equinox.internal.provisional.p2.core.Version;
26
import org.eclipse.equinox.internal.provisional.p2.metadata.IArtifactKey;
27
import org.eclipse.equinox.internal.provisional.p2.repository.IRepository;
28
import org.eclipse.equinox.p2.internal.repository.tools.MirrorApplication;
29
import org.eclipse.equinox.p2.internal.repository.tools.RepositoryDescriptor;
30
import org.eclipse.equinox.p2.tests.AbstractProvisioningTest;
31
import org.eclipse.equinox.p2.tests.TestActivator;
32
import org.eclipse.equinox.spi.p2.publisher.PublisherHelper;
33
import org.eclipse.osgi.framework.log.FrameworkLog;
34
import org.eclipse.osgi.util.NLS;
35
36
/*
37
 * Modified from ArtifactMirrorApplicationTest
38
 */
39
public class NewMirrorApplicationArtifactTest extends AbstractProvisioningTest {
40
	protected File destRepoLocation;
41
	protected File sourceRepoLocation; //helloworldfeature
42
	protected File sourceRepo2Location; //anotherfeature
43
	protected File sourceRepo3Location; //helloworldfeature + yetanotherfeature
44
	protected File sourceRepo4Location; //helloworldfeature v1.0.1
45
46
	/* (non-Javadoc)
47
	 * @see org.eclipse.equinox.p2.tests.AbstractProvisioningTest#setUp()
48
	 */
49
	protected void setUp() throws Exception {
50
		super.setUp();
51
		//load all the repositories
52
		sourceRepoLocation = getTestData("0.0", "/testData/mirror/mirrorSourceRepo1 with space");
53
		sourceRepo2Location = getTestData("0.1", "/testData/mirror/mirrorSourceRepo2");
54
		sourceRepo3Location = getTestData("0.2", "/testData/mirror/mirrorSourceRepo3");
55
		sourceRepo4Location = getTestData("0.3", "/testData/mirror/mirrorSourceRepo4");
56
57
		//create destination location
58
		destRepoLocation = new File(getTempFolder(), "BasicMirrorApplicationTest");
59
		delete(destRepoLocation);
60
	}
61
62
	/* (non-Javadoc)
63
	 * @see org.eclipse.equinox.p2.tests.AbstractProvisioningTest#tearDown()
64
	 */
65
	protected void tearDown() throws Exception {
66
		//remove all the repositories
67
		getArtifactRepositoryManager().removeRepository(destRepoLocation.toURI());
68
		getArtifactRepositoryManager().removeRepository(sourceRepoLocation.toURI());
69
		getArtifactRepositoryManager().removeRepository(sourceRepo2Location.toURI());
70
		getArtifactRepositoryManager().removeRepository(sourceRepo3Location.toURI());
71
		getArtifactRepositoryManager().removeRepository(sourceRepo4Location.toURI());
72
		//delete the destination location (no left over files for the next test)
73
		delete(destRepoLocation);
74
		super.tearDown();
75
	}
76
77
	private void basicRunMirrorApplication(String message, URI source, URI destination, Boolean append, Boolean formatDestination, String destName) throws Exception {
78
		MirrorApplication app = new MirrorApplication();
79
80
		if (destination != null) {
81
			RepositoryDescriptor dest = null;
82
			if (formatDestination != null && formatDestination)
83
				dest = createRepositoryDescriptor(destination, append, source, destName);
84
			else
85
				dest = createRepositoryDescriptor(destination, append, null, destName);
86
			app.addDestination(dest);
87
		}
88
89
		if (source != null) {
90
			RepositoryDescriptor src = createRepositoryDescriptor(source, null, null, null);
91
			app.addSource(src);
92
		}
93
		app.run(null);
94
	}
95
96
	private void basicRunMirrorApplication(String message, URI source, URI destination, Boolean append, Boolean formatDestination) throws Exception {
97
		basicRunMirrorApplication(message, source, destination, append, formatDestination, null);
98
	}
99
100
	private void basicRunMirrorApplication(String message, URI source, URI destination) throws Exception {
101
		basicRunMirrorApplication(message, source, destination, null, null, null);
102
	}
103
104
	private RepositoryDescriptor createRepositoryDescriptor(URI location, Boolean append, URI format, String name) {
105
		RepositoryDescriptor descriptor = new RepositoryDescriptor();
106
		descriptor.setLocation(location);
107
		descriptor.setKind("artifact");
108
		if (append != null)
109
			descriptor.setAppend(append);
110
		if (format != null)
111
			descriptor.setFormat(format);
112
		if (name != null)
113
			descriptor.setName(name);
114
		return descriptor;
115
	}
116
117
	/**
118
	 * just a wrapper method for compatibility
119
	 */
120
	private void runMirrorApplication(String message, File source, File destination, boolean append) {
121
		try {
122
			basicRunMirrorApplication(message, source.toURI(), destination.toURI(), append, false);
123
		} catch (Exception e) {
124
			fail(message, e);
125
		}
126
	}
127
128
	/**
129
	 * Tests mirroring all artifacts in a repository to an empty repository
130
	 * Source contains A, B
131
	 * Target contains
132
	 */
133
	private void artifactMirrorToEmpty(String message, boolean append, boolean format) {
134
		try {
135
			//destination repo is created blank
136
			basicRunMirrorApplication(message, sourceRepoLocation.toURI(), destRepoLocation.toURI(), append, format);
137
		} catch (Exception e) {
138
			fail(message, e);
139
		}
140
	}
141
142
	/**
143
	 * Tests mirroring all artifacts in a repository to a repository populated with non-duplicate entries
144
	 * Source contains A, B
145
	 * Target contains C, D
146
	 */
147
	private void artifactMirrorToPopulated(String message, boolean append) {
148
		//Setup: populate destination with non-duplicate artifacts
149
		runMirrorApplication(message + ".0", sourceRepo2Location, destRepoLocation, false); //value of append should not matter
150
151
		try {
152
			//Setup ensure setup completes successfully
153
			assertContentEquals(message + ".1", getArtifactRepositoryManager().loadRepository(sourceRepo2Location.toURI(), null), getArtifactRepositoryManager().loadRepository(destRepoLocation.toURI(), null));
154
		} catch (ProvisionException e) {
155
			fail(message + ".2", e);
156
		}
157
158
		//mirror test data
159
		runMirrorApplication(message + ".4", sourceRepoLocation, destRepoLocation, append);
160
	}
161
162
	/**
163
	 * Tests mirroring all artifacts in a repository to a repository populated with exact duplicate data
164
	 * Source contains A, B
165
	 * Target contains A, B
166
	 */
167
	private void artifactMirrorToFullDuplicate(String message, boolean append) {
168
		//Setup: populate destination with duplicate artifacts
169
		runMirrorApplication(message + ".0", sourceRepoLocation, destRepoLocation, false); //value of append should not matter
170
171
		try {
172
			//Setup: verify contents
173
			assertContentEquals(message + ".1", getArtifactRepositoryManager().loadRepository(sourceRepoLocation.toURI(), null), getArtifactRepositoryManager().loadRepository(destRepoLocation.toURI(), null));
174
		} catch (ProvisionException e) {
175
			fail(message + ".2", e);
176
		}
177
178
		//mirror test data
179
		runMirrorApplication(message + ".4", sourceRepoLocation, destRepoLocation, append);
180
	}
181
182
	/**
183
	 * Tests mirroring all artifacts in a repository to a repository populated with partially duplicate data
184
	 * Source contains A, B, C, D
185
	 * Target contains  A, B
186
	 */
187
	private void artifactMirrorToPartialDuplicate(String message, boolean append) {
188
		//Setup: populate destination with duplicate artifacts
189
		runMirrorApplication(message + ".0", sourceRepoLocation, destRepoLocation, false);
190
191
		try {
192
			//Setup: verify contents
193
			assertContentEquals(message + ".1", getArtifactRepositoryManager().loadRepository(sourceRepoLocation.toURI(), null), getArtifactRepositoryManager().loadRepository(destRepoLocation.toURI(), null));
194
		} catch (ProvisionException e) {
195
			fail(message + ".2", e);
196
		}
197
198
		//mirror test data
199
		runMirrorApplication(message + ".4", sourceRepo3Location, destRepoLocation, append);
200
	}
201
202
	/**
203
	 * Tests mirroring all artifacts in a repository to a repository populated with both full duplicate and non-duplicate data
204
	 * Source contains A, B
205
	 * Target contains A, B, C, D
206
	 */
207
	private void artifactMirrorToPopulatedWithFullDuplicate(String message, boolean append) {
208
		//Setup: populate destination with non-duplicate artifacts
209
		runMirrorApplication(message + ".0", sourceRepo3Location, destRepoLocation, false); //value of append should not matter
210
211
		try {
212
			//Setup: verify
213
			assertContentEquals(message + ".1", getArtifactRepositoryManager().loadRepository(sourceRepo3Location.toURI(), null), getArtifactRepositoryManager().loadRepository(destRepoLocation.toURI(), null));
214
		} catch (ProvisionException e) {
215
			fail(message + ".2", e);
216
		}
217
218
		//mirror duplicate data
219
		runMirrorApplication(message + ".4", sourceRepoLocation, destRepoLocation, append);
220
	}
221
222
	/**
223
	 * Tests mirroring all artifacts in a repository to a repository populated with both partial duplicate and non-duplicate data
224
	 * Source contains A, B, C, D
225
	 * Target contains A, B, E, F
226
	 */
227
	private void artifactMirrorToPopulatedWithPartialDuplicate(String message, boolean append) {
228
		//Setup: populate destination with non-duplicate artifacts
229
		runMirrorApplication(message + ".0", sourceRepo2Location, destRepoLocation, false); //value of append should not matter
230
231
		try {
232
			//Setup: verify
233
			assertContentEquals(message + ".1", getArtifactRepositoryManager().loadRepository(sourceRepo2Location.toURI(), null), getArtifactRepositoryManager().loadRepository(destRepoLocation.toURI(), null));
234
		} catch (ProvisionException e) {
235
			fail(message + ".2", e);
236
		}
237
238
		//Setup: populate destination with duplicate artifacts
239
		runMirrorApplication(message + ".4", sourceRepoLocation, destRepoLocation, true);
240
241
		try {
242
			//Setup: verify
243
			assertContains(message + ".5", getArtifactRepositoryManager().loadRepository(sourceRepoLocation.toURI(), null), getArtifactRepositoryManager().loadRepository(destRepoLocation.toURI(), null));
244
			assertContains(message + ".6", getArtifactRepositoryManager().loadRepository(sourceRepo2Location.toURI(), null), getArtifactRepositoryManager().loadRepository(destRepoLocation.toURI(), null));
245
		} catch (ProvisionException e) {
246
			fail(message + ".7", e);
247
		}
248
249
		//mirror duplicate data
250
		runMirrorApplication(message + ".9", sourceRepo3Location, destRepoLocation, append);
251
	}
252
253
	/**
254
	 * Tests mirroring all artifacts from an empty repository
255
	 * Source contains
256
	 */
257
	private File artifactMirrorEmpty(String message, boolean append) {
258
		//Setup: Create an empty repository
259
		File emptyRepository = new File(getTempFolder(), getUniqueString());
260
		//Setup: remove repository if it exists
261
		getArtifactRepositoryManager().removeRepository(emptyRepository.toURI());
262
		//Setup: delete any data that may be in the folder
263
		delete(emptyRepository);
264
		try {
265
			getArtifactRepositoryManager().createRepository(emptyRepository.toURI(), "Empty Repository", IArtifactRepositoryManager.TYPE_SIMPLE_REPOSITORY, null);
266
		} catch (ProvisionException e) {
267
			fail(message + ".1", e);
268
		}
269
270
		runMirrorApplication(message + ".0", emptyRepository, destRepoLocation, append);
271
		return emptyRepository; //return the repository for use in verification
272
	}
273
274
	/**
275
	 * Tests mirroring all artifacts from an empty repository
276
	 * Source contains
277
	 */
278
	private File artifactMirrorEmptyToPopulated(String message, boolean append) {
279
		//Setup: Populate the repository
280
		runMirrorApplication(message + ".0", sourceRepoLocation, destRepoLocation, false);
281
282
		return artifactMirrorEmpty(message + ".1", append); //create the empty repository, perform the mirror, pass the result back
283
	}
284
285
	/**
286
	 * Runs mirror app on source with missing artifact with "-ignoreErrors"
287
	 */
288
	private void mirrorWithError(boolean verbose) {
289
		File errorSourceLocation = getTestData("loading error data", "testData/mirror/mirrorErrorSourceRepo");
290
		//repo contains an artifact entry for a file that does not exist on disk. this should throw a file not found exception
291
		try {
292
			MirrorApplication app = new MirrorApplication();
293
			app.addSource(createRepositoryDescriptor(errorSourceLocation.toURI(), null, null, null));
294
			app.addDestination(createRepositoryDescriptor(destRepoLocation.toURI(), null, null, null));
295
			//Set ignoreErrors flag. Set verbose flag if verbose == true
296
			app.setVerbose(verbose);
297
			app.setIgnoreErrors(true);
298
			//run the mirror application
299
			app.run(null);
300
		} catch (Exception e) {
301
			fail("Running mirror application with errored source failed", e);
302
		}
303
	}
304
305
	/**
306
	 * ensures that all files with entries in the repo have corresponding files on disk.
307
	 * Not Biconditional.
308
	 */
309
	private void assertFileSizes(String message, SimpleArtifactRepository expected, SimpleArtifactRepository actual) {
310
		IArtifactKey[] expectedKeys = expected.getArtifactKeys();
311
312
		for (int i = 0; i < expectedKeys.length; i++) {
313
			IArtifactDescriptor[] expectedDescriptors = expected.getArtifactDescriptors(expectedKeys[i]);
314
			IArtifactDescriptor[] actualDescriptors = actual.getArtifactDescriptors(expectedKeys[i]);
315
316
			if (expectedDescriptors == null || actualDescriptors == null)
317
				if (!(expectedDescriptors == null && actualDescriptors == null))
318
					fail(message + " missing key " + expectedKeys[i]);
319
320
			top: for (int j = 0; j < expectedDescriptors.length; j++) {
321
				for (int k = 0; k < actualDescriptors.length; k++) {
322
					if (Arrays.equals(expectedDescriptors[j].getProcessingSteps(), actualDescriptors[k].getProcessingSteps())) {
323
						File expectedFile = expected.getArtifactFile(expectedDescriptors[j]);
324
						File actualFile = actual.getArtifactFile(actualDescriptors[k]);
325
						if (expectedFile == null || actualFile == null)
326
							fail(message + " descriptor mismatch");
327
						if (!(expectedFile.exists() && actualFile.exists()))
328
							fail(message + " file does not exist");
329
						assertTrue(expectedFile.length() == actualFile.length());
330
						continue top;
331
					}
332
				}
333
				fail(message + "Missing expected descriptor" + expectedDescriptors[j]);
334
			}
335
		}
336
	}
337
338
	/**
339
	 * Tests mirroring all artifacts in a repository to an empty repository
340
	 * Source contains A, B
341
	 * Target contains
342
	 * Expected is A, B
343
	 */
344
	public void testArtifactMirrorToEmpty() {
345
		artifactMirrorToEmpty("1.0", true, false); // run the test with append set to true
346
347
		try {
348
			//verify destination's content
349
			assertContentEquals("1.1", getArtifactRepositoryManager().loadRepository(sourceRepoLocation.toURI(), null), getArtifactRepositoryManager().loadRepository(destRepoLocation.toURI(), null));
350
		} catch (ProvisionException e) {
351
			fail("1.2", e);
352
		}
353
	}
354
355
	/**
356
	 * Tests mirroring all artifacts in a repository to an empty repository with "-writeMode clean"
357
	 * Source contains A, B
358
	 * Target contains
359
	 * Expected is A, B
360
	 */
361
	public void testArtifactMirrorToEmptyWithClean() {
362
		artifactMirrorToEmpty("2.0", false, false);
363
364
		try {
365
			//verify destination's content
366
			assertContentEquals("2.1", getArtifactRepositoryManager().loadRepository(sourceRepoLocation.toURI(), null), getArtifactRepositoryManager().loadRepository(destRepoLocation.toURI(), null));
367
		} catch (ProvisionException e) {
368
			fail("2.2", e);
369
		}
370
	}
371
372
	/**
373
	 * Tests mirroring all artifacts in a repository to a repository populated with exact duplicate data
374
	 * Source contains A, B
375
	 * Target contains A, B
376
	 * Expected is A, B
377
	 */
378
	public void testArtifactMirrorToFullDuplicate() {
379
		artifactMirrorToFullDuplicate("3.0", true); //run the test with append set to true
380
381
		try {
382
			//verify destination's content
383
			assertContentEquals("3.1", getArtifactRepositoryManager().loadRepository(sourceRepoLocation.toURI(), null), getArtifactRepositoryManager().loadRepository(destRepoLocation.toURI(), null));
384
		} catch (ProvisionException e) {
385
			fail("3.2", e);
386
		}
387
	}
388
389
	/**
390
	 * Tests mirroring all artifacts in a repository to a repository populated with exact duplicate data with "-writeMode clean"
391
	 * Source contains A, B
392
	 * Target contains A, B
393
	 * Expected is A, B
394
	 */
395
	public void testArtifactMirrorToFullDuplicateWithClean() {
396
		artifactMirrorToFullDuplicate("4.0", false);
397
398
		try {
399
			//verify destination's content
400
			assertContentEquals("4.1", getArtifactRepositoryManager().loadRepository(sourceRepoLocation.toURI(), null), getArtifactRepositoryManager().loadRepository(destRepoLocation.toURI(), null));
401
		} catch (ProvisionException e) {
402
			fail("4.2", e);
403
		}
404
	}
405
406
	/**
407
	 * Tests mirroring all artifacts in a repository to a repository populated with non-duplicate entries
408
	 * Source contains A, B
409
	 * Target contains C, D
410
	 * Expected is A, B, C, D
411
	 */
412
	public void testArtifactMirrorToPopulated() {
413
		artifactMirrorToPopulated("5.0", true); //run the test with append set to true
414
415
		try {
416
			//verify destination's content
417
			assertContains("5.1", getArtifactRepositoryManager().loadRepository(sourceRepoLocation.toURI(), null), getArtifactRepositoryManager().loadRepository(destRepoLocation.toURI(), null));
418
			assertContains("5.2", getArtifactRepositoryManager().loadRepository(sourceRepo2Location.toURI(), null), getArtifactRepositoryManager().loadRepository(destRepoLocation.toURI(), null));
419
			//checks that the destination has the correct number of keys (no extras)
420
			assertEquals("5.3", getArtifactRepositoryManager().loadRepository(sourceRepoLocation.toURI(), null).getArtifactKeys().length + getArtifactRepositoryManager().loadRepository(sourceRepo2Location.toURI(), null).getArtifactKeys().length, getArtifactRepositoryManager().loadRepository(destRepoLocation.toURI(), null).getArtifactKeys().length);
421
		} catch (ProvisionException e) {
422
			fail("5.4", e);
423
		}
424
	}
425
426
	/**
427
	 * Tests mirroring all artifacts in a repository to a repository populated with non-duplicate entries with "-writeMode clean"
428
	 * Source contains A, B
429
	 * Target contains C, D
430
	 * Expected is A, B
431
	 */
432
	public void testArtifactMirrorToPopulatedWithClean() {
433
		artifactMirrorToPopulated("6.0", false);
434
435
		try {
436
			//verify destination's content
437
			assertContentEquals("6.1", getArtifactRepositoryManager().loadRepository(sourceRepoLocation.toURI(), null), getArtifactRepositoryManager().loadRepository(destRepoLocation.toURI(), null));
438
		} catch (ProvisionException e) {
439
			fail("6.2", e);
440
		}
441
	}
442
443
	/**
444
	 * Tests mirroring all artifacts in a repository to a repository populated with partially duplicate data
445
	 * Source contains A, B, C, D
446
	 * Target contains  A, B
447
	 * Expected is A, B, C, D
448
	 */
449
	public void testArtifactMirrorToPartialDuplicate() {
450
		artifactMirrorToPartialDuplicate("7.0", true); //run the test with append set to true
451
452
		try {
453
			//verify destination's content
454
			assertContentEquals("7.1", getArtifactRepositoryManager().loadRepository(sourceRepo3Location.toURI(), null), getArtifactRepositoryManager().loadRepository(destRepoLocation.toURI(), null));
455
		} catch (ProvisionException e) {
456
			fail("7.2", e);
457
		}
458
	}
459
460
	/**
461
	 * Tests mirroring all artifacts in a repository to a repository populated with partially duplicate data with "-writeMode clean"
462
	 * Source contains A, B, C, D
463
	 * Target contains  A, B
464
	 * Expected is A, B, C, D
465
	 */
466
	public void testArtifactMirrorToPartialDuplicateWithClean() {
467
		artifactMirrorToPartialDuplicate("8.0", false);
468
469
		try {
470
			//verify destination's content
471
			assertContentEquals("8.1", getArtifactRepositoryManager().loadRepository(sourceRepo3Location.toURI(), null), getArtifactRepositoryManager().loadRepository(destRepoLocation.toURI(), null));
472
		} catch (ProvisionException e) {
473
			fail("8.2", e);
474
		}
475
	}
476
477
	/**
478
	 * Tests mirroring all artifacts in a repository to a repository populated with both full duplicate and non-duplicate data
479
	 * Source contains A, B
480
	 * Target contains A, B, C, D
481
	 * Expected is A, B, C, D
482
	 */
483
	public void testArtifactMirrorToPopulatedWithFullDuplicate() {
484
		artifactMirrorToPopulatedWithFullDuplicate("9.0", true); //run the test with append set to true
485
486
		try {
487
			//verify destination's content
488
			assertContentEquals("9.1", getArtifactRepositoryManager().loadRepository(sourceRepo3Location.toURI(), null), getArtifactRepositoryManager().loadRepository(destRepoLocation.toURI(), null));
489
		} catch (ProvisionException e) {
490
			fail("9.2", e);
491
		}
492
	}
493
494
	/**
495
	 * Tests mirroring all artifacts in a repository to a repository populated with both full duplicate and non-duplicate data with "-writeMode clean"
496
	 * Source contains A, B
497
	 * Target contains A, B, C, D
498
	 * Expected is A, B
499
	 */
500
	public void testArtifactMirrorToPopulatedWithFullDuplicateWithClean() {
501
		artifactMirrorToPopulatedWithFullDuplicate("10.0", false);
502
503
		try {
504
			//verify destination's content
505
			assertContentEquals("10.1", getArtifactRepositoryManager().loadRepository(sourceRepoLocation.toURI(), null), getArtifactRepositoryManager().loadRepository(destRepoLocation.toURI(), null));
506
		} catch (ProvisionException e) {
507
			fail("10.2", e);
508
		}
509
	}
510
511
	/**
512
	 * Tests mirroring all artifacts in a repository to a repository populated with both partial duplicate and non-duplicate data
513
	 * Source contains A, B, C, D
514
	 * Target contains A, B, E, F
515
	 * Expected is A, B, C, D, E, F
516
	 */
517
	public void testArtifactMirrorToPopulatedWithPartialDuplicate() {
518
		artifactMirrorToPopulatedWithPartialDuplicate("11.0", true); //run the test with append set to true
519
520
		try {
521
			//verify destination's content
522
			assertContains("11.1", getArtifactRepositoryManager().loadRepository(sourceRepo3Location.toURI(), null), getArtifactRepositoryManager().loadRepository(destRepoLocation.toURI(), null));
523
			assertContains("11.2", getArtifactRepositoryManager().loadRepository(sourceRepo2Location.toURI(), null), getArtifactRepositoryManager().loadRepository(destRepoLocation.toURI(), null));
524
			//checks that the destination has the correct number of keys (no extras)
525
			assertEquals("11.3", getArtifactRepositoryManager().loadRepository(sourceRepo2Location.toURI(), null).getArtifactKeys().length + getArtifactRepositoryManager().loadRepository(sourceRepo3Location.toURI(), null).getArtifactKeys().length, getArtifactRepositoryManager().loadRepository(destRepoLocation.toURI(), null).getArtifactKeys().length);
526
		} catch (ProvisionException e) {
527
			fail("11.4", e);
528
		}
529
	}
530
531
	/**
532
	 * Tests mirroring all artifacts in a repository to a repository populated with both partial duplicate and non-duplicate data with "-writeMode clean"
533
	 * Source contains A, B, C, D
534
	 * Target contains A, B, E, F
535
	 * Expected is A, B, C, D
536
	 */
537
	public void testArtifactMirrorToPopulatedWithPartialDuplicateWithClean() {
538
		artifactMirrorToPopulatedWithPartialDuplicate("12.0", false);
539
540
		try {
541
			//verify destination's content
542
			assertContentEquals("12.1", getArtifactRepositoryManager().loadRepository(sourceRepo3Location.toURI(), null), getArtifactRepositoryManager().loadRepository(destRepoLocation.toURI(), null));
543
		} catch (ProvisionException e) {
544
			fail("12.2", e);
545
		}
546
	}
547
548
	/**
549
	 * Tests MirrorApplication's behaviour when given an invalid source repository
550
	 */
551
	public void testArtifactMirrorFromInvalid() {
552
		File invalidRepository = new File(getTempFolder(), getUniqueString());
553
		delete(invalidRepository);
554
555
		try {
556
			basicRunMirrorApplication("13.1", invalidRepository.toURI(), destRepoLocation.toURI(), true, false);
557
			//we expect a provision exception to be thrown. We should never get here.
558
			fail("13.0 ProvisionExpection not thrown");
559
		} catch (ProvisionException e) {
560
			return; //correct type of exception has been thrown
561
		} catch (Exception e) {
562
			fail("13.2", e);
563
		}
564
	}
565
566
	/**
567
	 * Tests MirrorApplication's behaviour when given an invalid destination repository
568
	 */
569
	public void testArtifactMirrorToInvalid() {
570
		URI invalidDestRepository = null;
571
		try {
572
			//Setup: create a URI pointing to an unmodifiable place
573
			invalidDestRepository = new URI("http://foobar.com/abcdefg");
574
575
			//run the application with the modifiable destination
576
			basicRunMirrorApplication("14.1", sourceRepoLocation.toURI(), invalidDestRepository, true, false);
577
			//we're expecting an UnsupportedOperationException so we should never get here
578
			fail("14.0 UnsupportedOperationException not thrown");
579
		} catch (ProvisionException e) {
580
			assertEquals("Unexpected error message", NLS.bind(org.eclipse.equinox.p2.internal.repository.tools.Messages.exception_invalidDestination, URIUtil.toUnencodedString(invalidDestRepository)), e.getMessage());
581
			return; //correct type of exception has been thrown
582
		} catch (Exception e) {
583
			fail("14.2", e);
584
		} finally {
585
			if (invalidDestRepository != null)
586
				getArtifactRepositoryManager().removeRepository(invalidDestRepository);
587
		}
588
	}
589
590
	/**
591
	 * Tests MirrorApplication's behaviour when given both an invalid source and an invalid destination repository
592
	 */
593
	public void testArtifactMirrorBothInvalid() {
594
		//Setup: create a file that is not a valid repository
595
		File invalidRepository = new File(getTempFolder(), getUniqueString());
596
		//Setup: delete any leftover data
597
		delete(invalidRepository);
598
599
		try {
600
			//Setup: create a URI pointing to an unmodifiable place
601
			URI invalidDestRepository = new URI("http://foobar.com/abcdefg");
602
			basicRunMirrorApplication("15.1", invalidRepository.toURI(), invalidDestRepository, true, false);
603
			//We expect the ProvisionException to be thrown
604
			fail("15.0 ProvisionException not thrown");
605
		} catch (ProvisionException e) {
606
			return; //correct type of exception was thrown
607
		} catch (Exception e) {
608
			fail("15.2", e);
609
		}
610
	}
611
612
	/**
613
	 * Tests mirroring an empty repository to another empty repository
614
	 * Source contains
615
	 * Target contains
616
	 * Expected is
617
	 */
618
	public void testArtifactMirrorEmptyToEmpty() {
619
		File emptyRepository = artifactMirrorEmpty("16.0", true);
620
621
		try {
622
			//verify destination's content
623
			assertContentEquals("16.1", getArtifactRepositoryManager().loadRepository(emptyRepository.toURI(), null), getArtifactRepositoryManager().loadRepository(destRepoLocation.toURI(), null));
624
		} catch (ProvisionException e) {
625
			fail("16.2", e);
626
		}
627
628
		//remove the emptyRepository
629
		getArtifactRepositoryManager().removeRepository(emptyRepository.toURI());
630
		//delete any left over data
631
		delete(emptyRepository);
632
	}
633
634
	/**
635
	 * Tests mirroring an empty repository to a populated repository
636
	 * Source contains
637
	 * Target contains A, B
638
	 * Expected is A, B
639
	 */
640
	public void testArtifactMirrorEmptyToPopulated() {
641
		File emptyRepository = artifactMirrorEmptyToPopulated("17.0", true);
642
643
		try {
644
			//verify destination's content
645
			assertContains("17.1", getArtifactRepositoryManager().loadRepository(emptyRepository.toURI(), null), getArtifactRepositoryManager().loadRepository(destRepoLocation.toURI(), null));
646
			assertContentEquals("17.2", getArtifactRepositoryManager().loadRepository(sourceRepoLocation.toURI(), null), getArtifactRepositoryManager().loadRepository(destRepoLocation.toURI(), null));
647
		} catch (ProvisionException e) {
648
			fail("17.3", e);
649
		}
650
651
		//remove the empty repository
652
		getArtifactRepositoryManager().removeRepository(emptyRepository.toURI());
653
		//remove any leftover data
654
		delete(emptyRepository);
655
	}
656
657
	/**
658
	 * Tests mirroring an empty repository to a populated repository with "-writeMode clean"
659
	 * Source contains
660
	 * Target contains A, B
661
	 * Expected is
662
	 */
663
	public void testArtifactMirrorEmptyToPopulatedWithClean() {
664
		File emptyRepository = artifactMirrorEmptyToPopulated("18.0", false);
665
666
		try {
667
			//verify destination's content
668
			assertContentEquals("18.1", getArtifactRepositoryManager().loadRepository(emptyRepository.toURI(), null), getArtifactRepositoryManager().loadRepository(destRepoLocation.toURI(), null));
669
		} catch (ProvisionException e) {
670
			fail("18.2", e);
671
		}
672
673
		//remove the empty repository
674
		getArtifactRepositoryManager().removeRepository(emptyRepository.toURI());
675
		//delete any leftover data
676
		delete(emptyRepository);
677
	}
678
679
	/**
680
	 * Tests mirroring a repository to itself
681
	 * Source contains A, B
682
	 * Target contains A, B
683
	 * Expected is A, B
684
	 */
685
	public void testArtifactMirrorSourceIsDestination() {
686
		//Setup: Populate the repository
687
		runMirrorApplication("19.0", sourceRepoLocation, destRepoLocation, false);
688
689
		//run the application with the source and destination specified to the same place
690
		runMirrorApplication("19.1", destRepoLocation, destRepoLocation, true);
691
692
		try {
693
			//verify destination's content
694
			assertContentEquals("19.2", getArtifactRepositoryManager().loadRepository(sourceRepoLocation.toURI(), null), getArtifactRepositoryManager().loadRepository(destRepoLocation.toURI(), null));
695
		} catch (ProvisionException e) {
696
			fail("19.3", e);
697
		}
698
	}
699
700
	/**
701
	 * Tests mirroring a repository with a different version of the same package
702
	 * Source contains A, B (v1.0.1)
703
	 * Target contains A, B (v1.0.0)
704
	 * Expected is A, B (v1.0.0) and A, B (v1.0.1)
705
	 */
706
	public void testArtifactMirrorDifferentVersions() {
707
		//Setup: Populate the repository
708
		runMirrorApplication("20.0", sourceRepoLocation, destRepoLocation, false);
709
710
		//run the application with the source and destination specified to the same place
711
		runMirrorApplication("20.1", sourceRepo4Location, destRepoLocation, true);
712
713
		try {
714
			//verify destination's content
715
			assertContains("20.2", getArtifactRepositoryManager().loadRepository(sourceRepoLocation.toURI(), null), getArtifactRepositoryManager().loadRepository(destRepoLocation.toURI(), null));
716
			assertContains("20.3", getArtifactRepositoryManager().loadRepository(sourceRepo4Location.toURI(), null), getArtifactRepositoryManager().loadRepository(destRepoLocation.toURI(), null));
717
			//checks that the destination has the correct number of keys (no extras)
718
			assertEquals("20.4", getArtifactRepositoryManager().loadRepository(sourceRepoLocation.toURI(), null).getArtifactKeys().length + getArtifactRepositoryManager().loadRepository(sourceRepo4Location.toURI(), null).getArtifactKeys().length, getArtifactRepositoryManager().loadRepository(destRepoLocation.toURI(), null).getArtifactKeys().length);
719
		} catch (ProvisionException e) {
720
			fail("20.5", e);
721
		}
722
	}
723
724
	/**
725
	 * Tests how mirror application handles an unspecified source
726
	 */
727
	public void testArtifactMirrorNullSource() {
728
		try {
729
			basicRunMirrorApplication("21.1", null, destRepoLocation.toURI());
730
			//We expect the ProvisionException to be thrown
731
			fail("21.3 ProvisionException not thrown");
732
		} catch (ProvisionException e) {
733
			return; //expected type of exception has been thrown
734
		} catch (Exception e) {
735
			fail("21.2", e);
736
		}
737
	}
738
739
	/**
740
	 * Tests how mirror application handles an unspecified destination
741
	 */
742
	public void testArtifactMirrorNullDestination() {
743
		try {
744
			basicRunMirrorApplication("22.1", sourceRepoLocation.toURI(), null);
745
			//We expect the ProvisionException to be thrown
746
			fail("22.3 ProvisionException not thrown");
747
		} catch (ProvisionException e) {
748
			return; //expected type of exception has been thrown
749
		} catch (Exception e) {
750
			fail("22.2", e);
751
		}
752
	}
753
754
	/**
755
	 * Tests how mirror application handles both an unspecified source and an unspecified destination
756
	 */
757
	public void testArtifactMirrorNullBoth() {
758
		try {
759
			basicRunMirrorApplication("23.0", null, null);
760
			//We expect the ProvisionException to be thrown
761
			fail("23.2 ProvisionException not thrown");
762
		} catch (ProvisionException e) {
763
			return; //expected type of exception has been thrown
764
		} catch (Exception e) {
765
			fail("23.1", e);
766
		}
767
	}
768
769
	/**
770
	 * Ensures that a repository created by the mirror application is a copy of the source
771
	 */
772
	public void testNewArtifactRepoProperties() {
773
		//run mirror application with source not preexisting
774
		artifactMirrorToEmpty("24.0", true, true);
775
776
		try {
777
			IArtifactRepository sourceRepository = getArtifactRepositoryManager().loadRepository(sourceRepoLocation.toURI(), null);
778
			IArtifactRepository destinationRepository = getArtifactRepositoryManager().loadRepository(destRepoLocation.toURI(), null);
779
			assertEquals("24.1", sourceRepository.getName(), destinationRepository.getName());
780
			assertRepositoryProperties("24.2", sourceRepository.getProperties(), destinationRepository.getProperties());
781
		} catch (ProvisionException e) {
782
			fail("24.3", e);
783
		}
784
	}
785
786
	/**
787
	 * Ensures that a repository created before the mirror application is run does not have its properties changed
788
	 */
789
	public void testExistingArtifactRepoProperties() {
790
		//Setup: create the destination
791
		String name = "Destination Name";
792
		Map properties = null; //default properties
793
		try {
794
			//create the repository and get the resulting properties
795
			properties = getArtifactRepositoryManager().createRepository(destRepoLocation.toURI(), name, IArtifactRepositoryManager.TYPE_SIMPLE_REPOSITORY, properties).getProperties();
796
		} catch (ProvisionException e) {
797
			fail("25.0", e);
798
		}
799
800
		//run the mirror application
801
		artifactMirrorToEmpty("25.2", true, false);
802
803
		try {
804
			IArtifactRepository repository = getArtifactRepositoryManager().loadRepository(destRepoLocation.toURI(), null);
805
			assertEquals("25.3", name, repository.getName());
806
			assertRepositoryProperties("25.4", properties, repository.getProperties());
807
		} catch (ProvisionException e) {
808
			fail("25.5", e);
809
		}
810
	}
811
812
	/**
813
	 *  * Ensures that a repository created by the mirror application has specified name
814
	 * For Bug 256909
815
	 */
816
	public void testNewArtifactRepoWithNewName() {
817
		String name = "Bug 256909 test - new";
818
		try {
819
			basicRunMirrorApplication("Bug 256909 Test", sourceRepoLocation.toURI(), destRepoLocation.toURI(), true, false, name);
820
		} catch (MalformedURLException e) {
821
			fail("Error creating URLs for Source/Detination", e);
822
		} catch (Exception e) {
823
			fail("Error running mirror application", e);
824
		}
825
826
		try {
827
			assertEquals("Assert name was set correct", name, getArtifactRepositoryManager().loadRepository(destRepoLocation.toURI(), null).getName());
828
		} catch (ProvisionException e) {
829
			fail("Cannot obtain destination", e);
830
		}
831
	}
832
833
	/**
834
	 * Ensures that an existing destination used by the mirror application is given specified name
835
	 * For Bug 256909
836
	 */
837
	public void testExistingArtifactRepoWithNewName() {
838
		String oldName = "The original naem for Bug 256909 test - existing";
839
		String newName = "Bug 256909 test - existing";
840
		//Setup create the repository
841
		IArtifactRepository destinationRepo = null;
842
		try {
843
			destinationRepo = getArtifactRepositoryManager().createRepository(destRepoLocation.toURI(), oldName, IArtifactRepositoryManager.TYPE_SIMPLE_REPOSITORY, null);
844
		} catch (ProvisionException e) {
845
			fail("Error creating repo at destination", e);
846
		}
847
		assertEquals("Assert name is set correctly before mirror", oldName, destinationRepo.getName());
848
849
		try {
850
			MirrorApplication app = new MirrorApplication();
851
			app.addSource(createRepositoryDescriptor(sourceRepoLocation.toURI(), null, null, null));
852
			app.addDestination(createRepositoryDescriptor(destRepoLocation.toURI(), null, null, newName));
853
			//run the mirror application
854
			app.run(null);
855
856
		} catch (Exception e) {
857
			fail("Error running mirror application", e);
858
		}
859
860
		try {
861
			assertEquals("Assert name is set correctly after mirror", newName, getArtifactRepositoryManager().loadRepository(destRepoLocation.toURI(), null).getName());
862
		} catch (ProvisionException e) {
863
			fail("Error loading destination", e);
864
		}
865
	}
866
867
	/**
868
	 * Verifies that the mirror application copies files (including packed files) correctly
869
	 */
870
	public void testArtifactFileCopying() {
871
		//Setup: load the repository containing packed data
872
		File packedRepoLocation = getTestData("26.0", "/testData/mirror/mirrorPackedRepo");
873
874
		try {
875
			basicRunMirrorApplication("26.1", packedRepoLocation.toURI(), destRepoLocation.toURI(), false, false);
876
		} catch (Exception e) {
877
			fail("26.3", e);
878
		}
879
880
		try {
881
			//Verify Contents
882
			assertContentEquals("26.4", getArtifactRepositoryManager().loadRepository(packedRepoLocation.toURI(), null), getArtifactRepositoryManager().loadRepository(destRepoLocation.toURI(), null));
883
			//Verify files on disk
884
			assertFileSizes("26.5", (SimpleArtifactRepository) getArtifactRepositoryManager().loadRepository(packedRepoLocation.toURI(), null), (SimpleArtifactRepository) getArtifactRepositoryManager().loadRepository(destRepoLocation.toURI(), null));
885
		} catch (ProvisionException e) {
886
			fail("26.6", e);
887
		}
888
	}
889
890
	/**
891
	 * Verifies that the mirror application executes processing steps correctly
892
	 */
893
	public void testArtifactProcessingSteps() {
894
		//Setup: load the repository containing packed data
895
		File packedRepoLocation = getTestData("27.0", "/testData/mirror/mirrorPackedRepo");
896
		IArtifactRepository packedRepo = null;
897
		IArtifactRepository destinationRepo = null;
898
899
		try {
900
			packedRepo = getArtifactRepositoryManager().loadRepository(packedRepoLocation.toURI(), null);
901
			destinationRepo = getArtifactRepositoryManager().createRepository(destRepoLocation.toURI(), "Test Repo", IArtifactRepositoryManager.TYPE_SIMPLE_REPOSITORY, null);
902
		} catch (ProvisionException e1) {
903
			fail("");
904
		}
905
906
		IArtifactKey[] keys = packedRepo.getArtifactKeys();
907
908
		for (int i = 0; i < keys.length; i++) {
909
			IArtifactDescriptor[] srcDescriptors = packedRepo.getArtifactDescriptors(keys[i]);
910
911
			for (int j = 0; j < srcDescriptors.length; j++) {
912
				if (!(srcDescriptors[j].getProperty(IArtifactDescriptor.FORMAT) == null) && srcDescriptors[j].getProperty(IArtifactDescriptor.FORMAT).equals("packed")) {
913
					//if we have a packed artifact
914
					IArtifactDescriptor newDescriptor = new ArtifactDescriptor(keys[i]);
915
					Map properties = new OrderedProperties();
916
					properties.putAll(srcDescriptors[j].getProperties());
917
					properties.remove(IArtifactDescriptor.FORMAT);
918
					((ArtifactDescriptor) newDescriptor).addProperties(properties);
919
					//create appropriate descriptor
920
					try {
921
						OutputStream repositoryStream = null;
922
						try {
923
							//System.out.println("Mirroring: " + srcDescriptors[j].getArtifactKey() + " (Descriptor: " + srcDescriptors[j] + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
924
							repositoryStream = destinationRepo.getOutputStream(newDescriptor);
925
							if (repositoryStream == null)
926
								return;
927
							// TODO Is that ok to ignore the result?
928
							//TODO MAKE THIS WORK PROPERLY
929
							packedRepo.getArtifact(srcDescriptors[j], repositoryStream, new NullProgressMonitor());
930
						} finally {
931
							if (repositoryStream != null)
932
								repositoryStream.close();
933
						}
934
					} catch (ProvisionException e) {
935
						fail("27.1", e);
936
					} catch (IOException e) {
937
						fail("27.2", e);
938
					}
939
					//corresponding key should now be in the destination
940
					IArtifactDescriptor[] destDescriptors = destinationRepo.getArtifactDescriptors(keys[i]);
941
					boolean canonicalFound = false;
942
					for (int l = 0; !canonicalFound && (l < destDescriptors.length); l++) {
943
						//No processing steps mean item is canonical
944
						if (destDescriptors[l].getProcessingSteps().length == 0)
945
							canonicalFound = true;
946
					}
947
					if (!canonicalFound)
948
						fail("27.3 no canonical found for " + keys[i].toString());
949
950
					//ensure the canonical matches that in the expected
951
					assertFileSizes("27.3", (SimpleArtifactRepository) destinationRepo, (SimpleArtifactRepository) packedRepo);
952
				}
953
			}
954
		}
955
	}
956
957
	//for Bug 235683
958
	public void testMirrorCompressedSource() {
959
		File compressedSource = getTestData("0", "/testData/mirror/mirrorCompressedRepo");
960
961
		//Setup: get the artifacts.jar file
962
		File compressedArtifactsXML = new File(compressedSource.getAbsoluteFile() + "/artifacts.jar");
963
		//Setup: make sure artifacts.jar exists
964
		assertTrue("1", compressedArtifactsXML.exists());
965
966
		try {
967
			basicRunMirrorApplication("2", compressedSource.toURI(), destRepoLocation.toURI(), false, false);
968
		} catch (MalformedURLException e) {
969
			fail("3", e);
970
		} catch (Exception e) {
971
			fail("4", e);
972
		}
973
974
		//get the artifacts.jar file
975
		File destArtifactsXML = new File(destRepoLocation.getAbsolutePath() + "/artifacts.jar");
976
		//make sure artifacts.jar exists
977
		assertTrue("5", destArtifactsXML.exists());
978
	}
979
980
	//for Bug 235683
981
	public void testMirrorCompressedSourcetoUncompressedDestination() {
982
		File compressedSource = getTestData("0", "/testData/mirror/mirrorCompressedRepo");
983
984
		//Setup: get the artifacts.jar file
985
		File compressedArtifactsXML = new File(compressedSource.getAbsoluteFile() + "/artifacts.jar");
986
		//Setup: make sure artifacts.jar exists
987
		assertTrue("1", compressedArtifactsXML.exists());
988
989
		//Setup: create the destination
990
		try {
991
			String name = "Destination Name " + destRepoLocation;
992
			getArtifactRepositoryManager().createRepository(destRepoLocation.toURI(), name, IArtifactRepositoryManager.TYPE_SIMPLE_REPOSITORY, null);
993
		} catch (ProvisionException e) {
994
			fail("2", e);
995
		}
996
997
		assertTrue("2.1", new File(destRepoLocation, "artifacts.xml").exists());
998
		try {
999
			basicRunMirrorApplication("3", compressedSource.toURI(), destRepoLocation.toURI(), false, false);
1000
		} catch (MalformedURLException e) {
1001
			fail("4", e);
1002
		} catch (Exception e) {
1003
			fail("5", e);
1004
		}
1005
1006
		//get the artifacts.jar file
1007
		File destArtifactsXML = new File(destRepoLocation.getAbsolutePath() + "/artifacts.jar");
1008
		//make sure artifacts.jar does not exist
1009
		assertFalse("6", destArtifactsXML.exists());
1010
		//get the artifacts.xml file
1011
		destArtifactsXML = new File(destRepoLocation.getAbsolutePath() + "/artifacts.xml");
1012
		//make sure artifacts.xml exists
1013
		assertTrue("7", destArtifactsXML.exists());
1014
	}
1015
1016
	//for Bug 235683
1017
	public void testMirrorUncompressedSourceToCompressedDestination() {
1018
		File uncompressedSource = getTestData("0", "/testData/mirror/mirrorPackedRepo");
1019
1020
		//Setup: get the artifacts.xml file
1021
		File artifactsXML = new File(uncompressedSource.getAbsoluteFile() + "/artifacts.xml");
1022
		//Setup: make sure artifacts.xml exists
1023
		assertTrue("1", artifactsXML.exists());
1024
1025
		//Setup: create the destination
1026
		try {
1027
			String name = "Destination Name " + destRepoLocation;
1028
			Map property = new HashMap();
1029
			property.put(IRepository.PROP_COMPRESSED, "true");
1030
			getArtifactRepositoryManager().createRepository(destRepoLocation.toURI(), name, IArtifactRepositoryManager.TYPE_SIMPLE_REPOSITORY, property);
1031
		} catch (ProvisionException e) {
1032
			fail("2", e);
1033
		}
1034
1035
		assertTrue("2.1", new File(destRepoLocation, "artifacts.jar").exists());
1036
		try {
1037
			basicRunMirrorApplication("3", uncompressedSource.toURI(), destRepoLocation.toURI(), false, false);
1038
		} catch (MalformedURLException e) {
1039
			fail("4", e);
1040
		} catch (Exception e) {
1041
			fail("5", e);
1042
		}
1043
1044
		//get the artifacts.jar file
1045
		File destArtifactsXML = new File(destRepoLocation.getAbsolutePath() + "/artifacts.jar");
1046
		//make sure artifacts.jar does exist
1047
		assertTrue("6", destArtifactsXML.exists());
1048
		//get the artifacts.xml file
1049
		destArtifactsXML = new File(destRepoLocation.getAbsolutePath() + "/artifacts.xml");
1050
		//make sure artifacts.xml does not exist
1051
		assertFalse("7", destArtifactsXML.exists());
1052
	}
1053
1054
	public void testMirrorApplicationWithCompositeSource() {
1055
		//Setup Make composite repository
1056
		File repoLocation = new File(getTempFolder(), "CompositeArtifactMirrorTest");
1057
		AbstractProvisioningTest.delete(repoLocation);
1058
		IArtifactRepository repo = null;
1059
		try {
1060
			repo = getArtifactRepositoryManager().createRepository(repoLocation.toURI(), "artifact name", IArtifactRepositoryManager.TYPE_COMPOSITE_REPOSITORY, null);
1061
		} catch (ProvisionException e) {
1062
			fail("Could not create repository");
1063
		}
1064
		//ensure proper type of repository has been created
1065
		if (!(repo instanceof CompositeArtifactRepository))
1066
			fail("Repository is not a CompositeArtifactRepository");
1067
		//Populate source
1068
		File child1 = getTestData("1", "/testData/mirror/mirrorSourceRepo1 with space");
1069
		File child2 = getTestData("2", "/testData/mirror/mirrorSourceRepo2");
1070
		((CompositeArtifactRepository) repo).addChild(child1.toURI());
1071
		((CompositeArtifactRepository) repo).addChild(child2.toURI());
1072
1073
		runMirrorApplication("Mirroring from Composite Source", repoLocation, destRepoLocation, false);
1074
1075
		try {
1076
			assertContentEquals("Verifying contents", repo, getArtifactRepositoryManager().loadRepository(destRepoLocation.toURI(), null));
1077
1078
			//Verify that result is the same as mirroring from the 2 repositories separately
1079
			assertContains("3", getArtifactRepositoryManager().loadRepository(sourceRepoLocation.toURI(), null), getArtifactRepositoryManager().loadRepository(destRepoLocation.toURI(), null));
1080
			assertContains("4", getArtifactRepositoryManager().loadRepository(sourceRepo2Location.toURI(), null), getArtifactRepositoryManager().loadRepository(destRepoLocation.toURI(), null));
1081
			//checks that the destination has the correct number of keys (no extras)
1082
			assertEquals("5", getArtifactRepositoryManager().loadRepository(sourceRepoLocation.toURI(), null).getArtifactKeys().length + getArtifactRepositoryManager().loadRepository(sourceRepo2Location.toURI(), null).getArtifactKeys().length, getArtifactRepositoryManager().loadRepository(destRepoLocation.toURI(), null).getArtifactKeys().length);
1083
		} catch (ProvisionException e) {
1084
			fail("Could not load destination", e);
1085
		}
1086
	}
1087
1088
	//for Bug 250527
1089
	public void testIgnoreErrorsArgument() {
1090
		//Error prints to stderr, redirect that to a file
1091
		PrintStream oldErr = System.err;
1092
		PrintStream newErr = null;
1093
		try {
1094
			destRepoLocation.mkdir();
1095
			newErr = new PrintStream(new FileOutputStream(new File(destRepoLocation, "sys.err")));
1096
		} catch (FileNotFoundException e) {
1097
			fail("Error redirecting outputs", e);
1098
		}
1099
		System.setErr(newErr);
1100
1101
		//run test without verbose
1102
		mirrorWithError(false);
1103
1104
		System.setErr(oldErr);
1105
		newErr.close();
1106
1107
		try {
1108
			assertEquals("Verifying correct number of Keys", 1, getArtifactRepositoryManager().loadRepository(destRepoLocation.toURI(), null).getArtifactKeys().length);
1109
			//Because only 1 of the artifacts exists on disk, the number of artifacts in the destination should only be 1.
1110
			//Order in which mirror application mirrors artifacts is random.
1111
		} catch (ProvisionException e) {
1112
			fail("Error laoding destiantion repo", e);
1113
		}
1114
	}
1115
1116
	public void testCompareUsingMD5Comparator() {
1117
		//Setup create descriptors with different md5 values
1118
		IArtifactKey dupKey = PublisherHelper.createBinaryArtifactKey("testKeyId", new Version("1.2.3"));
1119
		File artifact1 = getTestData("0.0", "/testData/mirror/mirrorSourceRepo1 with space/artifacts.xml");
1120
		File artifact2 = getTestData("0.0", "/testData/mirror/mirrorSourceRepo2/artifacts.xml");
1121
		IArtifactDescriptor descriptor1 = PublisherHelper.createArtifactDescriptor(dupKey, artifact1);
1122
		IArtifactDescriptor descriptor2 = PublisherHelper.createArtifactDescriptor(dupKey, artifact2);
1123
1124
		assertEquals("Ensuring Descriptors are the same", descriptor1, descriptor2);
1125
		assertNotSame("Ensuring MD5 values are different", descriptor1.getProperty(IArtifactDescriptor.DOWNLOAD_MD5), descriptor2.getProperty(IArtifactDescriptor.DOWNLOAD_MD5));
1126
1127
		//Setup make repositories
1128
		File repo1Location = getTestFolder(getUniqueString());
1129
		File repo2Location = getTestFolder(getUniqueString());
1130
		IArtifactRepository repo1 = null;
1131
		IArtifactRepository repo2 = null;
1132
		try {
1133
			repo1 = getArtifactRepositoryManager().createRepository(repo1Location.toURI(), "Repo 1", IArtifactRepositoryManager.TYPE_SIMPLE_REPOSITORY, null);
1134
			repo1.addDescriptor(descriptor1);
1135
			repo2 = getArtifactRepositoryManager().createRepository(repo2Location.toURI(), "Repo 2", IArtifactRepositoryManager.TYPE_SIMPLE_REPOSITORY, null);
1136
			repo2.addDescriptor(descriptor2);
1137
		} catch (ProvisionException e) {
1138
			fail("Error creating repositories", e);
1139
		}
1140
1141
		//Comparator prints to stderr, redirect that to a file
1142
		PrintStream oldErr = System.err;
1143
		PrintStream newErr = null;
1144
		PrintStream oldOut = System.out;
1145
		PrintStream newOut = null;
1146
		try {
1147
			newErr = new PrintStream(new FileOutputStream(new File(repo2Location, "sys.err")));
1148
			newOut = new PrintStream(new FileOutputStream(new File(repo2Location, "sys.out")));
1149
		} catch (FileNotFoundException e) {
1150
			fail("Error redirecting outputs", e);
1151
		}
1152
		System.setErr(newErr);
1153
		System.setOut(newOut);
1154
		MirrorApplication app = null;
1155
		try {
1156
			app = new MirrorApplication();
1157
			app.addSource(createRepositoryDescriptor(repo1Location.toURI(), null, null, null));
1158
			app.addDestination(createRepositoryDescriptor(repo2Location.toURI(), null, null, null));
1159
			app.setVerbose(true);
1160
			//Set compare flag.
1161
			app.setCompare(true);
1162
			//run the mirror application
1163
			app.run(null);
1164
		} catch (Exception e) {
1165
			fail("Running mirror application with duplicate descriptors with different md5 values failed", e);
1166
		}
1167
		System.setErr(oldErr);
1168
		newErr.close();
1169
		System.setOut(oldOut);
1170
		newOut.close();
1171
1172
		IArtifactDescriptor[] destDescriptors = repo2.getArtifactDescriptors(descriptor2.getArtifactKey());
1173
		assertEquals("Ensuring destination has correct number of descriptors", 1, destDescriptors.length);
1174
		assertEquals("Ensuring proper descriptor exists in destination", descriptor2.getProperty(IArtifactDescriptor.DOWNLOAD_MD5), destDescriptors[0].getProperty(IArtifactDescriptor.DOWNLOAD_MD5));
1175
		String msg = NLS.bind(Messages.warning_differentMD5, new Object[] {URIUtil.toUnencodedString(repo1.getLocation()), URIUtil.toUnencodedString(repo2.getLocation()), descriptor1});
1176
		try {
1177
			assertLogContainsLine(TestActivator.getLogFile(), msg);
1178
		} catch (Exception e) {
1179
			fail("error verifying output", e);
1180
		}
1181
	}
1182
1183
	public void testBaselineCompareUsingMD5Comparator() {
1184
		//Setup create descriptors with different md5 values
1185
		IArtifactKey dupKey = PublisherHelper.createBinaryArtifactKey("testKeyId", new Version("1.2.3"));
1186
		File artifact1 = getTestData("0.0", "/testData/mirror/mirrorSourceRepo1 with space/content.xml");
1187
		File artifact2 = getTestData("0.0", "/testData/mirror/mirrorSourceRepo2/content.xml");
1188
1189
		//Setup Copy the file to the baseline
1190
		File repoLocation = getTestFolder(getUniqueString());
1191
		File baselineLocation = getTestFolder(getUniqueString());
1192
		File baselineBinaryDirectory = new File(baselineLocation, "binary");
1193
		baselineBinaryDirectory.mkdir();
1194
		File baselineContentLocation = new File(baselineBinaryDirectory, "testKeyId_1.2.3");
1195
		AbstractProvisioningTest.copy("Copying File to baseline", artifact2, baselineContentLocation);
1196
1197
		IArtifactDescriptor descriptor1 = PublisherHelper.createArtifactDescriptor(dupKey, artifact1);
1198
		IArtifactDescriptor descriptor2 = PublisherHelper.createArtifactDescriptor(dupKey, baselineContentLocation);
1199
1200
		assertEquals("Ensuring Descriptors are the same", descriptor1, descriptor2);
1201
		assertNotSame("Ensuring MD5 values are different", descriptor1.getProperty(IArtifactDescriptor.DOWNLOAD_MD5), descriptor2.getProperty(IArtifactDescriptor.DOWNLOAD_MD5));
1202
1203
		//Setup make repositories
1204
		IArtifactRepository repo = null;
1205
		IArtifactRepository baseline = null;
1206
		try {
1207
			repo = getArtifactRepositoryManager().createRepository(repoLocation.toURI(), "Repo 1", IArtifactRepositoryManager.TYPE_SIMPLE_REPOSITORY, null);
1208
			repo.addDescriptor(descriptor1);
1209
			baseline = getArtifactRepositoryManager().createRepository(baselineLocation.toURI(), "Repo 2", IArtifactRepositoryManager.TYPE_SIMPLE_REPOSITORY, null);
1210
			baseline.addDescriptor(descriptor2);
1211
		} catch (ProvisionException e) {
1212
			fail("Error creating repositories", e);
1213
		}
1214
1215
		//Comparator prints to stderr, redirect that to a file
1216
		PrintStream oldErr = System.err;
1217
		PrintStream newErr = null;
1218
		PrintStream oldOut = System.out;
1219
		PrintStream newOut = null;
1220
		try {
1221
			destRepoLocation.mkdir();
1222
			newErr = new PrintStream(new FileOutputStream(new File(destRepoLocation, "sys.err")));
1223
			newOut = new PrintStream(new FileOutputStream(new File(destRepoLocation, "sys.out")));
1224
		} catch (FileNotFoundException e) {
1225
			fail("Error redirecting outputs", e);
1226
		}
1227
		System.setErr(newErr);
1228
		System.setOut(newOut);
1229
		MirrorApplication app = null;
1230
		try {
1231
			app = new MirrorApplication();
1232
			app.addSource(createRepositoryDescriptor(repoLocation.toURI(), null, null, null));
1233
			app.addDestination(createRepositoryDescriptor(destRepoLocation.toURI(), null, null, null));
1234
			//Set baseline
1235
			app.setBaseline(baselineLocation.toURI());
1236
			app.setVerbose(true);
1237
			//Set compare flag.
1238
			app.setCompare(true);
1239
			//run the mirror application
1240
			app.run(null);
1241
		} catch (Exception e) {
1242
			fail("Running mirror application with baseline compare", e);
1243
		}
1244
		System.setErr(oldErr);
1245
		newErr.close();
1246
		System.setOut(oldOut);
1247
		newOut.close();
1248
1249
		IArtifactRepository destination = null;
1250
		try {
1251
			destination = getArtifactRepositoryManager().loadRepository(destRepoLocation.toURI(), null);
1252
		} catch (ProvisionException e) {
1253
			fail("Error loading destination", e);
1254
		}
1255
1256
		IArtifactDescriptor[] destDescriptors = destination.getArtifactDescriptors(descriptor2.getArtifactKey());
1257
		assertEquals("Ensuring destination has correct number of descriptors", 1, destDescriptors.length);
1258
		assertEquals("Ensuring destination contains the descriptor from the baseline", descriptor2.getProperty(IArtifactDescriptor.DOWNLOAD_MD5), destDescriptors[0].getProperty(IArtifactDescriptor.DOWNLOAD_MD5));
1259
		String msg = NLS.bind(Messages.warning_differentMD5, new Object[] {URIUtil.toUnencodedString(baseline.getLocation()), URIUtil.toUnencodedString(repo.getLocation()), descriptor1});
1260
		try {
1261
			assertLogContainsLine(TestActivator.getLogFile(), msg);
1262
		} catch (Exception e) {
1263
			fail("error verifying output", e);
1264
		}
1265
	}
1266
1267
	//for Bug 259111
1268
	public void testDownloadRetry() {
1269
		//repository that is known to force a retry
1270
		class TestRetryArtifactRepository extends SimpleArtifactRepository {
1271
			public boolean firstAttempt = true;
1272
			IArtifactRepository source;
1273
1274
			public TestRetryArtifactRepository(String repositoryName, URI location, URI srcLocation, Map properties, IArtifactRepositoryManager manager) {
1275
				super(repositoryName, location, properties);
1276
1277
				//initialize
1278
				try {
1279
					source = manager.loadRepository(srcLocation, null);
1280
				} catch (ProvisionException e) {
1281
					fail("Unable to load source for wrapping", e);
1282
				}
1283
				manager.removeRepository(srcLocation);
1284
			}
1285
1286
			public synchronized IArtifactKey[] getArtifactKeys() {
1287
				return source.getArtifactKeys();
1288
			}
1289
1290
			public synchronized IArtifactDescriptor[] getArtifactDescriptors(IArtifactKey key) {
1291
				return source.getArtifactDescriptors(key);
1292
			}
1293
1294
			public IStatus getRawArtifact(IArtifactDescriptor descriptor, OutputStream destination, IProgressMonitor monitor) {
1295
				if (firstAttempt) {
1296
					firstAttempt = false;
1297
					return new Status(IStatus.ERROR, Activator.ID, IArtifactRepository.CODE_RETRY, "Forcing Retry", new ProvisionException("Forcing retry"));
1298
				}
1299
1300
				return source.getRawArtifact(descriptor, destination, monitor);
1301
			}
1302
1303
			public synchronized boolean contains(IArtifactDescriptor descriptor) {
1304
				return source.contains(descriptor);
1305
			}
1306
		}
1307
1308
		//set up test repository
1309
		File retryRepoLoaction = new File(getTempFolder(), "259111 Repo");
1310
		IArtifactRepository retryRepo = new TestRetryArtifactRepository("Test Repo", retryRepoLoaction.toURI(), sourceRepoLocation.toURI(), null, getArtifactRepositoryManager());
1311
		((ArtifactRepositoryManager) getArtifactRepositoryManager()).addRepository(retryRepo);
1312
1313
		try {
1314
			basicRunMirrorApplication("Forcing Retry", retryRepo.getLocation(), destRepoLocation.toURI());
1315
		} catch (MalformedURLException e) {
1316
			fail("Error creating arguments", e);
1317
		} catch (Exception e) {
1318
			fail("Error while running Mirror Application and forcing retry", e);
1319
		}
1320
1321
		//ensure error was resulted
1322
		assertFalse(((TestRetryArtifactRepository) retryRepo).firstAttempt);
1323
		try {
1324
			//verify destination's content
1325
			assertContentEquals("Verifying content", getArtifactRepositoryManager().loadRepository(sourceRepoLocation.toURI(), null), getArtifactRepositoryManager().loadRepository(destRepoLocation.toURI(), null));
1326
		} catch (ProvisionException e) {
1327
			fail("Failure while verifying destination", e);
1328
		}
1329
	}
1330
1331
	//for Bug 259112
1332
	public void testErrorLoggingNoVerbose() {
1333
		//initialize log file
1334
		FrameworkLog log = (FrameworkLog) ServiceHelper.getService(Activator.getContext(), FrameworkLog.class.getName());
1335
		assertNotNull("Assert log file is not null", log);
1336
		assertTrue("Clearing log file", log.getFile().delete());
1337
1338
		//Comparator prints to stderr, redirect that to a file
1339
		PrintStream oldErr = System.err;
1340
		PrintStream newErr = null;
1341
		try {
1342
			destRepoLocation.mkdir();
1343
			newErr = new PrintStream(new FileOutputStream(new File(destRepoLocation, "sys.err")));
1344
		} catch (FileNotFoundException e) {
1345
			fail("Error redirecting outputs", e);
1346
		}
1347
		System.setErr(newErr);
1348
1349
		//run test without verbose resulting in error
1350
		mirrorWithError(false);
1351
1352
		System.setErr(oldErr);
1353
		newErr.close();
1354
1355
		//verify log
1356
		try {
1357
			String[] parts = new String[] {"java.io.FileNotFoundException: ", "helloworld_1.0.0.jar"};
1358
			assertLogContainsLine(log.getFile(), parts);
1359
		} catch (Exception e) {
1360
			fail("error verifying output", e);
1361
		}
1362
1363
		//run without verbose
1364
		artifactMirrorToFullDuplicate("Generating INFO entries", true);
1365
1366
		IArtifactRepository sourceRepository = null;
1367
		try {
1368
			sourceRepository = getArtifactRepositoryManager().loadRepository(sourceRepoLocation.toURI(), null);
1369
		} catch (ProvisionException e) {
1370
			fail("Error loading source repository for verification", e);
1371
		}
1372
1373
		try {
1374
			//Mirroring full duplicate, so any key will do.
1375
			IArtifactDescriptor[] descriptors = sourceRepository.getArtifactDescriptors(sourceRepository.getArtifactKeys()[0]);
1376
			//Mirroring full duplicate, so any descriptor will do.
1377
			String message = NLS.bind(org.eclipse.equinox.internal.p2.artifact.repository.Messages.mirror_alreadyExists, descriptors[0], destRepoLocation.toURI());
1378
			assertLogDoesNotContainLine(log.getFile(), message);
1379
		} catch (Exception e) {
1380
			fail("Error verifying log", e);
1381
		}
1382
	}
1383
1384
	//for Bug 259112
1385
	public void testErrorLoggingWithVerbose() {
1386
		//initialize log file
1387
		FrameworkLog log = (FrameworkLog) ServiceHelper.getService(Activator.getContext(), FrameworkLog.class.getName());
1388
		assertNotNull("Assert log file is not null", log);
1389
		assertTrue("Clearing log file", log.getFile().exists() && log.getFile().delete());
1390
1391
		//Comparator prints to stdout, redirect that to a file
1392
		PrintStream oldOut = System.out;
1393
		PrintStream newOut = null;
1394
		PrintStream oldErr = System.err;
1395
		PrintStream newErr = null;
1396
		try {
1397
			destRepoLocation.mkdir();
1398
			newOut = new PrintStream(new FileOutputStream(new File(destRepoLocation, "sys.out")));
1399
			newErr = new PrintStream(new FileOutputStream(new File(destRepoLocation, "sys.err")));
1400
		} catch (FileNotFoundException e) {
1401
			fail("Error redirecting output", e);
1402
		}
1403
		System.setOut(newOut);
1404
		System.setErr(newErr);
1405
1406
		//run test with verbose, results in error
1407
		mirrorWithError(true);
1408
1409
		//verify log
1410
		try {
1411
			String[] parts = new String[] {"java.io.FileNotFoundException: ", "helloworld_1.0.0.jar"};
1412
			assertLogContainsLine(log.getFile(), parts);
1413
		} catch (Exception e) {
1414
			fail("error verifying output", e);
1415
		}
1416
1417
		//run with verbose
1418
		//populate destination with duplicate artifacts. We assume this works
1419
		runMirrorApplication("Initializing Destiantion", sourceRepoLocation, destRepoLocation, false); //value of append should not matter
1420
1421
		try {
1422
			MirrorApplication app = new MirrorApplication();
1423
			app.addSource(createRepositoryDescriptor(sourceRepoLocation.toURI(), null, null, null));
1424
			app.addDestination(createRepositoryDescriptor(destRepoLocation.toURI(), null, null, null));
1425
			//set the arguments with verbose
1426
			app.setVerbose(true);
1427
			//run the mirror application
1428
			app.run(null);
1429
		} catch (Exception e) {
1430
			fail("Error running mirror application to generate INFO items", e);
1431
		}
1432
1433
		System.setOut(oldOut);
1434
		newOut.close();
1435
		System.setErr(oldErr);
1436
		newErr.close();
1437
1438
		IArtifactRepository sourceRepository = null;
1439
		try {
1440
			sourceRepository = getArtifactRepositoryManager().loadRepository(sourceRepoLocation.toURI(), null);
1441
		} catch (ProvisionException e) {
1442
			fail("Error loading source repository for verification", e);
1443
		}
1444
1445
		try {
1446
			//Mirroring full duplicate, so any key will do.
1447
			IArtifactDescriptor[] descriptors = sourceRepository.getArtifactDescriptors(sourceRepository.getArtifactKeys()[0]);
1448
			//Mirroring full duplicate, so any descriptor will do.
1449
			String message = NLS.bind(org.eclipse.equinox.internal.p2.artifact.repository.Messages.mirror_alreadyExists, descriptors[0], destRepoLocation.toURI());
1450
			assertLogContainsLine(log.getFile(), message);
1451
		} catch (Exception e) {
1452
			fail("Error verifying log", e);
1453
		}
1454
	}
1455
1456
	/**
1457
	 * Test how the mirror application handles a repository specified as a local path
1458
	 */
1459
	public void testArtifactMirrorNonURIDest() {
1460
		try {
1461
			basicRunMirrorApplication("Mirroring", sourceRepoLocation.toURI(), destRepoLocation.toURI());
1462
			assertContentEquals("2.1", getArtifactRepositoryManager().loadRepository(sourceRepoLocation.toURI(), null), getArtifactRepositoryManager().loadRepository(destRepoLocation.toURI(), null));
1463
		} catch (Exception e) {
1464
			fail("Error mirroring", e);
1465
		}
1466
	}
1467
}
(-)src/org/eclipse/equinox/p2/tests/AbstractAntProvisioningTest.java (+263 lines)
Added Link Here
1
/*******************************************************************************
2
 *  Copyright (c) 2009 IBM Corporation 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
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.equinox.p2.tests;
12
13
import java.io.File;
14
import java.io.FileOutputStream;
15
import java.net.URI;
16
import java.util.*;
17
import org.eclipse.ant.core.AntRunner;
18
import org.eclipse.core.runtime.CoreException;
19
import org.eclipse.core.runtime.URIUtil;
20
import org.eclipse.equinox.internal.p2.metadata.InstallableUnit;
21
import org.eclipse.equinox.internal.p2.persistence.XMLWriter;
22
import org.eclipse.equinox.internal.provisional.p2.artifact.repository.IArtifactRepository;
23
import org.eclipse.equinox.internal.provisional.p2.core.ProvisionException;
24
import org.eclipse.equinox.internal.provisional.p2.metadata.IInstallableUnit;
25
import org.eclipse.equinox.internal.provisional.p2.metadata.query.InstallableUnitQuery;
26
import org.eclipse.equinox.internal.provisional.p2.query.Collector;
27
import org.eclipse.equinox.internal.provisional.p2.query.IQueryable;
28
29
public class AbstractAntProvisioningTest extends AbstractProvisioningTest {
30
	protected static final String TYPE_ARTIFACT = "A";
31
	protected static final String TYPE_METADATA = "M";
32
	protected static final String TYPE_BOTH = null;
33
34
	private static final String TARGET = "target";
35
	private static final String ROOT = "project";
36
	private static final String NAME = "name";
37
	private static final String DEFAULT_TARGET = "default";
38
	private static final String DEFAULT_NAME = "default";
39
40
	AntTaskElement root, target;
41
	File buildScript, logLocation;
42
43
	public void setUp() throws Exception {
44
		super.setUp();
45
		buildScript = new File(getTempFolder(), getUniqueString());
46
		logLocation = new File(getTempFolder(), getUniqueString());
47
		createBuildScript();
48
	}
49
50
	public void tearDown() throws Exception {
51
		// Delete the build script
52
		delete(buildScript.getParentFile());
53
		delete(logLocation.getParentFile());
54
		super.tearDown();
55
	}
56
57
	/*
58
	 * Run the specified buildscript
59
	 */
60
	protected void runAntTask(File buildFile) {
61
		try {
62
			runAntTaskWithExceptions(buildFile);
63
		} catch (CoreException e) {
64
			fail(rootCause(e));
65
		}
66
	}
67
68
	private void runAntTaskWithExceptions(File buildFile) throws CoreException {
69
		AntRunner ant = new AntRunner();
70
		ant.setArguments("-logfile \"" + logLocation + "\"");
71
		ant.setBuildFileLocation(buildFile.getAbsolutePath());
72
		ant.addBuildLogger("org.apache.tools.ant.XmlLogger");
73
		ant.run();
74
	}
75
76
	/*
77
	 * Run the build script described programmatically
78
	 */
79
	protected void runAntTask() {
80
		try {
81
			runAntTaskWithExceptions();
82
		} catch (CoreException e) {
83
			fail(rootCause(e));
84
		}
85
	}
86
87
	protected void runAntTaskWithExceptions() throws CoreException {
88
		try {
89
			writeBuildScript();
90
		} catch (Exception e) {
91
			fail("Error writing build script", e);
92
		}
93
		runAntTaskWithExceptions(buildScript);
94
	}
95
96
	/*
97
	 * Adds an Ant Task to the build script
98
	 */
99
	protected void addTask(AntTaskElement task) {
100
		target.addElement(task);
101
	}
102
103
	/*
104
	 * Create and return an repository element for this address and type
105
	 */
106
	protected AntTaskElement getRepositoryElement(URI address, String kind) {
107
		return getRepositoryElement(address, kind, null, null, null, null);
108
	}
109
110
	protected AntTaskElement getRepositoryElement(URI address, String kind, String name, String format, Boolean compressed, Boolean append) {
111
		AntTaskElement repo = new AntTaskElement("repository");
112
		repo.addAttributes(new String[] {"location", URIUtil.toUnencodedString(address)});
113
		if (kind != null)
114
			repo.addAttributes(new String[] {"kind", kind});
115
		if (name != null)
116
			repo.addAttributes(new String[] {"name", name});
117
		if (format != null)
118
			repo.addAttributes(new String[] {"format", format});
119
		if (compressed != null)
120
			repo.addAttributes(new String[] {"compressed", compressed.toString()});
121
		if (append != null)
122
			repo.addAttributes(new String[] {"append", append.toString()});
123
		return repo;
124
	}
125
126
	/*
127
	 * Create the base elements of the build script
128
	 */
129
	private void createBuildScript() {
130
		root = new AntTaskElement(ROOT);
131
		root.addAttributes(new String[] {NAME, ROOT, DEFAULT_TARGET, DEFAULT_NAME});
132
		target = new AntTaskElement(TARGET);
133
		target.addAttributes(new String[] {NAME, DEFAULT_NAME});
134
		root.addElement(target);
135
	}
136
137
	/*
138
	 * Write the build script to disk
139
	 */
140
	private void writeBuildScript() throws Exception {
141
		FileOutputStream outputStream = null;
142
		try {
143
			outputStream = new FileOutputStream(buildScript);
144
			XMLWriter writer = new XMLWriter(outputStream, null);
145
			writeElement(writer, root);
146
			writer.flush();
147
		} finally {
148
			if (outputStream != null)
149
				outputStream.close();
150
		}
151
	}
152
153
	/*
154
	 * Write an element to the buildscript
155
	 */
156
	private void writeElement(XMLWriter writer, AntTaskElement task) {
157
		// Properties ought to occur in key-value pairs 
158
		assertTrue("Task " + task + " should have an even number of properties", (task.attributes.size() % 2) == 0);
159
160
		// Start tag
161
		writer.start(task.name);
162
163
		// write properties
164
		for (Iterator iter = task.attributes.iterator(); iter.hasNext();)
165
			writer.attribute((String) iter.next(), (String) iter.next());
166
167
		// write sub elements if applicable
168
		for (Iterator iter = task.elements.iterator(); iter.hasNext();)
169
			writeElement(writer, (AntTaskElement) iter.next());
170
171
		// close tag
172
		writer.end();
173
	}
174
175
	// Class which can be used to represent elements in a task
176
	protected class AntTaskElement {
177
		public String name;
178
		public List attributes = new ArrayList();
179
		public List elements = new ArrayList();
180
181
		public AntTaskElement(String name) {
182
			this.name = name;
183
		}
184
185
		public void addAttribute(String attribute, String value) {
186
			attributes.add(attribute);
187
			attributes.add(value);
188
		}
189
190
		public void addAttributes(String[] propertyArray) {
191
			attributes.addAll(Arrays.asList(propertyArray));
192
		}
193
194
		public void addElement(AntTaskElement element) {
195
			elements.add(element);
196
		}
197
198
		public String toString() {
199
			return name;
200
		}
201
	}
202
203
	protected static Throwable rootCause(Throwable e) {
204
		if (e.getCause() != null)
205
			return rootCause(e.getCause());
206
		return e;
207
	}
208
209
	protected static void fail(Throwable e) {
210
		fail("An exception occurred while running the task", e);
211
	}
212
213
	protected void assertLogContains(String content) {
214
		try {
215
			assertLogContainsLine(logLocation, content);
216
		} catch (Exception e) {
217
			fail("Error asserting log contents.", e);
218
		}
219
	}
220
221
	protected static void assertIUContentEquals(String message, IQueryable source, IQueryable destination) {
222
		assertContains(message, source, destination);
223
		assertContains(message, destination, source);
224
	}
225
226
	protected static void assertArtifactKeyContentEquals(String message, Collector ius, URI artifactRepositoryLocation) {
227
		try {
228
			IArtifactRepository repo = getArtifactRepositoryManager().loadRepository(artifactRepositoryLocation, null);
229
			List fromIUs = getArtifactKeys(ius);
230
			List fromRepo = Arrays.asList(repo.getArtifactKeys());
231
			assertContains(message, fromIUs, fromRepo);
232
			assertContains(message, fromRepo, fromIUs);
233
		} catch (ProvisionException e) {
234
			fail("Failed to load repository", e);
235
		}
236
237
	}
238
239
	protected static void assertContains(String message, IQueryable source, IQueryable destination) {
240
		Collector sourceCollector = source.query(InstallableUnitQuery.ANY, new Collector(), null);
241
		Iterator it = sourceCollector.iterator();
242
243
		while (it.hasNext()) {
244
			IInstallableUnit sourceIU = (IInstallableUnit) it.next();
245
			Collector destinationCollector = destination.query(new InstallableUnitQuery(sourceIU.getId(), sourceIU.getVersion()), new Collector(), null);
246
			assertEquals(message, 1, destinationCollector.size());
247
			assertTrue(message, sourceIU.equals(destinationCollector.iterator().next()));
248
		}
249
	}
250
251
	protected static void assertContains(String message, List fromIUs, List fromRepo) {
252
		for (Iterator iter = fromIUs.iterator(); iter.hasNext();)
253
			assertTrue(message, fromRepo.contains(iter.next()));
254
	}
255
256
	protected static List getArtifactKeys(Collector ius) {
257
		List keys = new ArrayList(ius.size());
258
259
		for (Iterator iter = ius.iterator(); iter.hasNext();)
260
			keys.addAll(Arrays.asList(((InstallableUnit) iter.next()).getArtifacts()));
261
		return keys;
262
	}
263
}

Return to bug 265550