Skip to content

Commit

Permalink
Added tests for FileUtils.
Browse files Browse the repository at this point in the history
  • Loading branch information
Aklakan committed Jun 17, 2024
1 parent 625c52c commit dedfd50
Showing 1 changed file with 68 additions and 0 deletions.
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);
}
}

0 comments on commit dedfd50

Please sign in to comment.