Add failing test for #3705

This commit is contained in:
Winston Li 2017-08-02 20:51:20 +01:00
parent 8e15d63a2f
commit ad687e5f58
5 changed files with 98 additions and 19 deletions

View file

@ -0,0 +1,11 @@
package uk.ac.ic.wlgitbridge.util;
/**
* BiConsumer interface that allows checked exceptions.
*/
@FunctionalInterface
public interface BiConsumerT<T, U, E extends Throwable> {
void accept(T t, U u) throws E;
}

View file

@ -0,0 +1,14 @@
package uk.ac.ic.wlgitbridge.util;
/**
* Function interface that allows checked exceptions.
* @param <T>
* @param <R>
* @param <E>
*/
@FunctionalInterface
public interface FunctionT<T, R, E extends Throwable> {
R apply(T t) throws E;
}

View file

@ -0,0 +1,32 @@
package uk.ac.ic.wlgitbridge.util;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
public class ResourceUtil {
/**
* Creates a copy of a resource folder. Mainly used for testing to prevent
* the original folder from being mangled.
*
* It will have the same name as the original.
* @param resource the resource name, e.g. "/uk/ac/ic/wlgitbridge/file.txt"
* @param folderProvider function used to create the folder.
* E.g. TemporaryFolder from junit
* @return
* @throws IOException
*/
public static File copyOfFolderResource(
String resource,
FunctionT<String, File, IOException> folderProvider
) throws IOException {
File original
= new File(ResourceUtil.class.getResource(resource).getFile());
File tmp = folderProvider.apply(original.getName());
FileUtils.copyDirectory(original, tmp);
return tmp;
}
}

View file

@ -1,6 +1,5 @@
package uk.ac.ic.wlgitbridge.util;
import org.apache.commons.io.FileUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
@ -8,8 +7,6 @@ import org.junit.rules.TemporaryFolder;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.nio.file.Paths;
import static org.junit.Assert.assertTrue;
@ -18,37 +15,62 @@ import static org.junit.Assert.assertTrue;
*/
public class TarTest {
private static final String RESOURCE_DIR
= "/uk/ac/ic/wlgitbridge/util/TarTest";
private File testDir;
private File dirWithEmptyFile;
private File tmpDir;
@Before
public void setup() throws IOException {
TemporaryFolder tmpFolder = new TemporaryFolder();
tmpFolder.create();
testDir = tmpFolder.newFolder("testdir");
Path resdir = Paths.get(
"src/test/resources/uk/ac/ic/wlgitbridge/util/TarTest/testdir"
);
FileUtils.copyDirectory(resdir.toFile(), testDir);
testDir = ResourceUtil.copyOfFolderResource(
RESOURCE_DIR + "/testdir",
tmpFolder::newFolder);
dirWithEmptyFile = ResourceUtil.copyOfFolderResource(
RESOURCE_DIR + "/dir_with_empty_file",
tmpFolder::newFolder);
tmpDir = tmpFolder.newFolder();
}
/**
* Compresses inputDir and decompresses to outputDir. Checks equality
* between outputDir and inputDir.
* @param inputDir the directory to compress
* @param outputDir the output directory. Must be empty.
* @param compressFunction compression function
* @param decompressFunction decompression function
* @throws IOException
*/
private static void assertCompDecompEqual(
File inputDir,
File outputDir,
FunctionT<File, InputStream, IOException> compressFunction,
BiConsumerT<InputStream, File, IOException> decompressFunction
) throws IOException {
try (InputStream tarbz2 = compressFunction.apply(inputDir)) {
decompressFunction.accept(tarbz2, outputDir);
File unzipped = new File(outputDir, inputDir.getName());
assertTrue(Files.contentsAreEqual(inputDir, unzipped));
}
}
@Test
public void tarAndUntarProducesTheSameResult() throws IOException {
try (InputStream tar = Tar.tar(testDir)) {
Tar.untar(tar, tmpDir);
File untarred = new File(tmpDir, "testdir");
assertTrue(Files.contentsAreEqual(testDir, untarred));
}
assertCompDecompEqual(testDir, tmpDir, Tar::tar, Tar::untar);
}
@Test
public void tarbz2AndUntarbz2ProducesTheSameResult() throws IOException {
try (InputStream tarbz2 = Tar.bz2.zip(testDir)) {
Tar.bz2.unzip(tarbz2, tmpDir);
File unzipped = new File(tmpDir, "testdir");
assertTrue(Files.contentsAreEqual(testDir, unzipped));
}
assertCompDecompEqual(testDir, tmpDir, Tar.bz2::zip, Tar.bz2::unzip);
}
}
@Test
public void tarbz2WorksOnDirectoriesWithAnEmptyFile() throws IOException {
assertCompDecompEqual(
dirWithEmptyFile, tmpDir, Tar.bz2::zip, Tar.bz2::unzip);
}
}