diff options
author | Håkon Hallingstad <hakon@verizonmedia.com> | 2020-03-27 19:51:39 +0100 |
---|---|---|
committer | Håkon Hallingstad <hakon@verizonmedia.com> | 2020-03-27 19:51:39 +0100 |
commit | 890c7594b2c2da2dd8eb18939696fdf8fc0ea274 (patch) | |
tree | c0883810627e2abfcafa5ea19ce7cdb4b3899146 /node-admin/src/test | |
parent | a51ae610f0a825315b56dce1c6015b34fdd411b9 (diff) |
UnixPath support for atomicWrite, getFilename, getParent, setLastModifiedTime
Diffstat (limited to 'node-admin/src/test')
-rw-r--r-- | node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/file/UnixPathTest.java | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/file/UnixPathTest.java b/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/file/UnixPathTest.java index 3b839f7f446..3159689c22e 100644 --- a/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/file/UnixPathTest.java +++ b/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/file/UnixPathTest.java @@ -3,6 +3,7 @@ package com.yahoo.vespa.hosted.node.admin.task.util.file; import com.yahoo.vespa.test.file.TestFileSystem; +import org.junit.ComparisonFailure; import org.junit.Test; import java.nio.file.FileSystem; @@ -13,6 +14,7 @@ import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; /** * @author hakonhall @@ -124,4 +126,44 @@ public class UnixPathTest { assertFalse(dir1 + " deleted recursively", Files.exists(dir1)); } + @Test + public void atomicWrite() { + var path = new UnixPath(fs.getPath("/dir/foo")); + path.createParents(); + path.writeUtf8File("bar"); + path.atomicWriteUt8("bar v2"); + assertEquals("bar v2", path.readUtf8File()); + } + + @Test + public void testParentAndFilename() { + var absolutePath = new UnixPath("/foo/bar"); + assertEquals("/foo", absolutePath.getParent().toString()); + assertEquals("bar", absolutePath.getFilename()); + + var pathWithoutSlash = new UnixPath("foo"); + assertRuntimeException(IllegalStateException.class, "Path has no parent directory: 'foo'", () -> pathWithoutSlash.getParent()); + assertEquals("foo", pathWithoutSlash.getFilename()); + + var pathWithSlash = new UnixPath("/foo"); + assertEquals("/", pathWithSlash.getParent().toString()); + assertEquals("foo", pathWithSlash.getFilename()); + + assertRuntimeException(IllegalStateException.class, "Path has no parent directory: '/'", () -> new UnixPath("/").getParent()); + assertRuntimeException(IllegalStateException.class, "Path has no filename: '/'", () -> new UnixPath("/").getFilename()); + } + + private <T extends RuntimeException> void assertRuntimeException(Class<T> baseClass, String message, Runnable runnable) { + try { + runnable.run(); + fail("No exception was thrown"); + } catch (RuntimeException e) { + if (!baseClass.isInstance(e)) { + throw new ComparisonFailure("Exception class mismatch", baseClass.getName(), e.getClass().getName()); + } + + assertEquals(message, e.getMessage()); + } + } + } |