-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
68 additions
and
0 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
...io-parent/aksw-commons-io-utils/src/test/java/org/aksw/commons/io/util/FileUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package org.aksw.commons.io.util; | ||
|
||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.FileAlreadyExistsException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
|
||
import org.aksw.commons.io.util.FileUtils.OverwritePolicy; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
|
||
public class FileUtilsTest { | ||
public static final String TEST_STRING = "Hello"; | ||
|
||
@Test | ||
public void test_safeCreate_00() throws Exception { | ||
Path path = Files.createTempFile("test", ".tmp"); | ||
writeTestFileAndAssert(path, OverwritePolicy.SKIP, TEST_STRING, ""); | ||
} | ||
|
||
@Test | ||
public void test_safeCreate_01() throws Exception { | ||
Path path = Files.createTempFile("test", ".tmp"); | ||
writeTestFileAndAssert(path, OverwritePolicy.OVERWRITE); | ||
} | ||
|
||
@Test | ||
public void test_safeCreate_02() throws Exception { | ||
Path path = Files.createTempFile("test", ".tmp"); | ||
writeTestFileAndAssert(path, OverwritePolicy.OVERWRITE_ALWAYS); | ||
} | ||
|
||
@Test(expected = FileAlreadyExistsException.class) | ||
public void test_safeCreate_03() throws Exception { | ||
Path path = Files.createTempFile("test", ".tmp"); | ||
writeTestFileAndAssert(path, OverwritePolicy.ERROR); | ||
} | ||
|
||
public void writeTestFileAndAssert(Path path, OverwritePolicy overwritePolicy, String writeStr, String assertStr) throws Exception { | ||
try { | ||
FileUtils.safeCreate(path, overwritePolicy, out -> out.write(writeStr.getBytes(StandardCharsets.UTF_8))); | ||
assertFileContent(path, assertStr); | ||
} finally { | ||
Files.deleteIfExists(path); | ||
} | ||
} | ||
|
||
public void writeTestFileAndAssert(Path path, OverwritePolicy overwritePolicy) throws Exception { | ||
try { | ||
FileUtils.safeCreate(path, overwritePolicy, out -> out.write(TEST_STRING.getBytes(StandardCharsets.UTF_8))); | ||
assertFileContent(path, TEST_STRING); | ||
} finally { | ||
Files.deleteIfExists(path); | ||
} | ||
} | ||
|
||
public static void assertFileContent(Path path, String expected) { | ||
String actual; | ||
try { | ||
actual = Files.readString(path, StandardCharsets.UTF_8); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
Assert.assertEquals(expected, actual); | ||
} | ||
} |