Commit 40f57b41 authored by Stein Eldar Johnsen's avatar Stein Eldar Johnsen 💬

Testing: ResourceUtils.copyResourceTo takes file and dir.

parent 6d701883
......@@ -23,23 +23,23 @@ import static org.junit.Assert.fail;
public class ResourceUtils {
/**
* Copy a resource to the target directory. The resource file retains it's
* name.
* name. If the target is a file, the file is overwritten with the content
* of the resource file.
*
* @param resource The resource path.
* @param dir Target directory.
* @param target Target file or directory.
* @return File created and written to.
*/
public static File copyResourceTo(String resource, File dir) {
if (!dir.exists()) {
fail("Trying to copy resource '" + resource + "' to non-existing directory: '" + dir + "'");
}
if (dir.isFile()) {
fail("Trying to copy resource '" + resource + "' to file: '" + dir + "', directory required");
public static File copyResourceTo(String resource, File target) {
if (!target.exists()) {
fail("Trying to copy resource '" + resource + "' to non-existing directory: '" + target + "'");
}
if (target.isDirectory()) {
int i = resource.lastIndexOf('/');
File file = new File(dir, resource.substring(i + 1));
target = new File(target, resource.substring(i + 1));
}
try (FileOutputStream fos = new FileOutputStream(file);
try (FileOutputStream fos = new FileOutputStream(target);
BufferedOutputStream out = new BufferedOutputStream(fos);
InputStream in = getResourceAsStream(resource)) {
IOUtils.copy(in, out);
......@@ -48,7 +48,7 @@ public class ResourceUtils {
} catch (IOException e) {
throw new UncheckedIOException(e);
}
return file;
return target;
}
/**
......
......@@ -16,8 +16,10 @@ import static java.nio.charset.StandardCharsets.UTF_8;
import static net.morimekta.testing.ResourceUtils.getResourceAsByteBuffer;
import static net.morimekta.testing.ResourceUtils.getResourceAsString;
import static net.morimekta.util.io.IOUtils.readString;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
......@@ -29,13 +31,22 @@ public class ResourceUtilsTest {
public TemporaryFolder temp = new TemporaryFolder();
@Test
public void testCopyResourceTo() throws IOException {
ResourceUtils.copyResourceTo("/net/morimekta/testing/test.txt", temp.getRoot());
public void testCopyResourceTo_dir() throws IOException {
File test = ResourceUtils.copyResourceTo("/net/morimekta/testing/test.txt", temp.getRoot());
File test = new File(temp.getRoot(), "test.txt");
assertTrue(test.exists());
assertTrue(test.isFile());
assertThat(test.getName(), is("test.txt"));
assertEquals("Test!\n", readString(new FileInputStream(test)));
}
@Test
public void testCopyResourceTo_file() throws IOException {
File test = ResourceUtils.copyResourceTo("/net/morimekta/testing/test.txt", temp.newFile("boo.txt"));
assertTrue(test.exists());
assertTrue(test.isFile());
assertThat(test.getName(), is("boo.txt"));
assertEquals("Test!\n", readString(new FileInputStream(test)));
}
......@@ -49,17 +60,6 @@ public class ResourceUtilsTest {
e.getMessage());
}
File tmp = temp.newFile();
tmp.createNewFile();
try {
ResourceUtils.copyResourceTo("/net/morimekta/testing/test.txt", tmp);
fail("No exception on target is file");
} catch (AssertionError e) {
assertEquals("Trying to copy resource '/net/morimekta/testing/test.txt' to file: '" + tmp.getAbsolutePath() +
"', directory required",
e.getMessage());
}
try {
temp.delete();
ResourceUtils.copyResourceTo("/net/morimekta/testing/test.txt", temp.getRoot());
......@@ -72,12 +72,11 @@ public class ResourceUtilsTest {
@Test
public void testWriteContentTo() throws IOException {
ResourceUtils.writeContentTo("/net/morimekta/testing/test.txt", temp.newFile("test2.txt"));
File test = new File(temp.getRoot(), "test2.txt");
File test = ResourceUtils.writeContentTo("/net/morimekta/testing/test.txt", temp.newFile("test2.txt"));
assertTrue(test.exists());
assertTrue(test.isFile());
assertThat(test.getName(), is("test2.txt"));
assertEquals("/net/morimekta/testing/test.txt", readString(new FileInputStream(test)));
}
......@@ -85,7 +84,7 @@ public class ResourceUtilsTest {
public void testWriteContentTo_fails() throws IOException {
File file = new File(temp.getRoot(), "a/b/c");
try {
ResourceUtils.writeContentTo("/net/morimekta/testing/test.txt", file);
ResourceUtils.writeContentTo("/net/morimekta/testing/test2.txt", file);
fail("No exception on unable to write to file.");
} catch (UncheckedIOException e) {
assertEquals("java.io.FileNotFoundException: " + file.getAbsolutePath() + " (No such file or directory)",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment