Skip to main content
summaryrefslogtreecommitdiffstats
blob: 69af5a8a7ae584ecb90781619cd8b2ea940416ac (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
/*******************************************************************************
 * Copyright (c) 2012, 2017 Landmark Graphics Corporation
 * 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:
 *     Landmark Graphics Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.p2.tests.engine;

import java.util.*;
import org.eclipse.core.runtime.*;
import org.eclipse.equinox.p2.engine.*;
import org.eclipse.equinox.p2.engine.spi.ProvisioningAction;
import org.eclipse.equinox.p2.engine.spi.Value;
import org.eclipse.equinox.p2.metadata.*;
import org.eclipse.equinox.p2.metadata.MetadataFactory.InstallableUnitDescription;
import org.eclipse.equinox.p2.tests.AbstractProvisioningTest;

public class VariableTest extends AbstractProvisioningTest {

	private IInstallableUnit createIUWithVariable() {
		InstallableUnitDescription description = new MetadataFactory.InstallableUnitDescription();
		description.setId("artifactWithZip");
		description.setVersion(Version.create("1.0.0"));
		Map touchpointData = new HashMap();
		touchpointData.put("install", "test.actionForVariableTesting( arg1: expectedValue ); test.actionForVariableTesting ( arg1: ${lastResult} ); test.actionForVariableTesting ( arg1: ${lastResult} )");

		description.addTouchpointData(MetadataFactory.createTouchpointData(touchpointData));

		return MetadataFactory.createInstallableUnit(description);
	}

	public void testVariable() {
		Action.reinitForNextTest();
		Map properties = new HashMap();
		properties.put(IProfile.PROP_INSTALL_FOLDER, getTempFolder().getAbsolutePath());
		IProfile profile = createProfile("testVariable1", properties);

		Iterator ius = getInstallableUnits(profile);
		assertFalse(ius.hasNext());

		IProvisioningPlan plan = getEngine().createPlan(profile, null);
		plan.addInstallableUnit(createIUWithVariable());
		IStatus result = getEngine().perform(plan, PhaseSetFactory.createPhaseSetIncluding(new String[] {PhaseSetFactory.PHASE_INSTALL}), new NullProgressMonitor());
		assertOK(result);
		assertEquals("expectedValueaaa", Action.result);
	}

	public void testUndo() {
		Action.reinitForNextTest();
		Action.failMode = true;
		Map properties = new HashMap();
		properties.put(IProfile.PROP_INSTALL_FOLDER, getTempFolder().getAbsolutePath());
		IProfile profile = createProfile("testVariable2", properties);

		Iterator ius = getInstallableUnits(profile);
		assertFalse(ius.hasNext());

		IProvisioningPlan plan = getEngine().createPlan(profile, null);
		plan.addInstallableUnit(createIUWithVariable());
		IStatus result = getEngine().perform(plan, PhaseSetFactory.createPhaseSetIncluding(new String[] {PhaseSetFactory.PHASE_INSTALL}), new NullProgressMonitor());
		assertNotOK(result);
		assertEquals(3, Action.inputValues.size());
		assertEquals(3, Action.undoValues.size());

		//The undo values should be the same than the input values
		assertEquals(Action.inputValues.get(0), Action.undoValues.get(2));
		assertEquals(Action.inputValues.get(1), Action.undoValues.get(1));
		assertEquals(Action.inputValues.get(2), Action.undoValues.get(0));
	}

	public static class Action extends ProvisioningAction {
		public static String result;
		public static boolean failMode;
		private static int count = 0;
		private static final int failAt = 3;

		public static ArrayList<String> inputValues = new ArrayList<>();
		public static ArrayList<String> undoValues = new ArrayList<>();

		public static void reinitForNextTest() {
			inputValues = new ArrayList<>();
			undoValues = new ArrayList<>();
			failMode = false;
			count = 0;
		}

		@Override
		public IStatus execute(Map<String, Object> parameters) {
			inputValues.add((String) parameters.get("arg1"));
			System.out.println(this.hashCode());
			System.out.println((String) parameters.get("arg1"));
			count++;
			if (failMode && count == failAt)
				throw new RuntimeException("GENERATED Exception");
			result = ((String) parameters.get("arg1")) + "a";

			return Status.OK_STATUS;
		}

		@Override
		public IStatus undo(Map<String, Object> parameters) {
			undoValues.add((String) parameters.get("arg1"));
			System.out.println("undo arg --> " + (String) parameters.get("arg1"));
			return Status.OK_STATUS;
		}

		@Override
		public Value<String> getResult() {
			return new Value<>(result);
		}
	}
}

Back to the top