Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 337016 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/equinox/internal/p2/operations/Messages.java (+3 lines)
Lines 66-71 Link Here
66
	public static String UpdateOperation_ResolveJobName;
66
	public static String UpdateOperation_ResolveJobName;
67
	public static String UpdateOperation_UpdateJobName;
67
	public static String UpdateOperation_UpdateJobName;
68
68
69
	public static String OperationFactory_noAgent;
70
	public static String OperationFactory_noIUFound;
71
69
	static {
72
	static {
70
		// initialize resource bundle
73
		// initialize resource bundle
71
		NLS.initializeMessages(BUNDLE_NAME, Messages.class);
74
		NLS.initializeMessages(BUNDLE_NAME, Messages.class);
(-)src/org/eclipse/equinox/internal/p2/operations/messages.properties (+3 lines)
Lines 42-44 Link Here
42
UpdateOperation_ProfileChangeRequestProgress=Computing update request
42
UpdateOperation_ProfileChangeRequestProgress=Computing update request
43
UpdateOperation_ResolveJobName=Computing Update Requirements
43
UpdateOperation_ResolveJobName=Computing Update Requirements
44
UpdateOperation_UpdateJobName=Updating Software
44
UpdateOperation_UpdateJobName=Updating Software
45
46
OperationFactory_noAgent=This installation has not been configured properly. No p2 agent can be found.
47
OperationFactory_noIUFound=No IU could be found for {0}.
(-)src/org/eclipse/equinox/p2/operations/OperationFactory.java (-22 / +21 lines)
Lines 13-21 Link Here
13
13
14
import java.net.URI;
14
import java.net.URI;
15
import java.util.*;
15
import java.util.*;
16
import org.eclipse.core.runtime.Assert;
16
import org.eclipse.core.runtime.*;
17
import org.eclipse.core.runtime.IProgressMonitor;
18
import org.eclipse.equinox.internal.p2.operations.Activator;
17
import org.eclipse.equinox.internal.p2.operations.Activator;
18
import org.eclipse.equinox.internal.p2.operations.Messages;
19
import org.eclipse.equinox.p2.core.IProvisioningAgent;
19
import org.eclipse.equinox.p2.core.IProvisioningAgent;
20
import org.eclipse.equinox.p2.core.ProvisionException;
20
import org.eclipse.equinox.p2.core.ProvisionException;
21
import org.eclipse.equinox.p2.engine.*;
21
import org.eclipse.equinox.p2.engine.*;
Lines 23-28 Link Here
23
import org.eclipse.equinox.p2.metadata.IInstallableUnit;
23
import org.eclipse.equinox.p2.metadata.IInstallableUnit;
24
import org.eclipse.equinox.p2.metadata.IVersionedId;
24
import org.eclipse.equinox.p2.metadata.IVersionedId;
25
import org.eclipse.equinox.p2.query.*;
25
import org.eclipse.equinox.p2.query.*;
26
import org.eclipse.osgi.util.NLS;
26
import org.osgi.framework.InvalidSyntaxException;
27
import org.osgi.framework.InvalidSyntaxException;
27
import org.osgi.framework.ServiceReference;
28
import org.osgi.framework.ServiceReference;
28
29
Lines 42-76 Link Here
42
			//ignore can't happen since we write the filter ourselves
43
			//ignore can't happen since we write the filter ourselves
43
		}
44
		}
44
		if (ref == null || ref.size() == 0)
45
		if (ref == null || ref.size() == 0)
45
			throw new IllegalStateException("p2 is not configured properly. No provisioning agent could be found.");
46
			throw new IllegalStateException(Messages.OperationFactory_noAgent);
46
		IProvisioningAgent agent = Activator.getContext().getService(ref.iterator().next());
47
		IProvisioningAgent agent = Activator.getContext().getService(ref.iterator().next());
47
		Activator.getContext().ungetService(ref.iterator().next());
48
		Activator.getContext().ungetService(ref.iterator().next());
48
		return agent;
49
		return agent;
49
	}
50
	}
50
51
51
	private Collection<IInstallableUnit> gatherIUs(ProvisioningContext context, Collection<IVersionedId> ius, IProgressMonitor monitor) {
52
	//Return a list of IUs from the list of versionedIDs originally provided
52
		Collection<IInstallableUnit> unitsToInstall = new ArrayList<IInstallableUnit>(ius.size());
53
	private Collection<IInstallableUnit> gatherIUs(IQueryable<IInstallableUnit> searchContext, Collection<IVersionedId> ius, boolean checkIUs, IProgressMonitor monitor) throws ProvisionException {
54
		Collection<IInstallableUnit> gatheredIUs = new ArrayList<IInstallableUnit>(ius.size());
53
55
54
		for (IVersionedId versionedId : ius) {
56
		for (IVersionedId versionedId : ius) {
55
			if (ius instanceof IInstallableUnit) {
57
			if (!checkIUs && versionedId instanceof IInstallableUnit) {
56
				unitsToInstall.add((IInstallableUnit) versionedId);
58
				gatheredIUs.add((IInstallableUnit) versionedId);
57
				continue;
59
				continue;
58
			}
60
			}
59
61
60
			IQuery<IInstallableUnit> installableUnits = QueryUtil.createIUQuery(versionedId.getId(), versionedId.getVersion());
62
			IQuery<IInstallableUnit> installableUnits = QueryUtil.createIUQuery(versionedId.getId(), versionedId.getVersion());
61
			IQueryResult<IInstallableUnit> matches = context.getMetadata(monitor).query(installableUnits, monitor);
63
			IQueryResult<IInstallableUnit> matches = searchContext.query(installableUnits, monitor);
62
			//			if (ius.isEmpty())
64
			if (matches.isEmpty())
63
			//				Do something smart. like throw an exception
65
				throw new ProvisionException(new Status(IStatus.ERROR, Activator.ID, NLS.bind(Messages.OperationFactory_noIUFound, versionedId)));
66
64
			//Add the first IU
67
			//Add the first IU
65
			Iterator<IInstallableUnit> iuIt = matches.iterator();
68
			Iterator<IInstallableUnit> iuIt = matches.iterator();
66
			unitsToInstall.add(iuIt.next());
69
			gatheredIUs.add(iuIt.next());
67
68
			if (iuIt.hasNext()) {
69
				// TODO
70
				System.out.println("Log the fact that we have a problem");
71
			}
72
		}
70
		}
73
		return unitsToInstall;
71
		return gatheredIUs;
74
	}
72
	}
75
73
76
	private ProvisioningContext createProvisioningContext(Collection<URI> repos, IProvisioningAgent agent) {
74
	private ProvisioningContext createProvisioningContext(Collection<URI> repos, IProvisioningAgent agent) {
Lines 92-102 Link Here
92
	public InstallOperation createInstallOperation(Collection<IVersionedId> toInstall, Collection<URI> repos, IProgressMonitor monitor) throws ProvisionException {
90
	public InstallOperation createInstallOperation(Collection<IVersionedId> toInstall, Collection<URI> repos, IProgressMonitor monitor) throws ProvisionException {
93
		Assert.isNotNull(toInstall);
91
		Assert.isNotNull(toInstall);
94
		IProvisioningAgent agent = getAgent();
92
		IProvisioningAgent agent = getAgent();
93
95
		//add the repos
94
		//add the repos
96
		ProvisioningContext ctx = createProvisioningContext(repos, agent);
95
		ProvisioningContext ctx = createProvisioningContext(repos, agent);
97
96
98
		//find the ius to install and create the operation
97
		//find the ius to install and create the operation
99
		InstallOperation resultingOperation = new InstallOperation(new ProvisioningSession(agent), gatherIUs(ctx, toInstall, monitor));
98
		InstallOperation resultingOperation = new InstallOperation(new ProvisioningSession(agent), gatherIUs(ctx.getMetadata(monitor), toInstall, false, monitor));
100
		resultingOperation.setProvisioningContext(ctx);
99
		resultingOperation.setProvisioningContext(ctx);
101
		resultingOperation.setProfileId(IProfileRegistry.SELF);
100
		resultingOperation.setProfileId(IProfileRegistry.SELF);
102
101
Lines 116-122 Link Here
116
		ProvisioningContext ctx = createProvisioningContext(repos, agent);
115
		ProvisioningContext ctx = createProvisioningContext(repos, agent);
117
116
118
		//find the ius to uninstall and create the operation
117
		//find the ius to uninstall and create the operation
119
		UninstallOperation resultingOperation = new UninstallOperation(new ProvisioningSession(agent), gatherIUs(ctx, toUninstall, monitor));
118
		UninstallOperation resultingOperation = new UninstallOperation(new ProvisioningSession(agent), gatherIUs(listInstalledElements(false, monitor), toUninstall, true, monitor));
120
		resultingOperation.setProvisioningContext(ctx);
119
		resultingOperation.setProvisioningContext(ctx);
121
		resultingOperation.setProfileId(IProfileRegistry.SELF);
120
		resultingOperation.setProfileId(IProfileRegistry.SELF);
122
121
Lines 149-155 Link Here
149
		ProvisioningContext ctx = createProvisioningContext(repos, agent);
148
		ProvisioningContext ctx = createProvisioningContext(repos, agent);
150
149
151
		//find the ius to update and create the operation
150
		//find the ius to update and create the operation
152
		UpdateOperation resultingOperation = new UpdateOperation(new ProvisioningSession(agent), toUpdate == null ? null : gatherIUs(ctx, toUpdate, monitor));
151
		UpdateOperation resultingOperation = new UpdateOperation(new ProvisioningSession(agent), toUpdate == null ? null : gatherIUs(listInstalledElements(false, monitor), toUpdate, false, monitor));
153
		resultingOperation.setProvisioningContext(ctx);
152
		resultingOperation.setProvisioningContext(ctx);
154
		resultingOperation.setProfileId(IProfileRegistry.SELF);
153
		resultingOperation.setProfileId(IProfileRegistry.SELF);
155
154
Lines 161-167 Link Here
161
	 * @param toInstall the elements to install. This can not be null.
160
	 * @param toInstall the elements to install. This can not be null.
162
	 * @param repos the repositories to install the elements from. 
161
	 * @param repos the repositories to install the elements from. 
163
	 * @param monitor the progress monitor
162
	 * @param monitor the progress monitor
164
	 * @return an instance of {@link InstallOperation}.
163
	 * @return an instance of {@link SynchronizeOperation}.
165
	 */
164
	 */
166
	public SynchronizeOperation createSynchronizeOperation(Collection<IVersionedId> toInstall, Collection<URI> repos, IProgressMonitor monitor) throws ProvisionException {
165
	public SynchronizeOperation createSynchronizeOperation(Collection<IVersionedId> toInstall, Collection<URI> repos, IProgressMonitor monitor) throws ProvisionException {
167
		IProvisioningAgent agent = getAgent();
166
		IProvisioningAgent agent = getAgent();
Lines 171-177 Link Here
171
		if (toInstall == null)
170
		if (toInstall == null)
172
			iusToInstall = ctx.getMetadata(monitor).query(QueryUtil.createIUGroupQuery(), monitor).toUnmodifiableSet();
171
			iusToInstall = ctx.getMetadata(monitor).query(QueryUtil.createIUGroupQuery(), monitor).toUnmodifiableSet();
173
		else
172
		else
174
			iusToInstall = gatherIUs(ctx, toInstall, monitor);
173
			iusToInstall = gatherIUs(ctx.getMetadata(monitor), toInstall, false, monitor);
175
174
176
		SynchronizeOperation resultingOperation = new SynchronizeOperation(new ProvisioningSession(agent), iusToInstall);
175
		SynchronizeOperation resultingOperation = new SynchronizeOperation(new ProvisioningSession(agent), iusToInstall);
177
		resultingOperation.setProvisioningContext(ctx);
176
		resultingOperation.setProvisioningContext(ctx);
(-)src/org/eclipse/equinox/p2/tests/ui/operations/OperationFactoryTest.java (+180 lines)
Added Link Here
1
/*******************************************************************************
2
 *  Copyright (c) 2011 Sonatype, Inc. and others.
3
 *  All rights reserved. This program and the accompanying materials
4
 *  are made available under the terms of the Eclipse Public License v1.0
5
 *  which accompanies this distribution, and is available at
6
 *  http://www.eclipse.org/legal/epl-v10.html
7
 * 
8
 *  Contributors:
9
 *     Sonatype, Inc. - initial API and implementation
10
 *******************************************************************************/
11
12
package org.eclipse.equinox.p2.tests.ui.operations;
13
14
import java.lang.reflect.Field;
15
import java.net.URI;
16
import java.util.*;
17
import org.eclipse.core.runtime.IStatus;
18
import org.eclipse.core.runtime.NullProgressMonitor;
19
import org.eclipse.equinox.internal.p2.engine.SimpleProfileRegistry;
20
import org.eclipse.equinox.p2.core.ProvisionException;
21
import org.eclipse.equinox.p2.engine.IProfileRegistry;
22
import org.eclipse.equinox.p2.metadata.*;
23
import org.eclipse.equinox.p2.operations.*;
24
import org.eclipse.equinox.p2.query.IQueryResult;
25
import org.eclipse.equinox.p2.tests.AbstractProvisioningTest;
26
27
public class OperationFactoryTest extends AbstractProvisioningTest {
28
29
	@Override
30
	protected void setUp() throws Exception {
31
		super.setUp();
32
	}
33
34
	public void testCreateInstallOperation() {
35
		OperationFactory of = new OperationFactory();
36
		Collection<IVersionedId> versions = new ArrayList<IVersionedId>();
37
		versions.add(new VersionedId("aBundle", "1.0.0"));
38
		Collection<URI> coll = new ArrayList<URI>();
39
		coll.add(getTestData("Simple repository", "testData/testRepos/simple.1").toURI());
40
		try {
41
			of.createInstallOperation(versions, coll, new NullProgressMonitor());
42
		} catch (ProvisionException e) {
43
			fail("Creation of the install operation failed", e);
44
		}
45
	}
46
47
	public void testCreateInstallOperationWithUnspecifiedVersion() {
48
		OperationFactory of = new OperationFactory();
49
		Collection<IVersionedId> versions = new ArrayList<IVersionedId>();
50
		versions.add(new VersionedId("aBundle", (Version) null));
51
		Collection<URI> coll = new ArrayList<URI>();
52
		coll.add(getTestData("Simple repository", "testData/testRepos/simple.1").toURI());
53
		try {
54
			of.createInstallOperation(versions, coll, new NullProgressMonitor());
55
		} catch (ProvisionException e) {
56
			fail("Creation of the install operation failed", e);
57
		}
58
	}
59
60
	public void testMissingVersionedIdInInstallOperation() {
61
		OperationFactory of = new OperationFactory();
62
		Collection<IVersionedId> versions = new ArrayList<IVersionedId>();
63
		versions.add(new VersionedId("aBundle", "1.0.0"));
64
		Exception exceptionMet = null;
65
		try {
66
			of.createInstallOperation(versions, Collections.<URI> emptyList(), new NullProgressMonitor());
67
		} catch (ProvisionException e) {
68
			exceptionMet = e;
69
		}
70
		assertNotNull("An exception was expected", exceptionMet);
71
	}
72
73
	public void testMissingElementToUninstall() {
74
		createProfileWithOneIU(getUniqueString());
75
76
		//While we are at it, test the installedElements
77
		OperationFactory of = new OperationFactory();
78
		IQueryResult<IInstallableUnit> installedElements = of.listInstalledElements(true, new NullProgressMonitor());
79
		assertEquals(1, installedElements.toSet().size());
80
81
		//Now test various removal scenarios
82
		testUninstall(new VersionedId("aBundle", (Version) null), false);
83
		testUninstall(new VersionedId("aBundle", Version.create("1.0.0")), false);
84
		testUninstall(new VersionedId("aBundle", Version.create("2.0.0")), true);
85
		testUninstall(new VersionedId("missingBundle", (Version) null), true);
86
		testUninstall(installedElements.toSet().iterator().next(), false);
87
		testUninstall(createEclipseIU("doesNotExist"), true);
88
	}
89
90
	private void createProfileWithOneIU(String profileName) {
91
		//create a profile and set it as self
92
		try {
93
			IProfileRegistry profileRegistry = getProfileRegistry();
94
			profileRegistry.addProfile(profileName);
95
			Field selfField = SimpleProfileRegistry.class.getDeclaredField("self"); //$NON-NLS-1$
96
			selfField.setAccessible(true);
97
			previousSelfValue = selfField.get(profileRegistry);
98
			selfField.set(profileRegistry, profileName);
99
		} catch (Exception e) {
100
			fail("Error while setting up uninstall test", e);
101
		}
102
103
		//install something using the install operation
104
		OperationFactory of = new OperationFactory();
105
		Collection<IVersionedId> versions = new ArrayList<IVersionedId>();
106
		versions.add(new VersionedId("aBundle", (Version) null));
107
		Collection<URI> coll = new ArrayList<URI>();
108
		coll.add(getTestData("Simple repository", "testData/testRepos/simple.1").toURI());
109
		try {
110
			InstallOperation iop = of.createInstallOperation(versions, coll, new NullProgressMonitor());
111
			IStatus result = iop.resolveModal(new NullProgressMonitor());
112
			if (result.isOK()) {
113
				iop.getProvisioningJob(new NullProgressMonitor()).runModal(new NullProgressMonitor());
114
			}
115
		} catch (ProvisionException e) {
116
			fail("Creation of the install operation failed", e);
117
		}
118
119
	}
120
121
	private void testUninstall(IVersionedId vid, boolean shouldFail) {
122
		OperationFactory of = new OperationFactory();
123
		Collection<IVersionedId> toRemove = new ArrayList<IVersionedId>();
124
		toRemove.add(vid);
125
		Exception expectedException = null;
126
		try {
127
			of.createUninstallOperation(toRemove, Collections.<URI> emptyList(), new NullProgressMonitor());
128
		} catch (ProvisionException e) {
129
			expectedException = e;
130
		}
131
		if (shouldFail)
132
			assertNotNull(expectedException);
133
		else
134
			assertNull(expectedException);
135
	}
136
137
	public void testUpdateOperation() {
138
		createProfileWithOneIU(getUniqueString());
139
		OperationFactory of = new OperationFactory();
140
		{
141
			Collection<IVersionedId> toUpdate = new ArrayList<IVersionedId>();
142
			toUpdate.add(new VersionedId("doesNotExist", (Version) null));
143
			Collection<URI> repos = new ArrayList<URI>();
144
			repos.add(getTestData("second repository", "testData/testRepos/simple.2").toURI());
145
			Exception expectedException = null;
146
			try {
147
				of.createUpdateOperation(toUpdate, repos, new NullProgressMonitor());
148
			} catch (ProvisionException e) {
149
				expectedException = e;
150
			}
151
			assertNotNull(expectedException);
152
		}
153
154
		{
155
			Collection<IVersionedId> toUpdate = new ArrayList<IVersionedId>();
156
			toUpdate.add(new VersionedId("aBundle", Version.parseVersion("1.0.0")));
157
			Collection<URI> repos = new ArrayList<URI>();
158
			repos.add(getTestData("second repository", "testData/testRepos/simple.2").toURI());
159
			try {
160
				UpdateOperation op = of.createUpdateOperation(toUpdate, repos, new NullProgressMonitor());
161
				op.resolveModal(new NullProgressMonitor());
162
				assertEquals(1, op.getPossibleUpdates().length);
163
			} catch (ProvisionException e) {
164
				fail("Exception not expected", e);
165
			}
166
		}
167
168
		{
169
			Collection<URI> repos = new ArrayList<URI>();
170
			repos.add(getTestData("second repository", "testData/testRepos/simple.2").toURI());
171
			try {
172
				UpdateOperation op = of.createUpdateOperation(null, repos, new NullProgressMonitor());
173
				op.resolveModal(new NullProgressMonitor());
174
				assertEquals(1, op.getPossibleUpdates().length);
175
			} catch (ProvisionException e) {
176
				fail("Exception not expected", e);
177
			}
178
		}
179
	}
180
}

Return to bug 337016