Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 09f20a92445f64537041d8ac0d0f957fd47c0049 (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
/*******************************************************************************
 * Copyright (c) 2009 Cloudsmith Inc. 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:
 *     Cloudsmith Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.p2.tests.touchpoint.natives;

import java.io.File;
import java.util.*;
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.CopyAction;
import org.eclipse.equinox.internal.provisional.p2.metadata.MetadataFactory;
import org.eclipse.equinox.internal.provisional.p2.metadata.MetadataFactory.InstallableUnitDescription;
import org.eclipse.equinox.p2.engine.IProfile;
import org.eclipse.equinox.p2.engine.InstallableUnitOperand;
import org.eclipse.equinox.p2.metadata.IArtifactKey;
import org.eclipse.equinox.p2.metadata.IInstallableUnit;
import org.eclipse.equinox.p2.tests.AbstractProvisioningTest;
import org.eclipse.equinox.spi.p2.publisher.PublisherHelper;

public class CopyActionTest extends AbstractProvisioningTest {

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

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

	public void testExecuteUndo() {
		Map parameters = createParameters("/testData/nativeTouchpoint/aFolder/a.txt", "a.txt", false);
		Map safeParameters = Collections.unmodifiableMap(parameters);

		CopyAction action = new CopyAction();
		action.execute(safeParameters);

		// Verify that the right file was copied
		File target = new File((String) parameters.get(ActionConstants.PARM_COPY_TARGET));
		assertFileContent("copied content", target, "A");

		// does nothing so should not alter parameters
		action.undo(safeParameters);
		assertFalse("Target should be removed after undo", target.exists());
	}

	public void testCopyDirectory() {
		Map parameters = createParameters("/testData/nativeTouchpoint/aFolder/", "aFolder", false);
		Map safeParameters = Collections.unmodifiableMap(parameters);

		CopyAction action = new CopyAction();
		action.execute(safeParameters);

		// Verify that the right files was copied
		File target = new File((String) parameters.get(ActionConstants.PARM_COPY_TARGET));
		assertFileContent("copied content A", new File(target, "a.txt"), "A");
		assertFileContent("copied content B", new File(target, "b.txt"), "B");

		// does nothing so should not alter parameters
		action.undo(safeParameters);
		assertFalse("Target should be removed after undo", target.exists());
	}

	public void testMergeDirectory() {
		Map parameters1 = createParameters("/testData/nativeTouchpoint/xFolder/", "aFolder", true);
		Map safeParameters1 = Collections.unmodifiableMap(parameters1);

		CopyAction action1 = new CopyAction();
		action1.execute(safeParameters1);

		// Verify that the right files was copied
		File target = new File((String) parameters1.get(ActionConstants.PARM_COPY_TARGET));
		assertFileContent("copied content X", new File(target, "x.txt"), "X");
		assertFileContent("copied content Y", new File(target, "y.txt"), "Y");

		Map parameters2 = new HashMap();
		parameters2.putAll(parameters1);
		parameters2.put(ActionConstants.PARM_COPY_SOURCE, getTestData("get folder A", "/testData/nativeTouchpoint/aFolder/").getAbsolutePath());
		Map safeParameters2 = Collections.unmodifiableMap(parameters2);

		CopyAction action2 = new CopyAction();
		action2.execute(safeParameters2);
		assertFileContent("copied content A", new File(target, "a.txt"), "A");
		assertFileContent("copied content B", new File(target, "b.txt"), "B");

		// undo copy of x and y
		action1.undo(safeParameters1);
		assertTrue("Target should exist after undo", target.exists());
		File tmp = new File(target, "x.txt");
		assertFalse("File x should not exist", tmp.exists());
		tmp = new File(target, "y.txt");
		assertFalse("File y should not exist", tmp.exists());
		assertFileContent("copied content A", new File(target, "a.txt"), "A");
		assertFileContent("copied content B", new File(target, "b.txt"), "B");

		// undo copy of a and b
		action2.undo(safeParameters2);
		assertFalse("Target should not exist after undo", target.exists());
	}

	public void testMergeOverwrite() {
		Map parameters1 = createParameters("/testData/nativeTouchpoint/bcFolder/", "aFolder", true);
		Map safeParameters1 = Collections.unmodifiableMap(parameters1);

		CopyAction action1 = new CopyAction();
		action1.execute(safeParameters1);

		// Verify that the right file was copied (a b.txt with a C in it [sic])
		File target = new File((String) parameters1.get(ActionConstants.PARM_COPY_TARGET));
		assertFileContent("copied content C", new File(target, "b.txt"), "C"); // [sic]

		Map parameters2 = new HashMap();
		parameters2.putAll(parameters1);
		parameters2.put(ActionConstants.PARM_COPY_SOURCE, getTestData("get folder A", "/testData/nativeTouchpoint/aFolder/").getAbsolutePath());
		Map safeParameters2 = Collections.unmodifiableMap(parameters2);

		CopyAction action2 = new CopyAction();
		action2.execute(safeParameters2);
		assertFileContent("copied content A", new File(target, "a.txt"), "A");
		assertFileContent("copied content B", new File(target, "b.txt"), "B");

		// undo copy of a and b
		action2.undo(safeParameters2);
		assertFalse("Target should not exist after undo", target.exists());
	}

	public void testBlockedMergeOverwrite() {
		Map parameters1 = createParameters("/testData/nativeTouchpoint/bcFolder/", "aFolder", false);
		Map safeParameters1 = Collections.unmodifiableMap(parameters1);

		CopyAction action1 = new CopyAction();
		action1.execute(safeParameters1);

		// Verify that the right file was copied (a b.txt with a C in it [sic])
		File target = new File((String) parameters1.get(ActionConstants.PARM_COPY_TARGET));
		assertFileContent("copied content B", new File(target, "b.txt"), "C"); // [sic]

		Map parameters2 = new HashMap();
		parameters2.putAll(parameters1);
		parameters2.put(ActionConstants.PARM_COPY_SOURCE, getTestData("get folder A", "/testData/nativeTouchpoint/aFolder/").getAbsolutePath());
		Map safeParameters2 = Collections.unmodifiableMap(parameters2);

		CopyAction action2 = new CopyAction();
		assertFalse("Overwrite of b.txt should not succeed", action2.execute(safeParameters2).isOK());
		assertFileContent("copied content B", new File(target, "b.txt"), "C"); // [sic]

	}

	public void testOverwrite() {
		Map parameters = createParameters("/testData/nativeTouchpoint/aFolder/a.txt", "a.txt", true);
		Map safeParameters = Collections.unmodifiableMap(parameters);

		File source = new File((String) parameters.get(ActionConstants.PARM_COPY_SOURCE));
		File target = new File((String) parameters.get(ActionConstants.PARM_COPY_TARGET));

		// test an overwrite - by first copying the b file
		copy("2.0", getTestData("1.0", "/testData/nativeTouchpoint/aFolder/b.txt"), target);

		CopyAction action = new CopyAction();
		action.execute(safeParameters);
		// Verify that the right file was copied
		assertFileContent("copied content", target, "A");
		// and that we did nothing bad to the source
		assertFileContent("source content", source, "A");

		assertTrue("copy action status", action.undo(safeParameters).isOK());
		assertFalse("Target should be removed after undo", target.exists());
	}

	public void testBlockedOverwrite() {
		Map parameters = createParameters("/testData/nativeTouchpoint/aFolder/a.txt", "a.txt", false);
		Map safeParameters = Collections.unmodifiableMap(parameters);

		File source = new File((String) parameters.get(ActionConstants.PARM_COPY_SOURCE));
		File target = new File((String) parameters.get(ActionConstants.PARM_COPY_TARGET));

		// test an overwrite - by first copying the b file
		copy("2.0", getTestData("1.0", "/testData/nativeTouchpoint/aFolder/b.txt"), target);

		CopyAction action = new CopyAction();
		assertFalse("copy action status", action.execute(safeParameters).isOK());

		// Verify that nothing was copied
		assertFileContent("original content", target, "B");
		// and that we did nothing bad to the source
		assertFileContent("source content", source, "A");

		// there is nothing to undo - the B file should still be there
		action.undo(safeParameters);
		assertTrue("Target should remain after undo", target.exists());
		assertFileContent("original content", target, "B");
	}

	/*
	 * TODO: testing of the following
	 * - copy of directory - check that it merges
	 * - copy of directory with overwrite false/true
	 */
	private Map createParameters(String sourceName, String targetName, boolean overwrite) {
		Properties profileProperties = new Properties();
		File installFolder = getTempFolder();
		profileProperties.setProperty(IProfile.PROP_INSTALL_FOLDER, installFolder.toString());
		IProfile profile = createProfile("test", profileProperties);

		File source = getTestData("1.0", sourceName);
		File target = new File(installFolder, targetName);

		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 parameters = new HashMap();
		parameters.put(ActionConstants.PARM_PROFILE, profile);
		InstallableUnitOperand operand = new InstallableUnitOperand(null, iu);
		parameters.put("iu", operand.second());
		parameters.put(ActionConstants.PARM_OPERAND, operand);
		parameters.put(ActionConstants.PARM_PROFILE, profile);

		NativeTouchpoint touchpoint = new NativeTouchpoint();
		touchpoint.initializePhase(null, profile, "test", parameters);

		parameters.put(ActionConstants.PARM_COPY_SOURCE, source.getAbsolutePath());
		parameters.put(ActionConstants.PARM_COPY_TARGET, target.getAbsolutePath());
		parameters.put(ActionConstants.PARM_COPY_OVERWRITE, Boolean.toString(overwrite));
		return parameters;
	}

}

Back to the top