add-some-Unit-Tests Added some Unit Tests to increase code coverage.
Project: http://git-wip-us.apache.org/repos/asf/commons-compress/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-compress/commit/b861b4f0 Tree: http://git-wip-us.apache.org/repos/asf/commons-compress/tree/b861b4f0 Diff: http://git-wip-us.apache.org/repos/asf/commons-compress/diff/b861b4f0 Branch: refs/heads/master Commit: b861b4f0e45135e737e1ec2a514d3051157c3c7b Parents: 7254daa Author: Michael Hausegger <michael.hauseg...@tugraz.at> Authored: Tue Jun 13 23:47:50 2017 +0200 Committer: Stefan Bodewig <bode...@apache.org> Committed: Sat Jun 17 10:20:10 2017 +0200 ---------------------------------------------------------------------- .../commons/compress/ArchiveUtilsTest.java | 52 +++++++- .../ChecksumCalculatingInputStreamTest.java | 122 +++++++++++++++++++ .../utils/ChecksumVerifyingInputStreamTest.java | 87 +++++++++++++ .../utils/ServiceLoaderIteratorTest.java | 74 +++++++++++ 4 files changed, 333 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-compress/blob/b861b4f0/src/test/java/org/apache/commons/compress/ArchiveUtilsTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/compress/ArchiveUtilsTest.java b/src/test/java/org/apache/commons/compress/ArchiveUtilsTest.java index 5dc7925..54a2451 100644 --- a/src/test/java/org/apache/commons/compress/ArchiveUtilsTest.java +++ b/src/test/java/org/apache/commons/compress/ArchiveUtilsTest.java @@ -18,11 +18,12 @@ package org.apache.commons.compress; -import static org.junit.Assert.*; - +import org.apache.commons.compress.archivers.sevenz.SevenZArchiveEntry; import org.apache.commons.compress.utils.ArchiveUtils; import org.junit.Test; +import static org.junit.Assert.*; + public class ArchiveUtilsTest extends AbstractTestCase { private static final int bytesToTest = 50; @@ -95,6 +96,53 @@ public class ArchiveUtilsTest extends AbstractTestCase { assertEquals(expected, ArchiveUtils.sanitize(input)); } + @Test + public void testIsEqualWithNullWithPositive() { + + byte[] byteArray = new byte[8]; + byteArray[1] = (byte) (-77); + + assertFalse(ArchiveUtils.isEqualWithNull(byteArray, 0, (byte)0, byteArray, (byte)0, (byte)80)); + + } + + @Test + public void testToAsciiBytes() { + + byte[] byteArray = ArchiveUtils.toAsciiBytes("SOCKET"); + + assertArrayEquals(new byte[] {(byte)83, (byte)79, (byte)67, (byte)75, (byte)69, (byte)84}, byteArray); + + assertFalse(ArchiveUtils.isEqualWithNull(byteArray, 0, 46, byteArray, 63, 0)); + + } + + @Test + public void testToStringWithNonNull() { + + SevenZArchiveEntry sevenZArchiveEntry = new SevenZArchiveEntry(); + String string = ArchiveUtils.toString(sevenZArchiveEntry); + + assertEquals("- 0 null", string); + + } + + @Test + public void testIsEqual() { + + assertTrue(ArchiveUtils.isEqual((byte[]) null, 0, 0, (byte[]) null, 0, 0)); + + } + + @Test(expected = StringIndexOutOfBoundsException.class) + public void testToAsciiStringThrowsStringIndexOutOfBoundsException() { + + byte[] byteArray = new byte[3]; + + ArchiveUtils.toAsciiString(byteArray, 940, 2730); + + } + private void asciiToByteAndBackOK(final String inputString) { assertEquals(inputString, ArchiveUtils.toAsciiString(ArchiveUtils.toAsciiBytes(inputString))); } http://git-wip-us.apache.org/repos/asf/commons-compress/blob/b861b4f0/src/test/java/org/apache/commons/compress/utils/ChecksumCalculatingInputStreamTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/compress/utils/ChecksumCalculatingInputStreamTest.java b/src/test/java/org/apache/commons/compress/utils/ChecksumCalculatingInputStreamTest.java new file mode 100644 index 0000000..09133d3 --- /dev/null +++ b/src/test/java/org/apache/commons/compress/utils/ChecksumCalculatingInputStreamTest.java @@ -0,0 +1,122 @@ +/* + * 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.utils; + +import org.junit.Test; + +import java.io.BufferedInputStream; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.util.zip.Adler32; + +import static org.junit.Assert.*; + +/** + * Unit tests for class {@link ChecksumCalculatingInputStream org.apache.commons.compress.utils.ChecksumCalculatingInputStream}. + * + * @author Michael Hausegger, hausegger.mich...@googlemail.com + * @date 13.06.2017 + * @see ChecksumCalculatingInputStream + **/ +public class ChecksumCalculatingInputStreamTest { + + + + @Test + public void testSkipReturningZero() throws IOException { + + Adler32 adler32_ = new Adler32(); + byte[] byteArray = new byte[0]; + ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray); + ChecksumCalculatingInputStream checksumCalculatingInputStream = new ChecksumCalculatingInputStream(adler32_, byteArrayInputStream); + long skipResult = checksumCalculatingInputStream.skip(60L); + + assertEquals(0L, skipResult); + + assertEquals(1L, checksumCalculatingInputStream.getValue()); + + + } + + + @Test + public void testSkipReturningPositive() throws IOException { + + Adler32 adler32_ = new Adler32(); + byte[] byteArray = new byte[6]; + ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray); + ChecksumCalculatingInputStream checksumCalculatingInputStream = new ChecksumCalculatingInputStream(adler32_, byteArrayInputStream); + long skipResult = checksumCalculatingInputStream.skip((byte)0); + + assertEquals(1L, skipResult); + + assertEquals(65537L, checksumCalculatingInputStream.getValue()); + + } + + + @Test + public void testReadTakingNoArguments() throws IOException { + + Adler32 adler32_ = new Adler32(); + byte[] byteArray = new byte[6]; + ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray); + ChecksumCalculatingInputStream checksumCalculatingInputStream = new ChecksumCalculatingInputStream(adler32_, byteArrayInputStream); + BufferedInputStream bufferedInputStream = new BufferedInputStream(checksumCalculatingInputStream); + int inputStreamReadResult = bufferedInputStream.read(byteArray, 0, 1); + int checkSumCalculationReadResult = checksumCalculatingInputStream.read(); + + assertFalse(checkSumCalculationReadResult == inputStreamReadResult); + assertEquals((-1), checkSumCalculationReadResult); + + assertEquals(0, byteArrayInputStream.available()); + + assertEquals(393217L, checksumCalculatingInputStream.getValue()); + + } + + + @Test(expected = NullPointerException.class) //I assume this behaviour to be a bug or at least a defect. + public void testGetValueThrowsNullPointerException() { + + ChecksumCalculatingInputStream checksumCalculatingInputStream = new ChecksumCalculatingInputStream(null,null); + + checksumCalculatingInputStream.getValue(); + + + } + + + @Test + public void testReadTakingByteArray() throws IOException { + + Adler32 adler32_ = new Adler32(); + byte[] byteArray = new byte[6]; + ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray); + ChecksumCalculatingInputStream checksumCalculatingInputStream = new ChecksumCalculatingInputStream(adler32_, byteArrayInputStream); + int readResult = checksumCalculatingInputStream.read(byteArray); + + assertEquals(6, readResult); + + assertEquals(0, byteArrayInputStream.available()); + assertEquals(393217L, checksumCalculatingInputStream.getValue()); + + } + + + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/commons-compress/blob/b861b4f0/src/test/java/org/apache/commons/compress/utils/ChecksumVerifyingInputStreamTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/compress/utils/ChecksumVerifyingInputStreamTest.java b/src/test/java/org/apache/commons/compress/utils/ChecksumVerifyingInputStreamTest.java new file mode 100644 index 0000000..e63d6aa --- /dev/null +++ b/src/test/java/org/apache/commons/compress/utils/ChecksumVerifyingInputStreamTest.java @@ -0,0 +1,87 @@ +/* + * 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.utils; + +import org.junit.Test; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.util.zip.Adler32; +import java.util.zip.CRC32; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; + +/** + * Unit tests for class {@link ChecksumVerifyingInputStream org.apache.commons.compress.utils.ChecksumVerifyingInputStream}. + * + * @author Michael Hausegger, hausegger.mich...@googlemail.com + * @date 13.06.2017 + * @see ChecksumVerifyingInputStream + **/ +public class ChecksumVerifyingInputStreamTest { + + + + @Test(expected = IOException.class) + public void testReadTakingByteArrayThrowsIOException() throws IOException { + + Adler32 adler32_ = new Adler32(); + byte[] byteArray = new byte[3]; + ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray); + ChecksumVerifyingInputStream checksumVerifyingInputStream = new ChecksumVerifyingInputStream(adler32_, byteArrayInputStream, (-1859L), (byte) (-68)); + + checksumVerifyingInputStream.read(byteArray); + + } + + + @Test(expected = IOException.class) + public void testReadTakingNoArgumentsThrowsIOException() throws IOException { + + CRC32 cRC32_ = new CRC32(); + byte[] byteArray = new byte[9]; + ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray); + ChecksumVerifyingInputStream checksumVerifyingInputStream = new ChecksumVerifyingInputStream(cRC32_, byteArrayInputStream, (byte)1, (byte)1); + + checksumVerifyingInputStream.read(); + + } + + + @Test + public void testSkip() throws IOException { + + CRC32 cRC32_ = new CRC32(); + byte[] byteArray = new byte[4]; + ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray); + ChecksumVerifyingInputStream checksumVerifyingInputStream = new ChecksumVerifyingInputStream(cRC32_, byteArrayInputStream, (byte)33, 2303L); + int intOne = checksumVerifyingInputStream.read(byteArray); + + long skipReturnValue = checksumVerifyingInputStream.skip((byte)1); + + assertEquals(558161692L, cRC32_.getValue()); + assertEquals(0, byteArrayInputStream.available()); + + assertArrayEquals(new byte[] {(byte)0, (byte)0, (byte)0, (byte)0}, byteArray); + assertEquals(0L, skipReturnValue); + + } + + + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/commons-compress/blob/b861b4f0/src/test/java/org/apache/commons/compress/utils/ServiceLoaderIteratorTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/compress/utils/ServiceLoaderIteratorTest.java b/src/test/java/org/apache/commons/compress/utils/ServiceLoaderIteratorTest.java new file mode 100644 index 0000000..0aa1248 --- /dev/null +++ b/src/test/java/org/apache/commons/compress/utils/ServiceLoaderIteratorTest.java @@ -0,0 +1,74 @@ +/* + * 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.utils; + +import org.junit.Test; + +import java.util.NoSuchElementException; + +import static org.junit.Assert.assertFalse; + +/** + * Unit tests for class {@link ServiceLoaderIterator org.apache.commons.compress.utils.ServiceLoaderIterator}. + * + * @author Michael Hausegger, hausegger.mich...@googlemail.com + * @date 13.06.2017 + * @see ServiceLoaderIterator + **/ +public class ServiceLoaderIteratorTest { + + + + @Test(expected = NoSuchElementException.class) + public void testNextThrowsNoSuchElementException() { + + Class<String> clasz = String.class; + ServiceLoaderIterator<String> serviceLoaderIterator = new ServiceLoaderIterator<String>(clasz); + + serviceLoaderIterator.next(); + + } + + + @Test + public void testHasNextReturnsFalse() { + + Class<Object> clasz = Object.class; + ServiceLoaderIterator<Object> serviceLoaderIterator = new ServiceLoaderIterator<Object>(clasz); + boolean result = serviceLoaderIterator.hasNext(); + + assertFalse(result); + + } + + + @Test(expected = UnsupportedOperationException.class) + public void testRemoveThrowsUnsupportedOperationException() { + + Class<Integer> clasz = Integer.class; + ServiceLoaderIterator<Integer> serviceLoaderIterator = new ServiceLoaderIterator<Integer>(clasz); + + serviceLoaderIterator.remove(); + + + } + + + +} \ No newline at end of file