1 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 ; 13 import java.io.ByteArrayOutputStream ; 14 import java.io.IOException ; 15 import java.math.BigDecimal ; 16 import java.math.BigInteger ; 17 import java.util.ArrayList ; 18 import java.util.Arrays ; 19 import java.util.Iterator ; 20 import java.util.List ; 21 import java.util.Random ; 22 23 public class TCObjectInputOutputStreamTest extends TCTestCase { 24 25 public void testBasic() throws IOException , ClassNotFoundException { 26 ByteArrayOutputStream bao = new ByteArrayOutputStream (1024); 27 28 TCObjectOutputStream os = new TCObjectOutputStream(bao); 29 30 ArrayList 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 boolean failed = false; 50 try { 51 os.writeObject(new Object ()); 52 failed = true; 53 } catch (Exception ex) { 54 } 56 Assert.assertFalse(failed); 57 try { 58 os.writeObject(l); 59 failed = true; 60 } catch (Exception ex) { 61 } 63 Assert.assertFalse(failed); 64 65 System.err.println("Now testing TCObjectInputStream..."); 66 67 ByteArrayInputStream bis = new ByteArrayInputStream (bao.toByteArray()); 68 69 TCObjectInputStream is = new TCObjectInputStream(bis); 70 byte[] b = new byte[2]; 71 Arrays.fill(b, (byte) 69); 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 ())); 89 } 90 91 private void writeObjects(TCObjectOutputStream os, ArrayList l) { 92 os.writeInt(l.size()); 93 for (Iterator i = l.iterator(); i.hasNext();) { 94 Object element = i.next(); 95 os.writeObject(element); 96 } 97 } 98 99 private List readObjects(TCObjectInputStream is, ArrayList l) throws IOException , ClassNotFoundException { 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 getLiteralObjects() { 108 ArrayList l = new ArrayList (); 109 l.add(new Integer (1009)); 110 l.add("Hello there "); 111 l.add(new Long (909999999)); 112 l.add(new Double (99999999.9374899999d)); 113 l.add(new Float (9034699.9374899999f)); 114 l.add(new Short ((short) 0x45)); 115 l.add(new Character ('S')); 116 l.add(new ObjectID(38745488234l)); 117 l.add(new Byte ((byte) 77)); 118 l.add(new Boolean (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 addStackTraceElements(l); 125 l.add(new BigInteger (128, new Random ())); 126 l.add(new BigDecimal (948754756.34365234d)); 127 return l; 128 } 129 130 private void addStackTraceElements(List l) { 131 StackTraceElement [] ste; 132 try { 133 throw new Exception (); 134 } catch (Exception 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 createString(int length) { 143 char[] chars = new char[length]; 144 Arrays.fill(chars, 't'); 145 return new String (chars); 146 } 147 148 } 149 | Popular Tags |