1 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 42 public class ReadByteStreamUTest extends TestCase 43 { 44 private static final Class THIS_CLASS = ReadByteStreamUTest.class; 47 48 public ReadByteStreamUTest( String name ) 49 { 50 super( name ); 51 } 52 53 54 57 public void testConstructor1() 58 { 59 try 60 { 61 new ReadByteStream( null ); 62 } 63 catch (IllegalArgumentException iae) 64 { 65 } 67 } 68 69 public void testConstructor2() 70 { 71 try 72 { 73 new ReadByteStream( null, 1, 1 ); 74 } 75 catch (IllegalArgumentException iae) 76 { 77 } 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 iae) 89 { 90 } 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 iae) 103 { 104 } 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 163 protected InputStream createStream( byte[] b ) 164 { 165 return new ByteArrayInputStream( b ); 166 } 167 168 169 protected void assertEquals( String 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 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 [] args ) 202 { 203 String [] name = { THIS_CLASS.getName() }; 204 205 208 junit.textui.TestRunner.main( name ); 209 } 210 211 protected void setUp() throws Exception 212 { 213 super.setUp(); 214 215 } 217 218 219 protected void tearDown() throws Exception 220 { 221 223 224 super.tearDown(); 225 } 226 227 } 228 | Popular Tags |