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: package org.apache.commons.io;
018:
019: import java.io.IOException;
020:
021: import org.apache.commons.io.output.ByteArrayOutputStream;
022:
023: import junit.framework.TestCase;
024:
025: /**
026: * @author Scott Sanders (sanders at apache dot org)
027: * @author Marc Johnson (mjohnson at apache dot org)
028: * @version $Revision: 437567 $ $Date: 2006-08-28 08:39:07 +0200 (Mo, 28 Aug 2006) $
029: */
030:
031: public class HexDumpTest extends TestCase {
032:
033: /**
034: * Creates new HexDumpTest
035: *
036: * @param name
037: */
038:
039: public HexDumpTest(String name) {
040: super (name);
041: }
042:
043: private char toHex(int n) {
044: char[] hexChars = { '0', '1', '2', '3', '4', '5', '6', '7',
045: '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
046:
047: return hexChars[n % 16];
048: }
049:
050: /**
051: * test dump method
052: *
053: * @exception IOException
054: */
055:
056: public void testDump() throws IOException {
057: byte[] testArray = new byte[256];
058:
059: for (int j = 0; j < 256; j++) {
060: testArray[j] = (byte) j;
061: }
062: ByteArrayOutputStream stream = new ByteArrayOutputStream();
063:
064: HexDump.dump(testArray, 0, stream, 0);
065: byte[] outputArray = new byte[16 * (73 + HexDump.EOL.length())];
066:
067: for (int j = 0; j < 16; j++) {
068: int offset = (73 + HexDump.EOL.length()) * j;
069:
070: outputArray[offset++] = (byte) '0';
071: outputArray[offset++] = (byte) '0';
072: outputArray[offset++] = (byte) '0';
073: outputArray[offset++] = (byte) '0';
074: outputArray[offset++] = (byte) '0';
075: outputArray[offset++] = (byte) '0';
076: outputArray[offset++] = (byte) toHex(j);
077: outputArray[offset++] = (byte) '0';
078: outputArray[offset++] = (byte) ' ';
079: for (int k = 0; k < 16; k++) {
080: outputArray[offset++] = (byte) toHex(j);
081: outputArray[offset++] = (byte) toHex(k);
082: outputArray[offset++] = (byte) ' ';
083: }
084: for (int k = 0; k < 16; k++) {
085: outputArray[offset++] = (byte) toAscii((j * 16) + k);
086: }
087: System.arraycopy(HexDump.EOL.getBytes(), 0, outputArray,
088: offset, HexDump.EOL.getBytes().length);
089: }
090: byte[] actualOutput = stream.toByteArray();
091:
092: assertEquals("array size mismatch", outputArray.length,
093: actualOutput.length);
094: for (int j = 0; j < outputArray.length; j++) {
095: assertEquals("array[ " + j + "] mismatch", outputArray[j],
096: actualOutput[j]);
097: }
098:
099: // verify proper behavior with non-zero offset
100: stream = new ByteArrayOutputStream();
101: HexDump.dump(testArray, 0x10000000, stream, 0);
102: outputArray = new byte[16 * (73 + HexDump.EOL.length())];
103: for (int j = 0; j < 16; j++) {
104: int offset = (73 + HexDump.EOL.length()) * j;
105:
106: outputArray[offset++] = (byte) '1';
107: outputArray[offset++] = (byte) '0';
108: outputArray[offset++] = (byte) '0';
109: outputArray[offset++] = (byte) '0';
110: outputArray[offset++] = (byte) '0';
111: outputArray[offset++] = (byte) '0';
112: outputArray[offset++] = (byte) toHex(j);
113: outputArray[offset++] = (byte) '0';
114: outputArray[offset++] = (byte) ' ';
115: for (int k = 0; k < 16; k++) {
116: outputArray[offset++] = (byte) toHex(j);
117: outputArray[offset++] = (byte) toHex(k);
118: outputArray[offset++] = (byte) ' ';
119: }
120: for (int k = 0; k < 16; k++) {
121: outputArray[offset++] = (byte) toAscii((j * 16) + k);
122: }
123: System.arraycopy(HexDump.EOL.getBytes(), 0, outputArray,
124: offset, HexDump.EOL.getBytes().length);
125: }
126: actualOutput = stream.toByteArray();
127: assertEquals("array size mismatch", outputArray.length,
128: actualOutput.length);
129: for (int j = 0; j < outputArray.length; j++) {
130: assertEquals("array[ " + j + "] mismatch", outputArray[j],
131: actualOutput[j]);
132: }
133:
134: // verify proper behavior with negative offset
135: stream = new ByteArrayOutputStream();
136: HexDump.dump(testArray, 0xFF000000, stream, 0);
137: outputArray = new byte[16 * (73 + HexDump.EOL.length())];
138: for (int j = 0; j < 16; j++) {
139: int offset = (73 + HexDump.EOL.length()) * j;
140:
141: outputArray[offset++] = (byte) 'F';
142: outputArray[offset++] = (byte) 'F';
143: outputArray[offset++] = (byte) '0';
144: outputArray[offset++] = (byte) '0';
145: outputArray[offset++] = (byte) '0';
146: outputArray[offset++] = (byte) '0';
147: outputArray[offset++] = (byte) toHex(j);
148: outputArray[offset++] = (byte) '0';
149: outputArray[offset++] = (byte) ' ';
150: for (int k = 0; k < 16; k++) {
151: outputArray[offset++] = (byte) toHex(j);
152: outputArray[offset++] = (byte) toHex(k);
153: outputArray[offset++] = (byte) ' ';
154: }
155: for (int k = 0; k < 16; k++) {
156: outputArray[offset++] = (byte) toAscii((j * 16) + k);
157: }
158: System.arraycopy(HexDump.EOL.getBytes(), 0, outputArray,
159: offset, HexDump.EOL.getBytes().length);
160: }
161: actualOutput = stream.toByteArray();
162: assertEquals("array size mismatch", outputArray.length,
163: actualOutput.length);
164: for (int j = 0; j < outputArray.length; j++) {
165: assertEquals("array[ " + j + "] mismatch", outputArray[j],
166: actualOutput[j]);
167: }
168:
169: // verify proper behavior with non-zero index
170: stream = new ByteArrayOutputStream();
171: HexDump.dump(testArray, 0x10000000, stream, 0x81);
172: outputArray = new byte[(8 * (73 + HexDump.EOL.length())) - 1];
173: for (int j = 0; j < 8; j++) {
174: int offset = (73 + HexDump.EOL.length()) * j;
175:
176: outputArray[offset++] = (byte) '1';
177: outputArray[offset++] = (byte) '0';
178: outputArray[offset++] = (byte) '0';
179: outputArray[offset++] = (byte) '0';
180: outputArray[offset++] = (byte) '0';
181: outputArray[offset++] = (byte) '0';
182: outputArray[offset++] = (byte) toHex(j + 8);
183: outputArray[offset++] = (byte) '1';
184: outputArray[offset++] = (byte) ' ';
185: for (int k = 0; k < 16; k++) {
186: int index = 0x81 + (j * 16) + k;
187:
188: if (index < 0x100) {
189: outputArray[offset++] = (byte) toHex(index / 16);
190: outputArray[offset++] = (byte) toHex(index);
191: } else {
192: outputArray[offset++] = (byte) ' ';
193: outputArray[offset++] = (byte) ' ';
194: }
195: outputArray[offset++] = (byte) ' ';
196: }
197: for (int k = 0; k < 16; k++) {
198: int index = 0x81 + (j * 16) + k;
199:
200: if (index < 0x100) {
201: outputArray[offset++] = (byte) toAscii(index);
202: }
203: }
204: System.arraycopy(HexDump.EOL.getBytes(), 0, outputArray,
205: offset, HexDump.EOL.getBytes().length);
206: }
207: actualOutput = stream.toByteArray();
208: assertEquals("array size mismatch", outputArray.length,
209: actualOutput.length);
210: for (int j = 0; j < outputArray.length; j++) {
211: assertEquals("array[ " + j + "] mismatch", outputArray[j],
212: actualOutput[j]);
213: }
214:
215: // verify proper behavior with negative index
216: try {
217: HexDump.dump(testArray, 0x10000000,
218: new ByteArrayOutputStream(), -1);
219: fail("should have caught ArrayIndexOutOfBoundsException on negative index");
220: } catch (ArrayIndexOutOfBoundsException ignored_exception) {
221:
222: // as expected
223: }
224:
225: // verify proper behavior with index that is too large
226: try {
227: HexDump.dump(testArray, 0x10000000,
228: new ByteArrayOutputStream(), testArray.length);
229: fail("should have caught ArrayIndexOutOfBoundsException on large index");
230: } catch (ArrayIndexOutOfBoundsException ignored_exception) {
231:
232: // as expected
233: }
234:
235: // verify proper behavior with null stream
236: try {
237: HexDump.dump(testArray, 0x10000000, null, 0);
238: fail("should have caught IllegalArgumentException on negative index");
239: } catch (IllegalArgumentException ignored_exception) {
240:
241: // as expected
242: }
243: }
244:
245: private char toAscii(int c) {
246: char rval = '.';
247:
248: if ((c >= 32) && (c <= 126)) {
249: rval = (char) c;
250: }
251: return rval;
252: }
253:
254: /**
255: * main method to run the unit tests
256: *
257: * @param ignored_args
258: */
259:
260: public static void main(String[] ignored_args) {
261: System.out.println("Testing io.HexDump functionality");
262: junit.textui.TestRunner.run(HexDumpTest.class);
263: }
264: }
|