KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > io > BasicStreamTest


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.io;
5
6 import java.io.IOException JavaDoc;
7 import java.util.Arrays JavaDoc;
8
9 import junit.framework.TestCase;
10
11 public class BasicStreamTest extends TestCase {
12
13   public void testBasic() throws IOException JavaDoc {
14     TCByteBufferOutputStream os = new TCByteBufferOutputStream();
15     
16     os.write(new byte[] { -5, 5 });
17     os.write(42);
18     os.writeBoolean(true);
19     os.writeBoolean(false);
20     os.writeByte(11);
21     os.writeChar('t');
22     os.writeDouble(3.14D);
23     os.writeFloat(2.78F);
24     os.writeInt(12345678);
25     os.writeLong(Long.MIN_VALUE);
26     os.writeShort(Short.MAX_VALUE);
27     os.writeString("yo yo yo");
28     os.writeString(null);
29     os.writeString(createString(100000));
30
31     TCByteBufferInputStream is = new TCByteBufferInputStream(os.toArray());
32     byte[] b = new byte[2];
33     Arrays.fill(b, (byte) 69); // these values will be overwritten
34
int read = is.read(b);
35     assertEquals(2, read);
36     assertTrue(Arrays.equals(new byte[] { -5, 5 }, b));
37     assertEquals(42, is.read());
38     assertEquals(true, is.readBoolean());
39     assertEquals(false, is.readBoolean());
40     assertEquals(11, is.readByte());
41     assertEquals('t', is.readChar());
42     assertEquals(Double.doubleToLongBits(3.14D), Double.doubleToLongBits(is.readDouble()));
43     assertEquals(Float.floatToIntBits(2.78F), Float.floatToIntBits(is.readFloat()));
44     assertEquals(12345678, is.readInt());
45     assertEquals(Long.MIN_VALUE, is.readLong());
46     assertEquals(Short.MAX_VALUE, is.readShort());
47     assertEquals("yo yo yo", is.readString());
48     assertEquals(null, is.readString());
49     assertEquals(createString(100000), is.readString());
50   }
51
52   private static String JavaDoc createString(int length) {
53     char[] chars = new char[length];
54     Arrays.fill(chars, 't');
55     return new String JavaDoc(chars);
56   }
57
58 }
59
Popular Tags