Skip to main content
summaryrefslogtreecommitdiffstats
blob: 1124a8161ff3873f9c7fc11b3b6dd88a2f21104c (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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*******************************************************************************
 *  Copyright (c) 2007, 2012 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.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.equinox.internal.p2.engine.*;
import org.eclipse.equinox.p2.engine.*;
import org.eclipse.equinox.p2.tests.AbstractProvisioningTest;
import org.junit.Test;

/**
 * Simple test of the engine API.
 */
public class PhaseSetTest extends AbstractProvisioningTest {
	//	PauseJob pause = null;

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

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

	public void testNullPhases() {
		try {
			new PhaseSet(null) {
				// empty PhaseSet
			};
		} catch (IllegalArgumentException exepcted) {
			return;
		}
		fail();
	}

	public void testNoTrustCheck() {
		IPhaseSet set1 = PhaseSetFactory.createDefaultPhaseSet();
		IPhaseSet set2 = PhaseSetFactory.createDefaultPhaseSetExcluding(new String[] {PhaseSetFactory.PHASE_CHECK_TRUST});
		assertTrue("1.0", !set1.equals(set2));
	}

	public void testEmptyPhases() {
		IProfile profile = createProfile("PhaseSetTest");
		PhaseSet phaseSet = new PhaseSet(new Phase[] {}) {
			// empty PhaseSet
		};
		InstallableUnitOperand op = new InstallableUnitOperand(createResolvedIU(createIU("iu")), null);
		InstallableUnitOperand[] operands = new InstallableUnitOperand[] {op};

		ProvisioningContext context = new ProvisioningContext(getAgent());
		IStatus result = phaseSet.perform(new EngineSession(null, profile, context), operands, new NullProgressMonitor());
		assertTrue(result.isOK());
	}

	@Test
	public void testPauseNotRunningPhaseSet() {
		PhaseSet set = (PhaseSet) PhaseSetFactory.createDefaultPhaseSet();
		assertFalse("Can pause not running phaseset.", set.pause());
	}

	@Test
	public void testResumeNotPausedPhaseSet() {
		PhaseSet set = (PhaseSet) PhaseSetFactory.createDefaultPhaseSet();
		assertFalse("Can resume not phaused phaseset.", set.resume());
	}

	/*	abstract class PauseJob extends Job {
			public PauseJob(String name) {
				super(name);
			}

			private boolean isPaused = false;
			Job resume = null;

			public boolean isPaused() {
				return isPaused;
			}

			public void setPause(boolean paused) {
				isPaused = paused;
			}
		}

		@Test
		public void testPauseAndResume() throws ProvisionException, OperationCanceledException, InterruptedException {
			URI repoLoc = getTestData("Load test data.", "/testData/pausefeature").toURI();
			final PhaseSet phaseSet = (PhaseSet) PhaseSetFactory.createDefaultPhaseSet();
			pause = new PauseJob("pause") {
				protected IStatus run(IProgressMonitor monitor) {
					if (!phaseSet.pause())
						return new Status(IStatus.ERROR, TestActivator.PI_PROV_TESTS, "pause() failed.");
					try {
						Thread.sleep(5000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					System.out.println(new Date() + " -- paused provisioning.");
					setPause(true);
					resume = new Job("resume") {
						@Override
						protected IStatus run(IProgressMonitor monitor1) {
							pause.setPause(false);
							System.out.println(new Date() + " -- will resume provisioning.");
							if (!phaseSet.resume())
								return new Status(IStatus.ERROR, TestActivator.PI_PROV_TESTS, "resume() failed.");
							return Status.OK_STATUS;
						}
					};
					resume.schedule(10000);
					return Status.OK_STATUS;
				}
			};
			basicTest(repoLoc, phaseSet, pause, QueryUtil.createIUQuery("org.eclipse.equinox.launcher"), 500, IStatus.OK, null);
			assertTrue("Pause job is failed.", pause.getResult().isOK());
			pause.resume.join();
			assertTrue("Resume job is failed.", pause.resume.getResult().isOK());
		}

		private void basicTest(URI repoURI, PhaseSet phaseSet, final PauseJob pauseJob, IQuery<IInstallableUnit> query, final long delay, int expectedCode, IProgressMonitor monitor) throws ProvisionException, InterruptedException {
			class ProvTestListener implements ProvisioningListener {
				boolean hasProvisioningEventAfterPaused = false;
				CountDownLatch latch = new CountDownLatch(1);

				public void notify(EventObject o) {
					if (o instanceof BeginOperationEvent) {
						pauseJob.schedule(delay);
						System.out.println(new Date() + " -- scheduled pause job.");
						return;
					}
					if (o instanceof RepositoryEvent || o instanceof ProfileEvent)
						return;
					System.out.println(new Date() + " -- recive event " + o.getClass().getName());
					if (o instanceof CommitOperationEvent || o instanceof RollbackOperationEvent) {
						latch.countDown();
						pauseJob.cancel();
						return;
					}
					if (pauseJob.isPaused() && !(o instanceof PhaseEvent)) {
						hasProvisioningEventAfterPaused = true;
					}
				}
			}

			ProvTestListener listener = new ProvTestListener();
			getEventBus().addListener(listener);
			try {
				getMetadataRepositoryManager().loadRepository(repoURI, null);
				getArtifactRepositoryManager().loadRepository(repoURI, null);
				doProvisioning(repoURI, phaseSet, query, expectedCode, monitor);
				// make sure the listener handles all event already that are dispatched asynchronously
				listener.latch.await(10, TimeUnit.SECONDS);
				assertFalse("Engine still do provisioning after pausing.", listener.hasProvisioningEventAfterPaused);
				pauseJob.join();
			} finally {
				getEventBus().removeListener(listener);
			}
		}

		private void doProvisioning(URI repoLoc, final PhaseSet phaseSet, IQuery<IInstallableUnit> query, int expectedResult, IProgressMonitor monitor) throws ProvisionException {
			File testFolder = new File(System.getProperty("java.io.tmpdir"), "testProvisioning");
			delete(testFolder);
			testFolder.mkdir();
			final String profileId = "test";
			try {
				ProvisioningContext context = new ProvisioningContext(getAgent());
				context.setArtifactRepositories(new URI[] {repoLoc});
				context.setMetadataRepositories(new URI[] {repoLoc});
				IEngine engine = getEngine();
				// restrict the installation to 'linux & gtk & x86' to match the test repo
				Map props = new HashMap<String, String>();
				props.put(IProfile.PROP_ENVIRONMENTS, "osgi.ws=gtk,osgi.arch=x86,osgi.os=linux");
				props.put(IProfile.PROP_INSTALL_FOLDER, testFolder.getAbsolutePath());
				IProfile profile = createProfile(profileId, props);
				ProfileChangeRequest request = ProfileChangeRequest.createByProfileId(getAgent(), profile.getProfileId());
				IQueryResult<IInstallableUnit> toBeInstalledIUs = getMetadataRepositoryManager().loadRepository(repoLoc, null).query(query, null);
				assertFalse("Test case has problem to find IU to be installed.", toBeInstalledIUs.isEmpty());
				request.addAll(toBeInstalledIUs.toSet());
				IProvisioningPlan plan = getPlanner(getAgent()).getProvisioningPlan(request, context, null);
				assertTrue("Provisioning plan can't be resolved.", plan.getStatus().isOK());
				IStatus status = engine.perform(plan, phaseSet, monitor);
				assertEquals("The reture code of provisioning is not expected.", expectedResult, status.getSeverity());
			} finally {
				delete(testFolder);
				getProfileRegistry().removeProfile(profileId);
				Util.getDownloadCacheRepo(getAgent()).removeAll(new NullProgressMonitor());
			}
		}

		@Test
		public void testPauseAndResumeMoreThanOnce() throws ProvisionException, InterruptedException {
			//		URI repoLoc = URI.create("http://download.eclipse.org/releases/indigo");
			URI repoLoc = getTestData("Load test data.", "/testData/pausefeature").toURI();
			final PhaseSet phaseSet = (PhaseSet) PhaseSetFactory.createDefaultPhaseSet();
			final int threhold = 3;
			class ResumeJob extends Job {

				private PauseJob pauseJob;
				private int count = 0;

				public ResumeJob(String name, PauseJob pauseJob) {
					super(name);
					this.pauseJob = pauseJob;
				}

				@Override
				protected IStatus run(IProgressMonitor monitor) {
					pauseJob.setPause(false);
					System.out.println(new Date() + " -- resume provisioning.");
					if (!phaseSet.resume())
						return new Status(IStatus.INFO, TestActivator.PI_PROV_TESTS, "resume() failed.");
					if (count++ < threhold)
						pauseJob.schedule(10000);
					return Status.OK_STATUS;
				}
			}
			pause = new PauseJob("pause") {
				protected IStatus run(IProgressMonitor monitor) {
					if (!phaseSet.pause())
						return new Status(IStatus.INFO, TestActivator.PI_PROV_TESTS, "pause() failed.");
					try {
						Thread.sleep(5000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					System.out.println(new Date() + " -- paused provisioning.");
					setPause(true);
					if (resume == null)
						resume = new ResumeJob("resume", this);
					resume.schedule(10000);
					return Status.OK_STATUS;
				}
			};

			basicTest(repoLoc, phaseSet, pause, QueryUtil.createLatestQuery(QueryUtil.createIUQuery("org.eclipse.equinox.executable.feature.group")), 3000, IStatus.OK, null);
		}

		@Test
		public void testCancelPausedProvisioing() throws ProvisionException, InterruptedException {
			URI repoLoc = getTestData("Load test data.", "/testData/pausefeature").toURI();
			final PhaseSet phaseSet = (PhaseSet) PhaseSetFactory.createDefaultPhaseSet();
			pause = new PauseJob("pause") {
				protected IStatus run(IProgressMonitor monitor) {
					if (!phaseSet.pause())
						return new Status(IStatus.ERROR, TestActivator.PI_PROV_TESTS, "pause() failed.");
					try {
						Thread.sleep(5000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					System.out.println(new Date() + " -- paused provisioning.");
					// wait seconds
					try {
						Thread.sleep(2000);
					} catch (InterruptedException e) {
						// 
					}
					setPause(true);
					return Status.OK_STATUS;
				}
			};

			basicTest(repoLoc, phaseSet, pause, QueryUtil.createLatestQuery(QueryUtil.createIUQuery("org.eclipse.equinox.executable.feature.group")), 3000, IStatus.CANCEL, new NullProgressMonitor() {
				@Override
				public boolean isCanceled() {
					return pause.isPaused();
				}
			});
		}*/
}

Back to the top