Skip to main content
summaryrefslogtreecommitdiffstats
blob: 4d4c1b4b4da38a5eb555b7d20b3d9c1d40be970d (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
/*******************************************************************************
 * Copyright (c) 2008, 2017 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 static org.eclipse.core.runtime.IStatus.ERROR;
import static org.eclipse.core.runtime.IStatus.OK;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.eclipse.equinox.internal.p2.director.Explanation.MissingIU;
import org.eclipse.equinox.internal.p2.director.ProfileChangeRequest;
import org.eclipse.equinox.internal.provisional.p2.director.PlannerStatus;
import org.eclipse.equinox.internal.provisional.p2.director.RequestStatus;
import org.eclipse.equinox.p2.engine.IProfile;
import org.eclipse.equinox.p2.engine.IProvisioningPlan;
import org.eclipse.equinox.p2.metadata.IInstallableUnit;
import org.eclipse.equinox.p2.metadata.IProvidedCapability;
import org.eclipse.equinox.p2.metadata.IRequirement;
import org.eclipse.equinox.p2.metadata.MetadataFactory;
import org.eclipse.equinox.p2.planner.IPlanner;
import org.eclipse.equinox.p2.tests.AbstractProvisioningTest;

public class PropertyMatchRequirement extends AbstractProvisioningTest {
	private IInstallableUnit providerIu;
	private IInstallableUnit consumerIu;

	private IProfile profile;
	private IPlanner planner;

	@Override
	protected void setUp() throws Exception {
		super.setUp();

		// A standard OSGi service representation
		String osgiService = "osgi.service";
		String objectClass = "objectClass";
		List<String> objectClassList = Arrays.asList("org.example.A", "org.example.B", "org.example.C");

		// Provider.
		// TODO Check if p2 really needs a name. IProvidedCapability.equals() can differentiate by properties only.
		Map<String, Object> capability = new HashMap<>();
		capability.put(osgiService, "ignored-1");
		capability.put(objectClass, objectClassList);

		IProvidedCapability[] provides = new IProvidedCapability[] {
				MetadataFactory.createProvidedCapability(osgiService, capability)
		};
		providerIu = createIU("provider", DEFAULT_VERSION, provides);

		// Consumer
		String requirement = String.format("(%s=%s)", objectClass, objectClassList.get(0));
		IRequirement[] requires = new IRequirement[] {
				MetadataFactory.createRequirement(osgiService, requirement, null, 1, 1, true)
		};
		consumerIu = createIU("consumer", DEFAULT_VERSION, requires);

		// Planner
		profile = createProfile("test." + getName());
		planner = createPlanner();
	}

	public void testMandatoryPresent() {
		createTestMetdataRepository(new IInstallableUnit[] {providerIu, consumerIu});

		ProfileChangeRequest req = new ProfileChangeRequest(profile);
		req.add(consumerIu);

		// Must pass
		IProvisioningPlan plan = planner.getProvisioningPlan(req, null, null);
		assertEquals(OK, plan.getStatus().getSeverity());

		// And both consumer and provider must be installed
		assertInstallOperand(plan, consumerIu);
		assertInstallOperand(plan, providerIu);
	}

	public void testMandatoryAbsent() {
		createTestMetdataRepository(new IInstallableUnit[] {consumerIu});

		ProfileChangeRequest req = new ProfileChangeRequest(profile);
		req.add(consumerIu);

		// Must fail
		IProvisioningPlan plan = planner.getProvisioningPlan(req, null, null);
		assertEquals(ERROR, plan.getStatus().getSeverity());

		// With a good explanation
		RequestStatus requestStatus = ((PlannerStatus) plan.getStatus()).getRequestStatus();
		IRequirement consumerReq = consumerIu.getRequirements().iterator().next();
		requestStatus.getExplanations()
				.stream()
				.filter(e -> e instanceof MissingIU)
				.filter(e -> (((MissingIU) e).req.toString()).equals(consumerReq.toString()))
				.findFirst()
				.orElseGet(() -> {
					fail("Did not find explanation for missing requirement: " + consumerReq);
					return null;
				});

		// And the consumer must not be installed
		assertNoOperand(plan, consumerIu);
	}
}

Back to the top