001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.io.serializer;
005:
006: import com.tc.object.ObjectID;
007: import com.tc.object.dna.impl.ClassInstance;
008: import com.tc.object.dna.impl.UTF8ByteDataHolder;
009: import com.tc.test.TCTestCase;
010: import com.tc.util.Assert;
011:
012: import java.io.ByteArrayInputStream;
013: import java.io.ByteArrayOutputStream;
014: import java.io.IOException;
015: import java.math.BigDecimal;
016: import java.math.BigInteger;
017: import java.util.ArrayList;
018: import java.util.Arrays;
019: import java.util.Iterator;
020: import java.util.List;
021: import java.util.Random;
022:
023: public class TCObjectInputOutputStreamTest extends TCTestCase {
024:
025: public void testBasic() throws IOException, ClassNotFoundException {
026: ByteArrayOutputStream bao = new ByteArrayOutputStream(1024);
027:
028: TCObjectOutputStream os = new TCObjectOutputStream(bao);
029:
030: ArrayList l = getLiteralObjects();
031:
032: os.write(new byte[] { -5, 5 });
033: os.write(42);
034: os.writeBoolean(true);
035: os.writeBoolean(false);
036: os.writeByte(11);
037: os.writeChar('t');
038: os.writeDouble(3.14D);
039: os.writeFloat(2.78F);
040: os.writeInt(12345678);
041: os.writeLong(Long.MIN_VALUE);
042: os.writeShort(Short.MAX_VALUE);
043: os.writeString("yo yo yo");
044: os.writeString(null);
045: os.writeString(createString(100000));
046: writeObjects(os, l);
047:
048: // Now try to write a non Literal Object
049: boolean failed = false;
050: try {
051: os.writeObject(new Object());
052: failed = true;
053: } catch (Exception ex) {
054: // this is normal
055: }
056: Assert.assertFalse(failed);
057: try {
058: os.writeObject(l);
059: failed = true;
060: } catch (Exception ex) {
061: // this is normal
062: }
063: Assert.assertFalse(failed);
064:
065: System.err.println("Now testing TCObjectInputStream...");
066:
067: ByteArrayInputStream bis = new ByteArrayInputStream(bao
068: .toByteArray());
069:
070: TCObjectInputStream is = new TCObjectInputStream(bis);
071: byte[] b = new byte[2];
072: Arrays.fill(b, (byte) 69); // these values will be overwritten
073: int read = is.read(b);
074: assertEquals(2, read);
075: assertTrue(Arrays.equals(new byte[] { -5, 5 }, b));
076: assertEquals(42, is.read());
077: assertEquals(true, is.readBoolean());
078: assertEquals(false, is.readBoolean());
079: assertEquals(11, is.readByte());
080: assertEquals('t', is.readChar());
081: assertEquals(Double.doubleToLongBits(3.14D), Double
082: .doubleToLongBits(is.readDouble()));
083: assertEquals(Float.floatToIntBits(2.78F), Float
084: .floatToIntBits(is.readFloat()));
085: assertEquals(12345678, is.readInt());
086: assertEquals(Long.MIN_VALUE, is.readLong());
087: assertEquals(Short.MAX_VALUE, is.readShort());
088: assertEquals("yo yo yo", is.readString());
089: assertEquals(null, is.readString());
090: assertEquals(createString(100000), is.readString());
091: assertEquals(l, readObjects(is, new ArrayList()));
092: }
093:
094: private void writeObjects(TCObjectOutputStream os, ArrayList l) {
095: os.writeInt(l.size());
096: for (Iterator i = l.iterator(); i.hasNext();) {
097: Object element = i.next();
098: os.writeObject(element);
099: }
100: }
101:
102: private List readObjects(TCObjectInputStream is, ArrayList l)
103: throws IOException, ClassNotFoundException {
104: int count = is.readInt();
105: for (int i = 0; i < count; i++) {
106: l.add(is.readObject());
107: }
108: return l;
109: }
110:
111: private ArrayList getLiteralObjects() {
112: ArrayList l = new ArrayList();
113: l.add(new Integer(1009));
114: l.add("Hello there ");
115: l.add(new Long(909999999));
116: l.add(new Double(99999999.9374899999d));
117: l.add(new Float(9034699.9374899999f));
118: l.add(new Short((short) 0x45));
119: l.add(new Character('S'));
120: l.add(new ObjectID(38745488234l));
121: l.add(new Byte((byte) 77));
122: l.add(new Boolean(true));
123: l.add(this .getClass());
124: l.add(new UTF8ByteDataHolder("Hello back"));
125: l.add(new ClassInstance(new UTF8ByteDataHolder(this .getClass()
126: .getName()), new UTF8ByteDataHolder("saroloader")));
127: // Object o[] = new Object[] { new ObjectID(88), new ObjectID(77), new Integer(66), new Long(55) };
128: // l.add(o);
129: addStackTraceElements(l);
130: l.add(new BigInteger(128, new Random()));
131: l.add(new BigDecimal(948754756.34365234d));
132: return l;
133: }
134:
135: private void addStackTraceElements(List l) {
136: StackTraceElement[] ste;
137: try {
138: throw new Exception();
139: } catch (Exception e) {
140: ste = e.getStackTrace();
141: }
142: for (int i = 0; i < ste.length; i++) {
143: l.add(ste[i]);
144: }
145: }
146:
147: private static String createString(int length) {
148: char[] chars = new char[length];
149: Arrays.fill(chars, 't');
150: return new String(chars);
151: }
152:
153: }
|