Author: ggregory Date: Tue Jul 28 07:53:10 2009 New Revision: 798433 URL: http://svn.apache.org/viewvc?rev=798433&view=rev Log: Refactor common checks in a method and bring code coverage to 100%/100% line/branch.
Modified: commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/BinaryCodec.java commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/BinaryCodecTest.java Modified: commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/BinaryCodec.java URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/BinaryCodec.java?rev=798433&r1=798432&r2=798433&view=diff ============================================================================== --- commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/BinaryCodec.java (original) +++ commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/BinaryCodec.java Tue Jul 28 07:53:10 2009 @@ -191,7 +191,7 @@ * @return the raw encoded binary where each bit corresponds to a byte in the byte array argument */ public static byte[] fromAscii(byte[] ascii) { - if (ascii == null || ascii.length == 0) { + if (isEmpty(ascii)) { return EMPTY_BYTE_ARRAY; } // get length/8 times bytes with 3 bit shifts to the right of the length @@ -211,6 +211,17 @@ } /** + * Returns <code>true</code> if the given array is <code>null</code> or empty (size 0.) + * + * @param array + * the source array + * @return <code>true</code> if the given array is <code>null</code> or empty (size 0.) + */ + private static boolean isEmpty(byte[] array) { + return array == null || array.length == 0; + } + + /** * Converts an array of raw binary data into an array of ASCII 0 and 1 character bytes - each byte is a truncated * char. * @@ -220,7 +231,7 @@ * @see org.apache.commons.codec.BinaryEncoder#encode(byte[]) */ public static byte[] toAsciiBytes(byte[] raw) { - if (raw == null || raw.length == 0) { + if (isEmpty(raw)) { return EMPTY_BYTE_ARRAY; } // get 8 times the bytes with 3 bit shifts to the left of the length @@ -250,7 +261,7 @@ * @see org.apache.commons.codec.BinaryEncoder#encode(byte[]) */ public static char[] toAsciiChars(byte[] raw) { - if (raw == null || raw.length == 0) { + if (isEmpty(raw)) { return EMPTY_CHAR_ARRAY; } // get 8 times the bytes with 3 bit shifts to the left of the length Modified: commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/BinaryCodecTest.java URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/BinaryCodecTest.java?rev=798433&r1=798432&r2=798433&view=diff ============================================================================== --- commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/BinaryCodecTest.java (original) +++ commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/BinaryCodecTest.java Tue Jul 28 07:53:10 2009 @@ -179,9 +179,9 @@ * Utility used to assert the encoded and decoded values. * * @param bits - * the pre-encoded data + * the pre-encoded data * @param encodeMe - * data to encode and compare + * data to encode and compare */ void assertDecodeObject(byte[] bits, String encodeMe) throws DecoderException { byte[] decoded; @@ -204,7 +204,7 @@ /* * Tests for byte[] decode(byte[]) */ - public void testDecodebyteArray() { + public void testDecodeByteArray() { // With a single raw binary byte[] bits = new byte[1]; byte[] decoded = instance.decode("00000000".getBytes()); @@ -389,7 +389,9 @@ /* * Tests for byte[] fromAscii(char[]) */ - public void testFromAsciicharArray() { + public void testFromAsciiCharArray() { + assertEquals(0, BinaryCodec.fromAscii((char[]) null).length); + assertEquals(0, BinaryCodec.fromAscii(new char[0]).length); // With a single raw binary byte[] bits = new byte[1]; byte[] decoded = BinaryCodec.fromAscii("00000000".toCharArray()); @@ -482,7 +484,9 @@ /* * Tests for byte[] fromAscii(byte[]) */ - public void testFromAsciibyteArray() { + public void testFromAsciiByteArray() { + assertEquals(0, BinaryCodec.fromAscii((byte[]) null).length); + assertEquals(0, BinaryCodec.fromAscii(new byte[0]).length); // With a single raw binary byte[] bits = new byte[1]; byte[] decoded = BinaryCodec.fromAscii("00000000".getBytes()); @@ -575,7 +579,7 @@ /* * Tests for byte[] encode(byte[]) */ - public void testEncodebyteArray() { + public void testEncodeByteArray() { // With a single raw binary byte[] bits = new byte[1]; String l_encoded = new String(instance.encode(bits));