Skip to content
This repository has been archived by the owner on Jul 26, 2022. It is now read-only.

Commit

Permalink
Merge pull request #615 from syncany/fix/inconsitent-tests
Browse files Browse the repository at this point in the history
Fixing inconsistent tests
  • Loading branch information
pimotte committed May 25, 2017
2 parents f726f66 + 8545328 commit 2ab2a6f
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 99 deletions.
Expand Up @@ -57,7 +57,7 @@ public void testCreateMasterKeyWithSalt() throws CipherException {
assertEquals("PBKDF2WithHmacSHA1", masterKeyForPasswordTestAndSalt123.getAlgorithm());
assertEquals("RAW", masterKeyForPasswordTestAndSalt123.getFormat());

assertTrue(timeDuration > 5000);
assertTrue(timeDuration > 3000);
}

@Test
Expand Down
Expand Up @@ -41,6 +41,7 @@ public void watchEventsOccurred() {
});

watcher.start();
Thread.sleep(100);

TestFileUtil.createRandomFileInDirectory(tempDir.toFile(), 111);
Thread.sleep(350);
Expand Down
13 changes: 0 additions & 13 deletions syncany-util/src/main/java/org/syncany/util/NormalizedPath.java
Expand Up @@ -43,19 +43,6 @@ public class NormalizedPath {
public NormalizedPath(File root, String normalizedPath) {
this.root = root;
this.normalizedPath = normalizedPath;
}

public static NormalizedPath get(File root, String relativePath) {
return get(root, relativePath, EnvironmentUtil.getOperatingSystem());
}

public static NormalizedPath get(File root, String relativePath, OperatingSystem operatingSystem) {
if (operatingSystem == OperatingSystem.WINDOWS) {
return new NormalizedPath(root, relativePath.replaceAll("\\\\$", "").replaceAll("\\\\", "/"));
}
else {
return new NormalizedPath(root, relativePath.replaceAll("/$", ""));
}
}

@Override
Expand Down
Expand Up @@ -33,152 +33,112 @@

public class NormalizedPathTest {
private OperatingSystem originalOperatingSystem;

@Before
public void storeOperatingSystem() {
originalOperatingSystem = EnvironmentUtil.getOperatingSystem();
}

@After
public void resetOperatingSystem() {
// Important: Restore the actual operating systems,
// or other tests might fail.

EnvironmentUtil.setOperatingSystem(originalOperatingSystem);
}

@Test
public void testGetRelativeFilePath() {
String expectedResult = "somefile";
File[] rootFolders = new File[] {
new File("/home/user/Syncany"),
new File("/home/user/Syncany/"),
new File("/home/user/Syncany//"),
new File("/home/user//Syncany"),
new File("/home/user//Syncany/"),
new File("/home/user//Syncany//")

File[] rootFolders = new File[]{
new File("/home/user/Syncany"),
new File("/home/user/Syncany/"),
new File("/home/user/Syncany//"),
new File("/home/user//Syncany"),
new File("/home/user//Syncany/"),
new File("/home/user//Syncany//")
};
File[] files = new File[] {
new File("/home/user/Syncany/somefile"),
new File("/home/user/Syncany/somefile/"),
new File("/home/user/Syncany/somefile//"),
new File("/home/user/Syncany//somefile"),
new File("/home/user/Syncany//somefile/"),
new File("/home/user/Syncany//somefile//")

File[] files = new File[]{
new File("/home/user/Syncany/somefile"),
new File("/home/user/Syncany/somefile/"),
new File("/home/user/Syncany/somefile//"),
new File("/home/user/Syncany//somefile"),
new File("/home/user/Syncany//somefile/"),
new File("/home/user/Syncany//somefile//")
};

for (File rootFolder : rootFolders) {
for (File file : files) {
String actualResult = FileUtil.getRelativePath(rootFolder, file);
assertEquals("Expected '"+expectedResult+"' for root folder '"+rootFolder+"' and file '"+file+"'", expectedResult, actualResult);
assertEquals("Expected '" + expectedResult + "' for root folder '" + rootFolder + "' and file '" + file + "'", expectedResult, actualResult);
}
}
}

@Test
public void testGetRelativeFilePathSpecialCases() {
assertEquals("", FileUtil.getRelativePath(new File("/home/user/"), new File("/home/user")));
assertEquals("", FileUtil.getRelativePath(new File("/home/user/"), new File("/home/user/")));
assertEquals("", FileUtil.getRelativePath(new File("/home/user/"), new File("/home/user//")));
assertEquals("", FileUtil.getRelativePath(new File("/home/user/"), new File("/home/user//")));
}

@Test
public void testNameAndParentPathForNormalizedPathsOnWindows() {
public void testNameAndParentPathForNormalizedPathsOnWindows() {
testNameAndParentPathForNormalizedPaths(OperatingSystem.WINDOWS);
}

@Test
public void testNameAndParentPathForNormalizedPathsOnUnixLikeSystems() {
public void testNameAndParentPathForNormalizedPathsOnUnixLikeSystems() {
testNameAndParentPathForNormalizedPaths(OperatingSystem.UNIX_LIKE);
}

private void testNameAndParentPathForNormalizedPaths(OperatingSystem operatingSystem) {
private void testNameAndParentPathForNormalizedPaths(OperatingSystem operatingSystem) {
EnvironmentUtil.setOperatingSystem(operatingSystem);

// Test 1: For a file called 'A black\white telephone ☎.jpg'
// Note: "A black" is NOT a directory, it's part of the filename (invalid on Windows!)
String alreadyNormalizedRelativePathFileStr = "Pictures/A black\\white telephone ☎.jpg";
NormalizedPath normalizedPathFile = new NormalizedPath(null, alreadyNormalizedRelativePathFileStr);

assertEquals("Pictures/A black\\white telephone ☎.jpg", normalizedPathFile.toString());
assertEquals("Pictures", normalizedPathFile.getParent().toString());

// Test 2: For directory called 'black\\white telephones ☎'
String alreadyNormalizedRelativePathDirStr = "Pictures/black\\white telephones ☎";
NormalizedPath normalizedPathDir = new NormalizedPath(null, alreadyNormalizedRelativePathDirStr);

assertEquals("Pictures/black\\white telephones ☎", normalizedPathDir.toString());
assertEquals("Pictures", normalizedPathDir.getParent().toString());

// Test 3: For directory called 'black\\white telephones ☎'
String alreadyNormalizedRelativePathFileWithBackslashesDirStr = "Pictures/Black\\White Pictures/Mostly\\Black Pictures/blacky.jpg";
NormalizedPath normalizedPathWithBackslashesDir = new NormalizedPath(null, alreadyNormalizedRelativePathFileWithBackslashesDirStr);

assertEquals("Pictures/Black\\White Pictures/Mostly\\Black Pictures/blacky.jpg", normalizedPathWithBackslashesDir.toString());
assertEquals("Pictures/Black\\White Pictures/Mostly\\Black Pictures", normalizedPathWithBackslashesDir.getParent().toString());
assertEquals("Pictures/Black\\White Pictures/Mostly\\Black Pictures", normalizedPathWithBackslashesDir.getParent().toString());
}

@Test
public void testNameAndParentPathForNormalizedPathsMoreTests() {
public void testNameAndParentPathForNormalizedPathsMoreTests() {
// Does not depend on OS

assertEquals("", new NormalizedPath(null, "Philipp").getParent().toString());
}

@Test
public void testNormalizationOnWindows() {
EnvironmentUtil.setOperatingSystem(OperatingSystem.WINDOWS);

assertEquals("C:/Philipp", NormalizedPath.get(null, "C:\\Philipp\\").toString());
assertEquals("C:/Philipp", NormalizedPath.get(null, "C:\\Philipp").toString());
assertEquals("C:/Philipp/image.jpg", NormalizedPath.get(null, "C:\\Philipp\\image.jpg").toString());
assertEquals("C:/Philipp/image", NormalizedPath.get(null, "C:\\Philipp\\image").toString());
assertEquals("C:/Philipp/file:with:colons.txt", NormalizedPath.get(null, "C:\\Philipp\\file:with:colons.txt").toString()); // Cannot happen on Windows
assertEquals("C:/Philipp/file/with/backslashes.txt", NormalizedPath.get(null, "C:\\Philipp\\file\\with\\backslashes.txt").toString());
assertEquals("C:/Philipp/folder/with/backslashes", NormalizedPath.get(null, "C:\\Philipp\\folder\\with\\backslashes\\").toString());

assertEquals("", new NormalizedPath(null, "Philipp").getParent().toString());
}



@Test
public void testCreatablizationOnWindows() throws Exception {
public void testCreatablizationOnWindows() throws Exception {
EnvironmentUtil.setOperatingSystem(OperatingSystem.WINDOWS);
File root = new File("C:\\Philipp");

assertEquals("Philipp", new NormalizedPath(root, "Philipp").toCreatable("filename conflict", true).toString());
assertEquals("Philipp", new NormalizedPath(root, "Philipp").toCreatable("filename conflict", true).toString());
assertEquals("Philipp/image.jpg", new NormalizedPath(root, "Philipp/image.jpg").toCreatable("filename conflict", true).toString());
assertEquals("Philipp/image", new NormalizedPath(root, "Philipp/image").toCreatable("filename conflict", true).toString());
assertEquals("Philipp/filewithcolons (filename conflict).txt", new NormalizedPath(root, "Philipp/file:with:colons.txt").toCreatable("filename conflict", true).toString()); // Cannot happen on Windows
assertEquals("Philipp/filewithbackslashes (filename conflict).txt", new NormalizedPath(root, "Philipp/file\\with\\backslashes.txt").toCreatable("filename conflict", true).toString());
assertEquals("Philipp/folderwithbackslashes (filename conflict)", new NormalizedPath(root, "Philipp/folder\\with\\backslashes").toCreatable("filename conflict", true).toString());
assertEquals("Philipp/filewithbackslashes (filename conflict).txt", new NormalizedPath(root, "Philipp/file\\with\\backslashes.txt").toCreatable("filename conflict", true).toString());
assertEquals("Philipp/folderwithbackslashes (filename conflict)", new NormalizedPath(root, "Philipp/folder\\with\\backslashes").toCreatable("filename conflict", true).toString());
}

@Test
public void testNormalizationOnUnixLikeSystems() {
EnvironmentUtil.setOperatingSystem(OperatingSystem.UNIX_LIKE);

assertEquals("/home/philipp", NormalizedPath.get(null, "/home/philipp/").toString());
assertEquals("/home/philipp", NormalizedPath.get(null, "/home/philipp").toString());
assertEquals("/home/philipp/image.jpg", NormalizedPath.get(null, "/home/philipp/image.jpg").toString());
assertEquals("/home/philipp/image", NormalizedPath.get(null, "/home/philipp/image").toString());
assertEquals("/home/philipp/file:with:colons", NormalizedPath.get(null, "/home/philipp/file:with:colons").toString());
assertEquals("/home/philipp/file\\with\\backslashes.txt", NormalizedPath.get(null, "/home/philipp/file\\with\\backslashes.txt").toString());
assertEquals("/home/philipp/folder\\with\\backslashes", NormalizedPath.get(null, "/home/philipp/folder\\with\\backslashes/").toString());
}

@Test
public void testNormalizationHasIllegalChars() {
EnvironmentUtil.setOperatingSystem(OperatingSystem.UNIX_LIKE);

assertFalse(NormalizedPath.get(null, "/home/philipp/").hasIllegalChars());
assertFalse(NormalizedPath.get(null, "/home/black\\white telephones ☎.txt").hasIllegalChars());
assertTrue(NormalizedPath.get(null, "/home/philipp/\0.txt").hasIllegalChars());

EnvironmentUtil.setOperatingSystem(OperatingSystem.WINDOWS);

assertFalse(NormalizedPath.get(null, "C:\\home\\philipp\\").hasIllegalChars());
assertFalse(NormalizedPath.get(null, "C:\\home\\black-white telephones ☎.txt").hasIllegalChars());
assertTrue(NormalizedPath.get(null, "C:\\home\\\0.txt").hasIllegalChars());
}
}

0 comments on commit 2ab2a6f

Please sign in to comment.