### Eclipse Workspace Patch 1.0 #P org.eclipse.equinox.p2.tests Index: src/org/eclipse/equinox/p2/tests/AutomatedTests.java =================================================================== RCS file: /cvsroot/rt/org.eclipse.equinox/p2/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/AutomatedTests.java,v retrieving revision 1.20 diff -u -r1.20 AutomatedTests.java --- src/org/eclipse/equinox/p2/tests/AutomatedTests.java 17 Sep 2008 15:49:27 -0000 1.20 +++ src/org/eclipse/equinox/p2/tests/AutomatedTests.java 23 Sep 2008 13:11:48 -0000 @@ -36,6 +36,7 @@ suite.addTest(org.eclipse.equinox.p2.tests.updatesite.AllTests.suite()); suite.addTest(org.eclipse.equinox.p2.tests.extensionlocation.AllTests.suite()); suite.addTest(org.eclipse.equinox.p2.tests.touchpoint.eclipse.AllTests.suite()); + suite.addTest(org.eclipse.equinox.p2.tests.mirror.AllTests.suite()); // disable the reconciler tests until after 3.5 M2 and we get the setup issues sorted out // suite.addTest(org.eclipse.equinox.p2.tests.reconciler.dropins.AllTests.suite()); return suite; Index: src/org/eclipse/equinox/p2/tests/AbstractProvisioningTest.java =================================================================== RCS file: /cvsroot/rt/org.eclipse.equinox/p2/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/AbstractProvisioningTest.java,v retrieving revision 1.58 diff -u -r1.58 AbstractProvisioningTest.java --- src/org/eclipse/equinox/p2/tests/AbstractProvisioningTest.java 18 Sep 2008 15:10:19 -0000 1.58 +++ src/org/eclipse/equinox/p2/tests/AbstractProvisioningTest.java 23 Sep 2008 13:11:48 -0000 @@ -16,6 +16,8 @@ import org.eclipse.core.runtime.*; import org.eclipse.equinox.internal.p2.core.helpers.ServiceHelper; import org.eclipse.equinox.internal.p2.metadata.repository.MetadataRepositoryManager; +import org.eclipse.equinox.internal.provisional.p2.artifact.repository.IArtifactDescriptor; +import org.eclipse.equinox.internal.provisional.p2.artifact.repository.IArtifactRepository; import org.eclipse.equinox.internal.provisional.p2.core.ProvisionException; import org.eclipse.equinox.internal.provisional.p2.director.*; import org.eclipse.equinox.internal.provisional.p2.engine.*; @@ -924,4 +926,81 @@ if (set.size() > 0) fail("Unexpected element '" + set.iterator().next() + "'"); } + + protected static void assertEquals(Copyright cpyrt1, Copyright cpyrt2) { + if (cpyrt1 == cpyrt2) + return; + if (cpyrt1 == null || cpyrt2 == null) { + fail(); + } + + assertEquals(cpyrt1.getBody(), cpyrt2.getBody()); + assertEquals(cpyrt1.getURL().toExternalForm(), cpyrt2.getURL().toExternalForm()); + } + + /** + * matches all descriptors from source in the destination + * Note: NOT BICONDITIONAL! assertContains(A, B) is NOT the same as assertContains(B, A) + */ + protected void assertContains(String message, IArtifactRepository sourceRepo, IArtifactRepository destinationRepo) { + IArtifactKey[] sourceKeys = sourceRepo.getArtifactKeys(); + + for (int i = 0; i < sourceKeys.length; i++) { + IArtifactDescriptor[] destinationDescriptors = destinationRepo.getArtifactDescriptors(sourceKeys[i]); + if (destinationDescriptors == null || destinationDescriptors.length == 0) + fail(message + ": unmatched key: " + sourceKeys[i].toString()); + //this implicitly verifies the keys are present + + IArtifactDescriptor[] sourceDescriptors = sourceRepo.getArtifactDescriptors(sourceKeys[i]); + assertEquals(message, sourceDescriptors, destinationDescriptors); + } + } + + /** + * Ensures 2 repositories are equal by ensure all items in repo1 are contained + * in repo2 and all items in repo2 are in repo1 + */ + protected void assertContentEquals(String message, IArtifactRepository repo1, IArtifactRepository repo2) { + assertContains(message, repo1, repo2); + assertContains(message, repo2, repo1); + } + + /** + * matches all metadata from source in the destination + * Note: NOT BICONDITIONAL! assertContains(A, B) is NOT the same as assertContains(B, A) + */ + protected void assertContains(String message, IMetadataRepository sourceRepo, IMetadataRepository destinationRepo) { + Collector sourceCollector = sourceRepo.query(InstallableUnitQuery.ANY, new Collector(), null); + Iterator it = sourceCollector.iterator(); + + while (it.hasNext()) { + IInstallableUnit sourceIU = (IInstallableUnit) it.next(); + Collector destinationCollector = destinationRepo.query(new InstallableUnitQuery(sourceIU.getId(), sourceIU.getVersion()), new Collector(), null); + if (destinationCollector.size() <= 0) + fail("IU not found."); + if (destinationCollector.size() > 1) + fail("More than one IU found."); + + assertEquals(sourceIU, (IInstallableUnit) destinationCollector.iterator().next()); + } + } + + /** + * Ensures 2 repositories are equal by ensure all items in repo1 are contained + * in repo2 and all items in repo2 are in repo1 + */ + protected void assertContentEquals(String message, IMetadataRepository repo1, IMetadataRepository repo2) { + assertContains(message, repo1, repo2); + assertContains(message, repo2, repo1); + } + + protected static boolean isEqual(IInstallableUnit iu1, IInstallableUnit iu2) { + try { + assertEquals(iu1, iu2); + } catch (AssertionFailedError e) { + return false; + } + + return true; + } } Index: META-INF/MANIFEST.MF =================================================================== RCS file: /cvsroot/rt/org.eclipse.equinox/p2/bundles/org.eclipse.equinox.p2.tests/META-INF/MANIFEST.MF,v retrieving revision 1.44 diff -u -r1.44 MANIFEST.MF --- META-INF/MANIFEST.MF 18 Sep 2008 15:10:20 -0000 1.44 +++ META-INF/MANIFEST.MF 23 Sep 2008 13:11:48 -0000 @@ -58,7 +58,9 @@ org.eclipse.equinox.frameworkadmin, org.eclipse.equinox.common, org.junit, - org.eclipse.core.runtime;bundle-version="3.4.100" + org.eclipse.core.runtime;bundle-version="3.4", + org.eclipse.equinox.p2.artifact.repository;bundle-version="1.0.100", + org.eclipse.equinox.p2.metadata.repository;bundle-version="1.0.100" Bundle-ActivationPolicy: lazy Eclipse-RegisterBuddy: org.eclipse.equinox.p2.artifact.repository Bundle-RequiredExecutionEnvironment: J2SE-1.4 Index: src/org/eclipse/equinox/p2/tests/mirror/ArtifactMirrorApplicationTest.java =================================================================== RCS file: src/org/eclipse/equinox/p2/tests/mirror/ArtifactMirrorApplicationTest.java diff -N src/org/eclipse/equinox/p2/tests/mirror/ArtifactMirrorApplicationTest.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/org/eclipse/equinox/p2/tests/mirror/ArtifactMirrorApplicationTest.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,455 @@ +/******************************************************************************* + * Copyright (c) 2008 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + * Code 9 - ongoing development + *******************************************************************************/ +package org.eclipse.equinox.p2.tests.mirror; + +import java.io.File; +import java.net.MalformedURLException; +import java.util.HashMap; +import java.util.Map; +import org.eclipse.equinox.app.IApplicationContext; +import org.eclipse.equinox.internal.p2.artifact.mirror.MirrorApplication; +import org.eclipse.equinox.internal.p2.core.helpers.ServiceHelper; +import org.eclipse.equinox.internal.provisional.p2.artifact.repository.IArtifactRepository; +import org.eclipse.equinox.internal.provisional.p2.artifact.repository.IArtifactRepositoryManager; +import org.eclipse.equinox.p2.tests.AbstractProvisioningTest; +import org.eclipse.equinox.p2.tests.TestActivator; +import org.osgi.framework.Bundle; + +/** + * Test API of the basic mirror application functionality's implementation. + */ +public class ArtifactMirrorApplicationTest extends AbstractProvisioningTest { + protected File destRepoLocation; + protected File sourceRepoLocation; //helloworldfeature + protected File sourceRepo2Location; //anotherfeature + protected File sourceRepo3Location; //helloworldfeature + yetanotherfeature + protected IArtifactRepository destRepo; + protected IArtifactRepository srcRepo; + protected IArtifactRepository srcRepo2; + protected IArtifactRepository srcRepo3; + + private IArtifactRepositoryManager getArtifactRepositoryManager() { + return (IArtifactRepositoryManager) ServiceHelper.getService(TestActivator.getContext(), IArtifactRepositoryManager.class.getName()); + } + + protected void setUp() throws Exception { + super.setUp(); + String tempDir = System.getProperty("java.io.tmpdir"); + sourceRepoLocation = getTestData("1.0", "/testData/mirror/mirrorSourceRepo1"); + sourceRepo2Location = getTestData("2.0", "/testData/mirror/mirrorSourceRepo2"); + sourceRepo3Location = getTestData("3.0", "/testData/mirror/mirrorSourceRepo3"); + destRepoLocation = new File(tempDir, "BasicMirrorApplicationTest"); + AbstractProvisioningTest.delete(destRepoLocation); + } + + protected void tearDown() throws Exception { + removeRepositories(); + delete(destRepoLocation); + super.tearDown(); + } + + /** + * runs default mirror. source is the source repo, destination is the destination repo + */ + private void runMirrorApplication(final File source, final File destination, final boolean append) throws Exception { + MirrorApplication application = new MirrorApplication(); + application.start(new IApplicationContext() { + + public void applicationRunning() { + } + + public Map getArguments() { + Map arguments = new HashMap(); + + try { + arguments.put("application.args", new String[] {"-source", source.toURL().toExternalForm(), "-destination", destination.toURL().toExternalForm(), append ? "-append" : ""}); + + } catch (MalformedURLException e) { + fail("invalid URL for source or target repo"); + } + + return arguments; + } + + public String getBrandingApplication() { + return null; + } + + public Bundle getBrandingBundle() { + return null; + } + + public String getBrandingDescription() { + return null; + } + + public String getBrandingId() { + return null; + } + + public String getBrandingName() { + return null; + } + + public String getBrandingProperty(String key) { + return null; + } + }); + } + + /** + * Removes all repositories in one go + */ + private void removeRepositories() throws Exception { + getArtifactRepositoryManager().removeRepository(destRepoLocation.toURL()); + getArtifactRepositoryManager().removeRepository(sourceRepoLocation.toURL()); + getArtifactRepositoryManager().removeRepository(sourceRepo2Location.toURL()); + getArtifactRepositoryManager().removeRepository(sourceRepo3Location.toURL()); + } + + /** + * Loads all repositories in one go + * @throws Exception + */ + private void loadRepositories() throws Exception { + destRepo = getArtifactRepositoryManager().loadRepository(destRepoLocation.toURL(), null); + srcRepo = getArtifactRepositoryManager().loadRepository(sourceRepoLocation.toURL(), null); + srcRepo2 = getArtifactRepositoryManager().loadRepository(sourceRepo2Location.toURL(), null); + srcRepo3 = getArtifactRepositoryManager().loadRepository(sourceRepo3Location.toURL(), null); + } + + private void assertNumberOfKeys(int expected, IArtifactRepository repo) { + assertEquals(expected, repo.getArtifactKeys().length); + } + + /** + * Tests mirroring all artifacts in a repository to an empty repository + * @throws Exception + * Source contains A, B + * Target contains + */ + private void artifactMirrorToEmpty(boolean append) throws Exception { + runMirrorApplication(sourceRepoLocation, destRepoLocation, append); //do not append + + //TODO replace with test to ensure repositories are closed once bug 247664 is fixed + removeRepositories(); + + loadRepositories(); + } + + /** + * Tests mirroring all artifacts in a repository to a repository populated with non-duplicate entries + * @throws Exception + * Source contains A, B + * Target contains C, D + */ + private void artifactMirrorToPopulated(boolean append) throws Exception { + //Setup: populate destination with non-duplicate artifacts + runMirrorApplication(sourceRepo2Location, destRepoLocation, false); //value of append does not matter + + //TODO replace with test to ensure repositories are closed once bug 247664 is fixed + removeRepositories(); + + loadRepositories(); + + //Setup ensure setup completes successfully + assertContentEquals("", srcRepo2, destRepo); + + //mirror test data + runMirrorApplication(sourceRepoLocation, destRepoLocation, append); + + //TODO replace with test to ensure repositories are closed once bug 247664 is fixed + removeRepositories(); + + loadRepositories(); + } + + /** + * Tests mirroring all artifacts in a repository to a repository populated with exact duplicate data + * @throws Exception + * Source contains A, B + * Target contains A, B + */ + private void artifactMirrorToFullDuplicate(boolean append) throws Exception { + //Setup: populate destination with duplicate artifacts + runMirrorApplication(sourceRepoLocation, destRepoLocation, false); //value of append does not matter + + //TODO replace with test to ensure repositories are closed once bug 247664 is fixed + removeRepositories(); + + loadRepositories(); + + //Setup: verify contents + assertContentEquals("", srcRepo, destRepo); + + //mirror test data + runMirrorApplication(sourceRepoLocation, destRepoLocation, append); + + //TODO replace with test to ensure repositories are closed once bug 247664 is fixed + removeRepositories(); + + loadRepositories(); + } + + /** + * Tests mirroring all artifacts in a repository to a repository populated with partially duplicate data + * @throws Exception + * Source contains A, B, C, D + * Target contains A, B + */ + private void artifactMirrorToPartialDuplicate(boolean append) throws Exception { + //Setup: populate destination with duplicate artifacts + runMirrorApplication(sourceRepoLocation, destRepoLocation, false); //value of append does not matter + + //TODO replace with test to ensure repositories are closed once bug 247664 is fixed + removeRepositories(); + + loadRepositories(); + + //Setup: verify contents + assertContentEquals("", srcRepo, destRepo); + + //mirror test data + runMirrorApplication(sourceRepo3Location, destRepoLocation, append); + + //TODO replace with test to ensure repositories are closed once bug 247664 is fixed + removeRepositories(); + + loadRepositories(); + } + + /** + * Tests mirroring all artifacts in a repository to a repository populated with both full duplicate and non-duplicate data + * @throws Exception + * Source contains A, B + * Target contains A, B, C, D + */ + private void artifactMirrorToPopulatedWithFullDuplicate(boolean append) throws Exception { + //Setup: populate destination with non-duplicate artifacts + runMirrorApplication(sourceRepo3Location, destRepoLocation, false); //value of append does not matter + + //TODO replace with test to ensure repositories are closed once bug 247664 is fixed + removeRepositories(); + + loadRepositories(); + + //Setup: verify + assertContentEquals("", srcRepo3, destRepo); + + //mirror duplicate data + runMirrorApplication(sourceRepoLocation, destRepoLocation, append); + + //TODO replace with test to ensure repositories are closed once bug 247664 is fixed + removeRepositories(); + + loadRepositories(); + } + + /** + * Tests mirroring all artifacts in a repository to a repository populated with both partial duplicate and non-duplicate data + * @throws Exception + * Source contains A, B, C, D + * Target contains A, B, E, F + */ + private void artifactMirrorToPopulatedWithPartialDuplicate(boolean append) throws Exception { + //Setup: populate destination with non-duplicate artifacts + runMirrorApplication(sourceRepo2Location, destRepoLocation, false); //value of append does not matter + + //TODO replace with test to ensure repositories are closed once bug 247664 is fixed + removeRepositories(); + + loadRepositories(); + + //Setup: verify + assertContentEquals("", srcRepo2, destRepo); + + //Setup: populate destination with duplicate artifacts + runMirrorApplication(sourceRepoLocation, destRepoLocation, true); + + //TODO replace with test to ensure repositories are closed once bug 247664 is fixed + removeRepositories(); + + loadRepositories(); + + //Setup: verify + assertContains("", srcRepo, destRepo); + assertContains("", srcRepo2, destRepo); + + //mirror duplicate data + runMirrorApplication(sourceRepo3Location, destRepoLocation, append); + + //TODO replace with test to ensure repositories are closed once bug 247664 is fixed + removeRepositories(); + + loadRepositories(); + } + + /** + * Tests mirroring all artifacts in a repository to an empty repository with "-append" + * @throws Exception + * Source contains A, B + * Target contains + * Expected is A, B + */ + public void testArtifactMirrorToEmptyWithAppend() throws Exception { + artifactMirrorToEmpty(true); + + assertContentEquals("", srcRepo, destRepo); + } + + /** + * Tests mirroring all artifacts in a repository to an empty repository without "-append" + * @throws Exception + * Source contains A, B + * Target contains + * Expected is A, B + */ + public void testArtifactMirrorToEmptyWithoutAppend() throws Exception { + artifactMirrorToEmpty(false); + + assertContentEquals("", srcRepo, destRepo); + } + + /** + * Tests mirroring all artifacts in a repository to a repository populated with exact duplicate data with "-append" + * @throws Exception + * Source contains A, B + * Target contains A, B + * Expected is A, B + */ + public void testArtifactMirrorToFullDuplicateWithAppend() throws Exception { + artifactMirrorToFullDuplicate(true); + + assertContentEquals("", srcRepo, destRepo); + } + + /** + * Tests mirroring all artifacts in a repository to a repository populated with exact duplicate data wihtout "-append" + * @throws Exception + * Source contains A, B + * Target contains A, B + * Expected is A, B + */ + public void testArtifactMirrorToFullDuplicateWithoutAppend() throws Exception { + artifactMirrorToFullDuplicate(false); + + assertContentEquals("", srcRepo, destRepo); + } + + /** + * Tests mirroring all artifacts in a repository to a repository populated with non-duplicate entries with "-append" + * @throws Exception + * Source contains A, B + * Target contains C, D + * Expected is A, B, C, D + */ + public void testArtifactMirrorToPopulatedWithAppend() throws Exception { + artifactMirrorToPopulated(true); + + assertContains("", srcRepo, destRepo); + assertContains("", srcRepo2, destRepo); + assertNumberOfKeys(srcRepo.getArtifactKeys().length + srcRepo2.getArtifactKeys().length, destRepo); + } + + /** + * Tests mirroring all artifacts in a repository to a repository populated with non-duplicate entries without "-append" + * @throws Exception + * Source contains A, B + * Target contains C, D + * Expected is A, B + */ + public void testArtifactMirrorToPopulatedWithoutAppend() throws Exception { + artifactMirrorToPopulated(false); + + assertContentEquals("", srcRepo, destRepo); + } + + /** + * Tests mirroring all artifacts in a repository to a repository populated with partially duplicate data + * @throws Exception + * Source contains A, B, C, D + * Target contains A, B + * Expected is A, B, C, D + */ + public void testArtifactMirrorToPartialDuplicateWithAppend() throws Exception { + artifactMirrorToPartialDuplicate(true); + + assertContentEquals("", srcRepo3, destRepo); + } + + /** + * Tests mirroring all artifacts in a repository to a repository populated with partially duplicate data + * @throws Exception + * Source contains A, B, C, D + * Target contains A, B + * Expected is A, B, C, D + */ + public void testArtifactMirrorToPartialDuplicateWithoutAppend() throws Exception { + artifactMirrorToPartialDuplicate(false); + + assertContentEquals("", srcRepo3, destRepo); + } + + /** + * Tests mirroring all artifacts in a repository to a repository populated with both full duplicate and non-duplicate data with "-append" + * @throws Exception + * Source contains A, B + * Target contains A, B, C, D + * Expected is A, B, C, D + */ + public void testArtifactMirrorToPopulatedWithFullDuplicateWithAppend() throws Exception { + artifactMirrorToPopulatedWithFullDuplicate(true); + + assertContentEquals("", srcRepo3, destRepo); + } + + /** + * Tests mirroring all artifacts in a repository to a repository populated with both full duplicate and non-duplicate data without "-append" + * @throws Exception + * Source contains A, B + * Target contains A, B, C, D + * Expected is A, B + */ + public void testArtifactMirrorToPopulatedWithFullDuplicateWithoutAppend() throws Exception { + artifactMirrorToPopulatedWithFullDuplicate(false); + + assertContentEquals("", srcRepo, destRepo); + } + + /** + * Tests mirroring all artifacts in a repository to a repository populated with both partial duplicate and non-duplicate data with "-append" + * @throws Exception + * Source contains A, B, C, D + * Target contains A, B, E, F + * Expected is A, B, C, D, E, F + */ + public void testArtifactMirrorToPopulatedWithPartialDuplicateWithAppend() throws Exception { + artifactMirrorToPopulatedWithPartialDuplicate(true); + + assertContains("", srcRepo3, destRepo); + assertContains("", srcRepo2, destRepo); + assertNumberOfKeys(srcRepo2.getArtifactKeys().length + srcRepo3.getArtifactKeys().length, destRepo); + } + + /** + * Tests mirroring all artifacts in a repository to a repository populated with both partial duplicate and non-duplicate data without "-append" + * @throws Exception + * Source contains A, B, C, D + * Target contains A, B, E, F + * Expected is A, B, C, D + */ + public void testArtifactMirrorToPopulatedWithPartialDuplicateWithoutAppend() throws Exception { + artifactMirrorToPopulatedWithPartialDuplicate(false); + + assertContentEquals("", srcRepo3, destRepo); + } +} Index: src/org/eclipse/equinox/p2/tests/mirror/AllTests.java =================================================================== RCS file: src/org/eclipse/equinox/p2/tests/mirror/AllTests.java diff -N src/org/eclipse/equinox/p2/tests/mirror/AllTests.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/org/eclipse/equinox/p2/tests/mirror/AllTests.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,27 @@ +/******************************************************************************* + * Copyright (c) 2007, 2008 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.equinox.p2.tests.mirror; + +import junit.framework.*; + +/** + * Performs all automated mirror application tests. + */ +public class AllTests extends TestCase { + + public static Test suite() { + TestSuite suite = new TestSuite(AllTests.class.getName()); + suite.addTestSuite(ArtifactMirrorApplicationTest.class); + suite.addTestSuite(MetadataMirrorApplicationTest.class); + return suite; + } + +} Index: src/org/eclipse/equinox/p2/tests/mirror/MetadataMirrorApplicationTest.java =================================================================== RCS file: src/org/eclipse/equinox/p2/tests/mirror/MetadataMirrorApplicationTest.java diff -N src/org/eclipse/equinox/p2/tests/mirror/MetadataMirrorApplicationTest.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/org/eclipse/equinox/p2/tests/mirror/MetadataMirrorApplicationTest.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,475 @@ +/******************************************************************************* + * Copyright (c) 2008 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + * Code 9 - ongoing development + *******************************************************************************/ +package org.eclipse.equinox.p2.tests.mirror; + +import java.io.File; +import java.net.MalformedURLException; +import java.util.HashMap; +import java.util.Map; +import org.eclipse.equinox.app.IApplicationContext; +import org.eclipse.equinox.internal.p2.core.helpers.ServiceHelper; +import org.eclipse.equinox.internal.p2.metadata.mirror.MirrorApplication; +import org.eclipse.equinox.internal.provisional.p2.metadata.IInstallableUnit; +import org.eclipse.equinox.internal.provisional.p2.metadata.query.InstallableUnitQuery; +import org.eclipse.equinox.internal.provisional.p2.metadata.repository.IMetadataRepository; +import org.eclipse.equinox.internal.provisional.p2.metadata.repository.IMetadataRepositoryManager; +import org.eclipse.equinox.internal.provisional.p2.query.Collector; +import org.eclipse.equinox.p2.tests.AbstractProvisioningTest; +import org.eclipse.equinox.p2.tests.TestActivator; +import org.osgi.framework.Bundle; + +/** + * Test API of the basic mirror application functionality's implementation. + */ +public class MetadataMirrorApplicationTest extends AbstractProvisioningTest { + protected File destRepoLocation; + protected File sourceRepoLocation; //helloworldfeature + protected File sourceRepo2Location; //anotherfeature + protected File sourceRepo3Location; //helloworldfeature + yetanotherfeature + protected IMetadataRepository destRepo; + protected IMetadataRepository srcRepo; + protected IMetadataRepository srcRepo2; + protected IMetadataRepository srcRepo3; + + private IMetadataRepositoryManager getMetadataRepositoryManager() { + return (IMetadataRepositoryManager) ServiceHelper.getService(TestActivator.getContext(), IMetadataRepositoryManager.class.getName()); + } + + protected void setUp() throws Exception { + super.setUp(); + String tempDir = System.getProperty("java.io.tmpdir"); + sourceRepoLocation = getTestData("1.0", "/testData/mirror/mirrorSourceRepo1"); + sourceRepo2Location = getTestData("2.0", "/testData/mirror/mirrorSourceRepo2"); + sourceRepo3Location = getTestData("3.0", "/testData/mirror/mirrorSourceRepo3"); + destRepoLocation = new File(tempDir, "BasicMirrorApplicationTest"); + AbstractProvisioningTest.delete(destRepoLocation); + } + + protected void tearDown() throws Exception { + removeRepositories(); + delete(destRepoLocation); + super.tearDown(); + } + + /** + * runs default mirror. source is the source repo, destination is the destination repo + */ + private void runMirrorApplication(final File source, final File destination, final boolean append) throws Exception { + MirrorApplication application = new MirrorApplication(); + application.start(new IApplicationContext() { + + public void applicationRunning() { + } + + public Map getArguments() { + Map arguments = new HashMap(); + + try { + arguments.put("application.args", new String[] {"-source", source.toURL().toExternalForm(), "-destination", destination.toURL().toExternalForm(), append ? "-append" : ""}); + + } catch (MalformedURLException e) { + fail("invalid URL for source or target repo"); + } + + return arguments; + } + + public String getBrandingApplication() { + return null; + } + + public Bundle getBrandingBundle() { + return null; + } + + public String getBrandingDescription() { + return null; + } + + public String getBrandingId() { + return null; + } + + public String getBrandingName() { + return null; + } + + public String getBrandingProperty(String key) { + return null; + } + }); + } + + /** + * Removes all repositories in one go + */ + private void removeRepositories() throws Exception { + getMetadataRepositoryManager().removeRepository(destRepoLocation.toURL()); + getMetadataRepositoryManager().removeRepository(sourceRepoLocation.toURL()); + getMetadataRepositoryManager().removeRepository(sourceRepo2Location.toURL()); + getMetadataRepositoryManager().removeRepository(sourceRepo3Location.toURL()); + } + + /** + * Loads all repositories in one go + * @throws Exception + */ + private void loadRepositories() throws Exception { + destRepo = getMetadataRepositoryManager().loadRepository(destRepoLocation.toURL(), null); + srcRepo = getMetadataRepositoryManager().loadRepository(sourceRepoLocation.toURL(), null); + srcRepo2 = getMetadataRepositoryManager().loadRepository(sourceRepo2Location.toURL(), null); + srcRepo3 = getMetadataRepositoryManager().loadRepository(sourceRepo3Location.toURL(), null); + } + + private int getNumUnique(Collector c1, Collector c2) { + Object[] repo1 = c1.toCollection().toArray(); + Object[] repo2 = c2.toCollection().toArray(); + + int numKeys = repo1.length + repo2.length; + + for (int i = 0; i < repo1.length; i++) { + for (int j = 0; j < repo2.length; j++) { + if (isEqual((IInstallableUnit) repo1[i], (IInstallableUnit) repo2[j])) + numKeys--; + } + } + + return numKeys; + } + + private void assertNumberOfKeys(int expected, IMetadataRepository repo) { + assertEquals(expected, repo.query(InstallableUnitQuery.ANY, new Collector(), null).size()); + } + + /** + * Tests mirroring all metadata in a repository to an empty repository + * @throws Exception + * Source contains A, B + * Target contains + */ + private void metadataMirrorToEmpty(boolean append) throws Exception { + runMirrorApplication(sourceRepoLocation, destRepoLocation, append); //do not append + + //TODO replace with test to ensure repositories are closed once bug 247664 is fixed + removeRepositories(); + + loadRepositories(); + } + + /** + * Tests mirroring all metadata in a repository to a repository populated with non-duplicate entries + * @throws Exception + * Source contains A, B + * Target contains C, D + */ + private void metadataMirrorToPopulated(boolean append) throws Exception { + //Setup: populate destination with non-duplicate metadata + runMirrorApplication(sourceRepo2Location, destRepoLocation, false); //value of append does not matter + + //TODO replace with test to ensure repositories are closed once bug 247664 is fixed + removeRepositories(); + + loadRepositories(); + + //Setup ensure setup completes successfully + assertContentEquals("", srcRepo2, destRepo); + + //mirror test data + runMirrorApplication(sourceRepoLocation, destRepoLocation, append); + + //TODO replace with test to ensure repositories are closed once bug 247664 is fixed + removeRepositories(); + + loadRepositories(); + } + + /** + * Tests mirroring all metadata in a repository to a repository populated with exact duplicate data + * @throws Exception + * Source contains A, B + * Target contains A, B + */ + private void metadataMirrorToFullDuplicate(boolean append) throws Exception { + //Setup: populate destination with duplicate metadata + runMirrorApplication(sourceRepoLocation, destRepoLocation, false); //value of append does not matter + + //TODO replace with test to ensure repositories are closed once bug 247664 is fixed + removeRepositories(); + + loadRepositories(); + + //Setup: verify contents + assertContentEquals("", srcRepo, destRepo); + + //mirror test data + runMirrorApplication(sourceRepoLocation, destRepoLocation, append); + + //TODO replace with test to ensure repositories are closed once bug 247664 is fixed + removeRepositories(); + + loadRepositories(); + } + + /** + * Tests mirroring all metadata in a repository to a repository populated with partially duplicate data + * @throws Exception + * Source contains A, B, C, D + * Target contains A, B + */ + private void metadataMirrorToPartialDuplicate(boolean append) throws Exception { + //Setup: populate destination with duplicate metadata + runMirrorApplication(sourceRepoLocation, destRepoLocation, false); //value of append does not matter + + //TODO replace with test to ensure repositories are closed once bug 247664 is fixed + removeRepositories(); + + loadRepositories(); + + //Setup: verify contents + assertContentEquals("", srcRepo, destRepo); + + //mirror test data + runMirrorApplication(sourceRepo3Location, destRepoLocation, append); + + //TODO replace with test to ensure repositories are closed once bug 247664 is fixed + removeRepositories(); + + loadRepositories(); + } + + /** + * Tests mirroring all metadata in a repository to a repository populated with both full duplicate and non-duplicate data + * @throws Exception + * Source contains A, B + * Target contains A, B, C, D + */ + private void metadataMirrorToPopulatedWithFullDuplicate(boolean append) throws Exception { + //Setup: populate destination with non-duplicate metadata + runMirrorApplication(sourceRepo3Location, destRepoLocation, false); //value of append does not matter + + //TODO replace with test to ensure repositories are closed once bug 247664 is fixed + removeRepositories(); + + loadRepositories(); + + //Setup: verify + assertContentEquals("", srcRepo3, destRepo); + + //mirror duplicate data + runMirrorApplication(sourceRepoLocation, destRepoLocation, append); + + //TODO replace with test to ensure repositories are closed once bug 247664 is fixed + removeRepositories(); + + loadRepositories(); + } + + /** + * Tests mirroring all metadata in a repository to a repository populated with both partial duplicate and non-duplicate data + * @throws Exception + * Source contains A, B, C, D + * Target contains A, B, E, F + */ + private void metadataMirrorToPopulatedWithPartialDuplicate(boolean append) throws Exception { + //Setup: populate destination with non-duplicate metadata + runMirrorApplication(sourceRepo2Location, destRepoLocation, false); //value of append does not matter + + //TODO replace with test to ensure repositories are closed once bug 247664 is fixed + removeRepositories(); + + loadRepositories(); + + //Setup: verify + assertContentEquals("", srcRepo2, destRepo); + + //Setup: populate destination with duplicate metadata + runMirrorApplication(sourceRepoLocation, destRepoLocation, true); + + //TODO replace with test to ensure repositories are closed once bug 247664 is fixed + removeRepositories(); + + loadRepositories(); + + //Setup: verify + assertContains("", srcRepo, destRepo); + assertContains("", srcRepo2, destRepo); + + //mirror duplicate data + runMirrorApplication(sourceRepo3Location, destRepoLocation, append); + + //TODO replace with test to ensure repositories are closed once bug 247664 is fixed + removeRepositories(); + + loadRepositories(); + } + + /** + * Tests mirroring all metadata in a repository to an empty repository with "-append" + * @throws Exception + * Source contains A, B + * Target contains + * Expected is A, B + */ + public void testMetadataMirrorToEmptyWithAppend() throws Exception { + metadataMirrorToEmpty(true); + + assertContentEquals("", srcRepo, destRepo); + } + + /** + * Tests mirroring all metadata in a repository to an empty repository without "-append" + * @throws Exception + * Source contains A, B + * Target contains + * Expected is A, B + */ + public void testMetadataMirrorToEmptyWithoutAppend() throws Exception { + metadataMirrorToEmpty(false); + + assertContentEquals("", srcRepo, destRepo); + } + + /** + * Tests mirroring all metadata in a repository to a repository populated with exact duplicate data with "-append" + * @throws Exception + * Source contains A, B + * Target contains A, B + * Expected is A, B + */ + public void testMetadataMirrorToFullDuplicateWithAppend() throws Exception { + metadataMirrorToFullDuplicate(true); + + assertContentEquals("", srcRepo, destRepo); + } + + /** + * Tests mirroring all metadata in a repository to a repository populated with exact duplicate data wihtout "-append" + * @throws Exception + * Source contains A, B + * Target contains A, B + * Expected is A, B + */ + public void testMetadataMirrorToFullDuplicateWithoutAppend() throws Exception { + metadataMirrorToFullDuplicate(false); + + assertContentEquals("", srcRepo, destRepo); + } + + /** + * Tests mirroring all metadata in a repository to a repository populated with non-duplicate entries with "-append" + * @throws Exception + * Source contains A, B + * Target contains C, D + * Expected is A, B, C, D + */ + public void testMetadataMirrorToPopulatedWithAppend() throws Exception { + metadataMirrorToPopulated(true); + + assertContains("", srcRepo, destRepo); + assertContains("", srcRepo2, destRepo); + assertNumberOfKeys(getNumUnique(srcRepo.query(InstallableUnitQuery.ANY, new Collector(), null), srcRepo2.query(InstallableUnitQuery.ANY, new Collector(), null)), destRepo); + } + + /** + * Tests mirroring all metadata in a repository to a repository populated with non-duplicate entries without "-append" + * @throws Exception + * Source contains A, B + * Target contains C, D + * Expected is A, B + */ + public void testMetadataMirrorToPopulatedWithoutAppend() throws Exception { + metadataMirrorToPopulated(false); + + assertContentEquals("", srcRepo, destRepo); + } + + /** + * Tests mirroring all metadata in a repository to a repository populated with partially duplicate data + * @throws Exception + * Source contains A, B, C, D + * Target contains A, B + * Expected is A, B, C, D + */ + public void testMetadataMirrorToPartialDuplicateWithAppend() throws Exception { + metadataMirrorToPartialDuplicate(true); + + assertContentEquals("", srcRepo3, destRepo); + } + + /** + * Tests mirroring all metadata in a repository to a repository populated with partially duplicate data + * @throws Exception + * Source contains A, B, C, D + * Target contains A, B + * Expected is A, B, C, D + */ + public void testMetadataMirrorToPartialDuplicateWithoutAppend() throws Exception { + metadataMirrorToPartialDuplicate(false); + + assertContentEquals("", srcRepo3, destRepo); + } + + /** + * Tests mirroring all metadata in a repository to a repository populated with both full duplicate and non-duplicate data with "-append" + * @throws Exception + * Source contains A, B + * Target contains A, B, C, D + * Expected is A, B, C, D + */ + public void testMetadataMirrorToPopulatedWithFullDuplicateWithAppend() throws Exception { + metadataMirrorToPopulatedWithFullDuplicate(true); + + assertContentEquals("", srcRepo3, destRepo); + } + + /** + * Tests mirroring all metadata in a repository to a repository populated with both full duplicate and non-duplicate data without "-append" + * @throws Exception + * Source contains A, B + * Target contains A, B, C, D + * Expected is A, B + */ + public void testMetadataMirrorToPopulatedWithFullDuplicateWithoutAppend() throws Exception { + metadataMirrorToPopulatedWithFullDuplicate(false); + + assertContentEquals("", srcRepo, destRepo); + } + + /** + * Tests mirroring all metadata in a repository to a repository populated with both partial duplicate and non-duplicate data with "-append" + * @throws Exception + * Source contains A, B, C, D + * Target contains A, B, E, F + * Expected is A, B, C, D, E, F + */ + public void testMetadataMirrorToPopulatedWithPartialDuplicateWithAppend() throws Exception { + metadataMirrorToPopulatedWithPartialDuplicate(true); + + assertContains("", srcRepo3, destRepo); + assertContains("", srcRepo2, destRepo); + assertNumberOfKeys(getNumUnique(srcRepo2.query(InstallableUnitQuery.ANY, new Collector(), null), srcRepo3.query(InstallableUnitQuery.ANY, new Collector(), null)), destRepo); + } + + /** + * Tests mirroring all metadata in a repository to a repository populated with both partial duplicate and non-duplicate data without "-append" + * @throws Exception + * Source contains A, B, C, D + * Target contains A, B, E, F + * Expected is A, B, C, D + */ + public void testMetadataMirrorToPopulatedWithPartialDuplicateWithoutAppend() throws Exception { + metadataMirrorToPopulatedWithPartialDuplicate(false); + + assertContentEquals("", srcRepo3, destRepo); + } + +}