001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.util;
019:
020: import junit.framework.*;
021:
022: import java.io.*;
023:
024: /**
025: * @author Glen Stampoultzis (glens at apache.org)
026: * @author Marc Johnson (mjohnson at apache dot org)
027: */
028:
029: public class TestHexDump extends TestCase {
030:
031: /**
032: * Creates new TestHexDump
033: *
034: * @param name
035: */
036:
037: public TestHexDump(String name) {
038: super (name);
039: }
040:
041: private char toHex(final int n) {
042: char[] hexChars = { '0', '1', '2', '3', '4', '5', '6', '7',
043: '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
044:
045: return hexChars[n % 16];
046: }
047:
048: /**
049: * test dump method
050: *
051: * @exception IOException
052: */
053:
054: public void testDump() throws IOException {
055: byte[] testArray = new byte[256];
056:
057: for (int j = 0; j < 256; j++) {
058: testArray[j] = (byte) j;
059: }
060: ByteArrayOutputStream stream = new ByteArrayOutputStream();
061:
062: HexDump.dump(testArray, 0, stream, 0);
063: byte[] outputArray = new byte[16 * (73 + HexDump.EOL.length())];
064:
065: for (int j = 0; j < 16; j++) {
066: int offset = (73 + HexDump.EOL.length()) * j;
067:
068: outputArray[offset++] = (byte) '0';
069: outputArray[offset++] = (byte) '0';
070: outputArray[offset++] = (byte) '0';
071: outputArray[offset++] = (byte) '0';
072: outputArray[offset++] = (byte) '0';
073: outputArray[offset++] = (byte) '0';
074: outputArray[offset++] = (byte) toHex(j);
075: outputArray[offset++] = (byte) '0';
076: outputArray[offset++] = (byte) ' ';
077: for (int k = 0; k < 16; k++) {
078: outputArray[offset++] = (byte) toHex(j);
079: outputArray[offset++] = (byte) toHex(k);
080: outputArray[offset++] = (byte) ' ';
081: }
082: for (int k = 0; k < 16; k++) {
083: outputArray[offset++] = (byte) toAscii((j * 16) + k);
084: }
085: System.arraycopy(HexDump.EOL.getBytes(), 0, outputArray,
086: offset, HexDump.EOL.getBytes().length);
087: }
088: byte[] actualOutput = stream.toByteArray();
089:
090: assertEquals("array size mismatch", outputArray.length,
091: actualOutput.length);
092: for (int j = 0; j < outputArray.length; j++) {
093: assertEquals("array[ " + j + "] mismatch", outputArray[j],
094: actualOutput[j]);
095: }
096:
097: // verify proper behavior with non-zero offset
098: stream = new ByteArrayOutputStream();
099: HexDump.dump(testArray, 0x10000000, stream, 0);
100: outputArray = new byte[16 * (73 + HexDump.EOL.length())];
101: for (int j = 0; j < 16; j++) {
102: int offset = (73 + HexDump.EOL.length()) * j;
103:
104: outputArray[offset++] = (byte) '1';
105: outputArray[offset++] = (byte) '0';
106: outputArray[offset++] = (byte) '0';
107: outputArray[offset++] = (byte) '0';
108: outputArray[offset++] = (byte) '0';
109: outputArray[offset++] = (byte) '0';
110: outputArray[offset++] = (byte) toHex(j);
111: outputArray[offset++] = (byte) '0';
112: outputArray[offset++] = (byte) ' ';
113: for (int k = 0; k < 16; k++) {
114: outputArray[offset++] = (byte) toHex(j);
115: outputArray[offset++] = (byte) toHex(k);
116: outputArray[offset++] = (byte) ' ';
117: }
118: for (int k = 0; k < 16; k++) {
119: outputArray[offset++] = (byte) toAscii((j * 16) + k);
120: }
121: System.arraycopy(HexDump.EOL.getBytes(), 0, outputArray,
122: offset, HexDump.EOL.getBytes().length);
123: }
124: actualOutput = stream.toByteArray();
125: assertEquals("array size mismatch", outputArray.length,
126: actualOutput.length);
127: for (int j = 0; j < outputArray.length; j++) {
128: assertEquals("array[ " + j + "] mismatch", outputArray[j],
129: actualOutput[j]);
130: }
131:
132: // verify proper behavior with negative offset
133: stream = new ByteArrayOutputStream();
134: HexDump.dump(testArray, 0xFF000000, stream, 0);
135: outputArray = new byte[16 * (73 + HexDump.EOL.length())];
136: for (int j = 0; j < 16; j++) {
137: int offset = (73 + HexDump.EOL.length()) * j;
138:
139: outputArray[offset++] = (byte) 'F';
140: outputArray[offset++] = (byte) 'F';
141: outputArray[offset++] = (byte) '0';
142: outputArray[offset++] = (byte) '0';
143: outputArray[offset++] = (byte) '0';
144: outputArray[offset++] = (byte) '0';
145: outputArray[offset++] = (byte) toHex(j);
146: outputArray[offset++] = (byte) '0';
147: outputArray[offset++] = (byte) ' ';
148: for (int k = 0; k < 16; k++) {
149: outputArray[offset++] = (byte) toHex(j);
150: outputArray[offset++] = (byte) toHex(k);
151: outputArray[offset++] = (byte) ' ';
152: }
153: for (int k = 0; k < 16; k++) {
154: outputArray[offset++] = (byte) toAscii((j * 16) + k);
155: }
156: System.arraycopy(HexDump.EOL.getBytes(), 0, outputArray,
157: offset, HexDump.EOL.getBytes().length);
158: }
159: actualOutput = stream.toByteArray();
160: assertEquals("array size mismatch", outputArray.length,
161: actualOutput.length);
162: for (int j = 0; j < outputArray.length; j++) {
163: assertEquals("array[ " + j + "] mismatch", outputArray[j],
164: actualOutput[j]);
165: }
166:
167: // verify proper behavior with non-zero index
168: stream = new ByteArrayOutputStream();
169: HexDump.dump(testArray, 0x10000000, stream, 0x81);
170: outputArray = new byte[(8 * (73 + HexDump.EOL.length())) - 1];
171: for (int j = 0; j < 8; j++) {
172: int offset = (73 + HexDump.EOL.length()) * j;
173:
174: outputArray[offset++] = (byte) '1';
175: outputArray[offset++] = (byte) '0';
176: outputArray[offset++] = (byte) '0';
177: outputArray[offset++] = (byte) '0';
178: outputArray[offset++] = (byte) '0';
179: outputArray[offset++] = (byte) '0';
180: outputArray[offset++] = (byte) toHex(j + 8);
181: outputArray[offset++] = (byte) '1';
182: outputArray[offset++] = (byte) ' ';
183: for (int k = 0; k < 16; k++) {
184: int index = 0x81 + (j * 16) + k;
185:
186: if (index < 0x100) {
187: outputArray[offset++] = (byte) toHex(index / 16);
188: outputArray[offset++] = (byte) toHex(index);
189: } else {
190: outputArray[offset++] = (byte) ' ';
191: outputArray[offset++] = (byte) ' ';
192: }
193: outputArray[offset++] = (byte) ' ';
194: }
195: for (int k = 0; k < 16; k++) {
196: int index = 0x81 + (j * 16) + k;
197:
198: if (index < 0x100) {
199: outputArray[offset++] = (byte) toAscii(index);
200: }
201: }
202: System.arraycopy(HexDump.EOL.getBytes(), 0, outputArray,
203: offset, HexDump.EOL.getBytes().length);
204: }
205: actualOutput = stream.toByteArray();
206: assertEquals("array size mismatch", outputArray.length,
207: actualOutput.length);
208: for (int j = 0; j < outputArray.length; j++) {
209: assertEquals("array[ " + j + "] mismatch", outputArray[j],
210: actualOutput[j]);
211: }
212:
213: // verify proper behavior with negative index
214: try {
215: HexDump.dump(testArray, 0x10000000,
216: new ByteArrayOutputStream(), -1);
217: fail("should have caught ArrayIndexOutOfBoundsException on negative index");
218: } catch (ArrayIndexOutOfBoundsException ignored_exception) {
219:
220: // as expected
221: }
222:
223: // verify proper behavior with index that is too large
224: try {
225: HexDump.dump(testArray, 0x10000000,
226: new ByteArrayOutputStream(), testArray.length);
227: fail("should have caught ArrayIndexOutOfBoundsException on large index");
228: } catch (ArrayIndexOutOfBoundsException ignored_exception) {
229:
230: // as expected
231: }
232:
233: // verify proper behavior with null stream
234: try {
235: HexDump.dump(testArray, 0x10000000, null, 0);
236: fail("should have caught IllegalArgumentException on negative index");
237: } catch (IllegalArgumentException ignored_exception) {
238:
239: // as expected
240: }
241:
242: // verify proper behaviour with empty byte array
243: ByteArrayOutputStream os = new ByteArrayOutputStream();
244: HexDump.dump(new byte[0], 0, os, 0);
245: assertEquals("No Data" + System.getProperty("line.separator"),
246: os.toString());
247:
248: }
249:
250: public void testToHex() throws Exception {
251: assertEquals("000A", HexDump.toHex((short) 0xA));
252: assertEquals("0A", HexDump.toHex((byte) 0xA));
253: assertEquals("0000000A", HexDump.toHex(0xA));
254:
255: assertEquals("FFFF", HexDump.toHex((short) 0xFFFF));
256:
257: }
258:
259: private char toAscii(final int c) {
260: char rval = '.';
261:
262: if ((c >= 32) && (c <= 126)) {
263: rval = (char) c;
264: }
265: return rval;
266: }
267:
268: /**
269: * main method to run the unit tests
270: *
271: * @param ignored_args
272: */
273:
274: public static void main(String[] ignored_args) {
275: System.out.println("Testing util.HexDump functionality");
276: junit.textui.TestRunner.run(TestHexDump.class);
277: }
278: }
|