Read reference files from the resource folder.
(, en)
For testing correct file outputs, I tend to have the reference files with the expected
result in the resource folder. And to streamline things, I use these convenience
functions (path needs to start with /
to target the right resource file).
package org.bargsten.testkit;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.zip.GZIPInputStream;
import org.apache.commons.io.IOUtils;
public final class TestFileUtil {
private TestFileUtil() {
}
public static String getResourceContent(Object o, String path) throws IOException {
InputStream resourceAsStream = o.getClass().getResourceAsStream(path);
return IOUtils.toString(resourceAsStream, StandardCharsets.UTF_8);
}
public static byte[] getResourceBytes(Object o, String path) throws IOException {
InputStream resourceAsStream = o.getClass().getResourceAsStream(path);
return IOUtils.toByteArray(resourceAsStream);
}
public static String getGzippedResourceContent(Object o, String path) throws IOException {
InputStream resourceAsStream = o.getClass().getResourceAsStream(path);
return IOUtils.toString(new GZIPInputStream(resourceAsStream), StandardCharsets.UTF_8);
}
public static byte[] getGzippedResourceBytes(Object o, String path) throws IOException {
InputStream resourceAsStream = o.getClass().getResourceAsStream(path);
return IOUtils.toByteArray(new GZIPInputStream(resourceAsStream));
}
}