Debian Codegraph @

test/jpypetest/test_utf8.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 # *****************************************************************************
3 # Copyright 2018 Rene Bakker
4 # *****************************************************************************
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18 # See NOTICE file for details.
19 #
20 # *****************************************************************************
21
22 '''
23 Test communication with jpype using 4-byte utf-8 characters (emoji)
24
25 IMPORTANT:
26 The routines have only be tested in Python3. Given the difference in string handling between
27 Python2 and Python3, it is not likely this routines have any significance for Python2.
28
29 The following tests are defined:
30 General:
31 1. Test if the java class (jpype.utf8.Utf8Test) can return the default ASCII string.
32 2. Pass an ASCII string to the java test class and check if it remained unchanged when returned from java.
33
34 Binary: python strings are injected into java.lang.String in binary format with str.encode()
35 3. Pass a series of reference UTF-8 strings and compare them with the reference strings
36 in the java class.
37 4. Pass a series of reference UTF-8 strings and check if they remained unchanged when returned from Java.
38 Allow for surrogate substitution in the utf-16 strings returned from Java.
39 5. Pass a series of reference UTF-8 strings and check if they remained unchanged when returned from Java.
40 Use the python default strict encoding rules for the returned string.
41
42 Navive strings: python strings are passed as-is into a java method, wich accepts String as argument
43 6. Pass a series of reference UTF-8 strings and compare them with the reference strings
44 in the java class.
45 7. Pass a series of reference UTF-8 strings and check if they remained unchanged when returned from Java.
46 Allow for surrogate substitution in the utf-16 strings returned from Java.
47 8. Pass a series of reference UTF-8 strings and check if they remained unchanged when returned from Java.
48 Use the python default strict encoding rules for the returned string.
49
50 At the time if writing:
51 Passed tests: 1, 2, 3, and 4
52 Failed tests:
53 5. encoding error returned string
54 6. uploaded string mutilated for emoji
55 7. follow-up of 6: mutilated string returned to python (emoji only=
56 8. idem 7.
57
58 Note on encoding errors:
59 UnicodeEncodeError: 'utf-8' codec can't encode characters in position xxx-xxx: surrogates not allowed
60 '''
61
62 import sys
63 from jpype.types import *
64 import common
65
66
67 class Utf8TestCase(common.JPypeTestCase):
68
69 def setUp(self):
70 common.JPypeTestCase.setUp(self)
71
72 # Java IO test class
73 self.Utf8Test = JClass('jpype.utf8.Utf8Test')
74
75 # Test strings
76 # IMPORTANT: they should be identical, and in the same order, as the test strings difned in the
77 # java class Utf8Test
78 self.TDICT = []
79 self.TDICT.append(['english',
80 ("I can eat glass and it doesn't hurt me.")])
81 self.TDICT.append(['french',
82 ("Je peux manger du verre, ça ne me fait pas mal.")])
83 self.TDICT.append(['rune',
84 ("ᛖᚴ ᚷᛖᛏ ᛖᛏᛁ ᚧ ᚷᛚᛖᚱ ᛘᚾ ᚦᛖᛋᛋ ᚨᚧ ᚡᛖ ᚱᚧᚨ ᛋᚨᚱ")])
85 self.TDICT.append(['cn_simp',
86 ("人人生而自由,在尊严和权利上一律平等。他们赋有理性和良心,并应以兄弟关系的精神互相对待。")])
87 self.TDICT.append(['cn_trad',
88 ("人人生而自由﹐在尊嚴和權利上一律平等。他們賦有理性和良心﹐並應以兄弟關係的精神互相對待。")])
89 self.TDICT.append(['arab',
90 ("أنا قادر على أكل الزجاج و هذا لا يؤلمني.")])
91 self.TDICT.append(['emoji',
92 ("😁😂😃😄😅😆😠😡😢😣😤😥😨😩😪🚉🚌🚏🚑🚒🚓🚕🚗🚙🚚🚢🚤🚥🚧🚨🚻🚼🚽🚾🛀🆕🆖🆗🆘🆙🆚🈁🈂🈚🈯🈹🈺🉐🉑8⃣9⃣7⃣6⃣1⃣0")])
93
94 def test_get_ascii(self):
95 """
96 Test if the default string returns from the java test class.
97 """
98 utf8_test = self.Utf8Test()
99 self.assertEqual("Utf8Test pure ASCII", utf8_test.get(),
100 "Utf8Test.java default string")
101
102 def test_ascii_upload(self):
103 """
104 Test uploading and downloading of a simple ASCII string.
105 """
106 test_string = 'Python Utf8Test ascii test string'
107 utf8_test = self.Utf8Test(test_string)
108 self.assertEqual(test_string, utf8_test.get(),
109 "Utf8Test.java uploaded ASCII string")
110
111 def test_binary_upload(self):
112 """
113 Test binary upload and check in Java if the strings are correct.
114 Assumes synchronized test strings in the java class and in this test class.
115 """
116 String = JClass('java.lang.String')
117 indx = 0
118 for lbl, val in self.TDICT:
119 utf8_test = self.Utf8Test(String(val.encode('utf-8'), 'UTF8'))
120 self.assertTrue(utf8_test.equalsTo(indx), "Utf8Test.java binary upload %d (%s) = %s" %
121 (indx, lbl, val))
122 indx += 1
123
124 def test_binary_upload_with_surrogates(self):
125 """
126 Test binary upload and download of utf strings.
127 Allow for surrogate unicode substitution of the return value.
128 """
129 String = JClass('java.lang.String')
130 for lbl, val in self.TDICT:
131 utf8_test = self.Utf8Test(String(val.encode('utf-8'), 'UTF8'))
132 try:
133 rval = str(utf8_test.get()).encode(
134 'utf-16').decode('utf-16')
135 except UnicodeEncodeError as uue:
136 rval = str(utf8_test.get()).encode(
137 'utf-16', errors='surrogatepass').decode('utf-16')
138 lbl += (' ' + str(uue))
139 self.assertEqual(
140 val, rval, "Utf8Test.java binary upload with surrogate substitution for: " + lbl)
141
142 def test_binary_upload_no_surrogates(self):
143 """
144 Test pure binary upload and download of utf strings.
145 """
146 String = JClass('java.lang.String')
147 for lbl, val in self.TDICT:
148 utf8_test = self.Utf8Test(String(val.encode('utf-8'), 'UTF8'))
149 self.assertEqual(val, str(utf8_test.get()),
150 "Utf8Test.java binary upload for: " + lbl)
151
152 def test_string_upload(self):
153 """
154 Test binary upload and check in Java if the strings are correct.
155 Assumes synchronized test strings in the java class and in this test class.
156 """
157 indx = 0
158 for lbl, val in self.TDICT:
159 utf8_test = self.Utf8Test(val)
160 self.assertTrue(utf8_test.equalsTo(
161 indx), "Utf8Test.java binary upload: indx %d = %s" % (indx, lbl))
162 indx += 1
163
164 def test_string_upload_with_surrogates(self):
165 """
166 Test python string upload and download of utf strings.
167 Allow for surrogate unicode substitution of the return value.
168 """
169 for lbl, val in self.TDICT:
170 utf8_test = self.Utf8Test(val)
171 try:
172 rval = str(utf8_test.get()).encode(
173 'utf-16').decode('utf-16')
174 except UnicodeEncodeError as uue:
175 rval = str(utf8_test.get()).encode(
176 'utf-16', errors='surrogatepass').decode('utf-16')
177 lbl += (' ' + str(uue))
178 self.assertEqual(
179 val, rval, "Utf8Test.java string upload with surrogate substitution for: " + lbl)
180
181 def test_string_upload_no_surrogates(self):
182 """
183 Test pure python string upload and download of utf strings.
184 """
185 for lbl, val in self.TDICT:
186 utf8_test = self.Utf8Test(val)
187 res = str(utf8_test.get())
188 self.assertEqual(
189 val, res, "Utf8Test.java string upload for: " + lbl)