File: test_java_subclasses.py

package info (click to toggle)
jython 2.5.3-16%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 43,772 kB
  • ctags: 106,434
  • sloc: python: 351,322; java: 216,349; xml: 1,584; sh: 330; perl: 114; ansic: 102; makefile: 45
file content (344 lines) | stat: -rw-r--r-- 11,730 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
'''Tests subclassing Java classes in Python'''
import os
import sys
import unittest

from test import test_support

from java.lang import (Boolean, Class, ClassLoader, Comparable,Integer, Object, Runnable, String,
        Thread, ThreadGroup)
from java.util import Date, Hashtable, Vector

from java.awt import Color, Component, Dimension, Rectangle
from javax.swing import ComboBoxModel, ListModel
from javax.swing.table import AbstractTableModel

from org.python.tests import BeanInterface, Callbacker, Coercions, OwnMethodCaller

class InterfaceTest(unittest.TestCase):
    def test_java_calling_python_interface_implementation(self):
        called = []
        class PyCallback(Callbacker.Callback):
            def call(self, extraarg=None):
                called.append(extraarg)
        Callbacker.callNoArg(PyCallback())
        Callbacker.callOneArg(PyCallback(), 4294967295L)
        self.assertEquals(None, called[0])
        self.assertEquals(4294967295L, called[1])
        class PyBadCallback(Callbacker.Callback):
            def call(pyself, extraarg):
                self.fail("Shouldn't be callable with a no args")
        self.assertRaises(TypeError, Callbacker.callNoArg, PyBadCallback())

    def test_inheriting_from_python_and_java_interface(self):
        calls = []
        class Runner(Runnable):
            def run(self):
                calls.append("Runner.run")

        class ComparableRunner(Comparable, Runner):
            def compareTo(self, other):
                calls.append("ComparableRunner.compareTo")
                return 0

        c = ComparableRunner()
        c.compareTo(None)
        c.run()
        self.assertEquals(calls, ["ComparableRunner.compareTo", "Runner.run"])

    def test_inherit_interface_twice(self):
        # http://bugs.jython.org/issue1504
        class A(ListModel): pass
        class B(A, ComboBoxModel): pass
        # Regression caused B's proxy to occur in B's mro twice. That
        # caused the declaration of C to fail with an inconsistent mro
        class C(B): pass
        

class TableModelTest(unittest.TestCase):
    def test_class_coercion(self):
        '''Python type instances coerce to a corresponding Java wrapper type in Object.getClass'''
        class TableModel(AbstractTableModel):
            columnNames = "First Name", "Last Name","Sport","# of Years","Vegetarian"
            data = [("Mary", "Campione", "Snowboarding", 5, False)]

            def getColumnCount(self):
                return len(self.columnNames)

            def getRowCount(self):
                return len(self.data)

            def getColumnName(self, col):
                return self.columnNames[col]

            def getValueAt(self, row, col):
                return self.data[row][col]

            def getColumnClass(self, c):
                return Object.getClass(self.getValueAt(0, c))

            def isCellEditable(self, row, col):
                return col >= 2

        model = TableModel()
        for i, expectedClass in enumerate([String, String, String, Integer, Boolean]):
            self.assertEquals(expectedClass, model.getColumnClass(i))

class AutoSuperTest(unittest.TestCase):
    def test_auto_super(self):
        class Implicit(Rectangle):
            def __init__(self):
                self.size = Dimension(6, 7)
        class Explicit(Rectangle):
            def __init__(self):
                Rectangle.__init__(self, 6, 7)
        self.assert_("width=6,height=7" in Implicit().toString())
        self.assert_("width=6,height=7" in Explicit().toString())

    def test_no_default_constructor(self):
        "Check autocreation when java superclass misses a default constructor."
        class A(ThreadGroup):
            def __init__(self):
                print self.name
        self.assertRaises(TypeError, A)

# The no-arg constructor for proxies attempts to look up its Python class by the Python class' name,
# so the class needs to be visible at the module level or the import will fail
class ModuleVisibleJavaSubclass(Object):
    pass
class PythonSubclassesTest(unittest.TestCase):
    def test_multiple_inheritance_prohibited(self):
        try:
            class MultiJava(Dimension, Color):
                pass
            self.fail("Shouldn't be able to subclass more than one concrete java class")
        except TypeError:
            pass

        class PyDim(Dimension):
            pass
        class PyDimRun(PyDim, Runnable):
            pass
        try:
            class PyDimRunCol(PyDimRun, Color):
                pass
            self.fail("Shouldn't be able to subclass more than one concrete java class")
        except TypeError:
            pass

    def test_multilevel_override(self):
        runs = []
        class SubDate(Date):
            def run(self):
                runs.append("SubDate")

            def method(self):
                return "SubDateMethod"

            def toString(self):
                s = Date.toString(self)
                return 'SubDate -> Date'

        class SubSubDate(SubDate, Runnable):
            def toString(self):
                return 'SubSubDate -> ' + SubDate.toString(self)

        self.assertEquals("SubDate -> Date", SubDate().toString())
        self.assertEquals("SubSubDate -> SubDate -> Date", SubSubDate().toString())
        self.assertEquals("SubDateMethod", SubSubDate().method())
        Coercions.runRunnable(SubSubDate())
        self.assertEquals(["SubDate"], runs)

    def test_passthrough(self):
        class CallbackPassthrough(Callbacker.Callback):
            def __init__(self, worker):
                self.worker = worker

            def __getattribute__(self, name):
                if name == 'call':
                    return getattr(self.worker, name)
                return object.__getattribute__(self, name)

        collector = Callbacker.CollectingCallback()
        c = CallbackPassthrough(collector)
        Callbacker.callNoArg(c)
        self.assertEquals("call()", collector.calls[0])
        c.call(7)
        self.assertEquals("call(7)", collector.calls[1])

    def test_Class_newInstance_works_on_proxies(self):
        Class.newInstance(ModuleVisibleJavaSubclass)

    def test_override(self):
        class Foo(Runnable):
            def toString(self): return "Foo"
        self.assertEquals(String.valueOf(Foo()), "Foo", "toString not overridden in interface")

        class A(Object):
            def toString(self):
                return 'name'
        self.assertEquals('name', String.valueOf(A()), 'toString not overriden in subclass')

    def test_can_subclass_abstract(self):
        class A(Component):
            pass
        A()

    def test_return_proxy(self):
        "Jython proxies properly return back from Java code"
        class FooVector(Vector):
            bar = 99

        ht = Hashtable()
        fv = FooVector()
        ht.put("a", fv)
        self.failUnless(fv is ht.get("a"))

    def test_proxy_generates_protected_methods(self):
        """Jython proxies should generate methods for protected methods on their superclasses

        Tests for bug #416871"""
        output = []
        class RegularBean(BeanInterface):
            def __init__(self):
                output.append("init")

            def getName(self):
                output.append("getName")
        class FinalizingBean(RegularBean):
            def finalize(self):
                pass
            def clone(self):
                return self.__class__()

        for a in FinalizingBean(), RegularBean():
            self.assertEquals("init", output.pop())
            a.getName()
            self.assertEquals("getName", output.pop())
            aa = a.clone()
            if isinstance(a, FinalizingBean):
                self.assertEquals("init", output.pop())
            aa.name
            self.assertEquals("getName", output.pop())

    def test_python_subclass_of_python_subclass_of_java_class_overriding(self):
        '''Test for http://bugs.jython.org/issue1297.

        Checks that getValue on SecondSubclass is overriden correctly when called from Java.'''
        class FirstSubclass(OwnMethodCaller):
            pass

        class SecondSubclass(FirstSubclass):
            def getValue(self):
                return 10

        self.assertEquals(10, SecondSubclass().callGetValue())


    def test_deep_subclasses(self):
        '''Checks for http://bugs.jython.org/issue1363.

        Inheriting several classes deep from a Java class caused inconsistent MROs.'''
        class A(Object): pass
        class B(A): pass
        class C(B): pass
        class D(C): pass
        d = D()

"""
public abstract class Abstract {
    public Abstract() {
        method();
    }

    public abstract void method();
}
"""
# The following is the correspoding bytecode for Abstract compiled with javac 1.5
ABSTRACT_CLASS = """\
eJw1TrsKwkAQnI1nEmMe/oKdSaHYiyCClWih2F+SQyOaQDz9LxsFCz/AjxL3Am6xw8zs7O7n+3oD
GKPnQcD30ELgIHQQEexJURZ6SmgN4h1BzKtcEaJlUarV9ZyqeivTEyv2WelDlRO8TXWtM7UojBrM
0ouuZaaHR3mTPtqwfXRgE9y/Q+gZb3SS5X60To8q06LPHwiYskAmxN1hFjMSYyd5gpIHrDsT3sU9
5IgZF4wuhCBzpnG9Ru/+AF4RJn8=
""".decode('base64').decode('zlib')

class AbstractOnSyspathTest(unittest.TestCase):
    '''Subclasses an abstract class that isn't on the startup classpath.

    Checks for http://jython.org/bugs/1861985
    '''
    def setUp(self):
        out = open('Abstract.class', 'wb')
        out.write(ABSTRACT_CLASS)
        out.close()
        self.orig_syspath = sys.path[:]
        sys.path.append('')

    def tearDown(self):
        os.unlink('Abstract.class')
        sys.path = self.orig_syspath

    def test_can_subclass_abstract(self):
        import Abstract

        class A(Abstract):
            def method(self):
                pass
        A()

"""
public abstract class ContextAbstract {
    public ContextAbstract() {
        method();
    }

    public abstract void method();
}
"""
# The following is the correspoding bytecode for ContextAbstract compiled with javac 1.5
# Needs to be named differently than Abstract above so the class loader won't just use it
CONTEXT_ABSTRACT = """\
eJxdTsEOwVAQnK2n1aq2Bz/ghgNxF4lInIQDcX9tX6jQJvWI33IhcfABPkrs69EedjKzM7v7+b7e
AEaIPAj4HmpoOQgchAR7nOWZnhBq3d6WIGZFqgjhIsvV8nKKVbmR8ZEV+6T0vkgJ3rq4lImaZ0Zt
z4pcq5uexmddykQPDvIqfdRh+3Bh86I/AyEyluFR5rvhKj6oRIsO/yNgygKZLHeHWY+RGN3+E9R/
wLozITS4BxwxdsHYgBBkrlVTr9KbP6qaLFc=
""".decode('base64').decode('zlib')
class ContextClassloaderTest(unittest.TestCase):
    '''Classes on the context classloader should be importable and subclassable.

    http://bugs.jython.org/issue1216'''
    def setUp(self):
        self.orig_context = Thread.currentThread().contextClassLoader
        class AbstractLoader(ClassLoader):
            def __init__(self):
                ClassLoader.__init__(self)
                c = self.super__defineClass("ContextAbstract", CONTEXT_ABSTRACT, 0,
                        len(CONTEXT_ABSTRACT), ClassLoader.protectionDomain)
                self.super__resolveClass(c)
        Thread.currentThread().contextClassLoader = AbstractLoader()

    def tearDown(self):
        Thread.currentThread().contextClassLoader = self.orig_context

    def test_can_subclass_abstract(self):
        import ContextAbstract

        called = []
        class A(ContextAbstract):
            def method(self):
                called.append(True)
        A()
        self.assertEquals(len(called), 1)


def test_main():
    test_support.run_unittest(InterfaceTest,
            TableModelTest,
            AutoSuperTest,
            PythonSubclassesTest,
            AbstractOnSyspathTest,
            ContextClassloaderTest)


if __name__ == '__main__':
    test_main()