aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLiam Fallon <liam.fallon@est.tech>2020-01-23 13:35:07 +0000
committerGerrit Code Review <gerrit@onap.org>2020-01-23 13:35:07 +0000
commitddf0410c7ad3721c2169dfb6c06234b680766c8a (patch)
tree750ca6645e7e7b57b052ef340be01a96ce3d9d31
parentb46e315c8f803e4fb54fc7ba49b94b8052f1c037 (diff)
parentb4f69e428f1677339ff432c27e2cb8bfa756114e (diff)
Merge "Create path to text file being created"
-rw-r--r--utils/src/main/java/org/onap/policy/common/utils/resources/TextFileUtils.java4
-rw-r--r--utils/src/test/java/org/onap/policy/common/utils/resources/TextFileUtilsTest.java22
2 files changed, 25 insertions, 1 deletions
diff --git a/utils/src/main/java/org/onap/policy/common/utils/resources/TextFileUtils.java b/utils/src/main/java/org/onap/policy/common/utils/resources/TextFileUtils.java
index 5aeacf26..01af7fd8 100644
--- a/utils/src/main/java/org/onap/policy/common/utils/resources/TextFileUtils.java
+++ b/utils/src/main/java/org/onap/policy/common/utils/resources/TextFileUtils.java
@@ -62,6 +62,10 @@ public abstract class TextFileUtils {
*/
public static void putStringAsTextFile(final String outString, final String textFilePath) throws IOException {
final File textFile = new File(textFilePath);
+ if (!textFile.getParentFile().exists()) {
+ textFile.getParentFile().mkdirs();
+ }
+
putStringAsFile(outString, textFile);
}
diff --git a/utils/src/test/java/org/onap/policy/common/utils/resources/TextFileUtilsTest.java b/utils/src/test/java/org/onap/policy/common/utils/resources/TextFileUtilsTest.java
index 7f246ab2..0952b168 100644
--- a/utils/src/test/java/org/onap/policy/common/utils/resources/TextFileUtilsTest.java
+++ b/utils/src/test/java/org/onap/policy/common/utils/resources/TextFileUtilsTest.java
@@ -30,6 +30,7 @@ import org.junit.Test;
/**
* Test text file utilities.
+ *
* @author Liam Fallon (liam.fallon@est.tech)
*/
public class TextFileUtilsTest {
@@ -37,7 +38,7 @@ public class TextFileUtilsTest {
private static final String FILE_CONTENT = "This is the contents of a text file";
@Test
- public void test() throws IOException {
+ public void testPutToFile() throws IOException {
final File tempTextFile = File.createTempFile("Test", "txt");
TextFileUtils.putStringAsTextFile(FILE_CONTENT, tempTextFile.getAbsolutePath());
@@ -48,6 +49,25 @@ public class TextFileUtilsTest {
final FileInputStream fis = new FileInputStream(tempTextFile);
final String textFileString1 = TextFileUtils.getStreamAsString(fis);
assertEquals(textFileString0, textFileString1);
+ }
+
+ @Test
+ public void testPutToFileWithNewPath() throws IOException {
+ String tempDirAndFileName = System.getProperty("java.io.tmpdir") + "/non/existant/path/Test.txt";
+
+ TextFileUtils.putStringAsTextFile(FILE_CONTENT, tempDirAndFileName);
+
+ final String textFileString0 = TextFileUtils.getTextFileAsString(tempDirAndFileName);
+ assertEquals(FILE_CONTENT, textFileString0);
+
+ final FileInputStream fis = new FileInputStream(tempDirAndFileName);
+ final String textFileString1 = TextFileUtils.getStreamAsString(fis);
+ assertEquals(textFileString0, textFileString1);
+ File tempDirAndFile = new File(tempDirAndFileName);
+ tempDirAndFile.delete();
+ tempDirAndFile.getParentFile().delete();
+ tempDirAndFile.getParentFile().getParentFile().delete();
+ tempDirAndFile.getParentFile().getParentFile().getParentFile().delete();
}
}