diff options
6 files changed, 224 insertions, 5 deletions
diff --git a/bundles/org.eclipse.equinox.p2.repository.tools/src/org/eclipse/equinox/p2/internal/repository/tools/MirrorApplication.java b/bundles/org.eclipse.equinox.p2.repository.tools/src/org/eclipse/equinox/p2/internal/repository/tools/MirrorApplication.java index b72482467..30dfeb06f 100644 --- a/bundles/org.eclipse.equinox.p2.repository.tools/src/org/eclipse/equinox/p2/internal/repository/tools/MirrorApplication.java +++ b/bundles/org.eclipse.equinox.p2.repository.tools/src/org/eclipse/equinox/p2/internal/repository/tools/MirrorApplication.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2009, 2011 IBM Corporation and others. + * Copyright (c) 2009, 2013 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 @@ -218,6 +218,7 @@ public class MirrorApplication extends AbstractApplication implements IApplicati protected Mirroring getMirroring(IQueryable<IInstallableUnit> slice, IProgressMonitor monitor) { // Obtain ArtifactKeys from IUs IQueryResult<IInstallableUnit> ius = slice.query(QueryUtil.createIUAnyQuery(), monitor); + boolean iusSpecified = !ius.isEmpty(); // call before ius.iterator() to avoid bug 420318 ArrayList<IArtifactKey> keys = new ArrayList<IArtifactKey>(); for (Iterator<IInstallableUnit> iterator = ius.iterator(); iterator.hasNext();) { IInstallableUnit iu = iterator.next(); @@ -234,7 +235,7 @@ public class MirrorApplication extends AbstractApplication implements IApplicati mirror.setIncludePacked(includePacked); // If IUs have been specified then only they should be mirrored, otherwise mirror everything. - if (keys.size() > 0) + if (iusSpecified) mirror.setArtifactKeys(keys.toArray(new IArtifactKey[keys.size()])); if (comparatorLog != null) diff --git a/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/AbstractProvisioningTest.java b/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/AbstractProvisioningTest.java index 15ff547c2..c9b6bcb89 100644 --- a/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/AbstractProvisioningTest.java +++ b/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/AbstractProvisioningTest.java @@ -985,7 +985,7 @@ public abstract class AbstractProvisioningTest extends TestCase { /* * Look up and return a file handle to the given entry in the bundle. */ - protected File getTestData(String message, String entry) { + public static File getTestData(String message, String entry) { if (entry == null) fail(message + " entry is null."); URL base = TestActivator.getContext().getBundle().getEntry(entry); diff --git a/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/TestAgentProvider.java b/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/TestAgentProvider.java new file mode 100644 index 000000000..3917719e9 --- /dev/null +++ b/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/TestAgentProvider.java @@ -0,0 +1,79 @@ +/******************************************************************************* + * Copyright (c) 2011, 2013 SAP AG 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: + * Tobias Oberlies (SAP AG) - initial API and implementation + *******************************************************************************/ +package org.eclipse.equinox.p2.tests; + +import java.io.File; +import java.io.IOException; +import org.eclipse.equinox.p2.core.*; +import org.junit.rules.ExternalResource; +import org.junit.rules.TemporaryFolder; +import org.osgi.framework.*; + +/** + * Provides {@link IProvisioningAgent} as tests. Use as JUnit 4 {@link Rule}. + */ +public final class TestAgentProvider extends ExternalResource { + private final TemporaryFolder tempManager; + private IProvisioningAgent agent; + + public TestAgentProvider() { + this.tempManager = new TemporaryFolder(); + } + + @Override + protected void before() throws Throwable { + tempManager.create(); + } + + @Override + protected void after() { + if (agent != null) { + agent.stop(); + } + tempManager.delete(); + } + + /** + * Returns an instance of an {@link IProvisioningAgent}. If this class is used as a JUnit method + * {@link Rule}, a separate instance is returned per test method. + */ + public IProvisioningAgent getAgent() throws ProvisionException { + if (agent == null) { + try { + agent = createProvisioningAgent(tempManager.newFolder("p2agent")); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + return agent; + } + + private IProvisioningAgent createProvisioningAgent(File location) throws ProvisionException { + BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext(); + ServiceReference<IProvisioningAgentProvider> serviceReference = bundleContext.getServiceReference(IProvisioningAgentProvider.class); + IProvisioningAgentProvider agentFactory = bundleContext.getService(serviceReference); + try { + return agentFactory.createAgent(location.toURI()); + } finally { + bundleContext.ungetService(serviceReference); + } + } + + /** + * Returns a service from the current agent. + * + * @see #getAgent() + */ + public <T> T getService(Class<T> type) throws ProvisionException { + return type.cast(getAgent().getService(type.getName())); + } + +} diff --git a/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/mirror/AllTests.java b/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/mirror/AllTests.java index 1a5a0043b..538745459 100644 --- a/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/mirror/AllTests.java +++ b/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/mirror/AllTests.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2008, 2009 IBM Corporation and others. + * Copyright (c) 2008, 2013 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 @@ -25,6 +25,7 @@ public class AllTests extends TestCase { suite.addTestSuite(MetadataRepositoryCleanupTest.class); suite.addTestSuite(NewMirrorApplicationArtifactTest.class); suite.addTestSuite(NewMirrorApplicationMetadataTest.class); + suite.addTest(new JUnit4TestAdapter(MirrorApplicationTest.class)); return suite; } diff --git a/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/mirror/MirrorApplicationTest.java b/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/mirror/MirrorApplicationTest.java new file mode 100644 index 000000000..979eab272 --- /dev/null +++ b/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/mirror/MirrorApplicationTest.java @@ -0,0 +1,138 @@ +/******************************************************************************* + * Copyright (c) 2013 SAP AG 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: + * SAP AG - initial API and implementation + *******************************************************************************/ +package org.eclipse.equinox.p2.tests.mirror; + +import static org.eclipse.equinox.p2.tests.AbstractProvisioningTest.getTestData; +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.assertThat; + +import java.io.File; +import java.io.PrintStream; +import java.net.URI; +import java.util.*; +import junit.framework.AssertionFailedError; +import org.eclipse.equinox.p2.core.ProvisionException; +import org.eclipse.equinox.p2.internal.repository.tools.MirrorApplication; +import org.eclipse.equinox.p2.internal.repository.tools.RepositoryDescriptor; +import org.eclipse.equinox.p2.metadata.IArtifactKey; +import org.eclipse.equinox.p2.metadata.IInstallableUnit; +import org.eclipse.equinox.p2.query.IQueryResult; +import org.eclipse.equinox.p2.query.QueryUtil; +import org.eclipse.equinox.p2.repository.artifact.IArtifactRepository; +import org.eclipse.equinox.p2.repository.artifact.IArtifactRepositoryManager; +import org.eclipse.equinox.p2.repository.metadata.IMetadataRepository; +import org.eclipse.equinox.p2.repository.metadata.IMetadataRepositoryManager; +import org.eclipse.equinox.p2.tests.StringBufferStream; +import org.eclipse.equinox.p2.tests.TestAgentProvider; +import org.junit.*; +import org.junit.rules.TemporaryFolder; + +/** + * Tests for joint mirroring of units and artifacts. See other test classes for unit-only and artifact-only mirroring tests. + */ +public class MirrorApplicationTest { + + @Rule + public TemporaryFolder tempFolder = new TemporaryFolder(); + @Rule + public TestAgentProvider agentProvider = new TestAgentProvider(); + + private File sourceRepoLocation; + private File destRepoLocation; + + private MirrorApplication subject; + + @Before + public void initRepositories() throws Exception { + sourceRepoLocation = getTestData("0.0", "/testData/mirror/mirrorSourceRepo1 with space"); + destRepoLocation = tempFolder.newFolder("destRepo"); + } + + @Test + public void testMirrorUnitWithArtifact() throws Exception { + IInstallableUnit unit = getUnitFromSourceRepo("helloworld"); + + subject = createApplication(sourceRepoLocation, destRepoLocation, true); + subject.setSourceIUs(Arrays.asList(unit)); + runApplication(subject); + + assertThat(artifactsIn(destRepoLocation), is(Collections.singleton("helloworld"))); + } + + @Test + public void testMirrorUnitWithoutArtifacts() throws Exception { + IInstallableUnit unitWithoutArtifact = getUnitFromSourceRepo("a.jre"); + assertThat(unitWithoutArtifact.getArtifacts(), not(hasItem(any(IArtifactKey.class)))); // self-test // TODO use is(empty()) once Hamcrest 1.3 is available + + subject = createApplication(sourceRepoLocation, destRepoLocation, true); + subject.setSourceIUs(Arrays.asList(unitWithoutArtifact)); + runApplication(subject); + + assertThat(artifactsIn(destRepoLocation), not(hasItem(any(String.class)))); // TODO use is(empty()) once Hamcrest 1.3 is available + } + + private IInstallableUnit getUnitFromSourceRepo(String id) throws Exception { + IMetadataRepository repository = agentProvider.getService(IMetadataRepositoryManager.class).loadRepository(sourceRepoLocation.toURI(), null); + IQueryResult<IInstallableUnit> queryResult = repository.query(QueryUtil.createIUQuery(id), null); + if (queryResult.isEmpty()) + throw new AssertionFailedError("No unit with ID '" + id + "' found in repository " + sourceRepoLocation); + return queryResult.iterator().next(); + } + + private Set<String> artifactsIn(File repositoryLocation) throws Exception { + IArtifactRepository repository = agentProvider.getService(IArtifactRepositoryManager.class).loadRepository(repositoryLocation.toURI(), null); + return artifactsIn(repository); + } + + private static Set<String> artifactsIn(IArtifactRepository repository) { + Set<String> result = new HashSet<String>(); + for (IArtifactKey artifactKey : repository.query(QueryUtil.createMatchQuery(IArtifactKey.class, "true"), null).toUnmodifiableSet()) { + result.add(artifactKey.getId()); + } + return result; + } + + private static MirrorApplication createApplication(File sourceLocation, File destLocation, Boolean append) { + MirrorApplication app = new MirrorApplication(); + + if (destLocation != null) { + RepositoryDescriptor dest = createRepositoryDescriptor(destLocation.toURI(), append); + app.addDestination(dest); + } + + if (sourceLocation != null) { + RepositoryDescriptor src = createRepositoryDescriptor(sourceLocation.toURI(), null); + app.addSource(src); + } + return app; + } + + private static RepositoryDescriptor createRepositoryDescriptor(URI location, Boolean append) { + RepositoryDescriptor descriptor = new RepositoryDescriptor(); + descriptor.setLocation(location); + if (append != null) + descriptor.setAppend(append); + return descriptor; + } + + private static StringBuffer runApplication(MirrorApplication app) throws ProvisionException { + StringBuffer buffer = new StringBuffer(); + PrintStream out = System.out; + try { + System.setOut(new PrintStream(new StringBufferStream(buffer))); + app.run(null); + } finally { + System.setOut(out); + } + return buffer; + } + +} diff --git a/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/mirror/NewMirrorApplicationMetadataTest.java b/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/mirror/NewMirrorApplicationMetadataTest.java index 7a032fc6b..29a183785 100644 --- a/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/mirror/NewMirrorApplicationMetadataTest.java +++ b/bundles/org.eclipse.equinox.p2.tests/src/org/eclipse/equinox/p2/tests/mirror/NewMirrorApplicationMetadataTest.java @@ -562,7 +562,7 @@ public class NewMirrorApplicationMetadataTest extends AbstractProvisioningTest { fail("14.1", e); } finally { if (invalidDestRepository != null) - getArtifactRepositoryManager().removeRepository(invalidDestRepository); + getMetadataRepositoryManager().removeRepository(invalidDestRepository); } } |