KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > util > HexDumpTest


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.util;
5
6 import com.tc.test.TCTestCase;
7
8 import java.io.StringWriter JavaDoc;
9
10 /**
11  * Unit test for {@link HexDump}.
12  */

13 public class HexDumpTest extends TCTestCase {
14
15   public void testZeroBytes() throws Exception JavaDoc {
16     assertEquals("0 bytes: ", HexDump.dump(new byte[0]));
17   }
18
19   public void testOneByte() throws Exception JavaDoc {
20     assertEquals("1 byte: 9c .", HexDump.dump(new byte[] { ((byte) 0x9c) }));
21   }
22
23   public void testTwoBytes() throws Exception JavaDoc {
24     assertEquals("2 bytes: 4142 AB", HexDump.dump(new byte[] { (byte) 'A', (byte) 'B' }));
25   }
26
27   public void testThreeBytes() throws Exception JavaDoc {
28     assertEquals("3 bytes: 4142 43 ABC", HexDump.dump(new byte[] { (byte) 'A', (byte) 'B', (byte) 'C' }));
29   }
30
31   public void testFifteenBytes() throws Exception JavaDoc {
32     assertEquals("15 bytes: 4142 4344 4546 4748 494a 4b4c 4d4e 4f ABCDEFGHIJKLMNO", HexDump.dump(new byte[] {
33         (byte) 'A', (byte) 'B', (byte) 'C', (byte) 'D', (byte) 'E', (byte) 'F', (byte) 'G', (byte) 'H', (byte) 'I',
34         (byte) 'J', (byte) 'K', (byte) 'L', (byte) 'M', (byte) 'N', (byte) 'O' }));
35   }
36
37   public void testSixteenBytes() throws Exception JavaDoc {
38     assertEquals("16 bytes: 4142 4344 4546 4748 494a 4b4c 4d4e 4f50 ABCDEFGHIJKLMNOP", HexDump.dump(new byte[] {
39         (byte) 'A', (byte) 'B', (byte) 'C', (byte) 'D', (byte) 'E', (byte) 'F', (byte) 'G', (byte) 'H', (byte) 'I',
40         (byte) 'J', (byte) 'K', (byte) 'L', (byte) 'M', (byte) 'N', (byte) 'O', (byte) 'P' }));
41   }
42
43   public void testSeventeenBytes() throws Exception JavaDoc {
44     assertEquals("17 bytes:\n" + "00000000: 4142 4344 4546 4748 494a 4b4c 4d4e 4f50 ABCDEFGHIJKLMNOP\n"
45                  + "00000010: 51 Q\n", HexDump.dump(new byte[] { (byte) 'A',
46         (byte) 'B', (byte) 'C', (byte) 'D', (byte) 'E', (byte) 'F', (byte) 'G', (byte) 'H', (byte) 'I', (byte) 'J',
47         (byte) 'K', (byte) 'L', (byte) 'M', (byte) 'N', (byte) 'O', (byte) 'P', (byte) 'Q' }));
48   }
49
50   public void testMoreBytes() throws Exception JavaDoc {
51     assertEquals("35 bytes:\n" + "00000000: 4142 4344 4546 4748 494a 4b4c 4d4e 4f50 ABCDEFGHIJKLMNOP\n"
52                  + "00000010: 5152 5354 5556 5758 595a 5b5c 5d5e 5f60 QRSTUVWXYZ[\\]^_`\n"
53                  + "00000020: 6162 63 abc\n", HexDump.dump(new byte[] { (byte) 'A',
54         (byte) 'B', (byte) 'C', (byte) 'D', (byte) 'E', (byte) 'F', (byte) 'G', (byte) 'H', (byte) 'I', (byte) 'J',
55         (byte) 'K', (byte) 'L', (byte) 'M', (byte) 'N', (byte) 'O', (byte) 'P', (byte) 'Q', (byte) 'R', (byte) 'S',
56         (byte) 'T', (byte) 'U', (byte) 'V', (byte) 'W', (byte) 'X', (byte) 'Y', (byte) 'Z', (byte) '[', (byte) '\\',
57         (byte) ']', (byte) '^', (byte) '_', (byte) '`', (byte) 'a', (byte) 'b', (byte) 'c' }));
58   }
59
60   public void testAllValues() throws Exception JavaDoc {
61     byte[] myArray = new byte[512];
62
63     for (int i = 0; i < myArray.length; ++i) {
64       myArray[i] = (byte) (i % 256);
65     }
66
67     assertEquals("512 bytes:\n" + "00000000: 0001 0203 0405 0607 0809 0a0b 0c0d 0e0f ................\n"
68                  + "00000010: 1011 1213 1415 1617 1819 1a1b 1c1d 1e1f ................\n"
69                  + "00000020: 2021 2223 2425 2627 2829 2a2b 2c2d 2e2f !\"#$%&'()*+,-./\n"
70                  + "00000030: 3031 3233 3435 3637 3839 3a3b 3c3d 3e3f 0123456789:;<=>?\n"
71                  + "00000040: 4041 4243 4445 4647 4849 4a4b 4c4d 4e4f @ABCDEFGHIJKLMNO\n"
72                  + "00000050: 5051 5253 5455 5657 5859 5a5b 5c5d 5e5f PQRSTUVWXYZ[\\]^_\n"
73                  + "00000060: 6061 6263 6465 6667 6869 6a6b 6c6d 6e6f `abcdefghijklmno\n"
74                  + "00000070: 7071 7273 7475 7677 7879 7a7b 7c7d 7e7f pqrstuvwxyz{|}~.\n"
75                  + "00000080: 8081 8283 8485 8687 8889 8a8b 8c8d 8e8f ................\n"
76                  + "00000090: 9091 9293 9495 9697 9899 9a9b 9c9d 9e9f ................\n"
77                  + "000000a0: a0a1 a2a3 a4a5 a6a7 a8a9 aaab acad aeaf ................\n"
78                  + "000000b0: b0b1 b2b3 b4b5 b6b7 b8b9 babb bcbd bebf ................\n"
79                  + "000000c0: c0c1 c2c3 c4c5 c6c7 c8c9 cacb cccd cecf ................\n"
80                  + "000000d0: d0d1 d2d3 d4d5 d6d7 d8d9 dadb dcdd dedf ................\n"
81                  + "000000e0: e0e1 e2e3 e4e5 e6e7 e8e9 eaeb eced eeef ................\n"
82                  + "000000f0: f0f1 f2f3 f4f5 f6f7 f8f9 fafb fcfd feff ................\n"
83                  + "00000100: 0001 0203 0405 0607 0809 0a0b 0c0d 0e0f ................\n"
84                  + "00000110: 1011 1213 1415 1617 1819 1a1b 1c1d 1e1f ................\n"
85                  + "00000120: 2021 2223 2425 2627 2829 2a2b 2c2d 2e2f !\"#$%&'()*+,-./\n"
86                  + "00000130: 3031 3233 3435 3637 3839 3a3b 3c3d 3e3f 0123456789:;<=>?\n"
87                  + "00000140: 4041 4243 4445 4647 4849 4a4b 4c4d 4e4f @ABCDEFGHIJKLMNO\n"
88                  + "00000150: 5051 5253 5455 5657 5859 5a5b 5c5d 5e5f PQRSTUVWXYZ[\\]^_\n"
89                  + "00000160: 6061 6263 6465 6667 6869 6a6b 6c6d 6e6f `abcdefghijklmno\n"
90                  + "00000170: 7071 7273 7475 7677 7879 7a7b 7c7d 7e7f pqrstuvwxyz{|}~.\n"
91                  + "00000180: 8081 8283 8485 8687 8889 8a8b 8c8d 8e8f ................\n"
92                  + "00000190: 9091 9293 9495 9697 9899 9a9b 9c9d 9e9f ................\n"
93                  + "000001a0: a0a1 a2a3 a4a5 a6a7 a8a9 aaab acad aeaf ................\n"
94                  + "000001b0: b0b1 b2b3 b4b5 b6b7 b8b9 babb bcbd bebf ................\n"
95                  + "000001c0: c0c1 c2c3 c4c5 c6c7 c8c9 cacb cccd cecf ................\n"
96                  + "000001d0: d0d1 d2d3 d4d5 d6d7 d8d9 dadb dcdd dedf ................\n"
97                  + "000001e0: e0e1 e2e3 e4e5 e6e7 e8e9 eaeb eced eeef ................\n"
98                  + "000001f0: f0f1 f2f3 f4f5 f6f7 f8f9 fafb fcfd feff ................\n", HexDump.dump(myArray));
99   }
100
101   public void testOffsetAndLength() throws Exception JavaDoc {
102     byte[] myArray = new byte[512];
103
104     for (int i = 0; i < myArray.length; ++i) {
105       myArray[i] = (byte) (i % 256);
106     }
107
108     assertEquals("35 bytes:\n" + "00000000: 4142 4344 4546 4748 494a 4b4c 4d4e 4f50 ABCDEFGHIJKLMNOP\n"
109                  + "00000010: 5152 5354 5556 5758 595a 5b5c 5d5e 5f60 QRSTUVWXYZ[\\]^_`\n"
110                  + "00000020: 6162 63 abc\n", HexDump.dump(myArray, 0x41, 35));
111
112     assertEquals("35 bytes:\n" + "00000000: 4142 4344 4546 4748 494a 4b4c 4d4e 4f50 ABCDEFGHIJKLMNOP\n"
113                  + "00000010: 5152 5354 5556 5758 595a 5b5c 5d5e 5f60 QRSTUVWXYZ[\\]^_`\n"
114                  + "00000020: 6162 63 abc\n", HexDump.dump(myArray, 0x141, 35));
115   }
116
117   public void testWriter() throws Exception JavaDoc {
118     StringWriter JavaDoc writer = new StringWriter JavaDoc();
119     HexDump.dump(new byte[] { (byte) 'A', (byte) 'B', (byte) 'C' }, writer);
120     String JavaDoc myString = writer.toString();
121     assertEquals("3 bytes: 4142 43 ABC", myString);
122   }
123
124   public void testOffsetAndLengthWithWriter() throws Exception JavaDoc {
125     byte[] myArray = new byte[512];
126
127     for (int i = 0; i < myArray.length; ++i) {
128       myArray[i] = (byte) (i % 256);
129     }
130
131     StringWriter JavaDoc writer = new StringWriter JavaDoc();
132     HexDump.dump(myArray, 0x41, 35, writer);
133     String JavaDoc myString = writer.toString();
134     assertEquals("35 bytes:\n" + "00000000: 4142 4344 4546 4748 494a 4b4c 4d4e 4f50 ABCDEFGHIJKLMNOP\n"
135                  + "00000010: 5152 5354 5556 5758 595a 5b5c 5d5e 5f60 QRSTUVWXYZ[\\]^_`\n"
136                  + "00000020: 6162 63 abc\n", myString);
137
138     writer = new StringWriter JavaDoc();
139     HexDump.dump(myArray, 0x141, 35, writer);
140     myString = writer.toString();
141     assertEquals("35 bytes:\n" + "00000000: 4142 4344 4546 4748 494a 4b4c 4d4e 4f50 ABCDEFGHIJKLMNOP\n"
142                  + "00000010: 5152 5354 5556 5758 595a 5b5c 5d5e 5f60 QRSTUVWXYZ[\\]^_`\n"
143                  + "00000020: 6162 63 abc\n", myString);
144
145   }
146
147 }
148
Popular Tags