KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > io > serializer > TCObjectInputOutputStreamTest


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.serializer;
5
6 import com.tc.object.ObjectID;
7 import com.tc.object.dna.impl.ClassInstance;
8 import com.tc.object.dna.impl.UTF8ByteDataHolder;
9 import com.tc.test.TCTestCase;
10 import com.tc.util.Assert;
11
12 import java.io.ByteArrayInputStream JavaDoc;
13 import java.io.ByteArrayOutputStream JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.math.BigDecimal JavaDoc;
16 import java.math.BigInteger JavaDoc;
17 import java.util.ArrayList JavaDoc;
18 import java.util.Arrays JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Random JavaDoc;
22
23 public class TCObjectInputOutputStreamTest extends TCTestCase {
24
25   public void testBasic() throws IOException JavaDoc, ClassNotFoundException JavaDoc {
26     ByteArrayOutputStream JavaDoc bao = new ByteArrayOutputStream JavaDoc(1024);
27
28     TCObjectOutputStream os = new TCObjectOutputStream(bao);
29
30     ArrayList JavaDoc l = getLiteralObjects();
31
32     os.write(new byte[] { -5, 5 });
33     os.write(42);
34     os.writeBoolean(true);
35     os.writeBoolean(false);
36     os.writeByte(11);
37     os.writeChar('t');
38     os.writeDouble(3.14D);
39     os.writeFloat(2.78F);
40     os.writeInt(12345678);
41     os.writeLong(Long.MIN_VALUE);
42     os.writeShort(Short.MAX_VALUE);
43     os.writeString("yo yo yo");
44     os.writeString(null);
45     os.writeString(createString(100000));
46     writeObjects(os, l);
47
48     // Now try to write a non Literal Object
49
boolean failed = false;
50     try {
51       os.writeObject(new Object JavaDoc());
52       failed = true;
53     } catch (Exception JavaDoc ex) {
54       // this is normal
55
}
56     Assert.assertFalse(failed);
57     try {
58       os.writeObject(l);
59       failed = true;
60     } catch (Exception JavaDoc ex) {
61       // this is normal
62
}
63     Assert.assertFalse(failed);
64
65     System.err.println("Now testing TCObjectInputStream...");
66
67     ByteArrayInputStream JavaDoc bis = new ByteArrayInputStream JavaDoc(bao.toByteArray());
68
69     TCObjectInputStream is = new TCObjectInputStream(bis);
70     byte[] b = new byte[2];
71     Arrays.fill(b, (byte) 69); // these values will be overwritten
72
int read = is.read(b);
73     assertEquals(2, read);
74     assertTrue(Arrays.equals(new byte[] { -5, 5 }, b));
75     assertEquals(42, is.read());
76     assertEquals(true, is.readBoolean());
77     assertEquals(false, is.readBoolean());
78     assertEquals(11, is.readByte());
79     assertEquals('t', is.readChar());
80     assertEquals(Double.doubleToLongBits(3.14D), Double.doubleToLongBits(is.readDouble()));
81     assertEquals(Float.floatToIntBits(2.78F), Float.floatToIntBits(is.readFloat()));
82     assertEquals(12345678, is.readInt());
83     assertEquals(Long.MIN_VALUE, is.readLong());
84     assertEquals(Short.MAX_VALUE, is.readShort());
85     assertEquals("yo yo yo", is.readString());
86     assertEquals(null, is.readString());
87     assertEquals(createString(100000), is.readString());
88     assertEquals(l, readObjects(is, new ArrayList JavaDoc()));
89   }
90
91   private void writeObjects(TCObjectOutputStream os, ArrayList JavaDoc l) {
92     os.writeInt(l.size());
93     for (Iterator JavaDoc i = l.iterator(); i.hasNext();) {
94       Object JavaDoc element = i.next();
95       os.writeObject(element);
96     }
97   }
98
99   private List JavaDoc readObjects(TCObjectInputStream is, ArrayList JavaDoc l) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
100     int count = is.readInt();
101     for (int i = 0; i < count; i++) {
102       l.add(is.readObject());
103     }
104     return l;
105   }
106
107   private ArrayList JavaDoc getLiteralObjects() {
108     ArrayList JavaDoc l = new ArrayList JavaDoc();
109     l.add(new Integer JavaDoc(1009));
110     l.add("Hello there ");
111     l.add(new Long JavaDoc(909999999));
112     l.add(new Double JavaDoc(99999999.9374899999d));
113     l.add(new Float JavaDoc(9034699.9374899999f));
114     l.add(new Short JavaDoc((short) 0x45));
115     l.add(new Character JavaDoc('S'));
116     l.add(new ObjectID(38745488234l));
117     l.add(new Byte JavaDoc((byte) 77));
118     l.add(new Boolean JavaDoc(true));
119     l.add(this.getClass());
120     l.add(new UTF8ByteDataHolder("Hello back"));
121     l.add(new ClassInstance(new UTF8ByteDataHolder(this.getClass().getName()) , new UTF8ByteDataHolder("saroloader")));
122     // Object o[] = new Object[] { new ObjectID(88), new ObjectID(77), new Integer(66), new Long(55) };
123
// l.add(o);
124
addStackTraceElements(l);
125     l.add(new BigInteger JavaDoc(128, new Random JavaDoc()));
126     l.add(new BigDecimal JavaDoc(948754756.34365234d));
127     return l;
128   }
129
130   private void addStackTraceElements(List JavaDoc l) {
131     StackTraceElement JavaDoc[] ste;
132     try {
133       throw new Exception JavaDoc();
134     } catch (Exception JavaDoc e) {
135       ste = e.getStackTrace();
136     }
137     for (int i = 0; i < ste.length; i++) {
138       l.add(ste[i]);
139     }
140   }
141
142   private static String JavaDoc createString(int length) {
143     char[] chars = new char[length];
144     Arrays.fill(chars, 't');
145     return new String JavaDoc(chars);
146   }
147
148 }
149
Popular Tags