Debian Codegraph @

test/jpypetest/test_collection.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 from jpype.types import *
20 import common
21 import collections.abc
22
23
24 class CollectionCase(common.JPypeTestCase):
25
26 def setUp(self):
27 common.JPypeTestCase.setUp(self)
28
29 @common.requirePythonAfter((3, 6, 0))
30 def testCollectionABC(self):
31 JCollection = JClass('java.util.Collection')
32 self.assertFalse(issubclass(JCollection, collections.abc.Sequence))
33 self.assertFalse(issubclass(JCollection, collections.abc.Reversible))
34 self.assertTrue(issubclass(JCollection, collections.abc.Collection))
35 self.assertTrue(issubclass(JCollection, collections.abc.Iterable))
36 self.assertTrue(issubclass(JCollection, collections.abc.Sized))
37
38 def testCollectionDelItem(self):
39 ja = JClass('java.util.ArrayList')(['1', '2', '3'])
40 jc = JObject(ja, 'java.util.Collection')
41 with self.assertRaisesRegex(TypeError, 'remove'):
42 del jc[1]
43
44 def testListAddAll(self):
45 l = [1, 2, 3, 4]
46 l2 = ['a', 'b']
47 jlist = JClass("java.util.ArrayList")()
48 jlist.addAll(l)
49 jcollection = JObject(jlist, JClass("java.util.Collection"))
50 jcollection.addAll(l2)
51 l.extend(l2)
52 self.assertEqual(l, list(jcollection))
53
54
55 class CollectionIteratorCase(common.JPypeTestCase):
56
57 def setUp(self):
58 common.JPypeTestCase.setUp(self)
59
60 def testIterator(self):
61 al = JClass("java.util.ArrayList")()
62 itr = al.iterator()
63 self.assertEqual(itr, iter(itr))
64
65
66 class CollectionListCase(common.JPypeTestCase):
67
68 def setUp(self):
69 common.JPypeTestCase.setUp(self)
70
71 def testListAdd(self):
72 collection = JClass("java.util.ArrayList")()
73 collection.add(1)
74 collection.add(2)
75 self.assertEqual([1, 2], [i for i in collection])
76
77 def testListGet(self):
78 jlist = JClass("java.util.ArrayList")()
79 jlist.addAll([1, 2, 3, 4])
80 self.assertEqual(jlist[0], 1)
81 self.assertEqual(jlist[3], 4)
82 self.assertEqual(jlist[-1], 4)
83 self.assertEqual(jlist[-4], 1)
84
85 def testListSlice(self):
86 jlist = JClass("java.util.ArrayList")()
87 jlist.addAll([1, 2, 3, 4])
88 jlist[1:3] = [5, 6]
89 self.assertEqual(jlist[1], 5)
90 self.assertEqual(jlist[2], 6)
91
92 def testListDel(self):
93 jlist = JClass("java.util.ArrayList")()
94 jlist.addAll([1, 2, 3, 4])
95 del jlist[0]
96 self.assertEqual(len(jlist), 3)
97 self.assertEqual(jlist[0], 2)
98
99 def testListSetItemNeg(self):
100 l = [1, 2, 3, 4]
101 jlist = JClass("java.util.ArrayList")()
102 jlist.addAll([1, 2, 3, 4])
103 jlist[-1] = 5
104 l[-1] = 5
105 self.assertEqual(l, list(jlist))
106 jlist[-2] = 6
107 l[-2] = 6
108 self.assertEqual(l, list(jlist))
109 with self.assertRaises(IndexError):
110 jlist[-5] = 6
111
112 def testListIter(self):
113 ls = JClass('java.util.ArrayList')([0, 1, 2, 3])
114 for i, j in enumerate(ls):
115 self.assertEqual(i, j)
116
117 def testUnmodifiableNext(self):
118 ArrayList = JClass('java.util.ArrayList')
119 Collections = JClass('java.util.Collections')
120 a = ArrayList()
121 a.add("first")
122 a.add("second")
123 a.add("third")
124 for i in a:
125 pass
126 for i in Collections.unmodifiableList(a):
127 pass
128
129 @common.requirePythonAfter((3, 6, 0))
130 def testListABC(self):
131 l = ['a', 'b', 'c', 'b']
132 JList = JClass('java.util.ArrayList')
133 al = JList(l)
134 for i, j in zip(reversed(al), reversed(l)):
135 self.assertEqual(i, j)
136 self.assertEqual(object() in al, object() in l)
137 self.assertEqual('a' in al, 'a' in l)
138 self.assertEqual(al.index('b'), l.index('b'))
139 self.assertEqual(al.count('b'), l.count('b'))
140 with self.assertRaises(ValueError):
141 al.index(object())
142 self.assertEqual(al.count(object()), l.count(object()))
143 self.assertIsInstance(al, collections.abc.Sequence)
144 self.assertIsInstance(al, collections.abc.Reversible)
145 self.assertIsInstance(al, collections.abc.Collection)
146 self.assertIsInstance(al, collections.abc.Iterable)
147 self.assertIsInstance(al, collections.abc.Sized)
148
149
150 class CollectionMapCase(common.JPypeTestCase):
151
152 def setUp(self):
153 common.JPypeTestCase.setUp(self)
154
155 def testIterateMap(self):
156 collection = JClass('java.util.HashMap')()
157 collection.put('A', 1)
158 collection.put('B', 2)
159 asdict = dict()
160 for x in collection.entrySet():
161 asdict[str(x.getKey())] = x.getValue().longValue()
162 self.assertEqual(asdict, {'A': 1, 'B': 2})
163
164 def testMapPut(self):
165 jmap = JClass("java.util.HashMap")()
166 jmap["a"] = 1
167 self.assertEqual(jmap["a"], 1)
168
169 def testMapPutAll(self):
170 jmap = JClass("java.util.HashMap")()
171 dic = {"a": "1", "b": "2", "c": "3"}
172 jmap.putAll(dic)
173 self.assertEqual(jmap["a"], "1")
174 self.assertEqual(jmap["b"], "2")
175 self.assertEqual(jmap["c"], "3")
176 with self.assertRaises(TypeError):
177 jmap.putAll([1, 2, 3])
178
179 def testMapKeyError(self):
180 hm = JClass('java.util.HashMap')()
181 with self.assertRaises(KeyError):
182 hm['foo']
183 hm['foo'] = None
184 self.assertEqual(hm['foo'], None)
185
186 def testHashMapEntryIter(self):
187 hm = JClass('java.util.HashMap')()
188 hm['alice'] = 'alice'
189 hm['betty'] = 'betty'
190 hm['catty'] = 'catty'
191 for p, v in hm.entrySet():
192 self.assertEqual(p, v)
193
194 def testTreeMapEntryIter(self):
195 hm = JClass('java.util.TreeMap')()
196 hm['alice'] = 'alice'
197 hm['betty'] = 'betty'
198 hm['catty'] = 'catty'
199 for p, v in hm.entrySet():
200 self.assertEqual(p, v)
201
202 def testMapEntry(self):
203 hm = JClass('java.util.TreeMap')()
204 hm['alice'] = 'alice'
205 h = hm.entrySet()
206 self.assertEqual(len(h.iterator().next()), 2)
207
208 def testEnumMap(self):
209 enumclass = JClass('jpype.collection.TestEnum')
210 enummap = JClass('java.util.EnumMap')(enumclass)
211 enummap.put(enumclass.A, 'ABC')
212 enummap.put(enumclass.B, 'DEF')
213 asdict = dict()
214 for x in enummap.entrySet():
215 asdict[str(x.getKey())] = x.getValue()
216 self.assertEqual({'A': 'ABC', 'B': 'DEF'}, asdict)
217
218 def testSetDelItem(self):
219 hs = JClass('java.util.HashSet')()
220 hs.add('a')
221 hs.add('b')
222 hs.add('c')
223 self.assertIn('a', hs)
224 del hs['a']
225 self.assertNotIn('a', hs)
226
227 def testHashMapCtor(self):
228 HashMap = JClass('java.util.HashMap')
229 dc = dict()
230 dc['fred'] = 1
231 dc['george'] = 2
232 dc['paul'] = 3
233 hm = HashMap(dc)
234 for p, v in dc.items():
235 self.assertEqual(hm[p], v)
236
237 def testHashMapPutAll(self):
238 HashMap = JClass('java.util.HashMap')
239 hm = HashMap()
240 dc = dict()
241 dc['fred'] = 1
242 dc['george'] = 2
243 dc['paul'] = 3
244 hm.putAll(dc)
245 for p, v in dc.items():
246 self.assertEqual(hm[p], v)
247
248 def testHashMapConvert(self):
249 HashMap = JClass('java.util.HashMap')
250 hm = HashMap()
251 hm['fred'] = 1
252 hm['george'] = 2
253 hm['paul'] = 3
254 dc = dict(hm)
255 for p, v in hm.items():
256 self.assertEqual(dc[p], v)
257
258 def testMapABC(self):
259 from collections.abc import Mapping, Sized, Iterable, Container
260 hm = JClass('java.util.HashMap')()
261 self.assertIsInstance(hm, Sized)
262 self.assertIsInstance(hm, Iterable)
263 self.assertIsInstance(hm, Container)
264 self.assertIsInstance(hm, Mapping)
265
266 def testMapContains(self):
267 hm = JClass('java.util.HashMap')()
268 hm['fred'] = 1
269 hm['george'] = 2
270 hm['paul'] = 3
271 self.assertTrue("fred" in hm)
272 self.assertFalse("sally" in hm)
273
274 def testMapNoConversion(self):
275 hm = JClass("java.util.HashMap")()
276 self.assertFalse(object() in hm)
277 with self.assertRaises(KeyError):
278 hm[object()]
279
280
281 class CollectionEnumerationCase(common.JPypeTestCase):
282
283 def setUp(self):
284 common.JPypeTestCase.setUp(self)
285
286 def testEnumeration(self):
287 st = JClass('java.util.StringTokenizer')("this is a test")
288 out = []
289 for i in st:
290 out.append(str(i))
291 self.assertEqual(len(i), 4)
292 self.assertEqual(" ".join(out), "this is a test")