Skip to main content
summaryrefslogtreecommitdiffstats
blob: 381939940d76940db2358dd82e45cbd9f978e99a (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
/*******************************************************************************
 *  Copyright (c) 2015, 2017 SAP SE 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:
 *      SAP SE - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.p2.tests.touchpoint.natives;

import static org.eclipse.equinox.p2.tests.AbstractProvisioningTest.assertOK;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assume.assumeThat;

import java.io.File;
import java.util.*;
import java.util.prefs.Preferences;
import org.eclipse.core.runtime.Platform;
import org.eclipse.equinox.internal.p2.touchpoint.natives.NativeTouchpoint;
import org.eclipse.equinox.internal.p2.touchpoint.natives.actions.ActionConstants;
import org.eclipse.equinox.internal.p2.touchpoint.natives.actions.CheckAndPromptNativePackageWindowsRegistry;
import org.eclipse.equinox.p2.engine.IProfile;
import org.eclipse.equinox.p2.engine.spi.ProvisioningAction;
import org.eclipse.equinox.p2.tests.AbstractProvisioningTest.ProvisioningTestRuleAdapter;
import org.junit.*;

public class CheckAndPromptNativePackageWindowsRegistryTest {

	@BeforeClass
	public static void classSetUp() throws Exception {
		assumeThat("Windows only: uses Windows registry", Platform.getOS(), equalTo(Platform.OS_WIN32));
	}

	@Rule
	public final ProvisioningTestRuleAdapter testHelper = new ProvisioningTestRuleAdapter();

	private Preferences prefsNode;

	@Before
	public void setUp() throws Exception {
		prefsNode = Preferences.userNodeForPackage(getClass());
		prefsNode.clear();
		prefsNode.flush();
	}

	@Test
	public void execute_StringAttribute() throws Exception {
		String attributeName = "attribute";
		String attributeValue = "value";
		prefsNode.put(attributeName, attributeValue);
		prefsNode.flush();

		Map<String, Object> parameters = new HashMap<>();
		NativeTouchpoint touchpoint = createTouchpoint(parameters);
		parameters.put(ActionConstants.PARM_LINUX_DISTRO, CheckAndPromptNativePackageWindowsRegistry.WINDOWS_DISTRO);
		parameters.put(ActionConstants.PARM_LINUX_PACKAGE_NAME, "windows package");
		parameters.put(ActionConstants.PARM_LINUX_PACKAGE_VERSION, "1.0");
		parameters.put(ActionConstants.PARM_WINDOWS_REGISTRY_KEY, registryPath(prefsNode));
		parameters.put(ActionConstants.PARM_WINDOWS_REGISTRY_ATTRIBUTE_NAME, attributeName);
		parameters.put(ActionConstants.PARM_WINDOWS_REGISTRY_ATTRIBUTE_VALUE, attributeValue);
		parameters = Collections.unmodifiableMap(parameters);

		ProvisioningAction action = new CheckAndPromptNativePackageWindowsRegistry();
		action.setTouchpoint(touchpoint);

		assertOK(action.execute(parameters));
		assertEquals(Collections.emptyList(), touchpoint.getPackagesToInstall());
	}

	@Test
	public void execute_StringAttribute_DifferentValues() throws Exception {
		String attributeName = "attribute";
		String attributeValue = "value";
		prefsNode.put(attributeName, attributeValue);
		prefsNode.flush();

		Map<String, Object> parameters = new HashMap<>();
		NativeTouchpoint touchpoint = createTouchpoint(parameters);
		parameters.put(ActionConstants.PARM_LINUX_DISTRO, CheckAndPromptNativePackageWindowsRegistry.WINDOWS_DISTRO);
		parameters.put(ActionConstants.PARM_LINUX_PACKAGE_NAME, "windows package");
		parameters.put(ActionConstants.PARM_LINUX_PACKAGE_VERSION, "1.0");
		parameters.put(ActionConstants.PARM_WINDOWS_REGISTRY_KEY, registryPath(prefsNode));
		parameters.put(ActionConstants.PARM_WINDOWS_REGISTRY_ATTRIBUTE_NAME, attributeName);
		parameters.put(ActionConstants.PARM_WINDOWS_REGISTRY_ATTRIBUTE_VALUE, attributeValue + "_DIFF");
		parameters = Collections.unmodifiableMap(parameters);

		ProvisioningAction action = new CheckAndPromptNativePackageWindowsRegistry();
		action.setTouchpoint(touchpoint);

		assertOK(action.execute(parameters));
		assertEquals(touchpoint.getPackagesToInstall().toString(), 1, touchpoint.getPackagesToInstall().size());
	}

	@Test
	public void execute_IntAttribute() throws Exception {
		String attributeName = "attribute";
		int attributeValue = 1;
		prefsNode.putInt(attributeName, attributeValue);
		prefsNode.flush();

		Map<String, Object> parameters = new HashMap<>();
		NativeTouchpoint touchpoint = createTouchpoint(parameters);
		parameters.put(ActionConstants.PARM_LINUX_DISTRO, CheckAndPromptNativePackageWindowsRegistry.WINDOWS_DISTRO);
		parameters.put(ActionConstants.PARM_LINUX_PACKAGE_NAME, "windows package");
		parameters.put(ActionConstants.PARM_LINUX_PACKAGE_VERSION, "1.0");
		parameters.put(ActionConstants.PARM_WINDOWS_REGISTRY_KEY, registryPath(prefsNode));
		parameters.put(ActionConstants.PARM_WINDOWS_REGISTRY_ATTRIBUTE_NAME, attributeName);
		parameters.put(ActionConstants.PARM_WINDOWS_REGISTRY_ATTRIBUTE_VALUE, String.valueOf(attributeValue));
		parameters = Collections.unmodifiableMap(parameters);

		ProvisioningAction action = new CheckAndPromptNativePackageWindowsRegistry();
		action.setTouchpoint(touchpoint);

		assertOK(action.execute(parameters));
		assertEquals(Collections.emptyList(), touchpoint.getPackagesToInstall());
	}

	@Test
	public void execute_IntAttribute_DifferentLiteralValue() throws Exception {
		String attributeName = "attribute";
		int attributeValue = 1;
		prefsNode.putInt(attributeName, attributeValue);
		prefsNode.flush();

		Map<String, Object> parameters = new HashMap<>();
		NativeTouchpoint touchpoint = createTouchpoint(parameters);
		parameters.put(ActionConstants.PARM_LINUX_DISTRO, CheckAndPromptNativePackageWindowsRegistry.WINDOWS_DISTRO);
		parameters.put(ActionConstants.PARM_LINUX_PACKAGE_NAME, "windows package");
		parameters.put(ActionConstants.PARM_LINUX_PACKAGE_VERSION, "1.0");
		parameters.put(ActionConstants.PARM_WINDOWS_REGISTRY_KEY, registryPath(prefsNode));
		parameters.put(ActionConstants.PARM_WINDOWS_REGISTRY_ATTRIBUTE_NAME, attributeName);
		// we simulate a different literal value
		parameters.put(ActionConstants.PARM_WINDOWS_REGISTRY_ATTRIBUTE_VALUE, "0" + String.valueOf(attributeValue));
		parameters = Collections.unmodifiableMap(parameters);

		ProvisioningAction action = new CheckAndPromptNativePackageWindowsRegistry();
		action.setTouchpoint(touchpoint);

		assertOK(action.execute(parameters));
		assertEquals(Collections.emptyList(), touchpoint.getPackagesToInstall());
	}

	@Test
	public void execute_KeyExistence() throws Exception {
		Map<String, Object> parameters = new HashMap<>();
		NativeTouchpoint touchpoint = createTouchpoint(parameters);
		parameters.put(ActionConstants.PARM_LINUX_DISTRO, CheckAndPromptNativePackageWindowsRegistry.WINDOWS_DISTRO);
		parameters.put(ActionConstants.PARM_LINUX_PACKAGE_NAME, "windows package");
		parameters.put(ActionConstants.PARM_LINUX_PACKAGE_VERSION, "1.0");
		parameters.put(ActionConstants.PARM_WINDOWS_REGISTRY_KEY, registryPath(prefsNode));
		parameters = Collections.unmodifiableMap(parameters);

		ProvisioningAction action = new CheckAndPromptNativePackageWindowsRegistry();
		action.setTouchpoint(touchpoint);

		assertOK(action.execute(parameters));
		assertEquals(Collections.emptyList(), touchpoint.getPackagesToInstall());
	}

	@Test
	public void execute_KeyExistence_DifferentKeys() throws Exception {
		Map<String, Object> parameters = new HashMap<>();
		NativeTouchpoint touchpoint = createTouchpoint(parameters);
		parameters.put(ActionConstants.PARM_LINUX_DISTRO, CheckAndPromptNativePackageWindowsRegistry.WINDOWS_DISTRO);
		parameters.put(ActionConstants.PARM_LINUX_PACKAGE_NAME, "windows package");
		parameters.put(ActionConstants.PARM_LINUX_PACKAGE_VERSION, "1.0");
		parameters.put(ActionConstants.PARM_WINDOWS_REGISTRY_KEY, registryPath(prefsNode) + "\\node");
		parameters = Collections.unmodifiableMap(parameters);

		ProvisioningAction action = new CheckAndPromptNativePackageWindowsRegistry();
		action.setTouchpoint(touchpoint);

		assertOK(action.execute(parameters));
		assertEquals(touchpoint.getPackagesToInstall().toString(), 1, touchpoint.getPackagesToInstall().size());
	}

	private NativeTouchpoint createTouchpoint(Map<String, Object> parameters) {
		Map<String, String> profileProperties = new HashMap<>();
		File installFolder = testHelper.getTempFolder();
		profileProperties.put(IProfile.PROP_INSTALL_FOLDER, installFolder.toString());
		IProfile profile = testHelper.createProfile("test", profileProperties);

		parameters.put(ActionConstants.PARM_PROFILE, profile);
		NativeTouchpoint touchpoint = new NativeTouchpoint();
		touchpoint.initializePhase(null, profile, "test", parameters);
		return touchpoint;
	}

	private static String registryPath(Preferences prefs) {
		String path = prefs.absolutePath().replace('/', '\\');
		String root = prefs.isUserNode() ? "HKCU" : "HKLM";
		return root + "\\Software\\JavaSoft\\Prefs" + path;
	}

}

Back to the top