PageRenderTime 35ms CodeModel.GetById 9ms app.highlight 20ms RepoModel.GetById 1ms app.codeStats 0ms

/interpreter/tags/at_build150307/test/edu/vub/at/actors/natives/SerializationTest.java

http://ambienttalk.googlecode.com/
Java | 118 lines | 69 code | 11 blank | 38 comment | 3 complexity | e9b0858734ef0f31f9d90fe4a8260715 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.ATStripe;
 35import edu.vub.at.objects.ATTable;
 36import edu.vub.at.objects.coercion.NativeStripes;
 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.objects.symbiosis.SymbioticATObjectMarker;
 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_getFieldName() == ass2.base_getFieldName());
 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 correctly implements writeReplace to return
103	 * the principal to serialize, instead of the coercer itself. Hence,
104	 * upon deserialization, we expect an object that is no longer wrapped
105	 * by a coercer.
106	 */
107	public void testCoercerSerialization() throws InterpreterException {
108		NATObject isolate = new NATObject(new ATStripe[] { NativeStripes._ISOLATE_, NativeStripes._TABLE_ });
109		ATTable coercer = isolate.asTable();
110		assertTrue(coercer instanceof SymbioticATObjectMarker);
111		Packet p = new Packet("test", coercer);
112		ATObject obj = p.unpack();
113		assertEquals(NATObject.class, obj.getClass());
114		assertFalse(obj instanceof SymbioticATObjectMarker);
115		assertFalse(obj == isolate);
116	}
117	
118}