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

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