Convert @SitePath from File to Path

This saves code in some cases, as it allows us to use some of the
nicer Java 7 Files static methods. Mostly, though, it opens the door
to using an in-memory filesystem in tests. Eventually there are even
more fun possibilities, like teaching parts of SitePaths to be read
out of somewhere else entirely (like a git repository).

Change-Id: Ifa13772a79ded03049bd9f62ade6e25d19e5bb05
This commit is contained in:
Dave Borowitz
2015-02-23 10:32:23 -08:00
parent 0f092fe518
commit 8e481f3c33
20 changed files with 181 additions and 136 deletions

View File

@@ -28,85 +28,86 @@ import org.junit.Test;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
public class SitePathsTest {
@Test
public void testCreate_NotExisting() throws IOException {
final File root = random();
final Path root = random();
final SitePaths site = new SitePaths(root);
assertTrue(site.isNew);
assertEquals(root, site.site_path);
assertEquals(new File(root, "etc"), site.etc_dir);
assertEquals(root.toFile(), site.site_path);
assertEquals(root.resolve("etc").toFile(), site.etc_dir);
}
@Test
public void testCreate_Empty() throws IOException {
final File root = random();
final Path root = random();
try {
assertTrue(root.mkdir());
Files.createDirectory(root);
final SitePaths site = new SitePaths(root);
assertTrue(site.isNew);
assertEquals(root, site.site_path);
assertEquals(root.toFile(), site.site_path);
} finally {
root.delete();
Files.delete(root);
}
}
@Test
public void testCreate_NonEmpty() throws IOException {
final File root = random();
final File txt = new File(root, "test.txt");
final Path root = random();
final Path txt = root.resolve("test.txt");
try {
assertTrue(root.mkdir());
assertTrue(txt.createNewFile());
Files.createDirectory(root);
Files.createFile(txt);
final SitePaths site = new SitePaths(root);
assertFalse(site.isNew);
assertEquals(root, site.site_path);
assertEquals(root.toFile(), site.site_path);
} finally {
txt.delete();
root.delete();
Files.delete(txt);
Files.delete(root);
}
}
@Test
public void testCreate_NotDirectory() throws IOException {
final File root = random();
final Path root = random();
try {
assertTrue(root.createNewFile());
Files.createFile(root);
try {
new SitePaths(root);
fail("Did not throw exception");
} catch (FileNotFoundException e) {
assertEquals("Not a directory: " + root.getPath(), e.getMessage());
assertEquals("Not a directory: " + root, e.getMessage());
}
} finally {
root.delete();
Files.delete(root);
}
}
@Test
public void testResolve() throws IOException {
final File root = random();
final Path root = random();
final SitePaths site = new SitePaths(root);
assertNull(site.resolve(null));
assertNull(site.resolve(""));
assertNotNull(site.resolve("a"));
assertEquals(new File(root, "a").getCanonicalFile(), site.resolve("a"));
assertEquals(root.resolve("a").toAbsolutePath().normalize().toFile(),
site.resolve("a"));
final String pfx = HostPlatform.isWin32() ? "C:/" : "/";
assertNotNull(site.resolve(pfx + "a"));
assertEquals(new File(pfx + "a").getCanonicalFile(), site.resolve(pfx + "a"));
}
private static File random() throws IOException {
File tmp = File.createTempFile("gerrit_test_", "_site");
if (!tmp.delete()) {
throw new IOException("Cannot create " + tmp.getPath());
}
private static Path random() throws IOException {
Path tmp = Files.createTempFile("gerrit_test_", "_site");
Files.deleteIfExists(tmp);
return tmp;
}
}