Charlie Groves wrote:
>
> What would be hugely helpful and could keep this from breaking in the
> future is adding some test cases for serializing Python subclasses of
> Java classes. If you could reduce your code above and whatever else
> you'd like to have work from your application to a test case we can
> run from regrtest and add a patch to bugs.jython.org, I'll apply it as
> part of my Java integration rework for 2.5 and make sure it passes for
> the next beta.
>
Well, the code I posted was minimal, so I'd say creating a test case is
expanding, not reducing, the code.
Anyway, I turned it into a basic set of junit tests:
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import org.junit.Test;
import org.python.core.PyModule;
import org.python.core.PyObject;
import org.python.core.imp;
import org.python.util.PythonInterpreter;
import org.python.util.PythonObjectInputStream;
public class SerializationTests {
private PythonInterpreter interp;
public void init() {
interp = new PythonInterpreter();
}
public void setupMain() {
final PyModule mod = imp.addModule("__main__");
interp.setLocals(mod.__dict__);
}
public void createObject() {
interp.exec("from java.lang import Object");
interp.exec("from java.io import Serializable");
interp.exec("class Test(Serializable):\n\tdef
__init__(self):\n\t\tObject.__init__(self)\n");
interp.exec("x = Test()");
}
public byte[] serialize() throws IOException {
final PyObject x = interp.get("x");
final ByteArrayOutputStream os = new ByteArrayOutputStream();
new ObjectOutputStream(os).writeObject(x);
return os.toByteArray();
}
public void deserialize(final byte[] b) throws IOException,
ClassNotFoundException {
final ByteArrayInputStream is = new ByteArrayInputStream(b);
new PythonObjectInputStream(is).readObject();
}
public void testSerialization() throws IOException, ClassNotFoundException
{
deserialize(serialize());
}
@Test
public void testDirect() throws IOException, ClassNotFoundException {
init();
createObject();
testSerialization();
}
@Test
public void testJython() {
init();
createObject();
interp.set("t", this);
interp.exec("t.testSerialization()");
}
@Test
public void testDirectWithMain() throws IOException, ClassNotFoundException
{
init();
setupMain();
createObject();
testSerialization();
}
@Test
public void testJythonWithMain() {
init();
setupMain();
createObject();
interp.set("t", this);
interp.exec("t.testSerialization()");
}
}
I couldn't figure out how to completely reset jython's state, so the test
results may be affected by the order in which you run them. But anyway, they
all fail (with errors), except the last one (testJythonWithMain).
The MOST important test, which really really must work reliably, is
testDirect. Currently it throws a NPE.
Adrian
--
View this message in context: http://www.nabble.com/Serialization-tp20719389p20733853.html
Sent from the jython-users mailing list archive at Nabble.com.
|