From 1f18b3a051e228a74ec404e8ec535e680a6dac0e Mon Sep 17 00:00:00 2001 From: Krzysiek Date: Sun, 15 Dec 2019 19:29:08 +0100 Subject: [PATCH 1/3] BAEL-3651: [Do Not Merge] Add diagnostic logging --- .../com/baeldung/file/FileClassUnitTest.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/core-java-modules/core-java-io-apis/src/test/java/com/baeldung/file/FileClassUnitTest.java b/core-java-modules/core-java-io-apis/src/test/java/com/baeldung/file/FileClassUnitTest.java index 57e12560bb..326d01b49c 100644 --- a/core-java-modules/core-java-io-apis/src/test/java/com/baeldung/file/FileClassUnitTest.java +++ b/core-java-modules/core-java-io-apis/src/test/java/com/baeldung/file/FileClassUnitTest.java @@ -1,15 +1,21 @@ package com.baeldung.file; + import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.io.*; import java.util.Arrays; import java.util.List; +import java.util.StringJoiner; import static org.junit.Assert.*; public class FileClassUnitTest { + private static final Logger log = LoggerFactory.getLogger("FileClassUnitTest"); + @Test public void givenDir_whenMkdir_thenDirIsDeleted() { File directory = new File("dir"); @@ -137,11 +143,17 @@ public class FileClassUnitTest { public void givenDataWritten_whenWrite_thenFreeSpaceReduces() { String home = System.getProperty("user.home"); + log.info("user.home: " + home); String sep = File.separator; File testDir = makeDir(home + sep + "test"); + log.info("testDir: " + testDir.toString()); File sample = new File(testDir, "sample.txt"); + log.info("sample: " + sample.toString()); long freeSpaceBefore = testDir.getFreeSpace(); + log.info("freeSpaceBefore: " + freeSpaceBefore); + + log.info("testDir list before: " + getListOfFiles(testDir)); try { writeSampleDataToFile(sample); } catch (IOException e) { @@ -149,11 +161,21 @@ public class FileClassUnitTest { } long freeSpaceAfter = testDir.getFreeSpace(); + log.info("freeSpaceAfter: " + freeSpaceAfter); + log.info("testDir list after: " + getListOfFiles(testDir)); assertTrue(freeSpaceAfter < freeSpaceBefore); removeDir(testDir); } + private String getListOfFiles(File dir){ + String list = ""; + for(String l : dir.list()){ + list += " _ " + l; + } + return list; + } + private static File makeDir(String name) { File directory = new File(name); directory.mkdir(); From fccdc24ad55244349305c069d020c7c211e084c2 Mon Sep 17 00:00:00 2001 From: Krzysiek Date: Sun, 15 Dec 2019 20:00:00 +0100 Subject: [PATCH 2/3] Revert "BAEL-3651: [Do Not Merge] Add diagnostic logging" This reverts commit 1f18b3a051e228a74ec404e8ec535e680a6dac0e. --- .../com/baeldung/file/FileClassUnitTest.java | 22 ------------------- 1 file changed, 22 deletions(-) diff --git a/core-java-modules/core-java-io-apis/src/test/java/com/baeldung/file/FileClassUnitTest.java b/core-java-modules/core-java-io-apis/src/test/java/com/baeldung/file/FileClassUnitTest.java index 326d01b49c..57e12560bb 100644 --- a/core-java-modules/core-java-io-apis/src/test/java/com/baeldung/file/FileClassUnitTest.java +++ b/core-java-modules/core-java-io-apis/src/test/java/com/baeldung/file/FileClassUnitTest.java @@ -1,21 +1,15 @@ package com.baeldung.file; - import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import java.io.*; import java.util.Arrays; import java.util.List; -import java.util.StringJoiner; import static org.junit.Assert.*; public class FileClassUnitTest { - private static final Logger log = LoggerFactory.getLogger("FileClassUnitTest"); - @Test public void givenDir_whenMkdir_thenDirIsDeleted() { File directory = new File("dir"); @@ -143,17 +137,11 @@ public class FileClassUnitTest { public void givenDataWritten_whenWrite_thenFreeSpaceReduces() { String home = System.getProperty("user.home"); - log.info("user.home: " + home); String sep = File.separator; File testDir = makeDir(home + sep + "test"); - log.info("testDir: " + testDir.toString()); File sample = new File(testDir, "sample.txt"); - log.info("sample: " + sample.toString()); long freeSpaceBefore = testDir.getFreeSpace(); - log.info("freeSpaceBefore: " + freeSpaceBefore); - - log.info("testDir list before: " + getListOfFiles(testDir)); try { writeSampleDataToFile(sample); } catch (IOException e) { @@ -161,21 +149,11 @@ public class FileClassUnitTest { } long freeSpaceAfter = testDir.getFreeSpace(); - log.info("freeSpaceAfter: " + freeSpaceAfter); - log.info("testDir list after: " + getListOfFiles(testDir)); assertTrue(freeSpaceAfter < freeSpaceBefore); removeDir(testDir); } - private String getListOfFiles(File dir){ - String list = ""; - for(String l : dir.list()){ - list += " _ " + l; - } - return list; - } - private static File makeDir(String name) { File directory = new File(name); directory.mkdir(); From 0c61a12d88aaa8b6faa061f9684cd4bf37e2b994 Mon Sep 17 00:00:00 2001 From: Krzysiek Date: Sun, 15 Dec 2019 20:09:01 +0100 Subject: [PATCH 3/3] BAEL-3651: Make sure the directory is always initially empty --- .../test/java/com/baeldung/file/FileClassUnitTest.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/core-java-modules/core-java-io-apis/src/test/java/com/baeldung/file/FileClassUnitTest.java b/core-java-modules/core-java-io-apis/src/test/java/com/baeldung/file/FileClassUnitTest.java index 57e12560bb..a4317af372 100644 --- a/core-java-modules/core-java-io-apis/src/test/java/com/baeldung/file/FileClassUnitTest.java +++ b/core-java-modules/core-java-io-apis/src/test/java/com/baeldung/file/FileClassUnitTest.java @@ -156,10 +156,16 @@ public class FileClassUnitTest { private static File makeDir(String name) { File directory = new File(name); - directory.mkdir(); - if (directory.isDirectory()) { + + // If the directory already exists, make sure we create it 'from scratch', i.e. all the files inside are deleted first + if (directory.exists()) { + removeDir(directory); + } + + if (directory.mkdir()) { return directory; } + throw new RuntimeException("'" + name + "' not made!"); }