001/* 002 * The contents of this file are subject to the license and copyright detailed 003 * in the LICENSE and NOTICE files at the root of the source tree. 004 */ 005package org.duraspace.bagit; 006 007 008import static org.junit.Assert.assertNotNull; 009import static org.junit.Assert.assertTrue; 010import static org.junit.Assert.fail; 011 012import java.io.IOException; 013import java.net.URISyntaxException; 014import java.net.URL; 015import java.nio.file.Files; 016import java.nio.file.Path; 017import java.nio.file.Paths; 018import java.util.Arrays; 019import java.util.Collection; 020import java.util.Objects; 021 022import org.apache.commons.io.FileUtils; 023import org.junit.After; 024import org.junit.Before; 025import org.junit.Test; 026import org.junit.runner.RunWith; 027import org.junit.runners.Parameterized; 028import org.junit.runners.Parameterized.Parameters; 029 030/** 031 * Test that zip, tar, and tar+gz extraction works as expected 032 * 033 * @author mikejritter 034 * @since 2020-02-13 035 */ 036@RunWith(Parameterized.class) 037public class BagDeserializerTest { 038 039 @Parameters(name = "extract {0}") 040 public static Collection<Object[]> data() { 041 return Arrays.asList(new Object[][] { 042 {"bag-tar.tar", "bag-tar"}, {"bag-zip.zip", "bag-zip"}, {"bag-gzip.tar.gz", "bag-gzip"}, 043 {"bag-tar-mismatch.tar", "bag-tar"} 044 }); 045 } 046 047 public static final String BAG_INFO_TXT = "bag-info.txt"; 048 public static final String DATA_DIR = "data"; 049 private final String group = "compress"; 050 private Path target; 051 052 private final String archive; 053 private final String expectedDir; 054 055 public BagDeserializerTest(final String archive, final String expectedDir) { 056 this.archive = archive; 057 this.expectedDir = expectedDir; 058 } 059 060 @Before 061 public void setup() throws URISyntaxException { 062 final URL sample = this.getClass().getClassLoader().getResource("sample"); 063 target = Paths.get(Objects.requireNonNull(sample).toURI()); 064 assertNotNull(target); 065 } 066 067 @After 068 public void cleanup() throws IOException { 069 FileUtils.deleteDirectory(target.resolve(group).resolve(expectedDir).toFile()); 070 } 071 072 @Test 073 public void testExtract() { 074 final Path path = target.resolve(group).resolve(archive); 075 try { 076 final BagProfile profile = new BagProfile(BagProfile.BuiltIn.BEYOND_THE_REPOSITORY); 077 final BagDeserializer deserializer = SerializationSupport.deserializerFor(path, profile); 078 deserializer.deserialize(path); 079 } catch (IOException e) { 080 fail("Unexpected exception:\n" + e.getMessage()); 081 } 082 083 final Path bag = target.resolve(group).resolve(expectedDir); 084 assertTrue(Files.exists(bag)); 085 assertTrue(Files.exists(bag.resolve(BAG_INFO_TXT))); 086 assertTrue(Files.exists(bag.resolve(DATA_DIR))); 087 assertTrue(Files.isDirectory(bag.resolve(DATA_DIR))); 088 } 089 090}