Added
Link Here
|
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2008 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 |
* Code 9 - ongoing development |
11 |
*******************************************************************************/ |
12 |
package org.eclipse.equinox.p2.tests.mirror; |
13 |
|
14 |
import java.io.File; |
15 |
import java.net.MalformedURLException; |
16 |
import java.util.HashMap; |
17 |
import java.util.Map; |
18 |
import org.eclipse.equinox.app.IApplicationContext; |
19 |
import org.eclipse.equinox.internal.p2.core.helpers.ServiceHelper; |
20 |
import org.eclipse.equinox.internal.p2.metadata.mirror.MirrorApplication; |
21 |
import org.eclipse.equinox.internal.provisional.p2.metadata.IInstallableUnit; |
22 |
import org.eclipse.equinox.internal.provisional.p2.metadata.query.InstallableUnitQuery; |
23 |
import org.eclipse.equinox.internal.provisional.p2.metadata.repository.IMetadataRepository; |
24 |
import org.eclipse.equinox.internal.provisional.p2.metadata.repository.IMetadataRepositoryManager; |
25 |
import org.eclipse.equinox.internal.provisional.p2.query.Collector; |
26 |
import org.eclipse.equinox.p2.tests.AbstractProvisioningTest; |
27 |
import org.eclipse.equinox.p2.tests.TestActivator; |
28 |
import org.osgi.framework.Bundle; |
29 |
|
30 |
/** |
31 |
* Test API of the basic mirror application functionality's implementation. |
32 |
*/ |
33 |
public class MetadataMirrorApplicationTest extends AbstractProvisioningTest { |
34 |
protected File destRepoLocation; |
35 |
protected File sourceRepoLocation; //helloworldfeature |
36 |
protected File sourceRepo2Location; //anotherfeature |
37 |
protected File sourceRepo3Location; //helloworldfeature + yetanotherfeature |
38 |
protected IMetadataRepository destRepo; |
39 |
protected IMetadataRepository srcRepo; |
40 |
protected IMetadataRepository srcRepo2; |
41 |
protected IMetadataRepository srcRepo3; |
42 |
|
43 |
private IMetadataRepositoryManager getMetadataRepositoryManager() { |
44 |
return (IMetadataRepositoryManager) ServiceHelper.getService(TestActivator.getContext(), IMetadataRepositoryManager.class.getName()); |
45 |
} |
46 |
|
47 |
protected void setUp() throws Exception { |
48 |
super.setUp(); |
49 |
String tempDir = System.getProperty("java.io.tmpdir"); |
50 |
sourceRepoLocation = getTestData("1.0", "/testData/mirror/mirrorSourceRepo1"); |
51 |
sourceRepo2Location = getTestData("2.0", "/testData/mirror/mirrorSourceRepo2"); |
52 |
sourceRepo3Location = getTestData("3.0", "/testData/mirror/mirrorSourceRepo3"); |
53 |
destRepoLocation = new File(tempDir, "BasicMirrorApplicationTest"); |
54 |
AbstractProvisioningTest.delete(destRepoLocation); |
55 |
} |
56 |
|
57 |
protected void tearDown() throws Exception { |
58 |
removeRepositories(); |
59 |
delete(destRepoLocation); |
60 |
super.tearDown(); |
61 |
} |
62 |
|
63 |
/** |
64 |
* runs default mirror. source is the source repo, destination is the destination repo |
65 |
*/ |
66 |
private void runMirrorApplication(final File source, final File destination, final boolean append) throws Exception { |
67 |
MirrorApplication application = new MirrorApplication(); |
68 |
application.start(new IApplicationContext() { |
69 |
|
70 |
public void applicationRunning() { |
71 |
} |
72 |
|
73 |
public Map getArguments() { |
74 |
Map arguments = new HashMap(); |
75 |
|
76 |
try { |
77 |
arguments.put("application.args", new String[] {"-source", source.toURL().toExternalForm(), "-destination", destination.toURL().toExternalForm(), append ? "-append" : ""}); |
78 |
|
79 |
} catch (MalformedURLException e) { |
80 |
fail("invalid URL for source or target repo"); |
81 |
} |
82 |
|
83 |
return arguments; |
84 |
} |
85 |
|
86 |
public String getBrandingApplication() { |
87 |
return null; |
88 |
} |
89 |
|
90 |
public Bundle getBrandingBundle() { |
91 |
return null; |
92 |
} |
93 |
|
94 |
public String getBrandingDescription() { |
95 |
return null; |
96 |
} |
97 |
|
98 |
public String getBrandingId() { |
99 |
return null; |
100 |
} |
101 |
|
102 |
public String getBrandingName() { |
103 |
return null; |
104 |
} |
105 |
|
106 |
public String getBrandingProperty(String key) { |
107 |
return null; |
108 |
} |
109 |
}); |
110 |
} |
111 |
|
112 |
/** |
113 |
* Removes all repositories in one go |
114 |
*/ |
115 |
private void removeRepositories() throws Exception { |
116 |
getMetadataRepositoryManager().removeRepository(destRepoLocation.toURL()); |
117 |
getMetadataRepositoryManager().removeRepository(sourceRepoLocation.toURL()); |
118 |
getMetadataRepositoryManager().removeRepository(sourceRepo2Location.toURL()); |
119 |
getMetadataRepositoryManager().removeRepository(sourceRepo3Location.toURL()); |
120 |
} |
121 |
|
122 |
/** |
123 |
* Loads all repositories in one go |
124 |
* @throws Exception |
125 |
*/ |
126 |
private void loadRepositories() throws Exception { |
127 |
destRepo = getMetadataRepositoryManager().loadRepository(destRepoLocation.toURL(), null); |
128 |
srcRepo = getMetadataRepositoryManager().loadRepository(sourceRepoLocation.toURL(), null); |
129 |
srcRepo2 = getMetadataRepositoryManager().loadRepository(sourceRepo2Location.toURL(), null); |
130 |
srcRepo3 = getMetadataRepositoryManager().loadRepository(sourceRepo3Location.toURL(), null); |
131 |
} |
132 |
|
133 |
private int getNumUnique(Collector c1, Collector c2) { |
134 |
Object[] repo1 = c1.toCollection().toArray(); |
135 |
Object[] repo2 = c2.toCollection().toArray(); |
136 |
|
137 |
int numKeys = repo1.length + repo2.length; |
138 |
|
139 |
for (int i = 0; i < repo1.length; i++) { |
140 |
for (int j = 0; j < repo2.length; j++) { |
141 |
if (isEqual((IInstallableUnit) repo1[i], (IInstallableUnit) repo2[j])) |
142 |
numKeys--; |
143 |
} |
144 |
} |
145 |
|
146 |
return numKeys; |
147 |
} |
148 |
|
149 |
private void assertNumberOfKeys(int expected, IMetadataRepository repo) { |
150 |
assertEquals(expected, repo.query(InstallableUnitQuery.ANY, new Collector(), null).size()); |
151 |
} |
152 |
|
153 |
/** |
154 |
* Tests mirroring all metadata in a repository to an empty repository |
155 |
* @throws Exception |
156 |
* Source contains A, B |
157 |
* Target contains |
158 |
*/ |
159 |
private void metadataMirrorToEmpty(boolean append) throws Exception { |
160 |
runMirrorApplication(sourceRepoLocation, destRepoLocation, append); //do not append |
161 |
|
162 |
//TODO replace with test to ensure repositories are closed once bug 247664 is fixed |
163 |
removeRepositories(); |
164 |
|
165 |
loadRepositories(); |
166 |
} |
167 |
|
168 |
/** |
169 |
* Tests mirroring all metadata in a repository to a repository populated with non-duplicate entries |
170 |
* @throws Exception |
171 |
* Source contains A, B |
172 |
* Target contains C, D |
173 |
*/ |
174 |
private void metadataMirrorToPopulated(boolean append) throws Exception { |
175 |
//Setup: populate destination with non-duplicate metadata |
176 |
runMirrorApplication(sourceRepo2Location, destRepoLocation, false); //value of append does not matter |
177 |
|
178 |
//TODO replace with test to ensure repositories are closed once bug 247664 is fixed |
179 |
removeRepositories(); |
180 |
|
181 |
loadRepositories(); |
182 |
|
183 |
//Setup ensure setup completes successfully |
184 |
assertContentEquals("", srcRepo2, destRepo); |
185 |
|
186 |
//mirror test data |
187 |
runMirrorApplication(sourceRepoLocation, destRepoLocation, append); |
188 |
|
189 |
//TODO replace with test to ensure repositories are closed once bug 247664 is fixed |
190 |
removeRepositories(); |
191 |
|
192 |
loadRepositories(); |
193 |
} |
194 |
|
195 |
/** |
196 |
* Tests mirroring all metadata in a repository to a repository populated with exact duplicate data |
197 |
* @throws Exception |
198 |
* Source contains A, B |
199 |
* Target contains A, B |
200 |
*/ |
201 |
private void metadataMirrorToFullDuplicate(boolean append) throws Exception { |
202 |
//Setup: populate destination with duplicate metadata |
203 |
runMirrorApplication(sourceRepoLocation, destRepoLocation, false); //value of append does not matter |
204 |
|
205 |
//TODO replace with test to ensure repositories are closed once bug 247664 is fixed |
206 |
removeRepositories(); |
207 |
|
208 |
loadRepositories(); |
209 |
|
210 |
//Setup: verify contents |
211 |
assertContentEquals("", srcRepo, destRepo); |
212 |
|
213 |
//mirror test data |
214 |
runMirrorApplication(sourceRepoLocation, destRepoLocation, append); |
215 |
|
216 |
//TODO replace with test to ensure repositories are closed once bug 247664 is fixed |
217 |
removeRepositories(); |
218 |
|
219 |
loadRepositories(); |
220 |
} |
221 |
|
222 |
/** |
223 |
* Tests mirroring all metadata in a repository to a repository populated with partially duplicate data |
224 |
* @throws Exception |
225 |
* Source contains A, B, C, D |
226 |
* Target contains A, B |
227 |
*/ |
228 |
private void metadataMirrorToPartialDuplicate(boolean append) throws Exception { |
229 |
//Setup: populate destination with duplicate metadata |
230 |
runMirrorApplication(sourceRepoLocation, destRepoLocation, false); //value of append does not matter |
231 |
|
232 |
//TODO replace with test to ensure repositories are closed once bug 247664 is fixed |
233 |
removeRepositories(); |
234 |
|
235 |
loadRepositories(); |
236 |
|
237 |
//Setup: verify contents |
238 |
assertContentEquals("", srcRepo, destRepo); |
239 |
|
240 |
//mirror test data |
241 |
runMirrorApplication(sourceRepo3Location, destRepoLocation, append); |
242 |
|
243 |
//TODO replace with test to ensure repositories are closed once bug 247664 is fixed |
244 |
removeRepositories(); |
245 |
|
246 |
loadRepositories(); |
247 |
} |
248 |
|
249 |
/** |
250 |
* Tests mirroring all metadata in a repository to a repository populated with both full duplicate and non-duplicate data |
251 |
* @throws Exception |
252 |
* Source contains A, B |
253 |
* Target contains A, B, C, D |
254 |
*/ |
255 |
private void metadataMirrorToPopulatedWithFullDuplicate(boolean append) throws Exception { |
256 |
//Setup: populate destination with non-duplicate metadata |
257 |
runMirrorApplication(sourceRepo3Location, destRepoLocation, false); //value of append does not matter |
258 |
|
259 |
//TODO replace with test to ensure repositories are closed once bug 247664 is fixed |
260 |
removeRepositories(); |
261 |
|
262 |
loadRepositories(); |
263 |
|
264 |
//Setup: verify |
265 |
assertContentEquals("", srcRepo3, destRepo); |
266 |
|
267 |
//mirror duplicate data |
268 |
runMirrorApplication(sourceRepoLocation, destRepoLocation, append); |
269 |
|
270 |
//TODO replace with test to ensure repositories are closed once bug 247664 is fixed |
271 |
removeRepositories(); |
272 |
|
273 |
loadRepositories(); |
274 |
} |
275 |
|
276 |
/** |
277 |
* Tests mirroring all metadata in a repository to a repository populated with both partial duplicate and non-duplicate data |
278 |
* @throws Exception |
279 |
* Source contains A, B, C, D |
280 |
* Target contains A, B, E, F |
281 |
*/ |
282 |
private void metadataMirrorToPopulatedWithPartialDuplicate(boolean append) throws Exception { |
283 |
//Setup: populate destination with non-duplicate metadata |
284 |
runMirrorApplication(sourceRepo2Location, destRepoLocation, false); //value of append does not matter |
285 |
|
286 |
//TODO replace with test to ensure repositories are closed once bug 247664 is fixed |
287 |
removeRepositories(); |
288 |
|
289 |
loadRepositories(); |
290 |
|
291 |
//Setup: verify |
292 |
assertContentEquals("", srcRepo2, destRepo); |
293 |
|
294 |
//Setup: populate destination with duplicate metadata |
295 |
runMirrorApplication(sourceRepoLocation, destRepoLocation, true); |
296 |
|
297 |
//TODO replace with test to ensure repositories are closed once bug 247664 is fixed |
298 |
removeRepositories(); |
299 |
|
300 |
loadRepositories(); |
301 |
|
302 |
//Setup: verify |
303 |
assertContains("", srcRepo, destRepo); |
304 |
assertContains("", srcRepo2, destRepo); |
305 |
|
306 |
//mirror duplicate data |
307 |
runMirrorApplication(sourceRepo3Location, destRepoLocation, append); |
308 |
|
309 |
//TODO replace with test to ensure repositories are closed once bug 247664 is fixed |
310 |
removeRepositories(); |
311 |
|
312 |
loadRepositories(); |
313 |
} |
314 |
|
315 |
/** |
316 |
* Tests mirroring all metadata in a repository to an empty repository with "-append" |
317 |
* @throws Exception |
318 |
* Source contains A, B |
319 |
* Target contains |
320 |
* Expected is A, B |
321 |
*/ |
322 |
public void testMetadataMirrorToEmptyWithAppend() throws Exception { |
323 |
metadataMirrorToEmpty(true); |
324 |
|
325 |
assertContentEquals("", srcRepo, destRepo); |
326 |
} |
327 |
|
328 |
/** |
329 |
* Tests mirroring all metadata in a repository to an empty repository without "-append" |
330 |
* @throws Exception |
331 |
* Source contains A, B |
332 |
* Target contains |
333 |
* Expected is A, B |
334 |
*/ |
335 |
public void testMetadataMirrorToEmptyWithoutAppend() throws Exception { |
336 |
metadataMirrorToEmpty(false); |
337 |
|
338 |
assertContentEquals("", srcRepo, destRepo); |
339 |
} |
340 |
|
341 |
/** |
342 |
* Tests mirroring all metadata in a repository to a repository populated with exact duplicate data with "-append" |
343 |
* @throws Exception |
344 |
* Source contains A, B |
345 |
* Target contains A, B |
346 |
* Expected is A, B |
347 |
*/ |
348 |
public void testMetadataMirrorToFullDuplicateWithAppend() throws Exception { |
349 |
metadataMirrorToFullDuplicate(true); |
350 |
|
351 |
assertContentEquals("", srcRepo, destRepo); |
352 |
} |
353 |
|
354 |
/** |
355 |
* Tests mirroring all metadata in a repository to a repository populated with exact duplicate data wihtout "-append" |
356 |
* @throws Exception |
357 |
* Source contains A, B |
358 |
* Target contains A, B |
359 |
* Expected is A, B |
360 |
*/ |
361 |
public void testMetadataMirrorToFullDuplicateWithoutAppend() throws Exception { |
362 |
metadataMirrorToFullDuplicate(false); |
363 |
|
364 |
assertContentEquals("", srcRepo, destRepo); |
365 |
} |
366 |
|
367 |
/** |
368 |
* Tests mirroring all metadata in a repository to a repository populated with non-duplicate entries with "-append" |
369 |
* @throws Exception |
370 |
* Source contains A, B |
371 |
* Target contains C, D |
372 |
* Expected is A, B, C, D |
373 |
*/ |
374 |
public void testMetadataMirrorToPopulatedWithAppend() throws Exception { |
375 |
metadataMirrorToPopulated(true); |
376 |
|
377 |
assertContains("", srcRepo, destRepo); |
378 |
assertContains("", srcRepo2, destRepo); |
379 |
assertNumberOfKeys(getNumUnique(srcRepo.query(InstallableUnitQuery.ANY, new Collector(), null), srcRepo2.query(InstallableUnitQuery.ANY, new Collector(), null)), destRepo); |
380 |
} |
381 |
|
382 |
/** |
383 |
* Tests mirroring all metadata in a repository to a repository populated with non-duplicate entries without "-append" |
384 |
* @throws Exception |
385 |
* Source contains A, B |
386 |
* Target contains C, D |
387 |
* Expected is A, B |
388 |
*/ |
389 |
public void testMetadataMirrorToPopulatedWithoutAppend() throws Exception { |
390 |
metadataMirrorToPopulated(false); |
391 |
|
392 |
assertContentEquals("", srcRepo, destRepo); |
393 |
} |
394 |
|
395 |
/** |
396 |
* Tests mirroring all metadata in a repository to a repository populated with partially duplicate data |
397 |
* @throws Exception |
398 |
* Source contains A, B, C, D |
399 |
* Target contains A, B |
400 |
* Expected is A, B, C, D |
401 |
*/ |
402 |
public void testMetadataMirrorToPartialDuplicateWithAppend() throws Exception { |
403 |
metadataMirrorToPartialDuplicate(true); |
404 |
|
405 |
assertContentEquals("", srcRepo3, destRepo); |
406 |
} |
407 |
|
408 |
/** |
409 |
* Tests mirroring all metadata in a repository to a repository populated with partially duplicate data |
410 |
* @throws Exception |
411 |
* Source contains A, B, C, D |
412 |
* Target contains A, B |
413 |
* Expected is A, B, C, D |
414 |
*/ |
415 |
public void testMetadataMirrorToPartialDuplicateWithoutAppend() throws Exception { |
416 |
metadataMirrorToPartialDuplicate(false); |
417 |
|
418 |
assertContentEquals("", srcRepo3, destRepo); |
419 |
} |
420 |
|
421 |
/** |
422 |
* Tests mirroring all metadata in a repository to a repository populated with both full duplicate and non-duplicate data with "-append" |
423 |
* @throws Exception |
424 |
* Source contains A, B |
425 |
* Target contains A, B, C, D |
426 |
* Expected is A, B, C, D |
427 |
*/ |
428 |
public void testMetadataMirrorToPopulatedWithFullDuplicateWithAppend() throws Exception { |
429 |
metadataMirrorToPopulatedWithFullDuplicate(true); |
430 |
|
431 |
assertContentEquals("", srcRepo3, destRepo); |
432 |
} |
433 |
|
434 |
/** |
435 |
* Tests mirroring all metadata in a repository to a repository populated with both full duplicate and non-duplicate data without "-append" |
436 |
* @throws Exception |
437 |
* Source contains A, B |
438 |
* Target contains A, B, C, D |
439 |
* Expected is A, B |
440 |
*/ |
441 |
public void testMetadataMirrorToPopulatedWithFullDuplicateWithoutAppend() throws Exception { |
442 |
metadataMirrorToPopulatedWithFullDuplicate(false); |
443 |
|
444 |
assertContentEquals("", srcRepo, destRepo); |
445 |
} |
446 |
|
447 |
/** |
448 |
* Tests mirroring all metadata in a repository to a repository populated with both partial duplicate and non-duplicate data with "-append" |
449 |
* @throws Exception |
450 |
* Source contains A, B, C, D |
451 |
* Target contains A, B, E, F |
452 |
* Expected is A, B, C, D, E, F |
453 |
*/ |
454 |
public void testMetadataMirrorToPopulatedWithPartialDuplicateWithAppend() throws Exception { |
455 |
metadataMirrorToPopulatedWithPartialDuplicate(true); |
456 |
|
457 |
assertContains("", srcRepo3, destRepo); |
458 |
assertContains("", srcRepo2, destRepo); |
459 |
assertNumberOfKeys(getNumUnique(srcRepo2.query(InstallableUnitQuery.ANY, new Collector(), null), srcRepo3.query(InstallableUnitQuery.ANY, new Collector(), null)), destRepo); |
460 |
} |
461 |
|
462 |
/** |
463 |
* Tests mirroring all metadata in a repository to a repository populated with both partial duplicate and non-duplicate data without "-append" |
464 |
* @throws Exception |
465 |
* Source contains A, B, C, D |
466 |
* Target contains A, B, E, F |
467 |
* Expected is A, B, C, D |
468 |
*/ |
469 |
public void testMetadataMirrorToPopulatedWithPartialDuplicateWithoutAppend() throws Exception { |
470 |
metadataMirrorToPopulatedWithPartialDuplicate(false); |
471 |
|
472 |
assertContentEquals("", srcRepo3, destRepo); |
473 |
} |
474 |
|
475 |
} |