1 /*******************************************************************************
2  *  Copyright (c) 2010, 2017 Sonatype, Inc and others.
3  *
4  *  This program and the accompanying materials
5  *  are made available under the terms of the Eclipse Public License 2.0
6  *  which accompanies this distribution, and is available at
7  *  https://www.eclipse.org/legal/epl-2.0/
8  *
9  *  SPDX-License-Identifier: EPL-2.0
10  *
11  *  Contributors:
12  *     Sonatype, Inc. - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.equinox.p2.tests.planner;
15 
16 import org.eclipse.core.runtime.IStatus;
17 import org.eclipse.core.runtime.NullProgressMonitor;
18 import org.eclipse.equinox.internal.p2.director.ProfileChangeRequest;
19 import org.eclipse.equinox.p2.engine.*;
20 import org.eclipse.equinox.p2.metadata.*;
21 import org.eclipse.equinox.p2.planner.IPlanner;
22 import org.eclipse.equinox.p2.planner.ProfileInclusionRules;
23 import org.eclipse.equinox.p2.query.QueryUtil;
24 import org.eclipse.equinox.p2.repository.metadata.IMetadataRepository;
25 import org.eclipse.equinox.p2.tests.AbstractProvisioningTest;
26 
27 //This test verify that one patch can replace another one.
28 //In this test we use the metadata from the original bug 300572 and simulate the optional addition of the patches
29 public class Bug300572Small3 extends AbstractProvisioningTest {
30 	IInstallableUnit featureBeingPatched;
31 	IInstallableUnitPatch p1, p2;
32 
33 	IProfile profile1;
34 	IPlanner planner;
35 	IEngine engine;
36 	private IInstallableUnit p1b;
37 	private IInstallableUnit p2b;
38 
39 	//	private IInstallableUnit p2c;
40 
41 	@Override
setUp()42 	protected void setUp() throws Exception {
43 		super.setUp();
44 		IMetadataRepository repo = getMetadataRepositoryManager().loadRepository(getTestData("bug300572 data", "testData/bug300572Small/repo/").toURI(), new NullProgressMonitor());
45 		featureBeingPatched = repo.query(QueryUtil.createIUQuery("hellofeature.feature.group"), null).iterator().next();
46 
47 		//All the IUs for the patch
48 		p1 = (IInstallableUnitPatch) repo.query(QueryUtil.createIUQuery("hellopatch.feature.group", Version.create("1.0.0")), null).iterator().next();
49 		p1b = repo.query(QueryUtil.createIUQuery("hello", Version.create("1.0.1.200911201237")), null).iterator().next();
50 		//		p1c = repo.query(QueryUtil.createIUQuery("hellopatch.feature.jar", Version.create("1.0.0")), null).iterator().next();
51 
52 		p2 = (IInstallableUnitPatch) repo.query(QueryUtil.createIUQuery("hellopatch.feature.group", Version.create("1.0.2.201001211536")), null).iterator().next();
53 		p2b = repo.query(QueryUtil.createIUQuery("hello", Version.create("1.0.2.201001211536")), null).iterator().next();
54 		//		p2c = repo.query(QueryUtil.createIUQuery("hellopatch.feature.jar", Version.create("1.0.2.201001211536")), null).iterator().next();
55 
56 		planner = createPlanner();
57 		engine = createEngine();
58 	}
59 
testInstallAandP1ThenP2()60 	public void testInstallAandP1ThenP2() {
61 		profile1 = createProfile("TestProfile." + getName());
62 		ProfileChangeRequest req1 = new ProfileChangeRequest(profile1);
63 		req1.addInstallableUnits(new IInstallableUnit[] {featureBeingPatched, p1, p1b});
64 		req1.setInstallableUnitInclusionRules(p1, ProfileInclusionRules.createOptionalInclusionRule(p1));
65 		req1.setInstallableUnitInclusionRules(p1b, ProfileInclusionRules.createOptionalInclusionRule(p1b));
66 
67 		IProvisioningPlan plan1 = planner.getProvisioningPlan(req1, null, null);
68 		assertEquals(IStatus.OK, plan1.getStatus().getSeverity());
69 		assertContains(plan1.getAdditions().query(QueryUtil.ALL_UNITS, null), p1);
70 		assertOK("plan execution", engine.perform(plan1, null));
71 		assertProfileContainsAll("Patch 1 not installed", profile1, new IInstallableUnit[] {p1, p1b});
72 
73 		ProfileChangeRequest req2 = new ProfileChangeRequest(profile1);
74 		req2.addInstallableUnits(new IInstallableUnit[] {p2, p2b});
75 		req2.setInstallableUnitInclusionRules(p2, ProfileInclusionRules.createOptionalInclusionRule(p2));
76 		req2.setInstallableUnitInclusionRules(p2, ProfileInclusionRules.createOptionalInclusionRule(p2b));
77 		IProvisioningPlan plan2 = planner.getProvisioningPlan(req2, null, null);
78 		assertOK("Planning for installing P2", plan2.getStatus());
79 		assertContains(plan2.getAdditions().query(QueryUtil.ALL_UNITS, null), p2);
80 		assertFalse(plan2.getAdditions().query(QueryUtil.createIUQuery(p2b), null).isEmpty());
81 		assertFalse(plan2.getAdditions().query(QueryUtil.createIUQuery(p2), null).isEmpty());
82 		assertFalse(plan2.getRemovals().query(QueryUtil.createIUQuery(p1), null).isEmpty());
83 		assertFalse(plan2.getRemovals().query(QueryUtil.createIUQuery(p1b), null).isEmpty());
84 	}
85 
86 }
87