Author: bodewig Date: Thu Oct 27 12:32:53 2011 New Revision: 1189720 URL: http://svn.apache.org/viewvc?rev=1189720&view=rev Log: increase test coverage
Added: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/cpio/CpioArchiveOutputStreamTest.java (with props) commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/cpio/CpioUtilTest.java (with props) commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/dump/DumpArchiveInputStreamTest.java (with props) Modified: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/ZipFileTest.java Added: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/cpio/CpioArchiveOutputStreamTest.java URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/cpio/CpioArchiveOutputStreamTest.java?rev=1189720&view=auto ============================================================================== --- commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/cpio/CpioArchiveOutputStreamTest.java (added) +++ commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/cpio/CpioArchiveOutputStreamTest.java Thu Oct 27 12:32:53 2011 @@ -0,0 +1,68 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.commons.compress.archivers.cpio; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.FileOutputStream; +import java.io.InputStream; + +import org.apache.commons.compress.AbstractTestCase; +import org.apache.commons.compress.utils.IOUtils; + +public class CpioArchiveOutputStreamTest extends AbstractTestCase { + + public void testWriteOldBinary() throws Exception { + final File f = getFile("test1.xml"); + final File output = new File(dir, "test.cpio"); + final FileOutputStream out = new FileOutputStream(output); + InputStream in = null; + try { + final CpioArchiveOutputStream os = + new CpioArchiveOutputStream(out, CpioArchiveOutputStream + .FORMAT_OLD_BINARY); + os.putArchiveEntry(new CpioArchiveEntry(CpioArchiveOutputStream + .FORMAT_OLD_BINARY, + f, "test1.xml")); + IOUtils.copy(in = new FileInputStream(f), os); + in.close(); + in = null; + os.closeArchiveEntry(); + os.close(); + } finally { + if (in != null) { + in.close(); + } + out.close(); + } + + try { + in = new CpioArchiveInputStream(new FileInputStream(output)); + CpioArchiveEntry e = ((CpioArchiveInputStream) in) + .getNextCPIOEntry(); + assertEquals("test1.xml", e.getName()); + assertNull(((CpioArchiveInputStream) in).getNextEntry()); + } finally { + if (in != null) { + in.close(); + } + } + } +} \ No newline at end of file Propchange: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/cpio/CpioArchiveOutputStreamTest.java ------------------------------------------------------------------------------ svn:eol-style = native Added: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/cpio/CpioUtilTest.java URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/cpio/CpioUtilTest.java?rev=1189720&view=auto ============================================================================== --- commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/cpio/CpioUtilTest.java (added) +++ commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/cpio/CpioUtilTest.java Thu Oct 27 12:32:53 2011 @@ -0,0 +1,55 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.commons.compress.archivers.cpio; + +import org.junit.Test; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; + +public class CpioUtilTest { + + @Test + public void oldBinMagic2ByteArrayNotSwapped() { + assertArrayEquals(new byte[] { (byte) 0xc7, 0x71 }, + CpioUtil.long2byteArray(CpioConstants.MAGIC_OLD_BINARY, + 2, false)); + } + + @Test + public void oldBinMagic2ByteArraySwapped() { + assertArrayEquals(new byte[] { 0x71, (byte) 0xc7, }, + CpioUtil.long2byteArray(CpioConstants.MAGIC_OLD_BINARY, + 2, true)); + } + + @Test + public void oldBinMagicFromByteArrayNotSwapped() { + assertEquals(CpioConstants.MAGIC_OLD_BINARY, + CpioUtil.byteArray2long(new byte[] { (byte) 0xc7, 0x71 }, + false)); + } + + @Test + public void oldBinMagicFromByteArraySwapped() { + assertEquals(CpioConstants.MAGIC_OLD_BINARY, + CpioUtil.byteArray2long(new byte[] { 0x71, (byte) 0xc7 }, + true)); + } + +} \ No newline at end of file Propchange: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/cpio/CpioUtilTest.java ------------------------------------------------------------------------------ svn:eol-style = native Added: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/dump/DumpArchiveInputStreamTest.java URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/dump/DumpArchiveInputStreamTest.java?rev=1189720&view=auto ============================================================================== --- commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/dump/DumpArchiveInputStreamTest.java (added) +++ commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/dump/DumpArchiveInputStreamTest.java Thu Oct 27 12:32:53 2011 @@ -0,0 +1,53 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.commons.compress.archivers.dump; + +import java.io.FileInputStream; +import org.apache.commons.compress.archivers.ArchiveException; +import org.apache.commons.compress.AbstractTestCase; + +public class DumpArchiveInputStreamTest extends AbstractTestCase { + + public void testNotADumpArchive() throws Exception { + FileInputStream is = new FileInputStream(getFile("bla.zip")); + try { + new DumpArchiveInputStream(is); + fail("expected an exception"); + } catch (ArchiveException ex) { + // expected + assertTrue(ex.getCause() instanceof ShortFileException); + } finally { + is.close(); + } + } + + public void testNotADumpArchiveButBigEnough() throws Exception { + FileInputStream is = new FileInputStream(getFile("zip64support.tar.bz2")); + try { + new DumpArchiveInputStream(is); + fail("expected an exception"); + } catch (ArchiveException ex) { + // expected + assertTrue(ex.getCause() instanceof UnrecognizedFormatException); + } finally { + is.close(); + } + } + +} Propchange: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/dump/DumpArchiveInputStreamTest.java ------------------------------------------------------------------------------ svn:eol-style = native Modified: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/ZipFileTest.java URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/ZipFileTest.java?rev=1189720&r1=1189719&r2=1189720&view=diff ============================================================================== --- commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/ZipFileTest.java (original) +++ commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/ZipFileTest.java Thu Oct 27 12:32:53 2011 @@ -19,6 +19,9 @@ package org.apache.commons.compress.archivers.zip; import java.io.File; +import java.io.FileOutputStream; +import java.io.InputStream; +import java.io.OutputStream; import java.net.URI; import java.net.URL; import java.util.ArrayList; @@ -99,6 +102,43 @@ public class ZipFileTest extends TestCas } } + public void testReadingOfStoredEntry() throws Exception { + File f = File.createTempFile("commons-compress-zipfiletest", ".zip"); + f.deleteOnExit(); + OutputStream o = null; + InputStream i = null; + try { + o = new FileOutputStream(f); + ZipArchiveOutputStream zo = new ZipArchiveOutputStream(o); + ZipArchiveEntry ze = new ZipArchiveEntry("foo"); + ze.setMethod(ZipArchiveEntry.STORED); + ze.setSize(4); + ze.setCrc(0xb63cfbcdl); + zo.putArchiveEntry(ze); + zo.write(new byte[] { 1, 2, 3, 4 }); + zo.closeArchiveEntry(); + zo.close(); + o.close(); + o = null; + + zf = new ZipFile(f); + ze = zf.getEntry("foo"); + assertNotNull(ze); + i = zf.getInputStream(ze); + byte[] b = new byte[4]; + assertEquals(4, i.read(b)); + assertEquals(-1, i.read()); + } finally { + if (o != null) { + o.close(); + } + if (i != null) { + i.close(); + } + f.delete(); + } + } + /* * ordertest.zip has been handcrafted. *