diff --git a/core-java/src/test/java/com/baeldung/java/nio2/FileUnitTest.java b/core-java/src/test/java/com/baeldung/java/nio2/FileUnitTest.java deleted file mode 100644 index a94c71fb17..0000000000 --- a/core-java/src/test/java/com/baeldung/java/nio2/FileUnitTest.java +++ /dev/null @@ -1,250 +0,0 @@ -package com.baeldung.java.nio2; - -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -import java.io.IOException; -import java.nio.file.CopyOption; -import java.nio.file.DirectoryNotEmptyException; -import java.nio.file.FileAlreadyExistsException; -import java.nio.file.Files; -import java.nio.file.NoSuchFileException; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.nio.file.StandardCopyOption; -import java.util.Date; - -import org.junit.Test; - -public class FileUnitTest { - private static final String HOME = System.getProperty("user.home"); - - // checking file or dir - @Test - public void givenExistentPath_whenConfirmsFileExists_thenCorrect() { - Path p = Paths.get(HOME); - assertTrue(Files.exists(p)); - } - - @Test - public void givenNonexistentPath_whenConfirmsFileNotExists_thenCorrect() { - Path p = Paths.get(HOME + "/inexistent_file.txt"); - assertTrue(Files.notExists(p)); - } - - @Test - public void givenExistentDirPath_whenConfirmsNotRegularFile_thenCorrect() { - Path p = Paths.get(HOME); - assertFalse(Files.isRegularFile(p)); - } - - @Test - public void givenExistentDirPath_whenConfirmsReadable_thenCorrect() { - Path p = Paths.get(HOME); - assertTrue(Files.isReadable(p)); - } - - @Test - public void givenExistentDirPath_whenConfirmsWritable_thenCorrect() { - Path p = Paths.get(HOME); - assertTrue(Files.isWritable(p)); - } - - @Test - public void givenExistentDirPath_whenConfirmsExecutable_thenCorrect() { - Path p = Paths.get(HOME); - assertTrue(Files.isExecutable(p)); - } - - @Test - public void givenSameFilePaths_whenConfirmsIsSame_thenCorrect() throws IOException { - Path p1 = Paths.get(HOME); - Path p2 = Paths.get(HOME); - assertTrue(Files.isSameFile(p1, p2)); - } - - // reading, writing and creating files - // creating file - @Test - public void givenFilePath_whenCreatesNewFile_thenCorrect() throws IOException { - String fileName = "myfile_" + new Date().getTime() + ".txt"; - Path p = Paths.get(HOME + "/" + fileName); - assertFalse(Files.exists(p)); - Files.createFile(p); - assertTrue(Files.exists(p)); - - } - - @Test - public void givenDirPath_whenCreatesNewDir_thenCorrect() throws IOException { - String dirName = "myDir_" + new Date().getTime(); - Path p = Paths.get(HOME + "/" + dirName); - assertFalse(Files.exists(p)); - Files.createDirectory(p); - assertTrue(Files.exists(p)); - assertFalse(Files.isRegularFile(p)); - assertTrue(Files.isDirectory(p)); - - } - - @Test(expected = NoSuchFileException.class) - public void givenDirPath_whenFailsToCreateRecursively_thenCorrect() throws IOException { - String dirName = "myDir_" + new Date().getTime() + "/subdir"; - Path p = Paths.get(HOME + "/" + dirName); - assertFalse(Files.exists(p)); - Files.createDirectory(p); - - } - - @Test - public void givenDirPath_whenCreatesRecursively_thenCorrect() throws IOException { - Path dir = Paths.get(HOME + "/myDir_" + new Date().getTime()); - Path subdir = dir.resolve("subdir"); - assertFalse(Files.exists(dir)); - assertFalse(Files.exists(subdir)); - Files.createDirectories(subdir); - assertTrue(Files.exists(dir)); - assertTrue(Files.exists(subdir)); - } - - @Test - public void givenFilePath_whenCreatesTempFile_thenCorrect() throws IOException { - String prefix = "log_"; - String suffix = ".txt"; - Path p = Paths.get(HOME + "/"); - p = Files.createTempFile(p, prefix, suffix); - // like log_8821081429012075286.txt - assertTrue(Files.exists(p)); - - } - - @Test - public void givenFilePath_whenCreatesTempFileWithDefaultsNaming_thenCorrect() throws IOException { - Path p = Paths.get(HOME + "/"); - p = Files.createTempFile(p, null, null); - // like 8600179353689423985.tmp - assertTrue(Files.exists(p)); - } - - @Test - public void givenNoFilePath_whenCreatesTempFileInTempDir_thenCorrect() throws IOException { - Path p = Files.createTempFile(null, null); - // like C:\Users\new\AppData\Local\Temp\6100927974988978748.tmp - assertTrue(Files.exists(p)); - - } - - // delete file - @Test - public void givenPath_whenDeletes_thenCorrect() throws IOException { - Path p = Paths.get(HOME + "/fileToDelete.txt"); - assertFalse(Files.exists(p)); - Files.createFile(p); - assertTrue(Files.exists(p)); - Files.delete(p); - assertFalse(Files.exists(p)); - - } - - @Test(expected = DirectoryNotEmptyException.class) - public void givenPath_whenFailsToDeleteNonEmptyDir_thenCorrect() throws IOException { - Path dir = Paths.get(HOME + "/emptyDir" + new Date().getTime()); - Files.createDirectory(dir); - assertTrue(Files.exists(dir)); - Path file = dir.resolve("file.txt"); - Files.createFile(file); - Files.delete(dir); - - assertTrue(Files.exists(dir)); - - } - - @Test - public void givenInexistentFile_whenDeleteFails_thenCorrect() throws IOException { - Path p = Paths.get(HOME + "/inexistentFile.txt"); - assertFalse(Files.exists(p)); - try { - Files.delete(p); - } catch (IOException e) { - assertTrue(e instanceof NoSuchFileException); - } - - } - - @Test - public void givenInexistentFile_whenDeleteIfExistsWorks_thenCorrect() throws IOException { - Path p = Paths.get(HOME + "/inexistentFile.txt"); - assertFalse(Files.exists(p)); - Files.deleteIfExists(p); - - } - - // copy file - @Test - public void givenFilePath_whenCopiesToNewLocation_thenCorrect() throws IOException { - Path dir1 = Paths.get(HOME + "/firstdir_" + new Date().getTime()); - Path dir2 = Paths.get(HOME + "/otherdir_" + new Date().getTime()); - Files.createDirectory(dir1); - Files.createDirectory(dir2); - Path file1 = dir1.resolve("filetocopy.txt"); - Path file2 = dir2.resolve("filetocopy.txt"); - Files.createFile(file1); - assertTrue(Files.exists(file1)); - assertFalse(Files.exists(file2)); - Files.copy(file1, file2); - assertTrue(Files.exists(file2)); - - } - - @Test(expected = FileAlreadyExistsException.class) - public void givenPath_whenCopyFailsDueToExistingFile_thenCorrect() throws IOException { - Path dir1 = Paths.get(HOME + "/firstdir_" + new Date().getTime()); - Path dir2 = Paths.get(HOME + "/otherdir_" + new Date().getTime()); - Files.createDirectory(dir1); - Files.createDirectory(dir2); - Path file1 = dir1.resolve("filetocopy.txt"); - Path file2 = dir2.resolve("filetocopy.txt"); - Files.createFile(file1); - Files.createFile(file2); - assertTrue(Files.exists(file1)); - assertTrue(Files.exists(file2)); - Files.copy(file1, file2); - Files.copy(file1, file2, StandardCopyOption.REPLACE_EXISTING); - } - - // moving files - @Test - public void givenFilePath_whenMovesToNewLocation_thenCorrect() throws IOException { - Path dir1 = Paths.get(HOME + "/firstdir_" + new Date().getTime()); - Path dir2 = Paths.get(HOME + "/otherdir_" + new Date().getTime()); - Files.createDirectory(dir1); - Files.createDirectory(dir2); - Path file1 = dir1.resolve("filetocopy.txt"); - Path file2 = dir2.resolve("filetocopy.txt"); - Files.createFile(file1); - assertTrue(Files.exists(file1)); - assertFalse(Files.exists(file2)); - Files.move(file1, file2); - assertTrue(Files.exists(file2)); - assertFalse(Files.exists(file1)); - - } - - @Test(expected = FileAlreadyExistsException.class) - public void givenFilePath_whenMoveFailsDueToExistingFile_thenCorrect() throws IOException { - Path dir1 = Paths.get(HOME + "/firstdir_" + new Date().getTime()); - Path dir2 = Paths.get(HOME + "/otherdir_" + new Date().getTime()); - Files.createDirectory(dir1); - Files.createDirectory(dir2); - Path file1 = dir1.resolve("filetocopy.txt"); - Path file2 = dir2.resolve("filetocopy.txt"); - Files.createFile(file1); - Files.createFile(file2); - assertTrue(Files.exists(file1)); - assertTrue(Files.exists(file2)); - Files.move(file1, file2); - Files.move(file1, file2, StandardCopyOption.REPLACE_EXISTING); - assertTrue(Files.exists(file2)); - assertFalse(Files.exists(file1)); - } -} diff --git a/core-java/src/test/java/com/baeldung/java/nio2/PathManualTest.java b/core-java/src/test/java/com/baeldung/java/nio2/PathManualTest.java deleted file mode 100644 index 83f8c64ff9..0000000000 --- a/core-java/src/test/java/com/baeldung/java/nio2/PathManualTest.java +++ /dev/null @@ -1,196 +0,0 @@ -package com.baeldung.java.nio2; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -import java.io.IOException; -import java.net.URI; -import java.nio.file.Path; -import java.nio.file.Paths; - -import org.junit.Test; - -public class PathManualTest { - - private static final String HOME = System.getProperty("user.home"); - - // creating a path - @Test - public void givenPathString_whenCreatesPathObject_thenCorrect() { - final Path p = Paths.get("/articles/baeldung"); - assertEquals("\\articles\\baeldung", p.toString()); - - } - - @Test - public void givenPathParts_whenCreatesPathObject_thenCorrect() { - final Path p = Paths.get("/articles", "baeldung"); - assertEquals("\\articles\\baeldung", p.toString()); - - } - - // retrieving path info - @Test - public void givenPath_whenRetrievesFileName_thenCorrect() { - final Path p = Paths.get("/articles/baeldung/logs"); - assertEquals("logs", p.getFileName().toString()); - } - - @Test - public void givenPath_whenRetrievesNameByIndex_thenCorrect() { - 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()); - } - - @Test - public void givenPath_whenCountsParts_thenCorrect() { - final Path p = Paths.get("/articles/baeldung/logs"); - assertEquals(3, p.getNameCount()); - } - - @Test - public void givenPath_whenCanRetrieveSubsequenceByIndex_thenCorrect() { - 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()); - assertEquals("baeldung", p.subpath(1, 2).toString()); - assertEquals("baeldung\\logs", p.subpath(1, 3).toString()); - assertEquals("logs", p.subpath(2, 3).toString()); - } - - @Test - public void givenPath_whenRetrievesParent_thenCorrect() { - 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()); - assertEquals("\\", p3.getParent().toString()); - assertEquals(null, p4.getParent()); - } - - @Test - public void givenPath_whenRetrievesRoot_thenCorrect() { - 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()); - } - - // removing redundancies from path - @Test - public void givenPath_whenRemovesRedundancies_thenCorrect1() { - Path p = Paths.get("/home/./baeldung/articles"); - p = p.normalize(); - assertEquals("\\home\\baeldung\\articles", p.toString()); - } - - @Test - public void givenPath_whenRemovesRedundancies_thenCorrect2() { - Path p = Paths.get("/home/baeldung/../articles"); - p = p.normalize(); - assertEquals("\\home\\articles", p.toString()); - } - - // converting a path - @Test - public void givenPath_whenConvertsToBrowseablePath_thenCorrect() { - 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() { - final Path p = Paths.get("/home/baeldung/articles.html"); - assertEquals("E:\\home\\baeldung\\articles.html", p.toAbsolutePath().toString()); - } - - @Test - public void givenAbsolutePath_whenRetainsAsAbsolute_thenCorrect() { - 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 { - final Path p = Paths.get(HOME); - assertEquals(HOME, p.toRealPath().toString()); - } - - @Test - public void givenInExistentPath_whenFailsToConvert_thenCorrect() { - final Path p = Paths.get("E:\\home\\baeldung\\articles.html"); - try { - p.toRealPath(); - } catch (final IOException e) { - assertTrue(true); - } - } - - // joining paths - @Test - public void givenTwoPaths_whenJoinsAndResolves_thenCorrect() throws IOException { - final Path p = Paths.get("/baeldung/articles"); - assertEquals("\\baeldung\\articles\\java", p.resolve("java").toString()); - } - - @Test - public void givenAbsolutePath_whenResolutionRetainsIt_thenCorrect() throws IOException { - 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 { - 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 { - 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 { - 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()); - } - - // comparing 2 paths - @Test - public void givenTwoPaths_whenTestsEquality_thenCorrect() throws IOException { - 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)); - } - - @Test - public void givenPath_whenInspectsStart_thenCorrect() { - final Path p1 = Paths.get("/baeldung/articles"); - assertTrue(p1.startsWith("/baeldung")); - } - - @Test - public void givenPath_whenInspectsEnd_thenCorrect() { - final Path p1 = Paths.get("/baeldung/articles"); - assertTrue(p1.endsWith("articles")); - } -} diff --git a/core-java/src/test/java/com/baeldung/java/nio2/PathTest.java b/core-java/src/test/java/com/baeldung/java/nio2/PathTest.java index c0cb45341e..004aeb3deb 100644 --- a/core-java/src/test/java/com/baeldung/java/nio2/PathTest.java +++ b/core-java/src/test/java/com/baeldung/java/nio2/PathTest.java @@ -6,6 +6,7 @@ import static org.junit.Assert.assertTrue; import java.io.IOException; import java.net.URI; +import java.nio.file.NoSuchFileException; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Date; @@ -126,14 +127,11 @@ public class PathTest { assertEquals(HOME, p.toRealPath().toString()); } - @Test - public void givenInExistentPath_whenFailsToConvert_thenCorrect() { + @Test(expected = NoSuchFileException.class) + public void givenInExistentPath_whenFailsToConvert_thenCorrect() throws IOException { Path p = Paths.get("E:\\home\\baeldung\\articles.html"); - try { - p.toRealPath(); - } catch (IOException e) { - assertTrue(true); - } + + p.toRealPath(); } // joining paths