Debian Codegraph @

test/jpypetest/test_legacy.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 jpype
19 import common
20 import subrun
21 import unittest
22 import os
23
24
25 def proxy(s):
26 if not isinstance(s, str):
27 raise TypeError("Fail")
28 return s
29
30 # This is a test case to exercise all of the paths that pass through
31 # the string conversion to make sure all are exercised.
32 @subrun.TestCase
33 class LegacyTestCase(unittest.TestCase):
34 @classmethod
35 def setUpClass(cls):
36 # Run with automatic string conversion
37 jpype.startJVM(classpath=os.path.abspath(
38 "test/classes"), convertStrings=True)
39
40 def setUp(self):
41 self._test = jpype.JClass("jpype.str.Test")
42 self._intf = jpype.JClass("jpype.str.StringFunction")
43
44 def testStaticField(self):
45 s = self._test.staticField
46 self.assertEqual(s, "staticField")
47 self.assertIsInstance(s, str)
48
49 def testMemberField(self):
50 s = self._test().memberField
51 self.assertEqual(s, "memberField")
52 self.assertIsInstance(s, str)
53
54 def testStaticMethod(self):
55 s = self._test.staticCall()
56 self.assertEqual(s, "staticCall")
57 self.assertIsInstance(s, str)
58
59 def testMemberMethod(self):
60 s = self._test().memberCall()
61 self.assertEqual(s, "memberCall")
62 self.assertIsInstance(s, str)
63
64 def testArrayItem(self):
65 tc = ('apples', 'banana', 'cherries', 'dates', 'elderberry')
66 for i in range(0, 5):
67 s = self._test.array[i]
68 self.assertEqual(s, tc[i])
69 self.assertIsInstance(s, str)
70
71 def testArrayRange(self):
72 tc = ('apples', 'banana', 'cherries', 'dates', 'elderberry')
73 slc = self._test.array[1:-1]
74 self.assertEqual(tuple(slc), tc[1:-1])
75 self.assertIsInstance(slc[1], str)
76
77 def testProxy(self):
78 p = jpype.JProxy([self._intf], dict={'call': proxy})
79 r = self._test().callProxy(p, "roundtrip")
80 self.assertEqual(r, "roundtrip")
81 self.assertIsInstance(r, str)
82
83 def testNullBytes(self):
84 s = self._test.callWithNullBytes()
85 self.assertEqual(s, "call\u0000With\u0000Null\u0000Bytes")
86 self.assertIsInstance(s, str)
87
88 s = self._test.returnArgument("\u0394 16 byte encoded text\u0000with null\u0000 bytes \u1394")
89 self.assertEqual(s, "\u0394 16 byte encoded text\u0000with null\u0000 bytes \u1394")
90 self.assertIsInstance(s, str)
91
92 s = self._test.returnArgument("\U0001F468\u200D\U0001F9B2 32 byte encoded text"
93 "\u0000with null\u0000 bytes \U0001F468\u200D\U0001F9B2")
94 self.assertEqual(s, "\U0001F468\u200D\U0001F9B2 32 byte encoded text"
95 "\u0000with null\u0000 bytes \U0001F468\u200D\U0001F9B2")
96 self.assertIsInstance(s, str)