KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > util > TestHexDump


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

17         
18
19 package org.apache.poi.util;
20
21 import junit.framework.*;
22
23 import java.io.*;
24
25 /**
26  * @author Marc Johnson (mjohnson at apache dot org)
27  */

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

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

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

314
315     public static void main(String JavaDoc [] ignored_args)
316     {
317         System.out.println("Testing util.HexDump functionality");
318         junit.textui.TestRunner.run(TestHexDump.class);
319     }
320 }
321
Popular Tags