Author: bodewig Date: Sat Aug 1 19:32:12 2009 New Revision: 799911 URL: http://svn.apache.org/viewvc?rev=799911&view=rev Log: check name/directory/size and lastModified read from tar is consistent with what we write
Modified: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/TarTestCase.java commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/ZipTestCase.java Modified: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/TarTestCase.java URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/TarTestCase.java?rev=799911&r1=799910&r2=799911&view=diff ============================================================================== --- commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/TarTestCase.java (original) +++ commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/TarTestCase.java Sat Aug 1 19:32:12 2009 @@ -27,6 +27,8 @@ import org.apache.commons.compress.AbstractTestCase; import org.apache.commons.compress.archivers.tar.TarArchiveEntry; +import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; +import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream; import org.apache.commons.compress.utils.IOUtils; public final class TarTestCase extends AbstractTestCase { @@ -112,4 +114,189 @@ out.close(); } + public void testDirectoryEntryFromFile() throws Exception { + File[] tmp = createTempDirAndFile(); + File archive = null; + TarArchiveOutputStream tos = null; + TarArchiveInputStream tis = null; + try { + archive = File.createTempFile("test.", ".tar", tmp[0]); + archive.deleteOnExit(); + tos = new TarArchiveOutputStream(new FileOutputStream(archive)); + long beforeArchiveWrite = tmp[0].lastModified(); + TarArchiveEntry in = new TarArchiveEntry(tmp[0], "foo"); + tos.putArchiveEntry(in); + tos.closeArchiveEntry(); + tos.close(); + tos = null; + tis = new TarArchiveInputStream(new FileInputStream(archive)); + TarArchiveEntry out = tis.getNextTarEntry(); + tis.close(); + tis = null; + assertNotNull(out); + assertEquals("foo/", out.getName()); + assertEquals(0, out.getSize()); + // TAR stores time with a granularity of 1 second + assertEquals(beforeArchiveWrite / 1000, + out.getLastModifiedDate().getTime() / 1000); + assertTrue(out.isDirectory()); + } finally { + if (tis != null) { + tis.close(); + } + if (tos != null) { + tos.close(); + } + if (archive != null) { + archive.delete(); + } + tmp[1].delete(); + tmp[0].delete(); + } + } + + public void testExplicitDirectoryEntry() throws Exception { + File[] tmp = createTempDirAndFile(); + File archive = null; + TarArchiveOutputStream tos = null; + TarArchiveInputStream tis = null; + try { + archive = File.createTempFile("test.", ".tar", tmp[0]); + archive.deleteOnExit(); + tos = new TarArchiveOutputStream(new FileOutputStream(archive)); + long beforeArchiveWrite = tmp[0].lastModified(); + TarArchiveEntry in = new TarArchiveEntry("foo/"); + in.setModTime(beforeArchiveWrite); + tos.putArchiveEntry(in); + tos.closeArchiveEntry(); + tos.close(); + tos = null; + tis = new TarArchiveInputStream(new FileInputStream(archive)); + TarArchiveEntry out = tis.getNextTarEntry(); + tis.close(); + tis = null; + assertNotNull(out); + assertEquals("foo/", out.getName()); + assertEquals(0, out.getSize()); + assertEquals(beforeArchiveWrite / 1000, + out.getLastModifiedDate().getTime() / 1000); + assertTrue(out.isDirectory()); + } finally { + if (tis != null) { + tis.close(); + } + if (tos != null) { + tos.close(); + } + if (archive != null) { + archive.delete(); + } + tmp[1].delete(); + tmp[0].delete(); + } + } + + public void testFileEntryFromFile() throws Exception { + File[] tmp = createTempDirAndFile(); + File archive = null; + TarArchiveOutputStream tos = null; + TarArchiveInputStream tis = null; + FileInputStream fis = null; + try { + archive = File.createTempFile("test.", ".tar", tmp[0]); + archive.deleteOnExit(); + tos = new TarArchiveOutputStream(new FileOutputStream(archive)); + TarArchiveEntry in = new TarArchiveEntry(tmp[1], "foo"); + tos.putArchiveEntry(in); + byte[] b = new byte[(int) tmp[1].length()]; + fis = new FileInputStream(tmp[1]); + int read; + while ((read = fis.read(b)) > 0) { + tos.write(b); + } + fis.close(); + fis = null; + tos.closeArchiveEntry(); + tos.close(); + tos = null; + tis = new TarArchiveInputStream(new FileInputStream(archive)); + TarArchiveEntry out = tis.getNextTarEntry(); + tis.close(); + tis = null; + assertNotNull(out); + assertEquals("foo", out.getName()); + assertEquals(tmp[1].length(), out.getSize()); + assertEquals(tmp[1].lastModified() / 1000, + out.getLastModifiedDate().getTime() / 1000); + assertFalse(out.isDirectory()); + } finally { + if (tis != null) { + tis.close(); + } + if (tos != null) { + tos.close(); + } + if (archive != null) { + archive.delete(); + } + if (fis != null) { + fis.close(); + } + tmp[1].delete(); + tmp[0].delete(); + } + } + + public void testExplicitFileEntry() throws Exception { + File[] tmp = createTempDirAndFile(); + File archive = null; + TarArchiveOutputStream tos = null; + TarArchiveInputStream tis = null; + FileInputStream fis = null; + try { + archive = File.createTempFile("test.", ".tar", tmp[0]); + archive.deleteOnExit(); + tos = new TarArchiveOutputStream(new FileOutputStream(archive)); + TarArchiveEntry in = new TarArchiveEntry("foo"); + in.setModTime(tmp[1].lastModified()); + in.setSize(tmp[1].length()); + tos.putArchiveEntry(in); + byte[] b = new byte[(int) tmp[1].length()]; + fis = new FileInputStream(tmp[1]); + int read; + while ((read = fis.read(b)) > 0) { + tos.write(b); + } + fis.close(); + fis = null; + tos.closeArchiveEntry(); + tos.close(); + tos = null; + tis = new TarArchiveInputStream(new FileInputStream(archive)); + TarArchiveEntry out = tis.getNextTarEntry(); + tis.close(); + tis = null; + assertNotNull(out); + assertEquals("foo", out.getName()); + assertEquals(tmp[1].length(), out.getSize()); + assertEquals(tmp[1].lastModified() / 1000, + out.getLastModifiedDate().getTime() / 1000); + assertFalse(out.isDirectory()); + } finally { + if (tis != null) { + tis.close(); + } + if (tos != null) { + tos.close(); + } + if (archive != null) { + archive.delete(); + } + if (fis != null) { + fis.close(); + } + tmp[1].delete(); + tmp[0].delete(); + } + } } Modified: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/ZipTestCase.java URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/ZipTestCase.java?rev=799911&r1=799910&r2=799911&view=diff ============================================================================== --- commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/ZipTestCase.java (original) +++ commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/ZipTestCase.java Sat Aug 1 19:32:12 2009 @@ -24,7 +24,6 @@ import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; -import java.util.Date; import java.util.List; import org.apache.commons.compress.AbstractTestCase;