001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.util;
005:
006: import com.tc.test.TCTestCase;
007:
008: import java.io.StringWriter;
009:
010: /**
011: * Unit test for {@link HexDump}.
012: */
013: public class HexDumpTest extends TCTestCase {
014:
015: public void testZeroBytes() throws Exception {
016: assertEquals("0 bytes: ", HexDump.dump(new byte[0]));
017: }
018:
019: public void testOneByte() throws Exception {
020: assertEquals("1 byte: 9c .", HexDump
021: .dump(new byte[] { ((byte) 0x9c) }));
022: }
023:
024: public void testTwoBytes() throws Exception {
025: assertEquals("2 bytes: 4142 AB", HexDump.dump(new byte[] {
026: (byte) 'A', (byte) 'B' }));
027: }
028:
029: public void testThreeBytes() throws Exception {
030: assertEquals("3 bytes: 4142 43 ABC", HexDump.dump(new byte[] {
031: (byte) 'A', (byte) 'B', (byte) 'C' }));
032: }
033:
034: public void testFifteenBytes() throws Exception {
035: assertEquals(
036: "15 bytes: 4142 4344 4546 4748 494a 4b4c 4d4e 4f ABCDEFGHIJKLMNO",
037: HexDump.dump(new byte[] { (byte) 'A', (byte) 'B',
038: (byte) 'C', (byte) 'D', (byte) 'E', (byte) 'F',
039: (byte) 'G', (byte) 'H', (byte) 'I', (byte) 'J',
040: (byte) 'K', (byte) 'L', (byte) 'M', (byte) 'N',
041: (byte) 'O' }));
042: }
043:
044: public void testSixteenBytes() throws Exception {
045: assertEquals(
046: "16 bytes: 4142 4344 4546 4748 494a 4b4c 4d4e 4f50 ABCDEFGHIJKLMNOP",
047: HexDump.dump(new byte[] { (byte) 'A', (byte) 'B',
048: (byte) 'C', (byte) 'D', (byte) 'E', (byte) 'F',
049: (byte) 'G', (byte) 'H', (byte) 'I', (byte) 'J',
050: (byte) 'K', (byte) 'L', (byte) 'M', (byte) 'N',
051: (byte) 'O', (byte) 'P' }));
052: }
053:
054: public void testSeventeenBytes() throws Exception {
055: assertEquals(
056: "17 bytes:\n"
057: + "00000000: 4142 4344 4546 4748 494a 4b4c 4d4e 4f50 ABCDEFGHIJKLMNOP\n"
058: + "00000010: 51 Q\n",
059: HexDump.dump(new byte[] { (byte) 'A', (byte) 'B',
060: (byte) 'C', (byte) 'D', (byte) 'E', (byte) 'F',
061: (byte) 'G', (byte) 'H', (byte) 'I', (byte) 'J',
062: (byte) 'K', (byte) 'L', (byte) 'M', (byte) 'N',
063: (byte) 'O', (byte) 'P', (byte) 'Q' }));
064: }
065:
066: public void testMoreBytes() throws Exception {
067: assertEquals(
068: "35 bytes:\n"
069: + "00000000: 4142 4344 4546 4748 494a 4b4c 4d4e 4f50 ABCDEFGHIJKLMNOP\n"
070: + "00000010: 5152 5354 5556 5758 595a 5b5c 5d5e 5f60 QRSTUVWXYZ[\\]^_`\n"
071: + "00000020: 6162 63 abc\n",
072: HexDump.dump(new byte[] { (byte) 'A', (byte) 'B',
073: (byte) 'C', (byte) 'D', (byte) 'E', (byte) 'F',
074: (byte) 'G', (byte) 'H', (byte) 'I', (byte) 'J',
075: (byte) 'K', (byte) 'L', (byte) 'M', (byte) 'N',
076: (byte) 'O', (byte) 'P', (byte) 'Q', (byte) 'R',
077: (byte) 'S', (byte) 'T', (byte) 'U', (byte) 'V',
078: (byte) 'W', (byte) 'X', (byte) 'Y', (byte) 'Z',
079: (byte) '[', (byte) '\\', (byte) ']',
080: (byte) '^', (byte) '_', (byte) '`', (byte) 'a',
081: (byte) 'b', (byte) 'c' }));
082: }
083:
084: public void testAllValues() throws Exception {
085: byte[] myArray = new byte[512];
086:
087: for (int i = 0; i < myArray.length; ++i) {
088: myArray[i] = (byte) (i % 256);
089: }
090:
091: assertEquals(
092: "512 bytes:\n"
093: + "00000000: 0001 0203 0405 0607 0809 0a0b 0c0d 0e0f ................\n"
094: + "00000010: 1011 1213 1415 1617 1819 1a1b 1c1d 1e1f ................\n"
095: + "00000020: 2021 2223 2425 2627 2829 2a2b 2c2d 2e2f !\"#$%&'()*+,-./\n"
096: + "00000030: 3031 3233 3435 3637 3839 3a3b 3c3d 3e3f 0123456789:;<=>?\n"
097: + "00000040: 4041 4243 4445 4647 4849 4a4b 4c4d 4e4f @ABCDEFGHIJKLMNO\n"
098: + "00000050: 5051 5253 5455 5657 5859 5a5b 5c5d 5e5f PQRSTUVWXYZ[\\]^_\n"
099: + "00000060: 6061 6263 6465 6667 6869 6a6b 6c6d 6e6f `abcdefghijklmno\n"
100: + "00000070: 7071 7273 7475 7677 7879 7a7b 7c7d 7e7f pqrstuvwxyz{|}~.\n"
101: + "00000080: 8081 8283 8485 8687 8889 8a8b 8c8d 8e8f ................\n"
102: + "00000090: 9091 9293 9495 9697 9899 9a9b 9c9d 9e9f ................\n"
103: + "000000a0: a0a1 a2a3 a4a5 a6a7 a8a9 aaab acad aeaf ................\n"
104: + "000000b0: b0b1 b2b3 b4b5 b6b7 b8b9 babb bcbd bebf ................\n"
105: + "000000c0: c0c1 c2c3 c4c5 c6c7 c8c9 cacb cccd cecf ................\n"
106: + "000000d0: d0d1 d2d3 d4d5 d6d7 d8d9 dadb dcdd dedf ................\n"
107: + "000000e0: e0e1 e2e3 e4e5 e6e7 e8e9 eaeb eced eeef ................\n"
108: + "000000f0: f0f1 f2f3 f4f5 f6f7 f8f9 fafb fcfd feff ................\n"
109: + "00000100: 0001 0203 0405 0607 0809 0a0b 0c0d 0e0f ................\n"
110: + "00000110: 1011 1213 1415 1617 1819 1a1b 1c1d 1e1f ................\n"
111: + "00000120: 2021 2223 2425 2627 2829 2a2b 2c2d 2e2f !\"#$%&'()*+,-./\n"
112: + "00000130: 3031 3233 3435 3637 3839 3a3b 3c3d 3e3f 0123456789:;<=>?\n"
113: + "00000140: 4041 4243 4445 4647 4849 4a4b 4c4d 4e4f @ABCDEFGHIJKLMNO\n"
114: + "00000150: 5051 5253 5455 5657 5859 5a5b 5c5d 5e5f PQRSTUVWXYZ[\\]^_\n"
115: + "00000160: 6061 6263 6465 6667 6869 6a6b 6c6d 6e6f `abcdefghijklmno\n"
116: + "00000170: 7071 7273 7475 7677 7879 7a7b 7c7d 7e7f pqrstuvwxyz{|}~.\n"
117: + "00000180: 8081 8283 8485 8687 8889 8a8b 8c8d 8e8f ................\n"
118: + "00000190: 9091 9293 9495 9697 9899 9a9b 9c9d 9e9f ................\n"
119: + "000001a0: a0a1 a2a3 a4a5 a6a7 a8a9 aaab acad aeaf ................\n"
120: + "000001b0: b0b1 b2b3 b4b5 b6b7 b8b9 babb bcbd bebf ................\n"
121: + "000001c0: c0c1 c2c3 c4c5 c6c7 c8c9 cacb cccd cecf ................\n"
122: + "000001d0: d0d1 d2d3 d4d5 d6d7 d8d9 dadb dcdd dedf ................\n"
123: + "000001e0: e0e1 e2e3 e4e5 e6e7 e8e9 eaeb eced eeef ................\n"
124: + "000001f0: f0f1 f2f3 f4f5 f6f7 f8f9 fafb fcfd feff ................\n",
125: HexDump.dump(myArray));
126: }
127:
128: public void testOffsetAndLength() throws Exception {
129: byte[] myArray = new byte[512];
130:
131: for (int i = 0; i < myArray.length; ++i) {
132: myArray[i] = (byte) (i % 256);
133: }
134:
135: assertEquals(
136: "35 bytes:\n"
137: + "00000000: 4142 4344 4546 4748 494a 4b4c 4d4e 4f50 ABCDEFGHIJKLMNOP\n"
138: + "00000010: 5152 5354 5556 5758 595a 5b5c 5d5e 5f60 QRSTUVWXYZ[\\]^_`\n"
139: + "00000020: 6162 63 abc\n",
140: HexDump.dump(myArray, 0x41, 35));
141:
142: assertEquals(
143: "35 bytes:\n"
144: + "00000000: 4142 4344 4546 4748 494a 4b4c 4d4e 4f50 ABCDEFGHIJKLMNOP\n"
145: + "00000010: 5152 5354 5556 5758 595a 5b5c 5d5e 5f60 QRSTUVWXYZ[\\]^_`\n"
146: + "00000020: 6162 63 abc\n",
147: HexDump.dump(myArray, 0x141, 35));
148: }
149:
150: public void testWriter() throws Exception {
151: StringWriter writer = new StringWriter();
152: HexDump.dump(new byte[] { (byte) 'A', (byte) 'B', (byte) 'C' },
153: writer);
154: String myString = writer.toString();
155: assertEquals("3 bytes: 4142 43 ABC", myString);
156: }
157:
158: public void testOffsetAndLengthWithWriter() throws Exception {
159: byte[] myArray = new byte[512];
160:
161: for (int i = 0; i < myArray.length; ++i) {
162: myArray[i] = (byte) (i % 256);
163: }
164:
165: StringWriter writer = new StringWriter();
166: HexDump.dump(myArray, 0x41, 35, writer);
167: String myString = writer.toString();
168: assertEquals(
169: "35 bytes:\n"
170: + "00000000: 4142 4344 4546 4748 494a 4b4c 4d4e 4f50 ABCDEFGHIJKLMNOP\n"
171: + "00000010: 5152 5354 5556 5758 595a 5b5c 5d5e 5f60 QRSTUVWXYZ[\\]^_`\n"
172: + "00000020: 6162 63 abc\n",
173: myString);
174:
175: writer = new StringWriter();
176: HexDump.dump(myArray, 0x141, 35, writer);
177: myString = writer.toString();
178: assertEquals(
179: "35 bytes:\n"
180: + "00000000: 4142 4344 4546 4748 494a 4b4c 4d4e 4f50 ABCDEFGHIJKLMNOP\n"
181: + "00000010: 5152 5354 5556 5758 595a 5b5c 5d5e 5f60 QRSTUVWXYZ[\\]^_`\n"
182: + "00000020: 6162 63 abc\n",
183: myString);
184:
185: }
186:
187: }
|