PageRenderTime 42ms CodeModel.GetById 35ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/interpreter/tags/reactive-pattern-matching/test/edu/vub/at/actors/natives/SerializationTest.java

http://ambienttalk.googlecode.com/
Java | 115 lines | 67 code | 11 blank | 37 comment | 3 complexity | 16765c96586d2a809f67858776bd8fe6 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.objects.ATAbstractGrammar;
 33import edu.vub.at.objects.ATObject;
 34import edu.vub.at.objects.ATTable;
 35import edu.vub.at.objects.ATTypeTag;
 36import edu.vub.at.objects.coercion.NativeTypeTags;
 37import edu.vub.at.objects.natives.NATObject;
 38import edu.vub.at.objects.natives.NATText;
 39import edu.vub.at.objects.natives.grammar.AGAssignField;
 40import edu.vub.at.objects.natives.grammar.AGSymbol;
 41import edu.vub.at.parser.NATParser;
 42
 43import java.io.ByteArrayInputStream;
 44import java.io.ByteArrayOutputStream;
 45import java.io.IOException;
 46import java.io.ObjectInputStream;
 47import java.io.ObjectOutputStream;
 48
 49/**
 50 * A test case for object serialization.
 51 *
 52 * @author tvcutsem
 53 */
 54public class SerializationTest extends AmbientTalkTest {
 55
 56	private byte[] serialize(Object o) throws IOException {
 57		ByteArrayOutputStream out = new ByteArrayOutputStream();
 58		ObjectOutputStream stream = new ObjectOutputStream(out);
 59		stream.writeObject(o);
 60		stream.flush();
 61		stream.close();
 62		return out.toByteArray();
 63	}
 64	
 65	private Object deserialize(byte[] b) throws IOException, ClassNotFoundException {
 66		ByteArrayInputStream in = new ByteArrayInputStream(b);
 67		ObjectInputStream instream = new ObjectInputStream(in);
 68		return instream.readObject();
 69	}
 70	
 71	private Object copy(Object o) throws IOException, ClassNotFoundException {
 72		return deserialize(serialize(o));
 73	}
 74	
 75	public void testTXTSerialization() throws IOException, ClassNotFoundException {
 76		NATText boeTXT = NATText.atValue("boe");
 77		Object cpy = copy(boeTXT);
 78		System.out.println(cpy.getClass());
 79		NATText boeTXT2 = (NATText) cpy; 
 80		assertEquals(boeTXT.toString(), boeTXT2.toString());
 81	}
 82	
 83	public void testAGSerialization() throws IOException, ClassNotFoundException {
 84		NATText boeTXT = NATText.atValue("boe");
 85		AGSymbol fooSYM = AGSymbol.jAlloc("foo");
 86		AGAssignField ass = new AGAssignField(boeTXT, fooSYM, boeTXT);
 87		AGAssignField ass2 = (AGAssignField) copy(ass);
 88		assertEquals(ass.toString(), ass2.toString());
 89		assertTrue(ass.base_fieldName() == ass2.base_fieldName());
 90	}
 91	
 92	public void testParseTreeSerialization() throws InterpreterException {
 93		ATAbstractGrammar ag = NATParser.parse("test", "{ |x,y| x + y }");
 94		Packet p = new Packet("test", ag);
 95		ATObject o = p.unpack();
 96		assertEquals(ag.toString(), o.toString());
 97		assertFalse(ag == o);
 98	}
 99	
100	/**
101	 * Tests whether a coercer is correctly serialized such that upon
102	 * deserialization it still holds that the deserialized value is an instance
103	 * of the given Java type.
104	 */
105	public void testCoercerSerialization() throws InterpreterException {
106		NATObject isolate = new NATObject(new ATTypeTag[] { NativeTypeTags._ISOLATE_, NativeTypeTags._TABLE_ });
107		ATTable coercer = isolate.asTable();
108		assertTrue(coercer instanceof ATTable);
109		Packet p = new Packet("test", coercer);
110		ATObject obj = p.unpack();
111		assertTrue(obj instanceof ATTable);
112		assertFalse(obj == isolate);
113	}
114	
115}