001: /*
002: * @(#)ReadByteStreamUTest.java
003: *
004: * Copyright (C) 2001,,2003 2002 Matt Albrecht
005: * groboclown@users.sourceforge.net
006: * http://groboutils.sourceforge.net
007: *
008: * Permission is hereby granted, free of charge, to any person obtaining a
009: * copy of this software and associated documentation files (the "Software"),
010: * to deal in the Software without restriction, including without limitation
011: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
012: * and/or sell copies of the Software, and to permit persons to whom the
013: * Software is furnished to do so, subject to the following conditions:
014: *
015: * The above copyright notice and this permission notice shall be included in
016: * all copies or substantial portions of the Software.
017: *
018: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
019: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
020: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
021: * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
022: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
023: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
024: * DEALINGS IN THE SOFTWARE.
025: */
026:
027: package net.sourceforge.groboutils.util.io.v1;
028:
029: import java.io.*;
030:
031: import junit.framework.Test;
032: import junit.framework.TestCase;
033: import junit.framework.TestSuite;
034:
035: /**
036: *
037: *
038: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
039: * @since 0.9.1d Alpha (sometime in 2001)
040: * @version $Date: 2003/05/23 22:37:58 $
041: */
042: public class ReadByteStreamUTest extends TestCase {
043: //-------------------------------------------------------------------------
044: // Global constructs that only depend upon the class name
045: private static final Class THIS_CLASS = ReadByteStreamUTest.class;
046:
047: public ReadByteStreamUTest(String name) {
048: super (name);
049: }
050:
051: //-------------------------------------------------------------------------
052: // Test methods
053:
054: public void testConstructor1() {
055: try {
056: new ReadByteStream(null);
057: } catch (IllegalArgumentException iae) {
058: // test exception
059: }
060: }
061:
062: public void testConstructor2() {
063: try {
064: new ReadByteStream(null, 1, 1);
065: } catch (IllegalArgumentException iae) {
066: // test exception
067: }
068: }
069:
070: public void testConstructor3() {
071: ByteArrayInputStream bais = new ByteArrayInputStream(
072: new byte[0]);
073: try {
074: new ReadByteStream(bais, 0, 1);
075: } catch (IllegalArgumentException iae) {
076: // test exception
077: }
078: }
079:
080: public void testConstructor4() {
081: ByteArrayInputStream bais = new ByteArrayInputStream(
082: new byte[0]);
083: try {
084: new ReadByteStream(bais, 1, 0);
085: } catch (IllegalArgumentException iae) {
086: // test exception
087: }
088: }
089:
090: public void testConstructor5() {
091: byte b[] = {};
092: new ReadByteStream(createStream(b), 1, 1);
093: }
094:
095: public void testReadByteStream1() throws IOException {
096: byte in[] = {};
097: ReadByteStream rbs = new ReadByteStream(createStream(in));
098: byte out[] = rbs.readByteStream();
099: assertEquals("Did not read empty stream correctly", in, out);
100: }
101:
102: public void testReadByteStream3() throws IOException {
103: byte in[] = { (byte) 1, Byte.MAX_VALUE, Byte.MIN_VALUE,
104: (byte) 0 };
105: byte out[] = ReadByteStream.readByteStream(createStream(in),
106: ReadByteStream.READ_TO_END_OF_STREAM, 2);
107: assertEquals("Did not read 4 byte stream correctly", in, out);
108: }
109:
110: public void testReadByteStream4() throws IOException {
111: byte in[] = { Byte.MAX_VALUE };
112: byte out[] = ReadByteStream.readByteStream(createStream(in),
113: ReadByteStream.READ_TO_END_OF_STREAM, 2);
114: assertEquals("Did not read 1 byte stream correctly", in, out);
115: }
116:
117: public void testReadByteStream5() throws IOException {
118: byte in[] = { (byte) 1, Byte.MAX_VALUE, Byte.MIN_VALUE };
119: byte out[] = ReadByteStream.readByteStream(createStream(in),
120: ReadByteStream.READ_TO_END_OF_STREAM, 2);
121: assertEquals("Did not read 3 byte stream correctly", in, out);
122: }
123:
124: public void testReadByteStream6() throws IOException {
125: byte in[] = { (byte) 1, Byte.MAX_VALUE, Byte.MIN_VALUE };
126: byte out[] = ReadByteStream.readByteStream(createStream(in));
127: assertEquals("Did not read 3 byte stream correctly", in, out);
128: }
129:
130: //-------------------------------------------------------------------------
131: // Helpers
132:
133: protected InputStream createStream(byte[] b) {
134: return new ByteArrayInputStream(b);
135: }
136:
137: protected void assertEquals(String msg, byte[] expected, byte[] real) {
138: if (expected == null) {
139: assertNull(msg, real);
140: return;
141: }
142: assertNotNull(msg, real);
143:
144: assertEquals(msg + ": lengths are different.", expected.length,
145: real.length);
146: for (int i = 0; i < expected.length; ++i) {
147: assertEquals(msg + ": index " + i + " is different.",
148: expected[i], real[i]);
149: }
150: }
151:
152: //-------------------------------------------------------------------------
153: // Global static methods that don't change.
154:
155: public static Test suite() {
156: TestSuite suite = new TestSuite(THIS_CLASS);
157:
158: return suite;
159: }
160:
161: public static void main(String[] args) {
162: String[] name = { THIS_CLASS.getName() };
163:
164: // junit.textui.TestRunner.main( name );
165: // junit.swingui.TestRunner.main( name );
166:
167: junit.textui.TestRunner.main(name);
168: }
169:
170: protected void setUp() throws Exception {
171: super .setUp();
172:
173: // set ourself up
174: }
175:
176: protected void tearDown() throws Exception {
177: // tear ourself down
178:
179: super.tearDown();
180: }
181:
182: }
|