Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 98413442266a6dab2e3bd2b6f23a13ba31f86509 (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
/*******************************************************************************
 * Copyright (c) 2007, 2008 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.engine;

import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.equinox.internal.p2.installregistry.IInstallRegistry;
import org.eclipse.equinox.internal.p2.installregistry.IProfileInstallRegistry;
import org.eclipse.equinox.internal.provisional.p2.engine.*;
import org.eclipse.equinox.internal.provisional.p2.metadata.IInstallableUnit;
import org.eclipse.equinox.internal.provisional.p2.metadata.MetadataFactory;
import org.eclipse.equinox.internal.provisional.p2.metadata.MetadataFactory.InstallableUnitDescription;
import org.eclipse.equinox.p2.tests.AbstractProvisioningTest;
import org.eclipse.equinox.p2.tests.TestActivator;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.Version;

/**
 * Simple test of the engine API.
 */
public class InstallRegistryTest extends AbstractProvisioningTest {
	private ServiceReference registryRef;
	private IInstallRegistry registry;
	private ServiceReference engineRef;
	private IEngine engine;

	public InstallRegistryTest(String name) {
		super(name);
	}

	public InstallRegistryTest() {
		super("");
	}

	protected void setUp() throws Exception {
		registryRef = TestActivator.getContext().getServiceReference(IInstallRegistry.class.getName());
		registry = (IInstallRegistry) TestActivator.getContext().getService(registryRef);
		engineRef = TestActivator.getContext().getServiceReference(IEngine.SERVICE_NAME);
		engine = (IEngine) TestActivator.getContext().getService(engineRef);
	}

	protected void tearDown() throws Exception {
		engine = null;
		TestActivator.getContext().ungetService(engineRef);
		registry = null;
		TestActivator.getContext().ungetService(registryRef);
	}

	public void testAddRemoveIU() {
		PhaseSet phaseSet = new DefaultPhaseSet();
		IProfile profile = createProfile("testProfile");
		String profileId = profile.getProfileId();
		assertEquals(0, getRegistrySize(profileId));
		engine.perform(profile, phaseSet, new InstallableUnitOperand[] {new InstallableUnitOperand(null, createTestIU())}, null, new NullProgressMonitor());
		assertEquals(1, getRegistrySize(profileId));
		engine.perform(profile, phaseSet, new InstallableUnitOperand[] {new InstallableUnitOperand(createTestIU(), null)}, null, new NullProgressMonitor());
		assertEquals(0, getRegistrySize(profileId));
		registry.getProfileInstallRegistries().remove(profile);
	}

	protected int getRegistrySize(String profileId) {
		IProfileInstallRegistry profileInstallRegistry = registry.getProfileInstallRegistry(profileId);
		return profileInstallRegistry == null ? 0 : profileInstallRegistry.getInstallableUnits().length;
	}

	public void testPeristence() {
		PhaseSet phaseSet = new DefaultPhaseSet();
		IProfile profile = createProfile("testProfile");
		String profileId = profile.getProfileId();
		assertEquals(0, getRegistrySize(profileId));
		engine.perform(profile, phaseSet, new InstallableUnitOperand[] {new InstallableUnitOperand(null, createTestIU())}, null, new NullProgressMonitor());
		assertEquals(1, getRegistrySize(profileId));

		restart();

		assertEquals(1, getRegistrySize(profileId));
		engine.perform(profile, phaseSet, new InstallableUnitOperand[] {new InstallableUnitOperand(createTestIU(), null)}, null, new NullProgressMonitor());
		assertEquals(0, getRegistrySize(profileId));
		restart();
		assertEquals(0, getRegistrySize(profileId));
	}

	private void restart() {
		try {
			tearDown();
			TestActivator.getBundle("org.eclipse.equinox.p2.exemplarysetup").stop();
			TestActivator.getBundle("org.eclipse.equinox.p2.exemplarysetup").start();
			setUp();
		} catch (Exception e) {
			fail();
			e.printStackTrace();
		}
	}

	private IInstallableUnit createTestIU() {
		InstallableUnitDescription description = new MetadataFactory.InstallableUnitDescription();
		description.setId("org.eclipse.test");
		description.setVersion(new Version("1.0.0"));
		description.setTouchpointType(MetadataFactory.createTouchpointType("null", new Version("1.0.0")));
		IInstallableUnit unit = MetadataFactory.createInstallableUnit(description);
		return createResolvedIU(unit);
	}
}

Back to the top