Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ef69bc853c8c3a4bf3a530f472e86d3dd9eea1c4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*******************************************************************************
 * Copyright (c) 2010, 2017 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.p2.tests.planner;

import java.io.File;
import java.lang.reflect.Field;
import java.net.URI;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.eclipse.equinox.internal.p2.director.ProfileChangeRequest;
import org.eclipse.equinox.internal.p2.engine.InstallableUnitOperand;
import org.eclipse.equinox.internal.p2.engine.Operand;
import org.eclipse.equinox.internal.p2.engine.ProvisioningPlan;
import org.eclipse.equinox.internal.p2.engine.SimpleProfileRegistry;
import org.eclipse.equinox.p2.engine.IProfile;
import org.eclipse.equinox.p2.engine.IProfileRegistry;
import org.eclipse.equinox.p2.engine.IProvisioningPlan;
import org.eclipse.equinox.p2.engine.ProvisioningContext;
import org.eclipse.equinox.p2.metadata.IInstallableUnit;
import org.eclipse.equinox.p2.planner.IProfileChangeRequest;
import org.eclipse.equinox.p2.planner.ProfileInclusionRules;
import org.eclipse.equinox.p2.repository.metadata.IMetadataRepository;
import org.eclipse.equinox.p2.tests.AbstractProvisioningTest;

/**
 * @since 1.0
 */
public abstract class AbstractPlannerTest extends AbstractProvisioningTest {
	private IProfile profile;
	private File previousStoreValue = null;
	IMetadataRepository repo = null;

	/*
	 * Return the root of the data for this test. e.g. "testData/bug302582"
	 */
	protected abstract String getTestDataPath();

	/*
	 * Return the profileID to be used for this test.
	 */
	protected abstract String getProfileId();

	protected IProfile getProfile() {
		return profile;
	}

	/*
	 * Take the given plan and compress additons/removals so they look like updates.
	 * Good for viewing while debugging.
	 */
	protected Collection<InstallableUnitOperand> compress(IProvisioningPlan plan) {
		Map<String, InstallableUnitOperand> result = new HashMap<>();
		Operand[] operands = ((ProvisioningPlan) plan).getOperands();
		for (int i = 0; i < operands.length; i++) {
			if (!(operands[i] instanceof InstallableUnitOperand))
				continue;
			InstallableUnitOperand operand = (InstallableUnitOperand) operands[i];
			String id = operand.first() == null ? operand.second().getId() : operand.first().getId();
			InstallableUnitOperand existing = result.get(id);
			if (existing == null) {
				result.put(id, operand);
			} else {
				IInstallableUnit first = existing.first() == null ? operand.first() : existing.first();
				IInstallableUnit second = existing.second() == null ? operand.second() : existing.second();
				result.put(id, new InstallableUnitOperand(first, second));
			}
		}
		return result.values();
	}

	protected ProvisioningContext getContext(Collection<URI> repoLocations) {
		ProvisioningContext result = new ProvisioningContext(getAgent());
		result.setMetadataRepositories(repoLocations == null ? new URI[0] : repoLocations.toArray(new URI[repoLocations.size()]));
		result.setArtifactRepositories(new URI[0]);
		return result;
	}

	@Override
	protected void setUp() throws Exception {
		super.setUp();
		File reporegistry1 = getTestData("loading planner test data", getTestDataPath());
		File tempFolder = getTempFolder();
		copy("0.2", reporegistry1, tempFolder);
		IProfileRegistry realProfileRegistry = getProfileRegistry();
		//Tweak the running profile registry
		Field profileStore = SimpleProfileRegistry.class.getDeclaredField("store");
		profileStore.setAccessible(true);
		previousStoreValue = (File) profileStore.get(realProfileRegistry);
		profileStore.set(realProfileRegistry, new File(tempFolder, "p2/org.eclipse.equinox.p2.engine/profileRegistry"));

		Field profilesMapField = SimpleProfileRegistry.class.getDeclaredField("profiles"); //$NON-NLS-1$
		profilesMapField.setAccessible(true);
		profilesMapField.set(realProfileRegistry, null);
		//End of tweaking the profile registry

		profile = realProfileRegistry.getProfile(getProfileId());
		assertNotNull(profile);
		repo = loadMetadataRepository(getTestData("planner test repo", getTestDataPath() + "/repo").toURI());
	}

	@Override
	protected void tearDown() throws Exception {
		SimpleProfileRegistry realProfileRegistry = (SimpleProfileRegistry) getProfileRegistry();

		Field profilesMapField = SimpleProfileRegistry.class.getDeclaredField("profiles"); //$NON-NLS-1$
		profilesMapField.setAccessible(true);
		profilesMapField.set(realProfileRegistry, null);

		Field profileStore = SimpleProfileRegistry.class.getDeclaredField("store");
		profileStore.setAccessible(true);
		profileStore.set(realProfileRegistry, previousStoreValue);
		super.tearDown();
	}

	/*
	 * Create and return a new profile change request with the given additions and removals.
	 */
	protected IProfileChangeRequest createProfileChangeRequest(Collection<IInstallableUnit> optionalAdds, Collection<IInstallableUnit> strictAdds, Collection<IInstallableUnit> toRemove) {
		IProfileChangeRequest result = new ProfileChangeRequest(profile);

		// add optional IUs
		if (optionalAdds != null) {
			for (Iterator<IInstallableUnit> iter = optionalAdds.iterator(); iter.hasNext();) {
				IInstallableUnit iu = iter.next();
				result.add(iu);
				result.setInstallableUnitInclusionRules(iu, ProfileInclusionRules.createOptionalInclusionRule(iu));
				result.setInstallableUnitProfileProperty(iu, "org.eclipse.equinox.p2.type.lock", "1");
				result.setInstallableUnitProfileProperty(iu, "org.eclipse.equinox.p2.reconciler.dropins", "true");
			}
		}

		// add strict IUs
		if (strictAdds != null) {
			for (Iterator<IInstallableUnit> iter = strictAdds.iterator(); iter.hasNext();) {
				IInstallableUnit iu = iter.next();
				result.add(iu);
				result.setInstallableUnitInclusionRules(iu, ProfileInclusionRules.createStrictInclusionRule(iu));
			}
		}

		// include removals
		if (toRemove != null) {
			for (Iterator<IInstallableUnit> iter = toRemove.iterator(); iter.hasNext();) {
				IInstallableUnit iu = iter.next();
				result.remove(iu);
			}
		}

		return result;
	}

	/*
	 * Assert that all the IU operands in the expected plan are contained in the actual plan.
	 */
	protected void assertContains(String message, IProvisioningPlan expectedPlan, IProvisioningPlan actualPlan) {
		Operand[] expectedOperands = ((ProvisioningPlan) expectedPlan).getOperands();
		Operand[] actualOperands = ((ProvisioningPlan) actualPlan).getOperands();

		// make sure the expected plan isn't empty
		assertFalse("0.9 Plan is empty.", expectedOperands.length == 0);
		for (int outer = 0; outer < expectedOperands.length; outer++) {
			if (!(expectedOperands[outer] instanceof InstallableUnitOperand))
				continue;
			IInstallableUnit first = ((InstallableUnitOperand) expectedOperands[outer]).first();
			IInstallableUnit second = ((InstallableUnitOperand) expectedOperands[outer]).second();

			// see if there is an operand in the actual plan which involved this IU.
			boolean found = false;
			for (int inner = 0; inner < actualOperands.length; inner++) {
				if (!(actualOperands[inner] instanceof InstallableUnitOperand))
					continue;
				InstallableUnitOperand actual = (InstallableUnitOperand) actualOperands[inner];

				// handle removals
				if (second == null) {
					if (actual.second() != null)
						continue;
					if (!actual.first().getId().equals(first.getId()))
						continue;
					// we are doing a removal and we have IUs with the same id... do they have the same version too?
					assertEquals("0.5", first, actual.first());
				}

				// treat additions and updates the same as long as we end up with the same IU in the end
				assertNotNull("1.2 " + actual, actual.second());
				if (!actual.second().getId().equals(second.getId()))
					continue;
				// we are doing an install or upgrade and we have IUs with the same id... do they have the same version too?
				assertEquals("2.0", second, actual.second());
				found = true;

			}
			if (!found)
				fail("3.0 Plan is missing install operand for: " + second);
		}
	}

}

Back to the top