Skip to main content
summaryrefslogtreecommitdiffstats
blob: 511787aa683d1819c6c9d37f013a8f8a269ea9bb (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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*******************************************************************************
 * Copyright (c) 2008, 2017 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.touchpoint.natives;

import java.io.*;
import java.util.*;
import org.eclipse.equinox.internal.p2.touchpoint.natives.IBackupStore;
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.UnzipAction;
import org.eclipse.equinox.p2.engine.IProfile;
import org.eclipse.equinox.p2.metadata.*;
import org.eclipse.equinox.p2.metadata.MetadataFactory.InstallableUnitDescription;
import org.eclipse.equinox.p2.tests.AbstractProvisioningTest;
import org.eclipse.equinox.spi.p2.publisher.PublisherHelper;

public class UnzipActionTest extends AbstractProvisioningTest {

	private static void writeToFile(File file, String content) throws IOException {
		file.getParentFile().mkdirs();
		file.createNewFile();
		try (FileWriter writer = new FileWriter(file); BufferedWriter out = new BufferedWriter(writer)) {
			out.write(content);
		}
	}

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

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

	public void testExecuteUndo() {
		Map<String, String> profileProperties = new HashMap<>();
		File installFolder = getTempFolder();
		profileProperties.put(IProfile.PROP_INSTALL_FOLDER, installFolder.toString());
		IProfile profile = createProfile("test", profileProperties);

		File zipSource = getTestData("1.0", "/testData/nativeTouchpoint/a.zip");
		File zipTarget = new File(installFolder, "a.zip");
		copy("2.0", zipSource, zipTarget);

		InstallableUnitDescription iuDesc = new MetadataFactory.InstallableUnitDescription();
		iuDesc.setId("test");
		iuDesc.setVersion(DEFAULT_VERSION);
		IArtifactKey key = PublisherHelper.createBinaryArtifactKey("test", DEFAULT_VERSION);
		iuDesc.setArtifacts(new IArtifactKey[] {key});
		iuDesc.setTouchpointType(PublisherHelper.TOUCHPOINT_NATIVE);
		IInstallableUnit iu = MetadataFactory.createInstallableUnit(iuDesc);

		Map<String, Object> parameters = new HashMap<>();
		parameters.put(ActionConstants.PARM_PROFILE, profile);
		parameters.put("iu", iu);
		parameters.put(ActionConstants.PARM_PROFILE, profile);
		NativeTouchpoint touchpoint = new NativeTouchpoint();
		touchpoint.initializePhase(null, profile, "test", parameters);

		parameters.put(ActionConstants.PARM_SOURCE, zipTarget.getAbsolutePath());
		parameters.put(ActionConstants.PARM_TARGET, installFolder.getAbsolutePath());
		parameters = Collections.unmodifiableMap(parameters);

		File aTxt = new File(installFolder, "a.txt");
		assertFalse(aTxt.exists());

		UnzipAction action = new UnzipAction();
		action.execute(parameters);
		assertTrue(aTxt.exists());
		// does nothing so should not alter parameters
		action.undo(parameters);
		assertFalse(aTxt.exists());
	}

	public void testExecuteUndoBackup() {
		Map<String, String> profileProperties = new HashMap<>();
		File installFolder = getTempFolder();
		profileProperties.put(IProfile.PROP_INSTALL_FOLDER, installFolder.toString());
		IProfile profile = createProfile("testExecuteUndoBackup", profileProperties);

		File zipSource = getTestData("1.0", "/testData/nativeTouchpoint/a.zip");
		File zipTarget = new File(installFolder, "a.zip");
		copy("2.0", zipSource, zipTarget);

		InstallableUnitDescription iuDesc = new MetadataFactory.InstallableUnitDescription();
		iuDesc.setId("testExecuteUndoBackup");
		iuDesc.setVersion(DEFAULT_VERSION);
		IArtifactKey key = PublisherHelper.createBinaryArtifactKey("testExecuteUndoBackup", DEFAULT_VERSION);
		iuDesc.setArtifacts(new IArtifactKey[] {key});
		iuDesc.setTouchpointType(PublisherHelper.TOUCHPOINT_NATIVE);
		IInstallableUnit iu = MetadataFactory.createInstallableUnit(iuDesc);

		Map<String, Object> parameters = new HashMap<>();
		parameters.put(ActionConstants.PARM_PROFILE, profile);
		parameters.put("iu", iu);
		parameters.put(ActionConstants.PARM_PROFILE, profile);
		NativeTouchpoint touchpoint = new NativeTouchpoint();
		touchpoint.initializePhase(null, profile, "testExecuteUndoBackup", parameters);

		parameters.put(ActionConstants.PARM_SOURCE, zipTarget.getAbsolutePath());
		parameters.put(ActionConstants.PARM_TARGET, installFolder.getAbsolutePath());
		parameters = Collections.unmodifiableMap(parameters);

		File aTxt = new File(installFolder, "a.txt");
		try {
			writeToFile(aTxt, "ORIGINAL-A");
		} catch (IOException e) {
			fail("Can not write to aTxt");
		}
		assertTrue(aTxt.exists());

		UnzipAction action = new UnzipAction();
		action.execute(parameters);
		assertTrue(aTxt.exists());
		assertFileContent("Should contain text 'nothing'", aTxt, "nothing");
		// does nothing so should not alter parameters
		action.undo(parameters);
		IBackupStore backup = (IBackupStore) parameters.get(NativeTouchpoint.PARM_BACKUP);
		try {
			backup.restore();
		} catch (IOException e) {
			fail("Restore of backup failed", e);
		}
		assertFileContent("Should contain text 'ORIGINAL-A'", aTxt, "ORIGINAL-A");

		backup.discard();
	}

	/**
	 * Tests executing and undoing an unzip action when the profile
	 * id contains characters that are not valid in file names. See bug 274182.
	 */
	public void testUndoBackUpWithSymbolsInProfileId() {
		Map<String, String> profileProperties = new HashMap<>();
		File installFolder = getTempFolder();
		profileProperties.put(IProfile.PROP_INSTALL_FOLDER, installFolder.toString());
		final String profileId = "Test:With\\Sym/bols";
		IProfile profile = createProfile(profileId, profileProperties);

		File zipSource = getTestData("1.0", "/testData/nativeTouchpoint/a.zip");
		File zipTarget = new File(installFolder, "a.zip");
		copy("2.0", zipSource, zipTarget);

		InstallableUnitDescription iuDesc = new MetadataFactory.InstallableUnitDescription();
		iuDesc.setId(profileId);
		iuDesc.setVersion(DEFAULT_VERSION);
		IArtifactKey key = PublisherHelper.createBinaryArtifactKey(profileId, DEFAULT_VERSION);
		iuDesc.setArtifacts(new IArtifactKey[] {key});
		iuDesc.setTouchpointType(PublisherHelper.TOUCHPOINT_NATIVE);
		IInstallableUnit iu = MetadataFactory.createInstallableUnit(iuDesc);

		Map<String, Object> parameters = new HashMap<>();
		parameters.put(ActionConstants.PARM_PROFILE, profile);
		parameters.put("iu", iu);
		parameters.put(ActionConstants.PARM_PROFILE, profile);
		NativeTouchpoint touchpoint = new NativeTouchpoint();
		touchpoint.initializePhase(null, profile, profileId, parameters);

		parameters.put(ActionConstants.PARM_SOURCE, zipTarget.getAbsolutePath());
		parameters.put(ActionConstants.PARM_TARGET, installFolder.getAbsolutePath());
		parameters = Collections.unmodifiableMap(parameters);

		File aTxt = new File(installFolder, "a.txt");
		try {
			writeToFile(aTxt, "ORIGINAL-A");
		} catch (IOException e) {
			fail("Can not write to aTxt");
		}
		assertTrue(aTxt.exists());

		UnzipAction action = new UnzipAction();
		action.execute(parameters);
		// does nothing so should not alter parameters
		action.undo(parameters);
		IBackupStore backup = (IBackupStore) parameters.get(NativeTouchpoint.PARM_BACKUP);
		try {
			backup.restore();
		} catch (IOException e) {
			fail("Restore of backup failed", e);
		}
		assertFileContent("Should contain text 'ORIGINAL-A'", aTxt, "ORIGINAL-A");

		backup.discard();
	}

	/**
	 * Test that when a path is used only files from that path down are unzipped to target as well as undo works.
	 */
	public void testPath() {
		String a = "a.txt";
		String b = "foo/b.txt";
		String c = "foo/bar/car/c.txt";
		String b1 = "b.txt";
		String c1 = "bar/car/c.txt";
		String c2 = "car/c.txt";

		{
			Map<String, String> parameters = new HashMap<>();
			parameters.put(ActionConstants.PARM_PATH, "foo");
			testUnzip(parameters, getTempFolder(), new String[] {b1, c1}, new String[] {a, b, c});
		}
		{
			Map<String, String> parameters = new HashMap<>();
			parameters.put(ActionConstants.PARM_PATH, "foo/");
			testUnzip(parameters, getTempFolder(), new String[] {b1, c1}, new String[] {a, b, c});
		}
		{
			Map<String, String> parameters = new HashMap<>();
			parameters.put(ActionConstants.PARM_PATH, "**/bar");
			testUnzip(parameters, getTempFolder(), new String[] {c2}, new String[] {a, b, c, b1});
		}
	}

	/**
	 * Tests that only the files specified by inclusion path are unzipped as well as undo works.
	 */
	public void testInclusion() {
		String a = "a.txt";
		String b = "foo/b.txt";
		String c = "foo/bar/car/c.txt";

		// full path
		{
			Map<String, String> parameters = new HashMap<>();
			parameters.put(ActionConstants.PARM_INCLUDE, "foo/b.txt");
			testUnzip(parameters, getTempFolder(), new String[] {b}, new String[] {a, c});
		}
		// wildcarded path
		{
			Map<String, String> parameters = new HashMap<>();
			parameters.put(ActionConstants.PARM_INCLUDE, "*/b.txt");
			testUnzip(parameters, getTempFolder(), new String[] {b}, new String[] {a, c});
		}
		// subdir wildcarded path
		{
			Map<String, String> parameters = new HashMap<>();
			parameters.put(ActionConstants.PARM_INCLUDE, "**/c.txt");
			testUnzip(parameters, getTempFolder(), new String[] {c}, new String[] {a, b});
		}
	}

	/**
	 * Tests that only the files specified by exclusion path are not unzipped as well as undo works.
	 */
	public void testExclusion() {
		String a = "a.txt";
		String b = "foo/b.txt";
		String c = "foo/bar/car/c.txt";

		// full path
		{
			Map<String, String> parameters = new HashMap<>();
			parameters.put(ActionConstants.PARM_EXCLUDE, "foo/b.txt");
			testUnzip(parameters, getTempFolder(), new String[] {a, c}, new String[] {b});
		}
		// wildcarded path
		{
			Map<String, String> parameters = new HashMap<>();
			parameters.put(ActionConstants.PARM_EXCLUDE, "*/b.txt");
			testUnzip(parameters, getTempFolder(), new String[] {a, c}, new String[] {b});
		}
		// subdir wildcarded path
		{
			Map<String, String> parameters = new HashMap<>();
			parameters.put(ActionConstants.PARM_EXCLUDE, "**/c.txt");
			testUnzip(parameters, getTempFolder(), new String[] {a, b}, new String[] {c});
		}
	}

	/**
	 * Tests that only the files specified by inclusion path and not in exclusion path are  unzipped as well as undo works.
	 */
	public void testInclusionAndExclusion() {
		String a = "a.txt";
		String b = "foo/b.txt";
		String c = "foo/bar/car/c.txt";

		Map<String, String> parameters = new HashMap<>();
		parameters.put(ActionConstants.PARM_INCLUDE, "*.txt");
		parameters.put(ActionConstants.PARM_EXCLUDE, "**/c.txt");
		testUnzip(parameters, getTempFolder(), new String[] {a, b}, new String[] {c});
	}

	private void testUnzip(Map<String, String> params, File installFolder, String[] shoudlExistNames, String[] shoudlNotExistNames) {

		ArrayList<File> shoudlExist = new ArrayList<>();
		ArrayList<File> shoudlNotExist = new ArrayList<>();

		// first check that are no files in install folder
		for (String fileName : shoudlExistNames) {
			File file = new File(installFolder, fileName);
			shoudlExist.add(file);
			assertFalse("File " + file.getPath() + " should not exist", file.exists());
		}
		for (String fileName : shoudlNotExistNames) {
			File file = new File(installFolder, fileName);
			shoudlNotExist.add(file);
			assertFalse("File " + file.getPath() + " should not exist", file.exists());
		}

		Map<String, String> profileProperties = new HashMap<>();
		profileProperties.put(IProfile.PROP_INSTALL_FOLDER, installFolder.toString());
		IProfile profile = createProfile("test", profileProperties);

		File zipSource = getTestData("1.0", "/testData/nativeTouchpoint/a.dir.zip");
		File zipTarget = new File(installFolder, "a.dir.zip");
		copy("2.0", zipSource, zipTarget);

		InstallableUnitDescription iuDesc = new MetadataFactory.InstallableUnitDescription();
		iuDesc.setId("test");
		iuDesc.setVersion(DEFAULT_VERSION);
		IArtifactKey key = PublisherHelper.createBinaryArtifactKey("test", DEFAULT_VERSION);
		iuDesc.setArtifacts(new IArtifactKey[] {key});
		iuDesc.setTouchpointType(PublisherHelper.TOUCHPOINT_NATIVE);
		IInstallableUnit iu = MetadataFactory.createInstallableUnit(iuDesc);

		Map<String, Object> parameters = new HashMap<>();
		parameters.put(ActionConstants.PARM_PROFILE, profile);
		parameters.put("iu", iu);
		parameters.put(ActionConstants.PARM_PROFILE, profile);
		NativeTouchpoint touchpoint = new NativeTouchpoint();
		touchpoint.initializePhase(null, profile, "test", parameters);

		parameters.put(ActionConstants.PARM_SOURCE, zipTarget.getAbsolutePath());
		parameters.put(ActionConstants.PARM_TARGET, installFolder.getAbsolutePath());
		parameters.putAll(params);
		parameters = Collections.unmodifiableMap(parameters);

		UnzipAction action = new UnzipAction();
		action.execute(parameters);
		for (File file : shoudlExist) {
			assertTrue("File " + file.getPath() + " should exist", file.exists());
		}
		for (File file : shoudlNotExist) {
			assertFalse("File " + file.getPath() + " should not exist", file.exists());
		}

		// does nothing so should not alter parameters
		action.undo(parameters);
		// check that undo removed all files
		for (File file : shoudlExist) {
			assertFalse("File " + file.getPath() + " should not exist", file.exists());
		}
		for (File file : shoudlNotExist) {
			assertFalse("File " + file.getPath() + " should not exist", file.exists());
		}
	}
}

Back to the top