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