1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| package com.jeantessier.text; |
34 |
| |
35 |
| import java.io.*; |
36 |
| |
37 |
| import junit.framework.*; |
38 |
| |
39 |
| public class TestHex extends TestCase { |
40 |
| private StringWriter sw; |
41 |
| private PrintWriter pw; |
42 |
| |
43 |
| private ByteArrayOutputStream baos; |
44 |
| private PrintStream ps; |
45 |
| |
46 |
21
| protected void setUp() throws Exception {
|
47 |
21
| super.setUp();
|
48 |
| |
49 |
21
| sw = new StringWriter();
|
50 |
21
| pw = new PrintWriter(sw);
|
51 |
| |
52 |
21
| baos = new ByteArrayOutputStream();
|
53 |
21
| ps = new PrintStream(baos);
|
54 |
| } |
55 |
| |
56 |
21
| protected void tearDown() throws Exception {
|
57 |
21
| try {
|
58 |
21
| pw.close();
|
59 |
21
| ps.close();
|
60 |
| } finally { |
61 |
21
| super.tearDown();
|
62 |
| } |
63 |
| } |
64 |
| |
65 |
1
| public void testPrintNullToWriter() {
|
66 |
1
| try {
|
67 |
1
| Hex.print(pw, null);
|
68 |
0
| fail("Printed null byte array");
|
69 |
| } catch (NullPointerException ex) { |
70 |
| |
71 |
| } |
72 |
| } |
73 |
| |
74 |
1
| public void testPrintNullToStream() {
|
75 |
1
| try {
|
76 |
1
| Hex.print(ps, null);
|
77 |
0
| fail("Printed null byte array");
|
78 |
| } catch (NullPointerException ex) { |
79 |
| |
80 |
| } |
81 |
| } |
82 |
| |
83 |
1
| public void testPrintEmptyToWriter() {
|
84 |
1
| Hex.print(pw, new byte[0]);
|
85 |
1
| pw.close();
|
86 |
1
| assertEquals("", sw.toString());
|
87 |
| } |
88 |
| |
89 |
1
| public void testPrintEmptyToStream() {
|
90 |
1
| Hex.print(ps, new byte[0]);
|
91 |
1
| pw.close();
|
92 |
1
| assertEquals("", baos.toString());
|
93 |
| } |
94 |
| |
95 |
1
| public void testPrintOneByteToWriter() {
|
96 |
1
| Hex.print(pw, new byte[] {0});
|
97 |
1
| pw.close();
|
98 |
1
| assertEquals("00", sw.toString());
|
99 |
| } |
100 |
| |
101 |
1
| public void testPrintOneByteToStream() {
|
102 |
1
| Hex.print(ps, new byte[] {0});
|
103 |
1
| pw.close();
|
104 |
1
| assertEquals("00", baos.toString());
|
105 |
| } |
106 |
| |
107 |
1
| public void testPrintFourBitsToWriter() {
|
108 |
1
| Hex.print(pw, new byte[] {7});
|
109 |
1
| pw.close();
|
110 |
1
| assertEquals("07", sw.toString());
|
111 |
| } |
112 |
| |
113 |
1
| public void testPrintFourBitsToStream() {
|
114 |
1
| Hex.print(ps, new byte[] {7});
|
115 |
1
| pw.close();
|
116 |
1
| assertEquals("07", baos.toString());
|
117 |
| } |
118 |
| |
119 |
1
| public void testPrintGeneratesCapitalsToWriter() {
|
120 |
1
| Hex.print(pw, new byte[] {10});
|
121 |
1
| pw.close();
|
122 |
1
| assertEquals("0A", sw.toString());
|
123 |
| } |
124 |
| |
125 |
1
| public void testPrintGeneratesCapitalsToStream() {
|
126 |
1
| Hex.print(ps, new byte[] {10});
|
127 |
1
| pw.close();
|
128 |
1
| assertEquals("0A", baos.toString());
|
129 |
| } |
130 |
| |
131 |
1
| public void testPrintEightBitsToWriter() {
|
132 |
1
| Hex.print(pw, new byte[] {(byte) 255});
|
133 |
1
| pw.close();
|
134 |
1
| assertEquals("FF", sw.toString());
|
135 |
| } |
136 |
| |
137 |
1
| public void testPrintEightBitsToStream() {
|
138 |
1
| Hex.print(ps, new byte[] {(byte) 255});
|
139 |
1
| pw.close();
|
140 |
1
| assertEquals("FF", baos.toString());
|
141 |
| } |
142 |
| |
143 |
1
| public void testPrintTwoBytesToWriter() {
|
144 |
1
| Hex.print(pw, new byte[] {0, 1});
|
145 |
1
| pw.close();
|
146 |
1
| assertEquals("0001", sw.toString());
|
147 |
| } |
148 |
| |
149 |
1
| public void testPrintTwoBytesToStream() {
|
150 |
1
| Hex.print(ps, new byte[] {0, 1});
|
151 |
1
| pw.close();
|
152 |
1
| assertEquals("0001", baos.toString());
|
153 |
| } |
154 |
| |
155 |
1
| public void testNullToString() {
|
156 |
1
| try {
|
157 |
1
| Hex.toString(null);
|
158 |
0
| fail("Printed null byte array");
|
159 |
| } catch (NullPointerException ex) { |
160 |
| |
161 |
| } |
162 |
| } |
163 |
| |
164 |
1
| public void testEmptyToString() {
|
165 |
1
| assertEquals("", Hex.toString(new byte[0]));
|
166 |
| } |
167 |
| |
168 |
1
| public void testOneByteToString() {
|
169 |
1
| assertEquals("00", Hex.toString(new byte[] {0}));
|
170 |
| } |
171 |
| |
172 |
1
| public void testFourBitsToString() {
|
173 |
1
| assertEquals("07", Hex.toString(new byte[] {7}));
|
174 |
| } |
175 |
| |
176 |
1
| public void testToStringGeneratesCapitals() {
|
177 |
1
| assertEquals("0A", Hex.toString(new byte[] {10}));
|
178 |
| } |
179 |
| |
180 |
1
| public void testEightBitsToString() {
|
181 |
1
| assertEquals("FF", Hex.toString(new byte[] {(byte) 255}));
|
182 |
| } |
183 |
| |
184 |
1
| public void testTwoBytesToString() {
|
185 |
1
| assertEquals("0001", Hex.toString(new byte[] {0, 1}));
|
186 |
| } |
187 |
| } |