001: /*
002: * Copyright 2001-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.commons.codec.binary;
018:
019: import java.util.Arrays;
020: import java.util.Random;
021:
022: import junit.framework.TestCase;
023: import org.apache.commons.codec.DecoderException;
024: import org.apache.commons.codec.EncoderException;
025:
026: /**
027: * Tests {@link org.apache.commons.codec.binary.Hex}.
028: *
029: * @author Apache Software Foundation
030: * @version $Id: HexTest.java,v 1.10 2004/04/18 18:22:33 ggregory Exp $
031: */
032:
033: public class HexTest extends TestCase {
034:
035: public HexTest(String name) {
036: super (name);
037: }
038:
039: public void testDecodeArrayOddCharacters() {
040: try {
041: new Hex().decode(new byte[] { 65 });
042: fail("An exception wasn't thrown when trying to decode an odd number of characters");
043: } catch (DecoderException e) {
044: // Expected exception
045: }
046: }
047:
048: public void testDecodeBadCharacterPos0() {
049: try {
050: new Hex().decode("q0");
051: fail("An exception wasn't thrown when trying to decode an illegal character");
052: } catch (DecoderException e) {
053: // Expected exception
054: }
055: }
056:
057: public void testDecodeBadCharacterPos1() {
058: try {
059: new Hex().decode("0q");
060: fail("An exception wasn't thrown when trying to decode an illegal character");
061: } catch (DecoderException e) {
062: // Expected exception
063: }
064: }
065:
066: public void testDecodeClassCastException() {
067: try {
068: new Hex().decode(new int[] { 65 });
069: fail("An exception wasn't thrown when trying to decode.");
070: } catch (DecoderException e) {
071: // Expected exception
072: }
073: }
074:
075: public void testDecodeHexOddCharacters() {
076: try {
077: Hex.decodeHex(new char[] { 'A' });
078: fail("An exception wasn't thrown when trying to decode an odd number of characters");
079: } catch (DecoderException e) {
080: // Expected exception
081: }
082: }
083:
084: public void testDecodeStringOddCharacters() {
085: try {
086: new Hex().decode("6");
087: fail("An exception wasn't thrown when trying to decode an odd number of characters");
088: } catch (DecoderException e) {
089: // Expected exception
090: }
091: }
092:
093: public void testDencodeEmpty() throws DecoderException {
094: assertTrue(Arrays.equals(new byte[0], Hex
095: .decodeHex(new char[0])));
096: assertTrue(Arrays.equals(new byte[0], new Hex()
097: .decode(new byte[0])));
098: assertTrue(Arrays.equals(new byte[0], (byte[]) new Hex()
099: .decode("")));
100: }
101:
102: public void testEncodeClassCastException() {
103: try {
104: new Hex().encode(new int[] { 65 });
105: fail("An exception wasn't thrown when trying to encode.");
106: } catch (EncoderException e) {
107: // Expected exception
108: }
109: }
110:
111: public void testEncodeDecodeRandom() throws DecoderException,
112: EncoderException {
113: Random random = new Random();
114:
115: Hex hex = new Hex();
116: for (int i = 5; i > 0; i--) {
117: byte[] data = new byte[random.nextInt(10000) + 1];
118: random.nextBytes(data);
119:
120: // static API
121: char[] encodedChars = Hex.encodeHex(data);
122: byte[] decodedBytes = Hex.decodeHex(encodedChars);
123: assertTrue(Arrays.equals(data, decodedBytes));
124:
125: // instance API with array parameter
126: byte[] encodedStringBytes = hex.encode(data);
127: decodedBytes = hex.decode(encodedStringBytes);
128: assertTrue(Arrays.equals(data, decodedBytes));
129:
130: // instance API with char[] (Object) parameter
131: String dataString = new String(encodedChars);
132: char[] encodedStringChars = (char[]) hex.encode(dataString);
133: decodedBytes = (byte[]) hex.decode(encodedStringChars);
134: assertTrue(Arrays.equals(dataString.getBytes(),
135: decodedBytes));
136:
137: // instance API with String (Object) parameter
138: dataString = new String(encodedChars);
139: encodedStringChars = (char[]) hex.encode(dataString);
140: decodedBytes = (byte[]) hex.decode(new String(
141: encodedStringChars));
142: assertTrue(Arrays.equals(dataString.getBytes(),
143: decodedBytes));
144: }
145: }
146:
147: public void testEncodeEmpty() throws EncoderException {
148: assertTrue(Arrays.equals(new char[0], Hex
149: .encodeHex(new byte[0])));
150: assertTrue(Arrays.equals(new byte[0], new Hex()
151: .encode(new byte[0])));
152: assertTrue(Arrays.equals(new char[0], (char[]) new Hex()
153: .encode("")));
154: }
155:
156: public void testEncodeZeroes() {
157: char[] c = Hex.encodeHex(new byte[36]);
158: assertEquals("000000000000000000000000000000000000"
159: + "000000000000000000000000000000000000", new String(c));
160: }
161:
162: public void testHelloWorld() {
163: byte[] b = "Hello World".getBytes();
164: char[] c = Hex.encodeHex(b);
165: assertEquals("48656c6c6f20576f726c64", new String(c));
166: }
167: }
|