001: /*
002: * Copyright (c) 2001-2007, Jean Tessier
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * * Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * * Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in the
014: * documentation and/or other materials provided with the distribution.
015: *
016: * * Neither the name of Jean Tessier nor the names of his contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032:
033: package com.jeantessier.text;
034:
035: import java.io.*;
036:
037: import junit.framework.*;
038:
039: public class TestHex extends TestCase {
040: private StringWriter sw;
041: private PrintWriter pw;
042:
043: private ByteArrayOutputStream baos;
044: private PrintStream ps;
045:
046: protected void setUp() throws Exception {
047: super .setUp();
048:
049: sw = new StringWriter();
050: pw = new PrintWriter(sw);
051:
052: baos = new ByteArrayOutputStream();
053: ps = new PrintStream(baos);
054: }
055:
056: protected void tearDown() throws Exception {
057: try {
058: pw.close();
059: ps.close();
060: } finally {
061: super .tearDown();
062: }
063: }
064:
065: public void testPrintNullToWriter() {
066: try {
067: Hex.print(pw, null);
068: fail("Printed null byte array");
069: } catch (NullPointerException ex) {
070: // Expected
071: }
072: }
073:
074: public void testPrintNullToStream() {
075: try {
076: Hex.print(ps, null);
077: fail("Printed null byte array");
078: } catch (NullPointerException ex) {
079: // Expected
080: }
081: }
082:
083: public void testPrintEmptyToWriter() {
084: Hex.print(pw, new byte[0]);
085: pw.close();
086: assertEquals("", sw.toString());
087: }
088:
089: public void testPrintEmptyToStream() {
090: Hex.print(ps, new byte[0]);
091: pw.close();
092: assertEquals("", baos.toString());
093: }
094:
095: public void testPrintOneByteToWriter() {
096: Hex.print(pw, new byte[] { 0 });
097: pw.close();
098: assertEquals("00", sw.toString());
099: }
100:
101: public void testPrintOneByteToStream() {
102: Hex.print(ps, new byte[] { 0 });
103: pw.close();
104: assertEquals("00", baos.toString());
105: }
106:
107: public void testPrintFourBitsToWriter() {
108: Hex.print(pw, new byte[] { 7 });
109: pw.close();
110: assertEquals("07", sw.toString());
111: }
112:
113: public void testPrintFourBitsToStream() {
114: Hex.print(ps, new byte[] { 7 });
115: pw.close();
116: assertEquals("07", baos.toString());
117: }
118:
119: public void testPrintGeneratesCapitalsToWriter() {
120: Hex.print(pw, new byte[] { 10 });
121: pw.close();
122: assertEquals("0A", sw.toString());
123: }
124:
125: public void testPrintGeneratesCapitalsToStream() {
126: Hex.print(ps, new byte[] { 10 });
127: pw.close();
128: assertEquals("0A", baos.toString());
129: }
130:
131: public void testPrintEightBitsToWriter() {
132: Hex.print(pw, new byte[] { (byte) 255 });
133: pw.close();
134: assertEquals("FF", sw.toString());
135: }
136:
137: public void testPrintEightBitsToStream() {
138: Hex.print(ps, new byte[] { (byte) 255 });
139: pw.close();
140: assertEquals("FF", baos.toString());
141: }
142:
143: public void testPrintTwoBytesToWriter() {
144: Hex.print(pw, new byte[] { 0, 1 });
145: pw.close();
146: assertEquals("0001", sw.toString());
147: }
148:
149: public void testPrintTwoBytesToStream() {
150: Hex.print(ps, new byte[] { 0, 1 });
151: pw.close();
152: assertEquals("0001", baos.toString());
153: }
154:
155: public void testNullToString() {
156: try {
157: Hex.toString(null);
158: fail("Printed null byte array");
159: } catch (NullPointerException ex) {
160: // Expected
161: }
162: }
163:
164: public void testEmptyToString() {
165: assertEquals("", Hex.toString(new byte[0]));
166: }
167:
168: public void testOneByteToString() {
169: assertEquals("00", Hex.toString(new byte[] { 0 }));
170: }
171:
172: public void testFourBitsToString() {
173: assertEquals("07", Hex.toString(new byte[] { 7 }));
174: }
175:
176: public void testToStringGeneratesCapitals() {
177: assertEquals("0A", Hex.toString(new byte[] { 10 }));
178: }
179:
180: public void testEightBitsToString() {
181: assertEquals("FF", Hex.toString(new byte[] { (byte) 255 }));
182: }
183:
184: public void testTwoBytesToString() {
185: assertEquals("0001", Hex.toString(new byte[] { 0, 1 }));
186: }
187: }
|