KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > util > io > v1 > ReadByteStreamUTest


1 /*
2  * @(#)ReadByteStreamUTest.java
3  *
4  * Copyright (C) 2001,,2003 2002 Matt Albrecht
5  * groboclown@users.sourceforge.net
6  * http://groboutils.sourceforge.net
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24  * DEALINGS IN THE SOFTWARE.
25  */

26
27 package net.sourceforge.groboutils.util.io.v1;
28
29 import java.io.*;
30
31 import junit.framework.Test;
32 import junit.framework.TestCase;
33 import junit.framework.TestSuite;
34
35 /**
36  *
37  *
38  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
39  * @since 0.9.1d Alpha (sometime in 2001)
40  * @version $Date: 2003/05/23 22:37:58 $
41  */

42 public class ReadByteStreamUTest extends TestCase
43 {
44     //-------------------------------------------------------------------------
45
// Global constructs that only depend upon the class name
46
private static final Class JavaDoc THIS_CLASS = ReadByteStreamUTest.class;
47     
48     public ReadByteStreamUTest( String JavaDoc name )
49     {
50         super( name );
51     }
52     
53     
54     //-------------------------------------------------------------------------
55
// Test methods
56

57     public void testConstructor1()
58     {
59         try
60         {
61             new ReadByteStream( null );
62         }
63         catch (IllegalArgumentException JavaDoc iae)
64         {
65             // test exception
66
}
67     }
68     
69     public void testConstructor2()
70     {
71         try
72         {
73             new ReadByteStream( null, 1, 1 );
74         }
75         catch (IllegalArgumentException JavaDoc iae)
76         {
77             // test exception
78
}
79     }
80     
81     public void testConstructor3()
82     {
83         ByteArrayInputStream bais = new ByteArrayInputStream( new byte[0] );
84         try
85         {
86             new ReadByteStream( bais, 0, 1 );
87         }
88         catch (IllegalArgumentException JavaDoc iae)
89         {
90             // test exception
91
}
92     }
93     
94     
95     public void testConstructor4()
96     {
97         ByteArrayInputStream bais = new ByteArrayInputStream( new byte[0] );
98         try
99         {
100             new ReadByteStream( bais, 1, 0 );
101         }
102         catch (IllegalArgumentException JavaDoc iae)
103         {
104             // test exception
105
}
106     }
107     
108     
109     public void testConstructor5()
110     {
111         byte b[] = {};
112         new ReadByteStream( createStream( b ), 1, 1 );
113     }
114     
115     
116     public void testReadByteStream1() throws IOException
117     {
118         byte in[] = {};
119         ReadByteStream rbs = new ReadByteStream( createStream( in ) );
120         byte out[] = rbs.readByteStream();
121         assertEquals( "Did not read empty stream correctly", in, out );
122     }
123     
124     
125     public void testReadByteStream3() throws IOException
126     {
127         byte in[] = { (byte)1, Byte.MAX_VALUE, Byte.MIN_VALUE, (byte)0 };
128         byte out[] = ReadByteStream.readByteStream( createStream( in ),
129             ReadByteStream.READ_TO_END_OF_STREAM, 2 );
130         assertEquals( "Did not read 4 byte stream correctly", in, out );
131     }
132     
133     
134     public void testReadByteStream4() throws IOException
135     {
136         byte in[] = { Byte.MAX_VALUE };
137         byte out[] = ReadByteStream.readByteStream( createStream( in ),
138             ReadByteStream.READ_TO_END_OF_STREAM, 2 );
139         assertEquals( "Did not read 1 byte stream correctly", in, out );
140     }
141     
142     
143     public void testReadByteStream5() throws IOException
144     {
145         byte in[] = { (byte)1, Byte.MAX_VALUE, Byte.MIN_VALUE };
146         byte out[] = ReadByteStream.readByteStream( createStream( in ),
147             ReadByteStream.READ_TO_END_OF_STREAM, 2 );
148         assertEquals( "Did not read 3 byte stream correctly", in, out );
149     }
150     
151     
152     public void testReadByteStream6() throws IOException
153     {
154         byte in[] = { (byte)1, Byte.MAX_VALUE, Byte.MIN_VALUE };
155         byte out[] = ReadByteStream.readByteStream( createStream( in ) );
156         assertEquals( "Did not read 3 byte stream correctly", in, out );
157     }
158     
159     
160     //-------------------------------------------------------------------------
161
// Helpers
162

163     protected InputStream createStream( byte[] b )
164     {
165         return new ByteArrayInputStream( b );
166     }
167     
168     
169     protected void assertEquals( String JavaDoc msg, byte[] expected, byte[] real )
170     {
171         if (expected == null)
172         {
173             assertNull( msg, real );
174             return;
175         }
176         assertNotNull( msg, real );
177         
178         assertEquals(
179             msg+": lengths are different.",
180             expected.length,
181             real.length);
182         for (int i = 0; i < expected.length; ++i)
183         {
184             assertEquals( msg+": index "+i+" is different.",
185                 expected[i],
186                 real[i] );
187         }
188     }
189     
190     
191     //-------------------------------------------------------------------------
192
// Global static methods that don't change.
193

194     public static Test suite()
195     {
196         TestSuite suite = new TestSuite( THIS_CLASS );
197         
198         return suite;
199     }
200     
201     public static void main( String JavaDoc[] args )
202     {
203         String JavaDoc[] name = { THIS_CLASS.getName() };
204         
205         // junit.textui.TestRunner.main( name );
206
// junit.swingui.TestRunner.main( name );
207

208         junit.textui.TestRunner.main( name );
209     }
210     
211     protected void setUp() throws Exception JavaDoc
212     {
213         super.setUp();
214         
215         // set ourself up
216
}
217     
218     
219     protected void tearDown() throws Exception JavaDoc
220     {
221         // tear ourself down
222

223         
224         super.tearDown();
225     }
226
227 }
228
Popular Tags