From 49606337a5a30edeafbc25b80f01cfa5aa9b6b4a Mon Sep 17 00:00:00 2001 From: Kevin Kraus Date: Fri, 6 Sep 2019 22:20:33 -0500 Subject: [PATCH 1/8] BAEL-3227 --- .../com/baeldung/files/CreatingFilesTest.java | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 core-java-modules/core-java/src/test/java/com/baeldung/files/CreatingFilesTest.java diff --git a/core-java-modules/core-java/src/test/java/com/baeldung/files/CreatingFilesTest.java b/core-java-modules/core-java/src/test/java/com/baeldung/files/CreatingFilesTest.java new file mode 100644 index 0000000000..1380846e24 --- /dev/null +++ b/core-java-modules/core-java/src/test/java/com/baeldung/files/CreatingFilesTest.java @@ -0,0 +1,29 @@ +import com.google.common.io.Files; +import org.junit.Test; + +import java.io.File; +import java.io.IOException; + +public class CreateFilesTest { + @Test(expected = IOException.class) + public void whenCreatingAFileWithAbsolutePath_thenExceptionIsThrown() throws IOException { + File fileWithAbsolutePath = new File("/myDirectory/testFile.txt"); + + Files.touch(fileWithAbsolutePath); + } + + @Test(expected = IOException.class) + public void givenAnExistingDirectory_whenCreatingANewDirectoryAndFile_thenExceptionIsThrown() throws IOException { + File tempDirectory = new File(System.getProperty("java.io.tmpdir")); + File fileWithRelativePath = new File(tempDirectory, "myDirectory/newFile.txt"); + + Files.touch(fileWithRelativePath); + } + + @Test(expected = IOException.class) + public void whenCreatingAFileWithFileSeparator_thenPathIsCreated() throws IOException { + File newFile = new File("src" + File.separator + "test" + File.separator + "resources" + File.separator + "newFile.txt"); + + Files.touch(newFile); + } +} From ed459a57863d0261cb724fdc1da6fd0c09af37a3 Mon Sep 17 00:00:00 2001 From: Kevin Kraus Date: Fri, 6 Sep 2019 22:40:06 -0500 Subject: [PATCH 2/8] BAEL-3227 renamed test --- .../{CreatingFilesTest.java => CreatingFilesUnitTest.java} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename core-java-modules/core-java/src/test/java/com/baeldung/files/{CreatingFilesTest.java => CreatingFilesUnitTest.java} (96%) diff --git a/core-java-modules/core-java/src/test/java/com/baeldung/files/CreatingFilesTest.java b/core-java-modules/core-java/src/test/java/com/baeldung/files/CreatingFilesUnitTest.java similarity index 96% rename from core-java-modules/core-java/src/test/java/com/baeldung/files/CreatingFilesTest.java rename to core-java-modules/core-java/src/test/java/com/baeldung/files/CreatingFilesUnitTest.java index 1380846e24..1f2c1ec505 100644 --- a/core-java-modules/core-java/src/test/java/com/baeldung/files/CreatingFilesTest.java +++ b/core-java-modules/core-java/src/test/java/com/baeldung/files/CreatingFilesUnitTest.java @@ -4,7 +4,7 @@ import org.junit.Test; import java.io.File; import java.io.IOException; -public class CreateFilesTest { +public class CreateFilesUnitTest { @Test(expected = IOException.class) public void whenCreatingAFileWithAbsolutePath_thenExceptionIsThrown() throws IOException { File fileWithAbsolutePath = new File("/myDirectory/testFile.txt"); From ed7b67f19c3d4a464b5b7e3a1ab97bf57220f0e2 Mon Sep 17 00:00:00 2001 From: Kevin Kraus Date: Fri, 6 Sep 2019 22:44:56 -0500 Subject: [PATCH 3/8] BAEL-3227 fixed tests --- .../baeldung/files/CreatingFilesUnitTest.java | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/core-java-modules/core-java/src/test/java/com/baeldung/files/CreatingFilesUnitTest.java b/core-java-modules/core-java/src/test/java/com/baeldung/files/CreatingFilesUnitTest.java index 1f2c1ec505..175a0001f1 100644 --- a/core-java-modules/core-java/src/test/java/com/baeldung/files/CreatingFilesUnitTest.java +++ b/core-java-modules/core-java/src/test/java/com/baeldung/files/CreatingFilesUnitTest.java @@ -4,6 +4,8 @@ import org.junit.Test; import java.io.File; import java.io.IOException; +import static org.junit.Assert.assertTrue; + public class CreateFilesUnitTest { @Test(expected = IOException.class) public void whenCreatingAFileWithAbsolutePath_thenExceptionIsThrown() throws IOException { @@ -12,18 +14,24 @@ public class CreateFilesUnitTest { Files.touch(fileWithAbsolutePath); } - @Test(expected = IOException.class) - public void givenAnExistingDirectory_whenCreatingANewDirectoryAndFile_thenExceptionIsThrown() throws IOException { + @Test + public void givenAnExistingDirectory_whenCreatingANewDirectoryAndFile_thenFileIsCreated() throws IOException { File tempDirectory = new File(System.getProperty("java.io.tmpdir")); File fileWithRelativePath = new File(tempDirectory, "myDirectory/newFile.txt"); + fileWithRelativePath.mkdirs(); Files.touch(fileWithRelativePath); + + assertTrue(fileWithRelativePath.exists()); } - @Test(expected = IOException.class) - public void whenCreatingAFileWithFileSeparator_thenPathIsCreated() throws IOException { + @Test + public void whenCreatingAFileWithFileSeparator_thenFileIsCreated() throws IOException { File newFile = new File("src" + File.separator + "test" + File.separator + "resources" + File.separator + "newFile.txt"); + newFile.mkdirs(); Files.touch(newFile); + + assertTrue(newFile.exists()); } } From 0eac69ca794063ed0d18d39e4b42c515a757d45c Mon Sep 17 00:00:00 2001 From: Kevin Kraus Date: Sun, 8 Sep 2019 21:51:37 -0500 Subject: [PATCH 4/8] fix package name --- .../src/test/java/com/baeldung/files/CreatingFilesUnitTest.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/core-java-modules/core-java/src/test/java/com/baeldung/files/CreatingFilesUnitTest.java b/core-java-modules/core-java/src/test/java/com/baeldung/files/CreatingFilesUnitTest.java index 175a0001f1..031d641ffc 100644 --- a/core-java-modules/core-java/src/test/java/com/baeldung/files/CreatingFilesUnitTest.java +++ b/core-java-modules/core-java/src/test/java/com/baeldung/files/CreatingFilesUnitTest.java @@ -1,3 +1,5 @@ +package com.baeldung.files; + import com.google.common.io.Files; import org.junit.Test; From 2a7f9f6edef83a6f979085e4917299e61d757de4 Mon Sep 17 00:00:00 2001 From: Kevin Kraus Date: Sun, 8 Sep 2019 22:00:47 -0500 Subject: [PATCH 5/8] improving absolute path test --- .../com/baeldung/files/CreatingFilesUnitTest.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/core-java-modules/core-java/src/test/java/com/baeldung/files/CreatingFilesUnitTest.java b/core-java-modules/core-java/src/test/java/com/baeldung/files/CreatingFilesUnitTest.java index 031d641ffc..f2216f58c7 100644 --- a/core-java-modules/core-java/src/test/java/com/baeldung/files/CreatingFilesUnitTest.java +++ b/core-java-modules/core-java/src/test/java/com/baeldung/files/CreatingFilesUnitTest.java @@ -9,15 +9,19 @@ import java.io.IOException; import static org.junit.Assert.assertTrue; public class CreateFilesUnitTest { - @Test(expected = IOException.class) - public void whenCreatingAFileWithAbsolutePath_thenExceptionIsThrown() throws IOException { - File fileWithAbsolutePath = new File("/myDirectory/testFile.txt"); + @Test + public void givenAnExistingDirectory_whenCreatingAFileWithAbsolutePath_thenFileIsCreated() throws IOException { + File tempDirectory = new File(System.getProperty("java.io.tmpdir")); + File fileWithAbsolutePath = new File(tempDirectory.getAbsolutePath() + "/myDirectory/testFile.txt"); + fileWithAbsolutePath.mkdirs(); Files.touch(fileWithAbsolutePath); + + assertTrue(fileWithAbsolutePath.exists()); } @Test - public void givenAnExistingDirectory_whenCreatingANewDirectoryAndFile_thenFileIsCreated() throws IOException { + public void givenAnExistingDirectory_whenCreatingANewDirectoryAndFileWithRelativePath_thenFileIsCreated() throws IOException { File tempDirectory = new File(System.getProperty("java.io.tmpdir")); File fileWithRelativePath = new File(tempDirectory, "myDirectory/newFile.txt"); From 9f2a9a9c361385cb93eb79cc85750c02c9ef39cd Mon Sep 17 00:00:00 2001 From: Kevin Kraus Date: Sun, 8 Sep 2019 22:14:40 -0500 Subject: [PATCH 6/8] renaming test --- .../{CreatingFilesUnitTest.java => CreateFilesUnitTest.java} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename core-java-modules/core-java/src/test/java/com/baeldung/files/{CreatingFilesUnitTest.java => CreateFilesUnitTest.java} (100%) diff --git a/core-java-modules/core-java/src/test/java/com/baeldung/files/CreatingFilesUnitTest.java b/core-java-modules/core-java/src/test/java/com/baeldung/files/CreateFilesUnitTest.java similarity index 100% rename from core-java-modules/core-java/src/test/java/com/baeldung/files/CreatingFilesUnitTest.java rename to core-java-modules/core-java/src/test/java/com/baeldung/files/CreateFilesUnitTest.java From 714e909e9778bace2283364122232674df344273 Mon Sep 17 00:00:00 2001 From: Kevin Kraus Date: Tue, 10 Sep 2019 20:47:13 -0500 Subject: [PATCH 7/8] move to core-java-io --- .../java/com/baeldung/createfiles}/CreateFilesUnitTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename core-java-modules/{core-java/src/test/java/com/baeldung/files => core-java-io-2/src/test/java/com/baeldung/createfiles}/CreateFilesUnitTest.java (97%) diff --git a/core-java-modules/core-java/src/test/java/com/baeldung/files/CreateFilesUnitTest.java b/core-java-modules/core-java-io-2/src/test/java/com/baeldung/createfiles/CreateFilesUnitTest.java similarity index 97% rename from core-java-modules/core-java/src/test/java/com/baeldung/files/CreateFilesUnitTest.java rename to core-java-modules/core-java-io-2/src/test/java/com/baeldung/createfiles/CreateFilesUnitTest.java index f2216f58c7..488b22ff89 100644 --- a/core-java-modules/core-java/src/test/java/com/baeldung/files/CreateFilesUnitTest.java +++ b/core-java-modules/core-java-io-2/src/test/java/com/baeldung/createfiles/CreateFilesUnitTest.java @@ -1,4 +1,4 @@ -package com.baeldung.files; +package com.baeldung.createfiles; import com.google.common.io.Files; import org.junit.Test; From 42b6bf5c02da3b2c9fa8e288f34da0d4ec223a7d Mon Sep 17 00:00:00 2001 From: Kevin Kraus Date: Tue, 10 Sep 2019 21:40:34 -0500 Subject: [PATCH 8/8] add asserts and use temp directory --- .../baeldung/createfiles/CreateFilesUnitTest.java | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/core-java-modules/core-java-io-2/src/test/java/com/baeldung/createfiles/CreateFilesUnitTest.java b/core-java-modules/core-java-io-2/src/test/java/com/baeldung/createfiles/CreateFilesUnitTest.java index 488b22ff89..338d53c597 100644 --- a/core-java-modules/core-java-io-2/src/test/java/com/baeldung/createfiles/CreateFilesUnitTest.java +++ b/core-java-modules/core-java-io-2/src/test/java/com/baeldung/createfiles/CreateFilesUnitTest.java @@ -6,6 +6,7 @@ import org.junit.Test; import java.io.File; import java.io.IOException; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; public class CreateFilesUnitTest { @@ -14,7 +15,8 @@ public class CreateFilesUnitTest { File tempDirectory = new File(System.getProperty("java.io.tmpdir")); File fileWithAbsolutePath = new File(tempDirectory.getAbsolutePath() + "/myDirectory/testFile.txt"); - fileWithAbsolutePath.mkdirs(); + assertFalse(fileWithAbsolutePath.exists()); + Files.touch(fileWithAbsolutePath); assertTrue(fileWithAbsolutePath.exists()); @@ -25,7 +27,8 @@ public class CreateFilesUnitTest { File tempDirectory = new File(System.getProperty("java.io.tmpdir")); File fileWithRelativePath = new File(tempDirectory, "myDirectory/newFile.txt"); - fileWithRelativePath.mkdirs(); + assertFalse(fileWithRelativePath.exists()); + Files.touch(fileWithRelativePath); assertTrue(fileWithRelativePath.exists()); @@ -33,9 +36,11 @@ public class CreateFilesUnitTest { @Test public void whenCreatingAFileWithFileSeparator_thenFileIsCreated() throws IOException { - File newFile = new File("src" + File.separator + "test" + File.separator + "resources" + File.separator + "newFile.txt"); + File tempDirectory = new File(System.getProperty("java.io.tmpdir")); + File newFile = new File(tempDirectory.getAbsolutePath() + File.separator + "newFile.txt"); + + assertFalse(newFile.exists()); - newFile.mkdirs(); Files.touch(newFile); assertTrue(newFile.exists());