Skip to content

Commit

Permalink
[GEOS-7946] Using java.nio.file.Files.move() with ATOMIC_MOVE option …
Browse files Browse the repository at this point in the history
…to avoid case sensitivity issues
  • Loading branch information
ridethepenguin committed Apr 9, 2017
1 parent 3831985 commit df864c7
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 37 deletions.
Expand Up @@ -15,6 +15,7 @@
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.nio.file.NoSuchFileException; import java.nio.file.NoSuchFileException;
import java.nio.file.StandardCopyOption;
import java.nio.file.attribute.BasicFileAttributes; import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
Expand Down Expand Up @@ -135,7 +136,10 @@ public boolean move(String path, String target) {


try { try {
dest.getParentFile().mkdirs(); // Make sure there's somewhere to move to. dest.getParentFile().mkdirs(); // Make sure there's somewhere to move to.
return Files.move(file, dest); java.nio.file.Files.move(java.nio.file.Paths.get(file.getAbsolutePath()),
java.nio.file.Paths.get(dest.getAbsolutePath()),
StandardCopyOption.ATOMIC_MOVE);
return true;
} catch (IOException e) { } catch (IOException e) {
throw new IllegalStateException("Unable to move " + path + " to " + target, e); throw new IllegalStateException("Unable to move " + path + " to " + target, e);
} }
Expand Down
@@ -1,22 +1,15 @@
package org.geoserver.platform.resource; package org.geoserver.platform.resource;


import org.junit.After; import static org.junit.Assert.assertEquals;
import org.junit.Before;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;


import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;


import static org.junit.Assert.assertEquals; import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;


@RunWith(PowerMockRunner.class)
@PrepareForTest({ System.class, Files.class})
public class FileSystemResourceStoreTest { public class FileSystemResourceStoreTest {


TemporaryFolder folder = new TemporaryFolder(); TemporaryFolder folder = new TemporaryFolder();
Expand All @@ -33,54 +26,70 @@ public void deleteTmpFolder() throws IOException {
} }


@Test @Test
public void renameDirNamesDifferLinux() throws IOException, InterruptedException { public void renameSameFileName() throws IOException, InterruptedException {
PowerMockito.mockStatic(System.class); String sameName = "Filea";
Mockito.when(System.getProperty("os.name")).thenReturn("linux");
String newName = "DirB";


attemptRename("DirA", newName); attemptRenameFile(sameName, sameName);


assertEquals(newName, folder.getRoot().list()[0]); assertEquals(sameName, folder.getRoot().list()[0]);
} }


@Test @Test
public void renameDirNamesCaseDifferLinux() throws IOException, InterruptedException { public void renameFileNamesCaseDiffer() throws IOException, InterruptedException {
PowerMockito.mockStatic(System.class); String newName = "Filea";
Mockito.when(System.getProperty("os.name")).thenReturn("linux");
String newName = "Dira";


attemptRename("DirA", newName); attemptRenameFile("FileA", newName);


assertEquals(newName, folder.getRoot().list()[0]); assertEquals(newName, folder.getRoot().list()[0]);
} }



@Test @Test
public void renameDirNamesDifferWindows() throws IOException, InterruptedException { public void renameFileNamesDiffer() throws IOException, InterruptedException {
PowerMockito.mockStatic(System.class); String newName = "FileB";
Mockito.when(System.getProperty("os.name")).thenReturn("Windows");
String newName = "DirB";


attemptRename("DirA", newName); attemptRenameFile("FileA", newName);


assertEquals(newName, folder.getRoot().list()[0]); assertEquals(newName, folder.getRoot().list()[0]);
}


@Test
public void renameSameDirName() throws IOException, InterruptedException {
String sameName = "Dira";

attemptRenameDir(sameName, sameName);

assertEquals(sameName, folder.getRoot().list()[0]);
} }


@Test @Test
public void renameDirNamesCaseDifferWindows() throws IOException, InterruptedException { public void renameDirNamesCaseDiffer() throws IOException, InterruptedException {
PowerMockito.mockStatic(System.class);
Mockito.when(System.getProperty("os.name")).thenReturn("Windows");
String oldName = "DirA";
String newName = "Dira"; String newName = "Dira";


attemptRename("DirA", newName); attemptRenameDir("DirA", newName);


assertEquals(oldName, folder.getRoot().list()[0]); assertEquals(newName, folder.getRoot().list()[0]);
} }


private void attemptRename(String oldName, String newName) throws IOException { @Test
public void renameDirNamesDiffer() throws IOException, InterruptedException {
String newName = "DirB";

attemptRenameDir("DirA", newName);

assertEquals(newName, folder.getRoot().list()[0]);
}

private void attemptRenameDir(String oldName, String newName) throws IOException {
File toBeRenamed = folder.newFolder(oldName); File toBeRenamed = folder.newFolder(oldName);
attemptRename(oldName, newName);
}

private void attemptRenameFile(String oldName, String newName) throws IOException {
File toBeRenamed = folder.newFile(oldName);
attemptRename(oldName, newName);
}

private void attemptRename(String oldName, String newName) throws IOException {
assertEquals(1, folder.getRoot().list().length); assertEquals(1, folder.getRoot().list().length);
FileSystemResourceStore toTest = new FileSystemResourceStore(folder.getRoot()); FileSystemResourceStore toTest = new FileSystemResourceStore(folder.getRoot());


Expand Down

0 comments on commit df864c7

Please sign in to comment.