KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > codec > binary > HexTest


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.commons.codec.binary;
18
19 import java.util.Arrays JavaDoc;
20 import java.util.Random JavaDoc;
21
22 import junit.framework.TestCase;
23 import org.apache.commons.codec.DecoderException;
24 import org.apache.commons.codec.EncoderException;
25
26 /**
27  * Tests {@link org.apache.commons.codec.binary.Hex}.
28  *
29  * @author Apache Software Foundation
30  * @version $Id: HexTest.java,v 1.10 2004/04/18 18:22:33 ggregory Exp $
31  */

32
33 public class HexTest extends TestCase {
34
35     public HexTest(String JavaDoc name) {
36         super(name);
37     }
38     
39     public void testDecodeArrayOddCharacters() {
40         try {
41             new Hex().decode(new byte[] { 65 });
42             fail("An exception wasn't thrown when trying to decode an odd number of characters");
43         }
44         catch (DecoderException e) {
45             // Expected exception
46
}
47     }
48
49     public void testDecodeBadCharacterPos0() {
50         try {
51             new Hex().decode("q0");
52             fail("An exception wasn't thrown when trying to decode an illegal character");
53         }
54         catch (DecoderException e) {
55             // Expected exception
56
}
57     }
58
59     public void testDecodeBadCharacterPos1() {
60         try {
61             new Hex().decode("0q");
62             fail("An exception wasn't thrown when trying to decode an illegal character");
63         }
64         catch (DecoderException e) {
65             // Expected exception
66
}
67     }
68
69     public void testDecodeClassCastException() {
70         try {
71             new Hex().decode(new int[] { 65 });
72             fail("An exception wasn't thrown when trying to decode.");
73         }
74         catch (DecoderException e) {
75             // Expected exception
76
}
77     }
78
79     public void testDecodeHexOddCharacters() {
80         try {
81             Hex.decodeHex(new char[] { 'A' });
82             fail("An exception wasn't thrown when trying to decode an odd number of characters");
83         }
84         catch (DecoderException e) {
85             // Expected exception
86
}
87     }
88
89     public void testDecodeStringOddCharacters() {
90         try {
91             new Hex().decode("6");
92             fail("An exception wasn't thrown when trying to decode an odd number of characters");
93         }
94         catch (DecoderException e) {
95             // Expected exception
96
}
97     }
98
99     public void testDencodeEmpty() throws DecoderException {
100         assertTrue(Arrays.equals(new byte[0], Hex.decodeHex(new char[0])));
101         assertTrue(Arrays.equals(new byte[0], new Hex().decode(new byte[0])));
102         assertTrue(Arrays.equals(new byte[0], (byte[])new Hex().decode("")));
103     }
104     
105     public void testEncodeClassCastException() {
106         try {
107             new Hex().encode(new int[] { 65 });
108             fail("An exception wasn't thrown when trying to encode.");
109         }
110         catch (EncoderException e) {
111             // Expected exception
112
}
113     }
114
115     public void testEncodeDecodeRandom() throws DecoderException, EncoderException {
116         Random JavaDoc random = new Random JavaDoc();
117
118         Hex hex = new Hex();
119         for (int i = 5; i > 0; i--) {
120             byte[] data = new byte[random.nextInt(10000) + 1];
121             random.nextBytes(data);
122
123             // static API
124
char[] encodedChars = Hex.encodeHex(data);
125             byte[] decodedBytes = Hex.decodeHex(encodedChars);
126             assertTrue(Arrays.equals(data, decodedBytes));
127             
128             // instance API with array parameter
129
byte[] encodedStringBytes = hex.encode(data);
130             decodedBytes = hex.decode(encodedStringBytes);
131             assertTrue(Arrays.equals(data, decodedBytes));
132
133             // instance API with char[] (Object) parameter
134
String JavaDoc dataString = new String JavaDoc(encodedChars);
135             char[] encodedStringChars = (char[])hex.encode(dataString);
136             decodedBytes = (byte[])hex.decode(encodedStringChars);
137             assertTrue(Arrays.equals(dataString.getBytes(), decodedBytes));
138
139             // instance API with String (Object) parameter
140
dataString = new String JavaDoc(encodedChars);
141             encodedStringChars = (char[])hex.encode(dataString);
142             decodedBytes = (byte[])hex.decode(new String JavaDoc(encodedStringChars));
143             assertTrue(Arrays.equals(dataString.getBytes(), decodedBytes));
144         }
145     }
146
147     public void testEncodeEmpty() throws EncoderException {
148         assertTrue(Arrays.equals(new char[0], Hex.encodeHex(new byte[0])));
149         assertTrue(Arrays.equals(new byte[0], new Hex().encode(new byte[0])));
150         assertTrue(Arrays.equals(new char[0], (char[])new Hex().encode("")));
151     }
152
153     public void testEncodeZeroes() {
154         char[] c = Hex.encodeHex(new byte[36]);
155         assertEquals(
156             "000000000000000000000000000000000000"
157                 + "000000000000000000000000000000000000",
158             new String JavaDoc(c));
159     }
160
161     public void testHelloWorld() {
162         byte[] b = "Hello World".getBytes();
163         char[] c = Hex.encodeHex(b);
164         assertEquals("48656c6c6f20576f726c64", new String JavaDoc(c));
165     }
166 }
167
Popular Tags