Skip to main content
summaryrefslogtreecommitdiffstats
blob: 71fe40b998b1f68ef3e1e6580227e319aadb5eac (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
/*******************************************************************************
 * Copyright (c) 2011 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.planner;

import java.net.URI;
import java.util.*;
import org.eclipse.core.runtime.*;
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.metadata.Version;
import org.eclipse.equinox.p2.planner.IPlanner;
import org.eclipse.equinox.p2.planner.IProfileChangeRequest;
import org.eclipse.equinox.p2.query.*;
import org.eclipse.equinox.p2.tests.TestActivator;

public class Bug362692 extends AbstractPlannerTest {

	// path to our data
	protected String getTestDataPath() {
		return "testData/bug362692";
	}

	// profile id
	protected String getProfileId() {
		return "bootProfile";
	}

	public void testInstall() {
		IPlanner planner = createPlanner();

		// this is the set of IUs we expect in the final result - highest version only
		Set<IInstallableUnit> expected = new HashSet<IInstallableUnit>();
		IQueryResult queryResult = repo.query(QueryUtil.createIUQuery("PluginA", Version.createOSGi(1, 1, 1, null)), new NullProgressMonitor());
		expected.addAll(queryResult.toSet());
		queryResult = repo.query(QueryUtil.createIUQuery("PluginB", Version.createOSGi(1, 1, 2, null)), new NullProgressMonitor());
		expected.addAll(queryResult.toSet());
		queryResult = repo.query(QueryUtil.createIUQuery("PluginC", Version.createOSGi(1, 1, 3, null)), new NullProgressMonitor());
		expected.addAll(queryResult.toSet());
		queryResult = repo.query(QueryUtil.createIUQuery("PluginD", Version.createOSGi(1, 1, 4, null)), new NullProgressMonitor());
		expected.addAll(queryResult.toSet());
		queryResult = repo.query(QueryUtil.createIUQuery("PluginE", Version.createOSGi(1, 1, 5, null)), new NullProgressMonitor());
		expected.addAll(queryResult.toSet());

		// create the actual plan - install everything in the repo as optional (mimic the dropins folder)
		Set<IInstallableUnit> toAdd = new HashSet<IInstallableUnit>();
		IQueryResult allIUs = repo.query(QueryUtil.createIUAnyQuery(), new NullProgressMonitor());
		// we don't want to re-install units which are already installed in the profile so remove them. (this is what the reconciler does)
		for (Iterator<IInstallableUnit> iter = allIUs.iterator(); iter.hasNext();) {
			IInstallableUnit iu = iter.next();
			queryResult = getProfile().query(QueryUtil.createIUQuery(iu.getId(), iu.getVersion()), new NullProgressMonitor());
			if (queryResult.isEmpty())
				toAdd.add(iu);
			else
				System.out.println("Already installed: " + iu.getId() + " " + iu.getVersion());
		}
		validate(expected, toAdd);

		// set the metadata repositories on the provisioning context. one for the dropins and one for the shared area
		Collection<URI> repoURLs = new ArrayList<URI>();
		repoURLs.add(repo.getLocation());
		repoURLs.add(new Path(getTestDataPath()).append("shared").toFile().toURI());
		ProvisioningContext context = getContext(repoURLs);
		context.setExtraInstallableUnits(new ArrayList(toAdd));
		IProfileChangeRequest actualChangeRequest = createProfileChangeRequest(toAdd, null, null);
		IProvisioningPlan plan = planner.getProvisioningPlan(actualChangeRequest, context, new NullProgressMonitor());
		Collection compressedPlan = compress(plan);
		for (Iterator iter = compressedPlan.iterator(); iter.hasNext();) {
			System.out.println("Plan: " + iter.next());
		}
		validate(expected, plan);
	}

	/*
	 * All of the expected IUs should either already be installed in the profile (and not be removed) 
	 * or in the list of additions.
	 */
	private void validate(Collection<IInstallableUnit> expected, Collection<IInstallableUnit> toAdd) {
		MultiStatus errors = new MultiStatus(TestActivator.PI_PROV_TESTS, IStatus.OK, "Errors while validating plan.", null);
		for (IInstallableUnit unit : expected) {
			IQuery query = QueryUtil.createIUQuery(unit.getId(), unit.getVersion());
			// already in the profile?
			IQueryResult queryResult = getProfile().query(query, new NullProgressMonitor());
			if (queryResult.isEmpty()) {
				// not in the profile, should be an incoming addition then
				if (!toAdd.contains(unit)) {
					errors.add(new Status(IStatus.ERROR, TestActivator.PI_PROV_TESTS, unit.getId() + " " + unit.getVersion() + " isn't in the profile and isn't an incoming addition."));
				}
			} else {
				// expected IU is already in the profile
			}
		}
		assertOK("Errors while validating plan.", errors);
	}

	/*
	 * All of the expected IUs should either already be installed in the profile (and not be removed) 
	 * or in the plan as an addition.
	 */
	private void validate(Collection<IInstallableUnit> expected, IProvisioningPlan plan) {
		MultiStatus errors = new MultiStatus(TestActivator.PI_PROV_TESTS, IStatus.OK, "Errors while validating plan.", null);
		for (IInstallableUnit unit : expected) {
			IQuery query = QueryUtil.createIUQuery(unit.getId(), unit.getVersion());
			// already in the profile?
			IQueryResult queryResult = getProfile().query(query, new NullProgressMonitor());
			if (queryResult.isEmpty()) {
				// not in the profile, should be an incoming addition then
				if (plan.getAdditions().query(query, new NullProgressMonitor()).isEmpty()) {
					errors.add(new Status(IStatus.ERROR, TestActivator.PI_PROV_TESTS, unit.getId() + " " + unit.getVersion() + " isn't in the profile and isn't an incoming addition."));
				}
			} else {
				// IU is in the profile, ensure we aren't removing it
				if (!plan.getRemovals().query(query, new NullProgressMonitor()).isEmpty()) {
					errors.add(new Status(IStatus.ERROR, TestActivator.PI_PROV_TESTS, unit.getId() + " " + unit.getVersion() + " is in the profile but is being removed."));
				}
			}
		}
		assertOK("Errors while validating plan.", errors);
	}
}

Back to the top