This commit is contained in:
DOHA 2016-10-30 13:12:08 +02:00
parent 1110c53d81
commit 2fcf097d88
1 changed files with 32 additions and 33 deletions

View File

@ -8,25 +8,24 @@ import java.io.IOException;
import java.net.URI;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Date;
import org.junit.Test;
public class PathTest {
public class PathManualTest {
private static final String HOME = System.getProperty("user.home");
// creating a path
@Test
public void givenPathString_whenCreatesPathObject_thenCorrect() {
Path p = Paths.get("/articles/baeldung");
final Path p = Paths.get("/articles/baeldung");
assertEquals("\\articles\\baeldung", p.toString());
}
@Test
public void givenPathParts_whenCreatesPathObject_thenCorrect() {
Path p = Paths.get("/articles", "baeldung");
final Path p = Paths.get("/articles", "baeldung");
assertEquals("\\articles\\baeldung", p.toString());
}
@ -34,13 +33,13 @@ public class PathTest {
// retrieving path info
@Test
public void givenPath_whenRetrievesFileName_thenCorrect() {
Path p = Paths.get("/articles/baeldung/logs");
final Path p = Paths.get("/articles/baeldung/logs");
assertEquals("logs", p.getFileName().toString());
}
@Test
public void givenPath_whenRetrievesNameByIndex_thenCorrect() {
Path p = Paths.get("/articles/baeldung/logs");
final Path p = Paths.get("/articles/baeldung/logs");
assertEquals("articles", p.getName(0).toString());
assertEquals("baeldung", p.getName(1).toString());
assertEquals("logs", p.getName(2).toString());
@ -48,13 +47,13 @@ public class PathTest {
@Test
public void givenPath_whenCountsParts_thenCorrect() {
Path p = Paths.get("/articles/baeldung/logs");
final Path p = Paths.get("/articles/baeldung/logs");
assertEquals(3, p.getNameCount());
}
@Test
public void givenPath_whenCanRetrieveSubsequenceByIndex_thenCorrect() {
Path p = Paths.get("/articles/baeldung/logs");
final Path p = Paths.get("/articles/baeldung/logs");
assertEquals("articles", p.subpath(0, 1).toString());
assertEquals("articles\\baeldung", p.subpath(0, 2).toString());
assertEquals("articles\\baeldung\\logs", p.subpath(0, 3).toString());
@ -65,10 +64,10 @@ public class PathTest {
@Test
public void givenPath_whenRetrievesParent_thenCorrect() {
Path p1 = Paths.get("/articles/baeldung/logs");
Path p2 = Paths.get("/articles/baeldung");
Path p3 = Paths.get("/articles");
Path p4 = Paths.get("/");
final Path p1 = Paths.get("/articles/baeldung/logs");
final Path p2 = Paths.get("/articles/baeldung");
final Path p3 = Paths.get("/articles");
final Path p4 = Paths.get("/");
assertEquals("\\articles\\baeldung", p1.getParent().toString());
assertEquals("\\articles", p2.getParent().toString());
@ -78,8 +77,8 @@ public class PathTest {
@Test
public void givenPath_whenRetrievesRoot_thenCorrect() {
Path p1 = Paths.get("/articles/baeldung/logs");
Path p2 = Paths.get("c:/articles/baeldung/logs");
final Path p1 = Paths.get("/articles/baeldung/logs");
final Path p2 = Paths.get("c:/articles/baeldung/logs");
assertEquals("\\", p1.getRoot().toString());
assertEquals("c:\\", p2.getRoot().toString());
@ -103,35 +102,35 @@ public class PathTest {
// converting a path
@Test
public void givenPath_whenConvertsToBrowseablePath_thenCorrect() {
Path p = Paths.get("/home/baeldung/articles.html");
URI uri = p.toUri();
final Path p = Paths.get("/home/baeldung/articles.html");
final URI uri = p.toUri();
assertEquals("file:///E:/home/baeldung/articles.html", uri.toString());
}
@Test
public void givenPath_whenConvertsToAbsolutePath_thenCorrect() {
Path p = Paths.get("/home/baeldung/articles.html");
final Path p = Paths.get("/home/baeldung/articles.html");
assertEquals("E:\\home\\baeldung\\articles.html", p.toAbsolutePath().toString());
}
@Test
public void givenAbsolutePath_whenRetainsAsAbsolute_thenCorrect() {
Path p = Paths.get("E:\\home\\baeldung\\articles.html");
final Path p = Paths.get("E:\\home\\baeldung\\articles.html");
assertEquals("E:\\home\\baeldung\\articles.html", p.toAbsolutePath().toString());
}
@Test
public void givenExistingPath_whenGetsRealPathToFile_thenCorrect() throws IOException {
Path p = Paths.get(HOME);
final Path p = Paths.get(HOME);
assertEquals(HOME, p.toRealPath().toString());
}
@Test
public void givenInExistentPath_whenFailsToConvert_thenCorrect() {
Path p = Paths.get("E:\\home\\baeldung\\articles.html");
final Path p = Paths.get("E:\\home\\baeldung\\articles.html");
try {
p.toRealPath();
} catch (IOException e) {
} catch (final IOException e) {
assertTrue(true);
}
}
@ -139,35 +138,35 @@ public class PathTest {
// joining paths
@Test
public void givenTwoPaths_whenJoinsAndResolves_thenCorrect() throws IOException {
Path p = Paths.get("/baeldung/articles");
final Path p = Paths.get("/baeldung/articles");
assertEquals("\\baeldung\\articles\\java", p.resolve("java").toString());
}
@Test
public void givenAbsolutePath_whenResolutionRetainsIt_thenCorrect() throws IOException {
Path p = Paths.get("/baeldung/articles");
final Path p = Paths.get("/baeldung/articles");
assertEquals("C:\\baeldung\\articles\\java", p.resolve("C:\\baeldung\\articles\\java").toString());
}
@Test
public void givenPathWithRoot_whenResolutionRetainsIt_thenCorrect2() throws IOException {
Path p = Paths.get("/baeldung/articles");
final Path p = Paths.get("/baeldung/articles");
assertEquals("\\java", p.resolve("/java").toString());
}
// creating a path between 2 paths
@Test
public void givenSiblingPaths_whenCreatesPathToOther_thenCorrect() throws IOException {
Path p1 = Paths.get("articles");
Path p2 = Paths.get("authors");
final Path p1 = Paths.get("articles");
final Path p2 = Paths.get("authors");
assertEquals("..\\authors", p1.relativize(p2).toString());
assertEquals("..\\articles", p2.relativize(p1).toString());
}
@Test
public void givenNonSiblingPaths_whenCreatesPathToOther_thenCorrect() throws IOException {
Path p1 = Paths.get("/baeldung");
Path p2 = Paths.get("/baeldung/authors/articles");
final Path p1 = Paths.get("/baeldung");
final Path p2 = Paths.get("/baeldung/authors/articles");
assertEquals("authors\\articles", p1.relativize(p2).toString());
assertEquals("..\\..", p2.relativize(p1).toString());
}
@ -175,9 +174,9 @@ public class PathTest {
// comparing 2 paths
@Test
public void givenTwoPaths_whenTestsEquality_thenCorrect() throws IOException {
Path p1 = Paths.get("/baeldung/articles");
Path p2 = Paths.get("/baeldung/articles");
Path p3 = Paths.get("/baeldung/authors");
final Path p1 = Paths.get("/baeldung/articles");
final Path p2 = Paths.get("/baeldung/articles");
final Path p3 = Paths.get("/baeldung/authors");
assertTrue(p1.equals(p2));
assertFalse(p1.equals(p3));
@ -185,13 +184,13 @@ public class PathTest {
@Test
public void givenPath_whenInspectsStart_thenCorrect() {
Path p1 = Paths.get("/baeldung/articles");
final Path p1 = Paths.get("/baeldung/articles");
assertTrue(p1.startsWith("/baeldung"));
}
@Test
public void givenPath_whenInspectsEnd_thenCorrect() {
Path p1 = Paths.get("/baeldung/articles");
final Path p1 = Paths.get("/baeldung/articles");
assertTrue(p1.endsWith("articles"));
}
}