Fixing FileSystem reference techniques for OSX testing on Java 8

This commit is contained in:
Joakim Erdfelt 2015-04-24 14:44:31 -07:00
parent 2de8eb936d
commit c4eb2da79c
3 changed files with 216 additions and 133 deletions

View File

@ -72,6 +72,10 @@ public class PathResource extends Resource
if (Files.exists(path)) if (Files.exists(path))
{ {
Path real = abs.toRealPath(FOLLOW_LINKS); Path real = abs.toRealPath(FOLLOW_LINKS);
// We don't use (!Files.isSameFile(abs,real)) here as this
// results in invalid alias detection on systems like OSX.
// Where a real file "foo" can be referenced via Path "FOO",
// this results in Files.isSameFile(Path("foo"), Path("FOO")) == true
if (!abs.equals(real)) if (!abs.equals(real))
return real; return real;
} }

View File

@ -50,8 +50,12 @@ public class ScannerTest
@BeforeClass @BeforeClass
public static void setUpBeforeClass() throws Exception public static void setUpBeforeClass() throws Exception
{ {
_directory = MavenTestingUtils.getTargetTestingDir(ScannerTest.class.getSimpleName()); File testDir = MavenTestingUtils.getTargetTestingDir(ScannerTest.class.getSimpleName());
FS.ensureEmpty(_directory); FS.ensureEmpty(testDir);
// Use full path, pointing to a real directory (for FileSystems that are case-insensitive, like Windows and OSX to use)
// This is only needed for the various comparisons below to make sense.
_directory = testDir.toPath().toRealPath().toFile();
_scanner = new Scanner(); _scanner = new Scanner();
_scanner.addScanDir(_directory); _scanner.addScanDir(_directory);

View File

@ -22,8 +22,8 @@ import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import static org.junit.Assume.*; import static org.junit.Assume.*;
import java.io.BufferedWriter;
import java.io.File; import java.io.File;
import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
@ -34,7 +34,6 @@ import java.net.URI;
import java.net.URL; import java.net.URL;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.channels.ReadableByteChannel; import java.nio.channels.ReadableByteChannel;
import java.nio.file.DirectoryIteratorException;
import java.nio.file.DirectoryStream; import java.nio.file.DirectoryStream;
import java.nio.file.FileSystem; import java.nio.file.FileSystem;
import java.nio.file.FileSystemException; import java.nio.file.FileSystemException;
@ -188,13 +187,6 @@ public class FileSystemResourceTest
}; };
} }
private URI createEmptyFile(String name) throws IOException
{
File file = testdir.getFile(name);
file.createNewFile();
return file.toURI();
}
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void testNonAbsoluteURI() throws Exception public void testNonAbsoluteURI() throws Exception
{ {
@ -229,11 +221,12 @@ public class FileSystemResourceTest
@Test @Test
public void testAddPath() throws Exception public void testAddPath() throws Exception
{ {
File dir = testdir.getDir(); Path dir = testdir.getDir().toPath().normalize().toRealPath();
File subdir = new File(dir,"sub");
FS.ensureDirExists(subdir); Path subdir = dir.resolve("sub");
FS.ensureDirExists(subdir.toFile());
try (Resource base = newResource(testdir.getDir())) try (Resource base = newResource(dir.toFile()))
{ {
Resource sub = base.addPath("sub"); Resource sub = base.addPath("sub");
assertThat("sub/.isDirectory",sub.isDirectory(),is(true)); assertThat("sub/.isDirectory",sub.isDirectory(),is(true));
@ -246,14 +239,14 @@ public class FileSystemResourceTest
@Test @Test
public void testAddRootPath() throws Exception public void testAddRootPath() throws Exception
{ {
File dir = testdir.getDir(); Path dir = testdir.getDir().toPath().normalize().toRealPath();
File subdir = new File(dir,"sub"); Path subdir = dir.resolve("sub");
FS.ensureDirExists(subdir); Files.createDirectories(subdir);
String readableRootDir = findRootDir(dir.toPath().getFileSystem()); String readableRootDir = findRootDir(dir.getFileSystem());
assumeThat("Readable Root Dir found",readableRootDir,notNullValue()); assumeThat("Readable Root Dir found",readableRootDir,notNullValue());
try (Resource base = newResource(testdir.getDir())) try (Resource base = newResource(dir.toFile()))
{ {
Resource sub = base.addPath("sub"); Resource sub = base.addPath("sub");
assertThat("sub",sub.isDirectory(),is(true)); assertThat("sub",sub.isDirectory(),is(true));
@ -286,9 +279,12 @@ public class FileSystemResourceTest
@Test @Test
public void testIsContainedIn() throws Exception public void testIsContainedIn() throws Exception
{ {
createEmptyFile("foo"); Path dir = testdir.getDir().toPath().normalize().toRealPath();
Files.createDirectories(dir);
Path foo = dir.resolve("foo");
Files.createFile(foo);
try (Resource base = newResource(testdir.getDir())) try (Resource base = newResource(dir.toFile()))
{ {
Resource res = base.addPath("foo"); Resource res = base.addPath("foo");
assertThat("is contained in",res.isContainedIn(base),is(false)); assertThat("is contained in",res.isContainedIn(base),is(false));
@ -298,13 +294,15 @@ public class FileSystemResourceTest
@Test @Test
public void testIsDirectory() throws Exception public void testIsDirectory() throws Exception
{ {
File dir = testdir.getDir(); Path dir = testdir.getDir().toPath().normalize().toRealPath();
createEmptyFile("foo"); Files.createDirectories(dir);
Path foo = dir.resolve("foo");
Files.createFile(foo);
File subdir = new File(dir,"sub"); Path subdir = dir.resolve("sub");
FS.ensureDirExists(subdir); Files.createDirectories(subdir);
try (Resource base = newResource(testdir.getDir())) try (Resource base = newResource(dir.toFile()))
{ {
Resource res = base.addPath("foo"); Resource res = base.addPath("foo");
assertThat("foo.isDirectory",res.isDirectory(),is(false)); assertThat("foo.isDirectory",res.isDirectory(),is(false));
@ -317,12 +315,13 @@ public class FileSystemResourceTest
@Test @Test
public void testLastModified() throws Exception public void testLastModified() throws Exception
{ {
Path dir = testdir.getDir().toPath().normalize().toRealPath();
File file = testdir.getFile("foo"); File file = testdir.getFile("foo");
file.createNewFile(); file.createNewFile();
long expected = file.lastModified(); long expected = file.lastModified();
try (Resource base = newResource(testdir.getDir())) try (Resource base = newResource(dir.toFile()))
{ {
Resource res = base.addPath("foo"); Resource res = base.addPath("foo");
assertThat("foo.lastModified",res.lastModified()/1000*1000,is(expected)); assertThat("foo.lastModified",res.lastModified()/1000*1000,is(expected));
@ -332,7 +331,9 @@ public class FileSystemResourceTest
@Test @Test
public void testLastModified_NotExists() throws Exception public void testLastModified_NotExists() throws Exception
{ {
try (Resource base = newResource(testdir.getDir())) Path dir = testdir.getDir().toPath().normalize().toRealPath();
try (Resource base = newResource(dir.toFile()))
{ {
Resource res = base.addPath("foo"); Resource res = base.addPath("foo");
assertThat("foo.lastModified",res.lastModified(),is(0L)); assertThat("foo.lastModified",res.lastModified(),is(0L));
@ -342,17 +343,20 @@ public class FileSystemResourceTest
@Test @Test
public void testLength() throws Exception public void testLength() throws Exception
{ {
File file = testdir.getFile("foo"); Path dir = testdir.getDir().toPath().normalize().toRealPath();
file.createNewFile(); Files.createDirectories(dir);
Path file = dir.resolve("foo");
try (StringReader reader = new StringReader("foo"); FileWriter writer = new FileWriter(file)) try (StringReader reader = new StringReader("foo");
BufferedWriter writer = Files.newBufferedWriter(file))
{ {
IO.copy(reader,writer); IO.copy(reader,writer);
} }
long expected = file.length(); long expected = Files.size(file);
try (Resource base = newResource(testdir.getDir())) try (Resource base = newResource(dir.toFile()))
{ {
Resource res = base.addPath("foo"); Resource res = base.addPath("foo");
assertThat("foo.length",res.length(),is(expected)); assertThat("foo.length",res.length(),is(expected));
@ -362,7 +366,10 @@ public class FileSystemResourceTest
@Test @Test
public void testLength_NotExists() throws Exception public void testLength_NotExists() throws Exception
{ {
try (Resource base = newResource(testdir.getDir())) Path dir = testdir.getDir().toPath().normalize().toRealPath();
Files.createDirectories(dir);
try (Resource base = newResource(dir.toFile()))
{ {
Resource res = base.addPath("foo"); Resource res = base.addPath("foo");
assertThat("foo.length",res.length(),is(0L)); assertThat("foo.length",res.length(),is(0L));
@ -372,10 +379,12 @@ public class FileSystemResourceTest
@Test @Test
public void testDelete() throws Exception public void testDelete() throws Exception
{ {
File file = testdir.getFile("foo"); Path dir = testdir.getDir().toPath().normalize().toRealPath();
file.createNewFile(); Files.createDirectories(dir);
Path file = dir.resolve("foo");
Files.createFile(file);
try (Resource base = newResource(testdir.getDir())) try (Resource base = newResource(dir.toFile()))
{ {
// Is it there? // Is it there?
Resource res = base.addPath("foo"); Resource res = base.addPath("foo");
@ -390,7 +399,10 @@ public class FileSystemResourceTest
@Test @Test
public void testDelete_NotExists() throws Exception public void testDelete_NotExists() throws Exception
{ {
try (Resource base = newResource(testdir.getDir())) Path dir = testdir.getDir().toPath().normalize().toRealPath();
Files.createDirectories(dir);
try (Resource base = newResource(dir.toFile()))
{ {
// Is it there? // Is it there?
Resource res = base.addPath("foo"); Resource res = base.addPath("foo");
@ -405,9 +417,12 @@ public class FileSystemResourceTest
@Test @Test
public void testName() throws Exception public void testName() throws Exception
{ {
String expected = testdir.getDir().getAbsolutePath(); Path dir = testdir.getDir().toPath().normalize().toRealPath();
Files.createDirectories(dir);
String expected = dir.toAbsolutePath().toString();
try (Resource base = newResource(testdir.getDir())) try (Resource base = newResource(dir.toFile()))
{ {
assertThat("base.name",base.getName(),is(expected)); assertThat("base.name",base.getName(),is(expected));
} }
@ -416,17 +431,19 @@ public class FileSystemResourceTest
@Test @Test
public void testInputStream() throws Exception public void testInputStream() throws Exception
{ {
File file = testdir.getFile("foo"); Path dir = testdir.getDir().toPath().normalize().toRealPath();
file.createNewFile(); Files.createDirectories(dir);
Path file = dir.resolve("foo");
String content = "Foo is here"; String content = "Foo is here";
try (StringReader reader = new StringReader(content); FileWriter writer = new FileWriter(file)) try (StringReader reader = new StringReader(content);
BufferedWriter writer = Files.newBufferedWriter(file))
{ {
IO.copy(reader,writer); IO.copy(reader,writer);
} }
try (Resource base = newResource(testdir.getDir())) try (Resource base = newResource(dir.toFile()))
{ {
Resource foo = base.addPath("foo"); Resource foo = base.addPath("foo");
try (InputStream stream = foo.getInputStream(); InputStreamReader reader = new InputStreamReader(stream); StringWriter writer = new StringWriter()) try (InputStream stream = foo.getInputStream(); InputStreamReader reader = new InputStreamReader(stream); StringWriter writer = new StringWriter())
@ -440,17 +457,19 @@ public class FileSystemResourceTest
@Test @Test
public void testReadableByteChannel() throws Exception public void testReadableByteChannel() throws Exception
{ {
File file = testdir.getFile("foo"); Path dir = testdir.getDir().toPath().normalize().toRealPath();
file.createNewFile(); Files.createDirectories(dir);
Path file = dir.resolve("foo");
String content = "Foo is here"; String content = "Foo is here";
try (StringReader reader = new StringReader(content); FileWriter writer = new FileWriter(file)) try (StringReader reader = new StringReader(content);
BufferedWriter writer = Files.newBufferedWriter(file))
{ {
IO.copy(reader,writer); IO.copy(reader,writer);
} }
try (Resource base = newResource(testdir.getDir())) try (Resource base = newResource(dir.toFile()))
{ {
Resource foo = base.addPath("foo"); Resource foo = base.addPath("foo");
try (ReadableByteChannel channel = foo.getReadableByteChannel()) try (ReadableByteChannel channel = foo.getReadableByteChannel())
@ -467,27 +486,34 @@ public class FileSystemResourceTest
@Test @Test
public void testGetURI() throws Exception public void testGetURI() throws Exception
{ {
File file = testdir.getFile("foo"); Path dir = testdir.getDir().toPath().normalize().toRealPath();
file.createNewFile(); Files.createDirectories(dir);
URI expected = file.toURI(); Path file = dir.resolve("foo");
Files.createFile(file);
try (Resource base = newResource(testdir.getDir())) URI expected = file.toUri();
try (Resource base = newResource(dir.toFile()))
{ {
Resource foo = base.addPath("foo"); Resource foo = base.addPath("foo");
assertThat("getURI",foo.getURI(),is(expected)); assertThat("getURI",foo.getURI(),is(expected));
} }
} }
@SuppressWarnings("deprecation")
@Test @Test
public void testGetURL() throws Exception public void testGetURL() throws Exception
{ {
File file = testdir.getFile("foo"); Path dir = testdir.getDir().toPath().normalize().toRealPath();
file.createNewFile(); Files.createDirectories(dir);
URL expected = file.toURI().toURL(); Path file = dir.resolve("foo");
Files.createFile(file);
try (Resource base = newResource(testdir.getDir())) URL expected = file.toUri().toURL();
try (Resource base = newResource(dir.toFile()))
{ {
Resource foo = base.addPath("foo"); Resource foo = base.addPath("foo");
assertThat("getURL",foo.getURL(),is(expected)); assertThat("getURL",foo.getURL(),is(expected));
@ -497,11 +523,13 @@ public class FileSystemResourceTest
@Test @Test
public void testList() throws Exception public void testList() throws Exception
{ {
File dir = testdir.getDir(); Path dir = testdir.getDir().toPath().normalize().toRealPath();
FS.touch(new File(dir, "foo")); Files.createDirectories(dir);
FS.touch(new File(dir, "bar"));
FS.ensureDirExists(new File(dir, "tick")); Files.createFile(dir.resolve("foo"));
FS.ensureDirExists(new File(dir, "tock")); Files.createFile(dir.resolve("bar"));
Files.createDirectories(dir.resolve("tick"));
Files.createDirectories(dir.resolve("tock"));
List<String> expected = new ArrayList<>(); List<String> expected = new ArrayList<>();
expected.add("foo"); expected.add("foo");
@ -509,7 +537,7 @@ public class FileSystemResourceTest
expected.add("tick/"); expected.add("tick/");
expected.add("tock/"); expected.add("tock/");
try (Resource base = newResource(testdir.getDir())) try (Resource base = newResource(dir.toFile()))
{ {
String list[] = base.list(); String list[] = base.list();
List<String> actual = Arrays.asList(list); List<String> actual = Arrays.asList(list);
@ -522,10 +550,10 @@ public class FileSystemResourceTest
@Test @Test
public void testSymlink() throws Exception public void testSymlink() throws Exception
{ {
File dir = testdir.getDir(); Path dir = testdir.getDir().toPath().normalize().toRealPath();
Path foo = new File(dir, "foo").toPath(); Path foo = dir.resolve("foo");
Path bar = new File(dir, "bar").toPath(); Path bar = dir.resolve("bar");
try try
{ {
@ -539,7 +567,7 @@ public class FileSystemResourceTest
assumeNoException(e); assumeNoException(e);
} }
try (Resource base = newResource(testdir.getDir())) try (Resource base = newResource(dir.toFile()))
{ {
Resource resFoo = base.addPath("foo"); Resource resFoo = base.addPath("foo");
Resource resBar = base.addPath("bar"); Resource resBar = base.addPath("bar");
@ -562,10 +590,11 @@ public class FileSystemResourceTest
@Test @Test
public void testNonExistantSymlink() throws Exception public void testNonExistantSymlink() throws Exception
{ {
File dir = testdir.getDir(); Path dir = testdir.getDir().toPath().normalize().toRealPath();
Files.createDirectories(dir);
Path foo = new File(dir, "foo").toPath(); Path foo = dir.resolve("foo");
Path bar = new File(dir, "bar").toPath(); Path bar = dir.resolve("bar");
try try
{ {
@ -578,7 +607,7 @@ public class FileSystemResourceTest
assumeNoException(e); assumeNoException(e);
} }
try (Resource base = newResource(testdir.getDir())) try (Resource base = newResource(dir.toFile()))
{ {
// FileResource does not pass this test! // FileResource does not pass this test!
assumeFalse(base instanceof FileResource); assumeFalse(base instanceof FileResource);
@ -600,16 +629,16 @@ public class FileSystemResourceTest
assertThat("file.alias", newResource(resBar.getFile()), isAliasFor(resFoo)); assertThat("file.alias", newResource(resBar.getFile()), isAliasFor(resFoo));
} }
} }
@Test @Test
public void testCaseInsensitiveAlias() throws Exception public void testCaseInsensitiveAlias() throws Exception
{ {
File dir = testdir.getDir(); Path dir = testdir.getDir().toPath().normalize().toRealPath();
Path path = new File(dir, "file").toPath(); Files.createDirectories(dir);
Path path = dir.resolve("file");
Files.createFile(path); Files.createFile(path);
try (Resource base = newResource(testdir.getDir())) try (Resource base = newResource(dir.toFile()))
{ {
// Reference to actual resource that exists // Reference to actual resource that exists
Resource resource = base.addPath("file"); Resource resource = base.addPath("file");
@ -641,11 +670,13 @@ public class FileSystemResourceTest
@Test @Test
public void testCase8dot3Alias() throws Exception public void testCase8dot3Alias() throws Exception
{ {
File dir = testdir.getDir(); Path dir = testdir.getDir().toPath().normalize().toRealPath();
Path path = new File(dir, "TextFile.Long.txt").toPath(); Files.createDirectories(dir);
Path path = dir.resolve("TextFile.Long.txt");
Files.createFile(path); Files.createFile(path);
try (Resource base = newResource(testdir.getDir())) try (Resource base = newResource(dir.toFile()))
{ {
// Long filename // Long filename
Resource resource = base.addPath("TextFile.Long.txt"); Resource resource = base.addPath("TextFile.Long.txt");
@ -676,11 +707,13 @@ public class FileSystemResourceTest
@Test @Test
public void testNTFSFileStreamAlias() throws Exception public void testNTFSFileStreamAlias() throws Exception
{ {
File dir = testdir.getDir(); Path dir = testdir.getDir().toPath().normalize().toRealPath();
Path path = new File(dir, "testfile").toPath(); Files.createDirectories(dir);
Path path = dir.resolve("testfile");
Files.createFile(path); Files.createFile(path);
try (Resource base = newResource(testdir.getDir())) try (Resource base = newResource(dir.toFile()))
{ {
Resource resource = base.addPath("testfile"); Resource resource = base.addPath("testfile");
@ -717,11 +750,13 @@ public class FileSystemResourceTest
@Test @Test
public void testNTFSFileDataStreamAlias() throws Exception public void testNTFSFileDataStreamAlias() throws Exception
{ {
File dir = testdir.getDir(); Path dir = testdir.getDir().toPath().normalize().toRealPath();
Path path = new File(dir, "testfile").toPath(); Files.createDirectories(dir);
Path path = dir.resolve("testfile");
Files.createFile(path); Files.createFile(path);
try (Resource base = newResource(testdir.getDir())) try (Resource base = newResource(dir.toFile()))
{ {
Resource resource = base.addPath("testfile"); Resource resource = base.addPath("testfile");
@ -760,11 +795,13 @@ public class FileSystemResourceTest
@Test @Test
public void testNTFSFileEncodedDataStreamAlias() throws Exception public void testNTFSFileEncodedDataStreamAlias() throws Exception
{ {
File dir = testdir.getDir(); Path dir = testdir.getDir().toPath().normalize().toRealPath();
Path path = new File(dir, "testfile").toPath(); Files.createDirectories(dir);
Path path = dir.resolve("testfile");
Files.createFile(path); Files.createFile(path);
try (Resource base = newResource(testdir.getDir())) try (Resource base = newResource(dir.toFile()))
{ {
Resource resource = base.addPath("testfile"); Resource resource = base.addPath("testfile");
@ -795,12 +832,12 @@ public class FileSystemResourceTest
@Test @Test
public void testSemicolon() throws Exception public void testSemicolon() throws Exception
{ {
File dir = testdir.getDir(); Path dir = testdir.getDir().toPath().normalize().toRealPath();
try try
{ {
// attempt to create file // attempt to create file
Path foo = new File(dir, "foo;").toPath(); Path foo = dir.resolve("foo;");
Files.createFile(foo); Files.createFile(foo);
} }
catch (Exception e) catch (Exception e)
@ -810,22 +847,23 @@ public class FileSystemResourceTest
assumeNoException(e); assumeNoException(e);
} }
try (Resource base = newResource(testdir.getDir())) try (Resource base = newResource(dir.toFile()))
{ {
Resource res = base.addPath("foo;"); Resource res = base.addPath("foo;");
assertThat("Alias: " + res,res.getAlias(),nullValue()); assertThat("Alias: " + res,res,hasNoAlias());
} }
} }
@Test @Test
public void testSingleQuote() throws Exception public void testSingleQuote() throws Exception
{ {
File dir = testdir.getDir(); Path dir = testdir.getDir().toPath().normalize().toRealPath();
Files.createDirectories(dir);
try try
{ {
// attempt to create file // attempt to create file
Path foo = new File(dir, "foo' bar").toPath(); Path foo = dir.resolve("foo' bar");
Files.createFile(foo); Files.createFile(foo);
} }
catch (Exception e) catch (Exception e)
@ -835,7 +873,7 @@ public class FileSystemResourceTest
assumeNoException(e); assumeNoException(e);
} }
try (Resource base = newResource(testdir.getDir())) try (Resource base = newResource(dir.toFile()))
{ {
Resource res = base.addPath("foo' bar"); Resource res = base.addPath("foo' bar");
assertThat("Alias: " + res,res.getAlias(),nullValue()); assertThat("Alias: " + res,res.getAlias(),nullValue());
@ -845,12 +883,13 @@ public class FileSystemResourceTest
@Test @Test
public void testSingleBackTick() throws Exception public void testSingleBackTick() throws Exception
{ {
File dir = testdir.getDir(); Path dir = testdir.getDir().toPath().normalize().toRealPath();
Files.createDirectories(dir);
try try
{ {
// attempt to create file // attempt to create file
Path foo = new File(dir, "foo` bar").toPath(); Path foo = dir.resolve("foo` bar");
Files.createFile(foo); Files.createFile(foo);
} }
catch (Exception e) catch (Exception e)
@ -860,7 +899,7 @@ public class FileSystemResourceTest
assumeNoException(e); assumeNoException(e);
} }
try (Resource base = newResource(testdir.getDir())) try (Resource base = newResource(dir.toFile()))
{ {
// FileResource does not pass this test! // FileResource does not pass this test!
assumeFalse(base instanceof FileResource); assumeFalse(base instanceof FileResource);
@ -873,12 +912,13 @@ public class FileSystemResourceTest
@Test @Test
public void testBrackets() throws Exception public void testBrackets() throws Exception
{ {
File dir = testdir.getDir(); Path dir = testdir.getDir().toPath().normalize().toRealPath();
Files.createDirectories(dir);
try try
{ {
// attempt to create file // attempt to create file
Path foo = new File(dir, "foo[1]").toPath(); Path foo = dir.resolve("foo[1]");
Files.createFile(foo); Files.createFile(foo);
} }
catch (Exception e) catch (Exception e)
@ -888,7 +928,7 @@ public class FileSystemResourceTest
assumeNoException(e); assumeNoException(e);
} }
try (Resource base = newResource(testdir.getDir())) try (Resource base = newResource(dir.toFile()))
{ {
Resource res = base.addPath("foo[1]"); Resource res = base.addPath("foo[1]");
assertThat("Alias: " + res,res.getAlias(),nullValue()); assertThat("Alias: " + res,res.getAlias(),nullValue());
@ -898,12 +938,13 @@ public class FileSystemResourceTest
@Test @Test
public void testBraces() throws Exception public void testBraces() throws Exception
{ {
File dir = testdir.getDir(); Path dir = testdir.getDir().toPath().normalize().toRealPath();
Files.createDirectories(dir);
try try
{ {
// attempt to create file // attempt to create file
Path foo = new File(dir, "foo.{bar}.txt").toPath(); Path foo = dir.resolve("foo.{bar}.txt");
Files.createFile(foo); Files.createFile(foo);
} }
catch (Exception e) catch (Exception e)
@ -913,7 +954,7 @@ public class FileSystemResourceTest
assumeNoException(e); assumeNoException(e);
} }
try (Resource base = newResource(testdir.getDir())) try (Resource base = newResource(dir.toFile()))
{ {
// FileResource does not pass this test! // FileResource does not pass this test!
assumeFalse(base instanceof FileResource); assumeFalse(base instanceof FileResource);
@ -926,12 +967,13 @@ public class FileSystemResourceTest
@Test @Test
public void testCaret() throws Exception public void testCaret() throws Exception
{ {
File dir = testdir.getDir(); Path dir = testdir.getDir().toPath().normalize().toRealPath();
Files.createDirectories(dir);
try try
{ {
// attempt to create file // attempt to create file
Path foo = new File(dir, "foo^3.txt").toPath(); Path foo = dir.resolve("foo^3.txt");
Files.createFile(foo); Files.createFile(foo);
} }
catch (Exception e) catch (Exception e)
@ -941,7 +983,7 @@ public class FileSystemResourceTest
assumeNoException(e); assumeNoException(e);
} }
try (Resource base = newResource(testdir.getDir())) try (Resource base = newResource(dir.toFile()))
{ {
// FileResource does not pass this test! // FileResource does not pass this test!
assumeFalse(base instanceof FileResource); assumeFalse(base instanceof FileResource);
@ -954,12 +996,13 @@ public class FileSystemResourceTest
@Test @Test
public void testPipe() throws Exception public void testPipe() throws Exception
{ {
File dir = testdir.getDir(); Path dir = testdir.getDir().toPath().normalize().toRealPath();
Files.createDirectories(dir);
try try
{ {
// attempt to create file // attempt to create file
Path foo = new File(dir, "foo|bar.txt").toPath(); Path foo = dir.resolve("foo|bar.txt");
Files.createFile(foo); Files.createFile(foo);
} }
catch (Exception e) catch (Exception e)
@ -969,7 +1012,7 @@ public class FileSystemResourceTest
assumeNoException(e); assumeNoException(e);
} }
try (Resource base = newResource(testdir.getDir())) try (Resource base = newResource(dir.toFile()))
{ {
// FileResource does not pass this test! // FileResource does not pass this test!
assumeFalse(base instanceof FileResource); assumeFalse(base instanceof FileResource);
@ -986,7 +1029,11 @@ public class FileSystemResourceTest
@Test @Test
public void testExist_Normal() throws Exception public void testExist_Normal() throws Exception
{ {
createEmptyFile("a.jsp"); Path dir = testdir.getDir().toPath().normalize().toRealPath();
Files.createDirectories(dir);
Path path = dir.resolve("a.jsp");
Files.createFile(path);
URI ref = testdir.getDir().toURI().resolve("a.jsp"); URI ref = testdir.getDir().toURI().resolve("a.jsp");
try (Resource fileres = newResource(ref)) try (Resource fileres = newResource(ref))
@ -998,18 +1045,28 @@ public class FileSystemResourceTest
@Test @Test
public void testSingleQuoteInFileName() throws Exception public void testSingleQuoteInFileName() throws Exception
{ {
createEmptyFile("foo's.txt"); Path dir = testdir.getDir().toPath().normalize().toRealPath();
createEmptyFile("f o's.txt"); Files.createDirectories(dir);
URI refQuoted = testdir.getDir().toURI().resolve("foo's.txt"); Path fooA = dir.resolve("foo's.txt");
Path fooB = dir.resolve("f o's.txt");
Files.createFile(fooA);
Files.createFile(fooB);
URI refQuoted = dir.resolve("foo's.txt").toUri();
try (Resource fileres = newResource(refQuoted)) try (Resource fileres = newResource(refQuoted))
{ {
// FileResource on OSX does not pass this test!
// on OSX "foo's.txt" with FileResource becomes "foo%27s.txt" incorrectly.
assumeFalse( (fileres instanceof FileResource) && (OS.IS_OSX) );
assertThat("Exists: " + refQuoted,fileres.exists(),is(true)); assertThat("Exists: " + refQuoted,fileres.exists(),is(true));
assertThat("Alias: " + refQuoted,fileres,hasNoAlias()); assertThat("Alias: " + refQuoted,fileres,hasNoAlias());
} }
URI refEncoded = testdir.getDir().toURI().resolve("foo%27s.txt"); URI refEncoded = dir.toUri().resolve("foo%27s.txt");
try (Resource fileres = newResource(refEncoded)) try (Resource fileres = newResource(refEncoded))
{ {
@ -1017,7 +1074,7 @@ public class FileSystemResourceTest
assertThat("Alias: " + refEncoded,fileres,hasNoAlias()); assertThat("Alias: " + refEncoded,fileres,hasNoAlias());
} }
URI refQuoteSpace = testdir.getDir().toURI().resolve("f%20o's.txt"); URI refQuoteSpace = dir.toUri().resolve("f%20o's.txt");
try (Resource fileres = newResource(refQuoteSpace)) try (Resource fileres = newResource(refQuoteSpace))
{ {
@ -1025,7 +1082,7 @@ public class FileSystemResourceTest
assertThat("Alias: " + refQuoteSpace,fileres,hasNoAlias()); assertThat("Alias: " + refQuoteSpace,fileres,hasNoAlias());
} }
URI refEncodedSpace = testdir.getDir().toURI().resolve("f%20o%27s.txt"); URI refEncodedSpace = dir.toUri().resolve("f%20o%27s.txt");
try (Resource fileres = newResource(refEncodedSpace)) try (Resource fileres = newResource(refEncodedSpace))
{ {
@ -1033,8 +1090,8 @@ public class FileSystemResourceTest
assertThat("Alias: " + refEncodedSpace,fileres,hasNoAlias()); assertThat("Alias: " + refEncodedSpace,fileres,hasNoAlias());
} }
URI refA = testdir.getDir().toURI().resolve("foo's.txt"); URI refA = dir.toUri().resolve("foo's.txt");
URI refB = testdir.getDir().toURI().resolve("foo%27s.txt"); URI refB = dir.toUri().resolve("foo%27s.txt");
StringBuilder msg = new StringBuilder(); StringBuilder msg = new StringBuilder();
msg.append("URI[a].equals(URI[b])").append(System.lineSeparator()); msg.append("URI[a].equals(URI[b])").append(System.lineSeparator());
@ -1054,7 +1111,11 @@ public class FileSystemResourceTest
@Test @Test
public void testExist_BadURINull() throws Exception public void testExist_BadURINull() throws Exception
{ {
createEmptyFile("a.jsp"); Path dir = testdir.getDir().toPath().normalize().toRealPath();
Files.createDirectories(dir);
Path path = dir.resolve("a.jsp");
Files.createFile(path);
try try
{ {
@ -1076,7 +1137,11 @@ public class FileSystemResourceTest
@Test @Test
public void testExist_BadURINullX() throws Exception public void testExist_BadURINullX() throws Exception
{ {
createEmptyFile("a.jsp"); Path dir = testdir.getDir().toPath().normalize().toRealPath();
Files.createDirectories(dir);
Path path = dir.resolve("a.jsp");
Files.createFile(path);
try try
{ {
@ -1098,8 +1163,13 @@ public class FileSystemResourceTest
@Test @Test
public void testEncoding() throws Exception public void testEncoding() throws Exception
{ {
File specials = testdir.getFile("a file with,spe#ials"); Path dir = testdir.getDir().toPath().normalize().toRealPath();
try(Resource res= newResource(specials)) Files.createDirectories(dir);
Path specials = dir.resolve("a file with,spe#ials");
Files.createFile(specials);
try(Resource res = newResource(specials.toFile()))
{ {
assertThat("Specials URL", res.getURI().toASCIIString(), containsString("a%20file%20with,spe%23ials")); assertThat("Specials URL", res.getURI().toASCIIString(), containsString("a%20file%20with,spe%23ials"));
assertThat("Specials Filename", res.getFile().toString(), containsString("a file with,spe#ials")); assertThat("Specials Filename", res.getFile().toString(), containsString("a file with,spe#ials"));
@ -1108,20 +1178,25 @@ public class FileSystemResourceTest
assertThat("File should have been deleted.",res.exists(),is(false)); assertThat("File should have been deleted.",res.exists(),is(false));
} }
} }
@Test @Test
public void testUtf8Dir() throws Exception public void testUtf8Dir() throws Exception
{ {
File dir=new File(testdir.getDir(),"bãm"); Path dir = testdir.getDir().toPath().normalize().toRealPath();
dir.mkdir(); Path utf8Dir = dir.resolve("bãm");
File file = new File(dir,"file.txt"); Files.createDirectories(utf8Dir);
file.createNewFile();
Resource base = newResource(dir); Path file = utf8Dir.resolve("file.txt");
assertNull(base.getAlias()); Files.createFile(file);
Resource r = base.addPath("file.txt"); try (Resource base = newResource(utf8Dir.toFile()))
assertNull(r.getAlias()); {
assertThat("Exists: " + utf8Dir,base.exists(),is(true));
assertThat("Alias: " + utf8Dir,base,hasNoAlias());
Resource r = base.addPath("file.txt");
assertThat("Exists: " + r,r.exists(),is(true));
assertThat("Alias: " + r,r,hasNoAlias());
}
} }
} }