|
Added
Link Here
|
| 1 |
package org.eclipse.equinox.p2.tests.touchpoint.natives; |
| 2 |
|
| 3 |
import java.io.*; |
| 4 |
import java.util.*; |
| 5 |
import org.eclipse.equinox.internal.p2.touchpoint.natives.NativeTouchpoint; |
| 6 |
import org.eclipse.equinox.internal.p2.touchpoint.natives.actions.ActionConstants; |
| 7 |
import org.eclipse.equinox.internal.p2.touchpoint.natives.actions.CopyAction; |
| 8 |
import org.eclipse.equinox.internal.provisional.p2.engine.*; |
| 9 |
import org.eclipse.equinox.internal.provisional.p2.metadata.*; |
| 10 |
import org.eclipse.equinox.internal.provisional.p2.metadata.MetadataFactory.InstallableUnitDescription; |
| 11 |
import org.eclipse.equinox.p2.tests.AbstractProvisioningTest; |
| 12 |
import org.eclipse.equinox.spi.p2.publisher.PublisherHelper; |
| 13 |
|
| 14 |
public class CopyActionTest extends AbstractProvisioningTest { |
| 15 |
|
| 16 |
public CopyActionTest(String name) { |
| 17 |
super(name); |
| 18 |
} |
| 19 |
|
| 20 |
public CopyActionTest() { |
| 21 |
super(""); |
| 22 |
} |
| 23 |
|
| 24 |
public void testExecuteUndo() { |
| 25 |
Map parameters = createParameters("/testData/nativeTouchpoint/aFolder/a.txt", "a.txt", false); |
| 26 |
Map safeParameters = Collections.unmodifiableMap(parameters); |
| 27 |
|
| 28 |
CopyAction action = new CopyAction(); |
| 29 |
action.execute(safeParameters); |
| 30 |
|
| 31 |
// Verify that the right file was copied |
| 32 |
File target = new File((String) parameters.get(ActionConstants.PARM_COPY_TARGET)); |
| 33 |
assertFileContent("copied content", target, "A"); |
| 34 |
|
| 35 |
// does nothing so should not alter parameters |
| 36 |
action.undo(safeParameters); |
| 37 |
assertFalse("Target should be removed after undo", target.exists()); |
| 38 |
} |
| 39 |
|
| 40 |
public void testCopyDirectory() { |
| 41 |
Map parameters = createParameters("/testData/nativeTouchpoint/aFolder/", "aFolder", false); |
| 42 |
Map safeParameters = Collections.unmodifiableMap(parameters); |
| 43 |
|
| 44 |
CopyAction action = new CopyAction(); |
| 45 |
action.execute(safeParameters); |
| 46 |
|
| 47 |
// Verify that the right files was copied |
| 48 |
File target = new File((String) parameters.get(ActionConstants.PARM_COPY_TARGET)); |
| 49 |
assertFileContent("copied content A", new File(target, "a.txt"), "A"); |
| 50 |
assertFileContent("copied content B", new File(target, "b.txt"), "B"); |
| 51 |
|
| 52 |
// does nothing so should not alter parameters |
| 53 |
action.undo(safeParameters); |
| 54 |
assertFalse("Target should be removed after undo", target.exists()); |
| 55 |
} |
| 56 |
|
| 57 |
public void testMergeDirectory() { |
| 58 |
Map parameters1 = createParameters("/testData/nativeTouchpoint/xFolder/", "aFolder", true); |
| 59 |
Map safeParameters1 = Collections.unmodifiableMap(parameters1); |
| 60 |
|
| 61 |
CopyAction action1 = new CopyAction(); |
| 62 |
action1.execute(safeParameters1); |
| 63 |
|
| 64 |
// Verify that the right files was copied |
| 65 |
File target = new File((String) parameters1.get(ActionConstants.PARM_COPY_TARGET)); |
| 66 |
assertFileContent("copied content X", new File(target, "x.txt"), "X"); |
| 67 |
assertFileContent("copied content Y", new File(target, "y.txt"), "Y"); |
| 68 |
|
| 69 |
Map parameters2 = new HashMap(); |
| 70 |
parameters2.putAll(parameters1); |
| 71 |
parameters2.put(ActionConstants.PARM_COPY_SOURCE, getTestData("get folder A", "/testData/nativeTouchpoint/aFolder/").getAbsolutePath()); |
| 72 |
Map safeParameters2 = Collections.unmodifiableMap(parameters2); |
| 73 |
|
| 74 |
CopyAction action2 = new CopyAction(); |
| 75 |
action2.execute(safeParameters2); |
| 76 |
assertFileContent("copied content A", new File(target, "a.txt"), "A"); |
| 77 |
assertFileContent("copied content B", new File(target, "b.txt"), "B"); |
| 78 |
|
| 79 |
// undo copy of x and y |
| 80 |
action1.undo(safeParameters1); |
| 81 |
assertTrue("Target should exist after undo", target.exists()); |
| 82 |
File tmp = new File(target, "x.txt"); |
| 83 |
assertFalse("File x should not exist", tmp.exists()); |
| 84 |
tmp = new File(target, "y.txt"); |
| 85 |
assertFalse("File y should not exist", tmp.exists()); |
| 86 |
assertFileContent("copied content A", new File(target, "a.txt"), "A"); |
| 87 |
assertFileContent("copied content B", new File(target, "b.txt"), "B"); |
| 88 |
|
| 89 |
// undo copy of a and b |
| 90 |
action2.undo(safeParameters2); |
| 91 |
assertFalse("Target should not exist after undo", target.exists()); |
| 92 |
} |
| 93 |
|
| 94 |
public void testMergeOverwrite() { |
| 95 |
Map parameters1 = createParameters("/testData/nativeTouchpoint/bcFolder/", "aFolder", true); |
| 96 |
Map safeParameters1 = Collections.unmodifiableMap(parameters1); |
| 97 |
|
| 98 |
CopyAction action1 = new CopyAction(); |
| 99 |
action1.execute(safeParameters1); |
| 100 |
|
| 101 |
// Verify that the right file was copied (a b.txt with a C in it [sic]) |
| 102 |
File target = new File((String) parameters1.get(ActionConstants.PARM_COPY_TARGET)); |
| 103 |
assertFileContent("copied content C", new File(target, "b.txt"), "C"); // [sic] |
| 104 |
|
| 105 |
Map parameters2 = new HashMap(); |
| 106 |
parameters2.putAll(parameters1); |
| 107 |
parameters2.put(ActionConstants.PARM_COPY_SOURCE, getTestData("get folder A", "/testData/nativeTouchpoint/aFolder/").getAbsolutePath()); |
| 108 |
Map safeParameters2 = Collections.unmodifiableMap(parameters2); |
| 109 |
|
| 110 |
CopyAction action2 = new CopyAction(); |
| 111 |
action2.execute(safeParameters2); |
| 112 |
assertFileContent("copied content A", new File(target, "a.txt"), "A"); |
| 113 |
assertFileContent("copied content B", new File(target, "b.txt"), "B"); |
| 114 |
|
| 115 |
// undo copy of a and b |
| 116 |
action2.undo(safeParameters2); |
| 117 |
assertFalse("Target should not exist after undo", target.exists()); |
| 118 |
} |
| 119 |
|
| 120 |
public void testBlockedMergeOverwrite() { |
| 121 |
Map parameters1 = createParameters("/testData/nativeTouchpoint/bcFolder/", "aFolder", false); |
| 122 |
Map safeParameters1 = Collections.unmodifiableMap(parameters1); |
| 123 |
|
| 124 |
CopyAction action1 = new CopyAction(); |
| 125 |
action1.execute(safeParameters1); |
| 126 |
|
| 127 |
// Verify that the right file was copied (a b.txt with a C in it [sic]) |
| 128 |
File target = new File((String) parameters1.get(ActionConstants.PARM_COPY_TARGET)); |
| 129 |
assertFileContent("copied content B", new File(target, "b.txt"), "C"); // [sic] |
| 130 |
|
| 131 |
Map parameters2 = new HashMap(); |
| 132 |
parameters2.putAll(parameters1); |
| 133 |
parameters2.put(ActionConstants.PARM_COPY_SOURCE, getTestData("get folder A", "/testData/nativeTouchpoint/aFolder/").getAbsolutePath()); |
| 134 |
Map safeParameters2 = Collections.unmodifiableMap(parameters2); |
| 135 |
|
| 136 |
CopyAction action2 = new CopyAction(); |
| 137 |
assertFalse("Overwrite of b.txt should not succeed", action2.execute(safeParameters2).isOK()); |
| 138 |
assertFileContent("copied content B", new File(target, "b.txt"), "C"); // [sic] |
| 139 |
|
| 140 |
} |
| 141 |
|
| 142 |
public void testOverwrite() { |
| 143 |
Map parameters = createParameters("/testData/nativeTouchpoint/aFolder/a.txt", "a.txt", true); |
| 144 |
Map safeParameters = Collections.unmodifiableMap(parameters); |
| 145 |
|
| 146 |
File source = new File((String) parameters.get(ActionConstants.PARM_COPY_SOURCE)); |
| 147 |
File target = new File((String) parameters.get(ActionConstants.PARM_COPY_TARGET)); |
| 148 |
|
| 149 |
// test an overwrite - by first copying the b file |
| 150 |
copy("2.0", getTestData("1.0", "/testData/nativeTouchpoint/aFolder/b.txt"), target); |
| 151 |
|
| 152 |
CopyAction action = new CopyAction(); |
| 153 |
action.execute(safeParameters); |
| 154 |
// Verify that the right file was copied |
| 155 |
assertFileContent("copied content", target, "A"); |
| 156 |
// and that we did nothing bad to the source |
| 157 |
assertFileContent("source content", source, "A"); |
| 158 |
|
| 159 |
assertTrue("copy action status", action.undo(safeParameters).isOK()); |
| 160 |
assertFalse("Target should be removed after undo", target.exists()); |
| 161 |
} |
| 162 |
|
| 163 |
public void testBlockedOverwrite() { |
| 164 |
Map parameters = createParameters("/testData/nativeTouchpoint/aFolder/a.txt", "a.txt", false); |
| 165 |
Map safeParameters = Collections.unmodifiableMap(parameters); |
| 166 |
|
| 167 |
File source = new File((String) parameters.get(ActionConstants.PARM_COPY_SOURCE)); |
| 168 |
File target = new File((String) parameters.get(ActionConstants.PARM_COPY_TARGET)); |
| 169 |
|
| 170 |
// test an overwrite - by first copying the b file |
| 171 |
copy("2.0", getTestData("1.0", "/testData/nativeTouchpoint/aFolder/b.txt"), target); |
| 172 |
|
| 173 |
CopyAction action = new CopyAction(); |
| 174 |
assertFalse("copy action status", action.execute(safeParameters).isOK()); |
| 175 |
|
| 176 |
// Verify that nothing was copied |
| 177 |
assertFileContent("original content", target, "B"); |
| 178 |
// and that we did nothing bad to the source |
| 179 |
assertFileContent("source content", source, "A"); |
| 180 |
|
| 181 |
// there is nothing to undo - the B file should still be there |
| 182 |
action.undo(safeParameters); |
| 183 |
assertTrue("Target should remain after undo", target.exists()); |
| 184 |
assertFileContent("original content", target, "B"); |
| 185 |
} |
| 186 |
|
| 187 |
/* |
| 188 |
* TODO: testing of the following |
| 189 |
* - copy of directory - check that it merges |
| 190 |
* - copy of directory with overwrite false/true |
| 191 |
*/ |
| 192 |
private Map createParameters(String sourceName, String targetName, boolean overwrite) { |
| 193 |
Properties profileProperties = new Properties(); |
| 194 |
File installFolder = getTempFolder(); |
| 195 |
profileProperties.setProperty(IProfile.PROP_INSTALL_FOLDER, installFolder.toString()); |
| 196 |
IProfile profile = createProfile("test", null, profileProperties); |
| 197 |
|
| 198 |
File source = getTestData("1.0", sourceName); |
| 199 |
File target = new File(installFolder, targetName); |
| 200 |
|
| 201 |
InstallableUnitDescription iuDesc = new MetadataFactory.InstallableUnitDescription(); |
| 202 |
iuDesc.setId("test"); |
| 203 |
iuDesc.setVersion(DEFAULT_VERSION); |
| 204 |
IArtifactKey key = PublisherHelper.createBinaryArtifactKey("test", DEFAULT_VERSION); |
| 205 |
iuDesc.setArtifacts(new IArtifactKey[] {key}); |
| 206 |
iuDesc.setTouchpointType(PublisherHelper.TOUCHPOINT_NATIVE); |
| 207 |
IInstallableUnit iu = MetadataFactory.createInstallableUnit(iuDesc); |
| 208 |
|
| 209 |
Map parameters = new HashMap(); |
| 210 |
parameters.put(ActionConstants.PARM_PROFILE, profile); |
| 211 |
parameters.put(InstallableUnitPhase.PARM_ARTIFACT_REQUESTS, new ArrayList()); |
| 212 |
InstallableUnitOperand operand = new InstallableUnitOperand(null, iu); |
| 213 |
parameters.put("iu", operand.second()); |
| 214 |
parameters.put(ActionConstants.PARM_OPERAND, operand); |
| 215 |
parameters.put(ActionConstants.PARM_PROFILE, profile); |
| 216 |
|
| 217 |
NativeTouchpoint touchpoint = new NativeTouchpoint(); |
| 218 |
touchpoint.initializePhase(null, profile, "test", parameters); |
| 219 |
|
| 220 |
parameters.put(ActionConstants.PARM_COPY_SOURCE, source.getAbsolutePath()); |
| 221 |
parameters.put(ActionConstants.PARM_COPY_TARGET, target.getAbsolutePath()); |
| 222 |
parameters.put(ActionConstants.PARM_COPY_OVERWRITE, Boolean.toString(overwrite)); |
| 223 |
return parameters; |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* Asserts that the first line of text in f equals the content. |
| 228 |
* @param f |
| 229 |
* @param content |
| 230 |
*/ |
| 231 |
public static void assertFileContent(String message, File f, String content) { |
| 232 |
//try{ |
| 233 |
BufferedReader reader; |
| 234 |
try { |
| 235 |
reader = new BufferedReader(new InputStreamReader(new FileInputStream(f))); |
| 236 |
String line = reader.readLine(); |
| 237 |
assertEquals(message, content, line); |
| 238 |
} catch (FileNotFoundException e) { |
| 239 |
fail("Getting copy target", e); |
| 240 |
} catch (IOException e) { |
| 241 |
fail("reading copy target", e); |
| 242 |
} |
| 243 |
} |
| 244 |
} |