Debian Codegraph @

test/jpypetest/test_pickle.py

raw symbols SCIP
1 # *****************************************************************************
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 #
15 # See NOTICE file for details.
16 #
17 # *****************************************************************************
18 import os
19 import pickle
20 import sys
21
22 import common
23 import jpype
24 from jpype import java
25 from jpype.pickle import JPickler, JUnpickler
26
27
28 def dump(fname):
29 with open(fname, "rb") as fd:
30 data = fd.read()
31 out = ["%02x" % i for i in data]
32 print("Pickle fail", " ".join(out), file=sys.stderr)
33
34
35 class PickleTestCase(common.JPypeTestCase):
36 def setUp(self):
37 super(PickleTestCase, self).setUp()
38 self.filename = "test.pic"
39
40 def tearDown(self):
41 try:
42 os.unlink(self.filename)
43 except OSError:
44 pass
45
46 def testString(self):
47 try:
48 s = java.lang.String("test")
49 with open(self.filename, "wb") as fd:
50 JPickler(fd).dump(s)
51 with open(self.filename, "rb") as fd:
52 s2 = JUnpickler(fd).load()
53 except pickle.UnpicklingError:
54 dump(self.filename)
55 self.assertEqual(s, s2)
56
57 def testString2(self):
58 try:
59 s1 = java.lang.String("test1")
60 s2 = java.lang.String("test2")
61 with open(self.filename, "wb") as fd:
62 pickler = JPickler(fd)
63 pickler.dump(s1)
64 pickler.dump(s2)
65 with open(self.filename, "rb") as fd:
66 unpickler = JUnpickler(fd)
67 s1_ = unpickler.load()
68 s2_ = unpickler.load()
69
70 self.assertEqual(s1, s1_)
71 self.assertEqual(s2, s2_)
72 except pickle.UnpicklingError:
73 dump(self.filename)
74
75 def testList(self):
76 s = java.util.ArrayList()
77 s.add("test")
78 s.add("this")
79 try:
80 with open(self.filename, "wb") as fd:
81 JPickler(fd).dump(s)
82 with open(self.filename, "rb") as fd:
83 s2 = JUnpickler(fd).load()
84 except pickle.UnpicklingError:
85 dump(self.filename)
86 self.assertEqual(s2.get(0), "test")
87 self.assertEqual(s2.get(1), "this")
88
89 def testMixed(self):
90 d = {"array": java.util.ArrayList(),
91 "string": java.lang.String("food")}
92 try:
93 with open(self.filename, "wb") as fd:
94 JPickler(fd).dump(d)
95 with open(self.filename, "rb") as fd:
96 d2 = JUnpickler(fd).load()
97 except pickle.UnpicklingError:
98 dump(self.filename)
99 self.assertEqual(d2['string'], "food")
100 self.assertIsInstance(d2['array'], java.util.ArrayList)
101
102 def testMultiObject(self):
103 """Regression test for https://github.com/jpype-project/jpype/issues/1201
104
105 Issue occurs when a python object contains multiple java objects above
106 a certain size. The issue occurs because of a buffer overflow in
107 ``native/java/org/jpype/pickle/ByteBufferInputStream.java``.
108 """
109 JString = jpype.JClass("java.lang.String")
110 composite_object = {"a": JString('A' * 512), "b": JString('B' * 512)}
111
112 with open(self.filename, "wb") as fd:
113 JPickler(fd).dump(composite_object)
114
115 with open(self.filename, "rb") as fd:
116 loaded_object = JUnpickler(fd).load()
117
118 assert loaded_object['a'] == str(composite_object['a'])
119 assert loaded_object['b'] == str(composite_object['b'])
120
121 def testByteBufferInputStream(self):
122 JByteBufferInputStream = jpype.JClass("org.jpype.pickle.ByteBufferInputStream")
123 stream = JByteBufferInputStream()
124 bb = jpype.JClass("java.nio.ByteBuffer").allocate(10)
125 stream.put(b"abc")
126 stream.put(b"def")
127
128 assert stream.read() == ord('a')
129 assert stream.read(bb.array()) == 5
130 assert bytes(bb.array()) == b'bcdef\x00\x00\x00\x00\x00'
131
132 def testFail(self):
133 s = java.lang.Object()
134 with self.assertRaises(java.io.NotSerializableException):
135 with open(self.filename, "wb") as fd:
136 JPickler(fd).dump(s)