KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > io > HexDumpTest


1 /*
2  * Copyright 1999-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 package org.apache.commons.io;
17
18 import java.io.IOException JavaDoc;
19
20 import org.apache.commons.io.output.ByteArrayOutputStream;
21
22 import junit.framework.TestCase;
23
24
25 /**
26  * @author Scott Sanders (sanders at apache dot org)
27  * @author Marc Johnson (mjohnson at apache dot org)
28  * @version $Revision: 1.5 $ $Date: 2004/02/23 05:02:25 $
29  */

30
31 public class HexDumpTest extends TestCase {
32
33     /**
34      * Creates new HexDumpTest
35      *
36      * @param name
37      */

38
39     public HexDumpTest(String JavaDoc name) {
40         super(name);
41     }
42
43     private char toHex(int n) {
44         char[] hexChars =
45                 {
46                     '0', '1', '2', '3', '4', '5', '6', '7',
47                     '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
48                 };
49
50         return hexChars[n % 16];
51     }
52
53     /**
54      * test dump method
55      *
56      * @exception IOException
57      */

58
59     public void testDump()
60             throws IOException JavaDoc {
61         byte[] testArray = new byte[256];
62
63         for (int j = 0; j < 256; j++) {
64             testArray[j] = (byte) j;
65         }
66         ByteArrayOutputStream stream = new ByteArrayOutputStream();
67
68         HexDump.dump(testArray, 0, stream, 0);
69         byte[] outputArray = new byte[16 * (73 + HexDump.EOL.length())];
70
71         for (int j = 0; j < 16; j++) {
72             int offset = (73 + HexDump.EOL.length()) * j;
73
74             outputArray[offset++] = (byte) '0';
75             outputArray[offset++] = (byte) '0';
76             outputArray[offset++] = (byte) '0';
77             outputArray[offset++] = (byte) '0';
78             outputArray[offset++] = (byte) '0';
79             outputArray[offset++] = (byte) '0';
80             outputArray[offset++] = (byte) toHex(j);
81             outputArray[offset++] = (byte) '0';
82             outputArray[offset++] = (byte) ' ';
83             for (int k = 0; k < 16; k++) {
84                 outputArray[offset++] = (byte) toHex(j);
85                 outputArray[offset++] = (byte) toHex(k);
86                 outputArray[offset++] = (byte) ' ';
87             }
88             for (int k = 0; k < 16; k++) {
89                 outputArray[offset++] = (byte) toAscii((j * 16) + k);
90             }
91             System.arraycopy(HexDump.EOL.getBytes(), 0, outputArray, offset,
92                     HexDump.EOL.getBytes().length);
93         }
94         byte[] actualOutput = stream.toByteArray();
95
96         assertEquals("array size mismatch", outputArray.length,
97                 actualOutput.length);
98         for (int j = 0; j < outputArray.length; j++) {
99             assertEquals("array[ " + j + "] mismatch", outputArray[j],
100                     actualOutput[j]);
101         }
102
103         // verify proper behavior with non-zero offset
104
stream = new ByteArrayOutputStream();
105         HexDump.dump(testArray, 0x10000000, stream, 0);
106         outputArray = new byte[16 * (73 + HexDump.EOL.length())];
107         for (int j = 0; j < 16; j++) {
108             int offset = (73 + HexDump.EOL.length()) * j;
109
110             outputArray[offset++] = (byte) '1';
111             outputArray[offset++] = (byte) '0';
112             outputArray[offset++] = (byte) '0';
113             outputArray[offset++] = (byte) '0';
114             outputArray[offset++] = (byte) '0';
115             outputArray[offset++] = (byte) '0';
116             outputArray[offset++] = (byte) toHex(j);
117             outputArray[offset++] = (byte) '0';
118             outputArray[offset++] = (byte) ' ';
119             for (int k = 0; k < 16; k++) {
120                 outputArray[offset++] = (byte) toHex(j);
121                 outputArray[offset++] = (byte) toHex(k);
122                 outputArray[offset++] = (byte) ' ';
123             }
124             for (int k = 0; k < 16; k++) {
125                 outputArray[offset++] = (byte) toAscii((j * 16) + k);
126             }
127             System.arraycopy(HexDump.EOL.getBytes(), 0, outputArray, offset,
128                     HexDump.EOL.getBytes().length);
129         }
130         actualOutput = stream.toByteArray();
131         assertEquals("array size mismatch", outputArray.length,
132                 actualOutput.length);
133         for (int j = 0; j < outputArray.length; j++) {
134             assertEquals("array[ " + j + "] mismatch", outputArray[j],
135                     actualOutput[j]);
136         }
137
138         // verify proper behavior with negative offset
139
stream = new ByteArrayOutputStream();
140         HexDump.dump(testArray, 0xFF000000, stream, 0);
141         outputArray = new byte[16 * (73 + HexDump.EOL.length())];
142         for (int j = 0; j < 16; j++) {
143             int offset = (73 + HexDump.EOL.length()) * j;
144
145             outputArray[offset++] = (byte) 'F';
146             outputArray[offset++] = (byte) 'F';
147             outputArray[offset++] = (byte) '0';
148             outputArray[offset++] = (byte) '0';
149             outputArray[offset++] = (byte) '0';
150             outputArray[offset++] = (byte) '0';
151             outputArray[offset++] = (byte) toHex(j);
152             outputArray[offset++] = (byte) '0';
153             outputArray[offset++] = (byte) ' ';
154             for (int k = 0; k < 16; k++) {
155                 outputArray[offset++] = (byte) toHex(j);
156                 outputArray[offset++] = (byte) toHex(k);
157                 outputArray[offset++] = (byte) ' ';
158             }
159             for (int k = 0; k < 16; k++) {
160                 outputArray[offset++] = (byte) toAscii((j * 16) + k);
161             }
162             System.arraycopy(HexDump.EOL.getBytes(), 0, outputArray, offset,
163                     HexDump.EOL.getBytes().length);
164         }
165         actualOutput = stream.toByteArray();
166         assertEquals("array size mismatch", outputArray.length,
167                 actualOutput.length);
168         for (int j = 0; j < outputArray.length; j++) {
169             assertEquals("array[ " + j + "] mismatch", outputArray[j],
170                     actualOutput[j]);
171         }
172
173         // verify proper behavior with non-zero index
174
stream = new ByteArrayOutputStream();
175         HexDump.dump(testArray, 0x10000000, stream, 0x81);
176         outputArray = new byte[(8 * (73 + HexDump.EOL.length())) - 1];
177         for (int j = 0; j < 8; j++) {
178             int offset = (73 + HexDump.EOL.length()) * j;
179
180             outputArray[offset++] = (byte) '1';
181             outputArray[offset++] = (byte) '0';
182             outputArray[offset++] = (byte) '0';
183             outputArray[offset++] = (byte) '0';
184             outputArray[offset++] = (byte) '0';
185             outputArray[offset++] = (byte) '0';
186             outputArray[offset++] = (byte) toHex(j + 8);
187             outputArray[offset++] = (byte) '1';
188             outputArray[offset++] = (byte) ' ';
189             for (int k = 0; k < 16; k++) {
190                 int index = 0x81 + (j * 16) + k;
191
192                 if (index < 0x100) {
193                     outputArray[offset++] = (byte) toHex(index / 16);
194                     outputArray[offset++] = (byte) toHex(index);
195                 } else {
196                     outputArray[offset++] = (byte) ' ';
197                     outputArray[offset++] = (byte) ' ';
198                 }
199                 outputArray[offset++] = (byte) ' ';
200             }
201             for (int k = 0; k < 16; k++) {
202                 int index = 0x81 + (j * 16) + k;
203
204                 if (index < 0x100) {
205                     outputArray[offset++] = (byte) toAscii(index);
206                 }
207             }
208             System.arraycopy(HexDump.EOL.getBytes(), 0, outputArray, offset,
209                     HexDump.EOL.getBytes().length);
210         }
211         actualOutput = stream.toByteArray();
212         assertEquals("array size mismatch", outputArray.length,
213                 actualOutput.length);
214         for (int j = 0; j < outputArray.length; j++) {
215             assertEquals("array[ " + j + "] mismatch", outputArray[j],
216                     actualOutput[j]);
217         }
218
219         // verify proper behavior with negative index
220
try {
221             HexDump.dump(testArray, 0x10000000, new ByteArrayOutputStream(),
222                     -1);
223             fail("should have caught ArrayIndexOutOfBoundsException on negative index");
224         } catch (ArrayIndexOutOfBoundsException JavaDoc ignored_exception) {
225
226             // as expected
227
}
228
229         // verify proper behavior with index that is too large
230
try {
231             HexDump.dump(testArray, 0x10000000, new ByteArrayOutputStream(),
232                     testArray.length);
233             fail("should have caught ArrayIndexOutOfBoundsException on large index");
234         } catch (ArrayIndexOutOfBoundsException JavaDoc ignored_exception) {
235
236             // as expected
237
}
238
239         // verify proper behavior with null stream
240
try {
241             HexDump.dump(testArray, 0x10000000, null, 0);
242             fail("should have caught IllegalArgumentException on negative index");
243         } catch (IllegalArgumentException JavaDoc ignored_exception) {
244
245             // as expected
246
}
247     }
248
249     private char toAscii(int c) {
250         char rval = '.';
251
252         if ((c >= 32) && (c <= 126)) {
253             rval = (char) c;
254         }
255         return rval;
256     }
257
258     /**
259      * main method to run the unit tests
260      *
261      * @param ignored_args
262      */

263
264     public static void main(String JavaDoc[] ignored_args) {
265         System.out.println("Testing io.HexDump functionality");
266         junit.textui.TestRunner.run(HexDumpTest.class);
267     }
268 }
269
Popular Tags