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

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

Return to bug 265550