Index: tests/java/org/python/tests/SerializationTest.java =================================================================== --- tests/java/org/python/tests/SerializationTest.java (revision 0) +++ tests/java/org/python/tests/SerializationTest.java (revision 0) @@ -0,0 +1,76 @@ +package org.python.tests; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.ObjectOutputStream; + +import junit.framework.TestCase; + +import org.python.core.PyModule; +import org.python.core.PyObject; +import org.python.core.PyStringMap; +import org.python.core.PySystemState; +import org.python.core.imp; +import org.python.util.PythonInterpreter; +import org.python.util.PythonObjectInputStream; + +public class SerializationTest extends TestCase { + + private PythonInterpreter interp; + + @Override + protected void setUp() throws Exception { + interp = new PythonInterpreter(new PyStringMap(), new PySystemState()); + } + + public void setupMain() { + PyModule mod = imp.addModule("__main__"); + interp.setLocals(mod.__dict__); + } + + public void createObject() { + interp.exec("from java.io import Serializable"); + interp.exec("class Test(Serializable): pass"); + interp.exec("x = Test()"); + } + + public byte[] serialize() throws IOException { + PyObject x = interp.get("x"); + ByteArrayOutputStream os = new ByteArrayOutputStream(); + new ObjectOutputStream(os).writeObject(x); + return os.toByteArray(); + } + + public void deserialize(final byte[] b) throws IOException, ClassNotFoundException { + ByteArrayInputStream is = new ByteArrayInputStream(b); + new PythonObjectInputStream(is).readObject(); + } + + public void testSerialization() throws IOException, ClassNotFoundException { + deserialize(serialize()); + } + + public void testDirect() throws IOException, ClassNotFoundException { + createObject(); + testSerialization(); + } + + public void testJython() { + createObject(); + interp.set("t", this); + interp.exec("t.testSerialization()"); + } + + public void testDirectWithMain() throws IOException, ClassNotFoundException { + setupMain(); + createObject(); + testSerialization(); + } + + public void testJythonWithMain() { + setupMain(); + createObject(); + interp.set("t", this); + interp.exec("t.testSerialization()"); + } +}