1 /*******************************************************************************
2  * Copyright (c) 2008, 2017 IBM Corporation and others.
3  *
4  * This
5  * program and the accompanying materials are made available under the terms of
6  * the Eclipse Public License 2.0 which accompanies this distribution, and is
7  * available at
8  * https://www.eclipse.org/legal/epl-2.0/
9  *
10  * SPDX-License-Identifier: EPL-2.0
11  *
12  * Contributors: IBM Corporation - 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.equinox.internal.p2.director.ProfileChangeRequest;
18 import org.eclipse.equinox.p2.engine.*;
19 import org.eclipse.equinox.p2.metadata.IInstallableUnit;
20 import org.eclipse.equinox.p2.metadata.Version;
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.tests.AbstractProvisioningTest;
25 
26 public class InclusionRuleTest extends AbstractProvisioningTest {
27 	IInstallableUnit a1;
28 	IInstallableUnit a2;
29 	IInstallableUnit b1;
30 	IProfile profile1;
31 	IProfile profile2;
32 	IProfile profile3;
33 	IProfile profile4;
34 	IPlanner planner;
35 	IEngine engine;
36 
37 	@Override
setUp()38 	protected void setUp() throws Exception {
39 		super.setUp();
40 		a1 = createIU("A", Version.create("1.0.0"), true);
41 
42 		a2 = createIU("A", Version.create("2.0.0"), true);
43 
44 		b1 = createIU("B", Version.create("1.0.0"), true);
45 
46 		createTestMetdataRepository(new IInstallableUnit[] {a1, a2});
47 
48 		planner = createPlanner();
49 		engine = createEngine();
50 	}
51 
testMultipleInstallations()52 	public void testMultipleInstallations() {
53 		profile1 = createProfile("TestProfile." + getName());
54 		ProfileChangeRequest req = new ProfileChangeRequest(profile1);
55 		req.addInstallableUnits(new IInstallableUnit[] {a1});
56 		IProvisioningPlan plan = planner.getProvisioningPlan(req, null, null);
57 		assertEquals(IStatus.OK, plan.getStatus().getSeverity());
58 		engine.perform(plan, null);
59 		assertProfileContainsAll("A1 is missing", profile1, new IInstallableUnit[] {a1});
60 		assertEquals(queryResultSize(profile1.query(QueryUtil.createIUAnyQuery(), null)), 1);
61 
62 		//Make a1 optional.
63 		ProfileChangeRequest req2 = new ProfileChangeRequest(profile1);
64 		req2.setInstallableUnitInclusionRules(a1, ProfileInclusionRules.createOptionalInclusionRule(a1));
65 		IProvisioningPlan plan2 = planner.getProvisioningPlan(req2, null, null);
66 		assertEquals(IStatus.OK, plan2.getStatus().getSeverity());
67 		engine.perform(plan2, null);
68 		assertProfileContainsAll("A1 is missing", profile1, new IInstallableUnit[] {a1});
69 		assertEquals(queryResultSize(profile1.query(QueryUtil.createIUAnyQuery(), null)), 1);
70 
71 		//Install b1 (this should not change anything for a1)
72 		ProfileChangeRequest req3 = new ProfileChangeRequest(profile1);
73 		req3.addInstallableUnits(new IInstallableUnit[] {b1});
74 		IProvisioningPlan plan3 = planner.getProvisioningPlan(req3, null, null);
75 		assertEquals(IStatus.OK, plan3.getStatus().getSeverity());
76 		engine.perform(plan3, null);
77 		assertProfileContainsAll("A1 or B1 is missing", profile1, new IInstallableUnit[] {a1, b1});
78 		assertEquals(queryResultSize(profile1.query(QueryUtil.createIUAnyQuery(), null)), 2);
79 
80 		//Add a2, this removes a1.
81 		ProfileChangeRequest req4 = new ProfileChangeRequest(profile1);
82 		req4.addInstallableUnits(new IInstallableUnit[] {a2});
83 		IProvisioningPlan plan4 = planner.getProvisioningPlan(req4, null, null);
84 		assertEquals(IStatus.OK, plan4.getStatus().getSeverity());
85 		engine.perform(plan4, null);
86 		assertProfileContainsAll("A2 is missing", profile1, new IInstallableUnit[] {a2});
87 		assertNotIUs(new IInstallableUnit[] {a1}, profile1.query(QueryUtil.createIUAnyQuery(), null).iterator());
88 		assertEquals(queryResultSize(profile1.query(QueryUtil.createIUAnyQuery(), null)), 2);
89 
90 		//Try to add a1 again. This will fail because since a1 has been uninstalled in the previous step and we no longer know about its optional inclusion
91 		ProfileChangeRequest req5 = new ProfileChangeRequest(profile1);
92 		req5.addInstallableUnits(new IInstallableUnit[] {a1});
93 		IProvisioningPlan plan5 = planner.getProvisioningPlan(req5, null, null);
94 		assertEquals(IStatus.ERROR, plan5.getStatus().getSeverity());
95 	}
96 
testRemoveInclusionRule()97 	public void testRemoveInclusionRule() {
98 		profile2 = createProfile("TestProfile2." + getName());
99 		//Install a1
100 		ProfileChangeRequest req = new ProfileChangeRequest(profile2);
101 		req.addInstallableUnits(new IInstallableUnit[] {a1});
102 		IProvisioningPlan plan = planner.getProvisioningPlan(req, null, null);
103 		assertEquals(IStatus.OK, plan.getStatus().getSeverity());
104 		engine.perform(plan, null);
105 		assertProfileContainsAll("A1 is missing", profile2, new IInstallableUnit[] {a1});
106 		assertEquals(queryResultSize(profile2.query(QueryUtil.createIUAnyQuery(), null)), 1);
107 
108 		//Make a1 optional.
109 		ProfileChangeRequest req2 = new ProfileChangeRequest(profile2);
110 		req2.setInstallableUnitInclusionRules(a1, ProfileInclusionRules.createOptionalInclusionRule(a1));
111 		IProvisioningPlan plan2 = planner.getProvisioningPlan(req2, null, null);
112 		assertEquals(IStatus.OK, plan2.getStatus().getSeverity());
113 		engine.perform(plan2, null);
114 		assertProfileContainsAll("A1 is missing", profile2, new IInstallableUnit[] {a1});
115 		assertEquals(queryResultSize(profile2.query(QueryUtil.createIUAnyQuery(), null)), 1);
116 
117 		//Install b1 (this should not change anything for a1)
118 		ProfileChangeRequest req3 = new ProfileChangeRequest(profile2);
119 		req3.addInstallableUnits(new IInstallableUnit[] {b1});
120 		IProvisioningPlan plan3 = planner.getProvisioningPlan(req3, null, null);
121 		assertEquals(IStatus.OK, plan3.getStatus().getSeverity());
122 		engine.perform(plan3, null);
123 		profile2 = getProfile(profile2.getProfileId());
124 		assertProfileContainsAll("A1 or B1 is missing", profile2, new IInstallableUnit[] {a1, b1});
125 		assertEquals(queryResultSize(profile2.query(QueryUtil.createIUAnyQuery(), null)), 2);
126 
127 		//Remove the optional inclusion rule from a1. a1 and b1 are still here
128 		ProfileChangeRequest req5 = new ProfileChangeRequest(profile2);
129 		req5.removeInstallableUnitInclusionRules(a1);
130 		IProvisioningPlan plan5 = planner.getProvisioningPlan(req5, null, null);
131 		assertEquals(IStatus.OK, plan5.getStatus().getSeverity());
132 		engine.perform(plan5, null);
133 		profile2 = getProfile(profile2.getProfileId());
134 		assertProfileContainsAll("A1 or B1 is missing", profile2, new IInstallableUnit[] {a1, b1});
135 		assertEquals(queryResultSize(profile2.query(QueryUtil.createIUAnyQuery(), null)), 2);
136 	}
137 
testRemoveIUandInclusionRule()138 	public void testRemoveIUandInclusionRule() {
139 		profile3 = createProfile("TestProfile3." + getName());
140 		ProfileChangeRequest req = new ProfileChangeRequest(profile3);
141 		req.addInstallableUnits(new IInstallableUnit[] {a1});
142 		IProvisioningPlan plan = planner.getProvisioningPlan(req, null, null);
143 		assertEquals(IStatus.OK, plan.getStatus().getSeverity());
144 		engine.perform(plan, null);
145 		assertProfileContainsAll("A1 is missing", profile3, new IInstallableUnit[] {a1});
146 		assertEquals(queryResultSize(profile3.query(QueryUtil.createIUAnyQuery(), null)), 1);
147 
148 		//Make a1 optional.
149 		ProfileChangeRequest req2 = new ProfileChangeRequest(profile3);
150 		req2.setInstallableUnitInclusionRules(a1, ProfileInclusionRules.createOptionalInclusionRule(a1));
151 		IProvisioningPlan plan2 = planner.getProvisioningPlan(req2, null, null);
152 		assertEquals(IStatus.OK, plan2.getStatus().getSeverity());
153 		engine.perform(plan2, null);
154 		assertProfileContainsAll("A1 is missing", profile3, new IInstallableUnit[] {a1});
155 		assertEquals(queryResultSize(profile3.query(QueryUtil.createIUAnyQuery(), null)), 1);
156 
157 		//Install b1 (this should not change anything for a1)
158 		ProfileChangeRequest req3 = new ProfileChangeRequest(profile3);
159 		req3.addInstallableUnits(new IInstallableUnit[] {b1});
160 		IProvisioningPlan plan3 = planner.getProvisioningPlan(req3, null, null);
161 		assertEquals(IStatus.OK, plan3.getStatus().getSeverity());
162 		engine.perform(plan3, null);
163 		assertProfileContainsAll("A1 or B1 is missing", profile3, new IInstallableUnit[] {a1, b1});
164 		assertEquals(queryResultSize(profile3.query(QueryUtil.createIUAnyQuery(), null)), 2);
165 
166 		//Remove the a1 and its inclusion rule
167 		ProfileChangeRequest req5 = new ProfileChangeRequest(profile3);
168 		req5.removeInstallableUnits(new IInstallableUnit[] {a1});
169 		req5.removeInstallableUnitInclusionRules(a1);
170 		IProvisioningPlan plan5 = planner.getProvisioningPlan(req5, null, null);
171 		assertEquals(IStatus.OK, plan5.getStatus().getSeverity());
172 		engine.perform(plan5, null);
173 		assertProfileContainsAll("bB1 is missing", profile3, new IInstallableUnit[] {b1});
174 		assertEquals(queryResultSize(profile3.query(QueryUtil.createIUAnyQuery(), null)), 1);
175 	}
176 
testAdditionWithInclusionRule()177 	public void testAdditionWithInclusionRule() {
178 		profile4 = createProfile("TestProfile4." + getName());
179 		//Try to Install a1 and a2
180 		ProfileChangeRequest req5 = new ProfileChangeRequest(profile4);
181 		req5.addInstallableUnits(new IInstallableUnit[] {a1, a2});
182 		IProvisioningPlan plan5 = planner.getProvisioningPlan(req5, null, null);
183 		assertEquals(IStatus.ERROR, plan5.getStatus().getSeverity());
184 
185 		//Install a1 and a2 marking a1 optional
186 		ProfileChangeRequest req = new ProfileChangeRequest(profile4);
187 		req.addInstallableUnits(new IInstallableUnit[] {a1, a2});
188 		req.setInstallableUnitInclusionRules(a1, ProfileInclusionRules.createOptionalInclusionRule(a1));
189 		IProvisioningPlan plan = planner.getProvisioningPlan(req, null, null);
190 		assertEquals(IStatus.OK, plan.getStatus().getSeverity());
191 		engine.perform(plan, null);
192 		assertProfileContainsAll("A2 is missing", profile4, new IInstallableUnit[] {a2});
193 		assertEquals(queryResultSize(profile4.query(QueryUtil.createIUAnyQuery(), null)), 1);
194 
195 		//Make a1 optional, this is a no-op since a1 is not in the system
196 		ProfileChangeRequest req2 = new ProfileChangeRequest(profile4);
197 		req2.setInstallableUnitInclusionRules(a1, ProfileInclusionRules.createOptionalInclusionRule(a1));
198 		IProvisioningPlan plan2 = planner.getProvisioningPlan(req2, null, null);
199 		assertEquals(IStatus.OK, plan2.getStatus().getSeverity());
200 		engine.perform(plan2, null);
201 		assertProfileContainsAll("A2 is missing", profile4, new IInstallableUnit[] {a2});
202 		assertEquals(queryResultSize(profile4.query(QueryUtil.createIUAnyQuery(), null)), 1);
203 
204 		//Install a1, this is expected to fail
205 		ProfileChangeRequest req3 = new ProfileChangeRequest(profile4);
206 		req3.addInstallableUnits(new IInstallableUnit[] {a1});
207 		IProvisioningPlan plan3 = planner.getProvisioningPlan(req3, null, null);
208 		assertEquals(IStatus.ERROR, plan3.getStatus().getSeverity());
209 	}
210 }
211