/interpreter/tags/at2dist110511/test/edu/vub/at/actors/natives/SerializationTest.java
Java | 124 lines | 74 code | 12 blank | 38 comment | 3 complexity | b78bbc8c6272536a3a0456a9ef8317e6 MD5 | raw file
1/** 2 * AmbientTalk/2 Project 3 * SerializationTest.java created on 28-dec-2006 at 19:48:13 4 * (c) Programming Technology Lab, 2006 - 2007 5 * Authors: Tom Van Cutsem & Stijn Mostinckx 6 * 7 * Permission is hereby granted, free of charge, to any person 8 * obtaining a copy of this software and associated documentation 9 * files (the "Software"), to deal in the Software without 10 * restriction, including without limitation the rights to use, 11 * copy, modify, merge, publish, distribute, sublicense, and/or 12 * sell copies of the Software, and to permit persons to whom the 13 * Software is furnished to do so, subject to the following 14 * conditions: 15 * 16 * The above copyright notice and this permission notice shall be 17 * included in all copies or substantial portions of the Software. 18 * 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 21 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 22 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 23 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 24 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 25 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 26 * OTHER DEALINGS IN THE SOFTWARE. 27 */ 28package edu.vub.at.actors.natives; 29 30import edu.vub.at.AmbientTalkTest; 31import edu.vub.at.exceptions.InterpreterException; 32import edu.vub.at.exceptions.XIOProblem; 33import edu.vub.at.objects.ATAbstractGrammar; 34import edu.vub.at.objects.ATObject; 35import edu.vub.at.objects.ATTable; 36import edu.vub.at.objects.ATTypeTag; 37import edu.vub.at.objects.coercion.NativeTypeTags; 38import edu.vub.at.objects.natives.NATObject; 39import edu.vub.at.objects.natives.NATText; 40import edu.vub.at.objects.natives.grammar.AGAssignField; 41import edu.vub.at.objects.natives.grammar.AGSymbol; 42import edu.vub.at.parser.NATParser; 43 44import java.io.ByteArrayInputStream; 45import java.io.ByteArrayOutputStream; 46import java.io.IOException; 47import java.io.ObjectInputStream; 48import java.io.ObjectOutputStream; 49 50/** 51 * A test case for object serialization. 52 * 53 * @author tvcutsem 54 */ 55public class SerializationTest extends AmbientTalkTest { 56 57 private byte[] serialize(Object o) throws IOException { 58 ByteArrayOutputStream out = new ByteArrayOutputStream(); 59 ObjectOutputStream stream = new ObjectOutputStream(out); 60 stream.writeObject(o); 61 stream.flush(); 62 stream.close(); 63 return out.toByteArray(); 64 } 65 66 private Object deserialize(byte[] b) throws IOException, ClassNotFoundException { 67 ByteArrayInputStream in = new ByteArrayInputStream(b); 68 ObjectInputStream instream = new ObjectInputStream(in); 69 return instream.readObject(); 70 } 71 72 private Object copy(Object o) throws IOException, ClassNotFoundException { 73 return deserialize(serialize(o)); 74 } 75 76 public void testTXTSerialization() throws IOException, ClassNotFoundException { 77 NATText boeTXT = NATText.atValue("boe"); 78 Object cpy = copy(boeTXT); 79 System.out.println(cpy.getClass()); 80 NATText boeTXT2 = (NATText) cpy; 81 assertEquals(boeTXT.toString(), boeTXT2.toString()); 82 } 83 84 public void testAGSerialization() throws IOException, ClassNotFoundException { 85 NATText boeTXT = NATText.atValue("boe"); 86 AGSymbol fooSYM = AGSymbol.jAlloc("foo"); 87 AGAssignField ass = new AGAssignField(boeTXT, fooSYM, boeTXT); 88 AGAssignField ass2 = (AGAssignField) copy(ass); 89 assertEquals(ass.toString(), ass2.toString()); 90 assertTrue(ass.base_fieldName() == ass2.base_fieldName()); 91 } 92 93 public void testParseTreeSerialization() throws InterpreterException { 94 ATAbstractGrammar ag = NATParser.parse("test", "{ |x,y| x + y }"); 95 Packet p = new Packet("test", ag); 96 ATObject o = p.unpack(); 97 assertEquals(ag.toString(), o.toString()); 98 assertFalse(ag == o); 99 } 100 101 /** 102 * Tests whether a coercer is correctly serialized such that upon 103 * deserialization it still holds that the deserialized value is an instance 104 * of the given Java type. 105 */ 106 public void testCoercerSerialization() throws InterpreterException { 107 NATObject isolate = new NATObject(new ATTypeTag[] { NativeTypeTags._ISOLATE_, NativeTypeTags._TABLE_ }); 108 ATTable coercer = isolate.asTable(); 109 assertTrue(coercer instanceof ATTable); 110 Packet p = new Packet("test", coercer); 111 ATObject obj = p.unpack(); 112 assertTrue(obj instanceof ATTable); 113 assertFalse(obj == isolate); 114 } 115 116 /** Apparently, the payload generated by different packets is different even for the same object */ 117 public void testEquality() throws InterpreterException { 118 ATObject obj = new NATObject(); 119 Packet p1 = new Packet(obj); 120 Packet p2 = new Packet(obj); 121 assertFalse(p1.equals(p2)); 122 } 123 124}