File: test_java_integration.py

package info (click to toggle)
jython 2.7.3%2Brepack1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 62,820 kB
  • sloc: python: 641,384; java: 306,981; xml: 2,066; sh: 514; ansic: 126; makefile: 77
file content (987 lines) | stat: -rw-r--r-- 35,075 bytes parent folder | download
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
import copy
import glob
import importlib
import operator
import os
import os.path
import unittest
import shutil
import subprocess
import sys
import tempfile
import re

from collections import deque
from test import test_support
from distutils.spawn import find_executable

from java.lang import (
    ClassCastException, ExceptionInInitializerError, UnsupportedOperationException,
    String, Runnable, System, Runtime, Math, Byte)
from java.math import BigDecimal, BigInteger
from java.net import URI
from java.io import (ByteArrayInputStream, ByteArrayOutputStream, File, FileInputStream,
                     FileNotFoundException, FileOutputStream, FileWriter, ObjectInputStream,
                     ObjectOutputStream, OutputStreamWriter, UnsupportedEncodingException)
from java.util import ArrayList, Date, HashMap, Hashtable, StringTokenizer, Vector
from java.util.concurrent import Executors

from java.awt import Dimension, Color, Component, Container
from java.awt.event import ComponentEvent
from javax.swing.tree import TreePath
from javax.tools import SimpleJavaFileObject, JavaFileObject, ToolProvider

from org.python.core.util import FileUtil
from org.python.compiler import CustomMaker
from org.python.tests import (BeanImplementation, Child, Child2,
                              CustomizableMapHolder, Listenable, ToUnicode)
from org.python.tests.mro import (ConfusedOnGetitemAdd, FirstPredefinedGetitem, GetitemAdder)
from org.python.util import PythonInterpreter

import java
import org.python.core.Options

from javatests import Issue1833
from javatests.ProxyTests import NullToString, Person

from clamp import SerializableProxies
from unittest.case import skip



class InstantiationTest(unittest.TestCase):
    def test_cant_instantiate_abstract(self):
        self.assertRaises(TypeError, Component)

    def test_no_public_constructors(self):
        self.assertRaises(TypeError, Math)

    def test_invalid_self_to_java_constructor(self):
        self.assertRaises(TypeError, Color.__init__, 10, 10, 10)

    def test_str_doesnt_coerce_to_int(self):
        self.assertRaises(TypeError, Date, '99-01-01', 1, 1)

    def test_class_in_failed_constructor(self):
        try:
            Dimension(123, 456, 789)
        except TypeError, exc:
            self.failUnless("java.awt.Dimension" in exc.message)


class BeanTest(unittest.TestCase):
    def test_shared_names(self):
        self.failUnless(callable(Vector.size),
                'size method should be preferred to writeonly field')

    def test_multiple_listeners(self):
        '''Check that multiple BEP can be assigned to a single cast listener'''
        m = Listenable()
        called = []
        def f(evt, called=called):
            called.append(0)

        m.componentShown = f
        m.componentHidden = f

        m.fireComponentShown(ComponentEvent(Container(), 0))
        self.assertEquals(1, len(called))
        m.fireComponentHidden(ComponentEvent(Container(), 0))
        self.assertEquals(2, len(called))

    def test_bean_interface(self):
        b = BeanImplementation()
        self.assertEquals("name", b.getName())
        self.assertEquals("name", b.name)
        # Tests for #610576
        class SubBean(BeanImplementation):
            def __init__(bself):
                self.assertEquals("name", bself.getName())
        SubBean()

    def test_inheriting_half_bean(self):
        c = Child()
        self.assertEquals("blah", c.value)
        c.value = "bleh"
        self.assertEquals("bleh", c.value)
        self.assertEquals(7, c.id)
        c.id = 16
        self.assertEquals(16, c.id)

    def test_inheriting_half_bean_issue1333(self):
        # http://bugs.jython.org/issue1333
        c = Child2()
        self.assertEquals("blah", c.value)
        c.value = "bleh"
        self.assertEquals("Child2 bleh", c.value)

    def test_awt_hack(self):
        # We ignore several deprecated methods in java.awt.* in favor of bean properties that were
        # added in Java 1.1.  This tests that one of those bean properties is visible.
        c = Container()
        c.size = 400, 300
        self.assertEquals(Dimension(400, 300), c.size)

class SysIntegrationTest(unittest.TestCase):
    def setUp(self):
        self.orig_stdout = sys.stdout

    def tearDown(self):
        sys.stdout = self.orig_stdout
        test_support.unlink(test_support.TESTFN)

    def test_stdout_outputstream(self):
        out = FileOutputStream(test_support.TESTFN)
        sys.stdout = out
        print 'hello',
        out.close()
        f = open(test_support.TESTFN)
        self.assertEquals('hello', f.read())
        f.close()

class IOTest(unittest.TestCase):

    def test_io_errors(self):
        "Check that IOException isn't mangled into an IOError"
        self.assertRaises(UnsupportedEncodingException, OutputStreamWriter, System.out, "garbage")

    def test_fileio_error(self):
        self.assertRaises(FileNotFoundException, FileInputStream, "garbage")

    def fileutil_is_helper(self, mode, expected):
        old_linesep = System.setProperty("line.separator", "\r\n")
        try:
            inputstream = ByteArrayInputStream(bytearray('1\r\n2\r\n3\r\n'))
            inputfile = FileUtil.wrap(inputstream, mode)
            actual = inputfile.readlines()
            inputfile.close()
            self.assertEquals(expected, actual)
        finally:
            System.setProperty("line.separator", old_linesep)

    def test_fileutil_wrap_inputstream(self):
        self.fileutil_is_helper('r', ['1\n', '2\n', '3\n'])

    def test_fileutil_wrap_inputstream_binary(self):
        self.fileutil_is_helper('rb', ['1\r\n', '2\r\n', '3\r\n'])

    def fileutil_os_helper(self, mode, expected):
        old_linesep = System.setProperty("line.separator", "\r\n")
        try:
            outputstream = ByteArrayOutputStream()
            outputfile = FileUtil.wrap(outputstream, mode)
            outputfile.writelines(["1\n", "2\n", "3\n"])
            outputfile.close()
            self.assertEquals(bytearray(outputstream.toByteArray()), expected)
        finally:
            System.setProperty("line.separator", old_linesep)

    def test_fileutil_wrap_outputstream_default_textmode(self):
        self.fileutil_os_helper("w", bytearray("1\r\n2\r\n3\r\n"))

    def test_fileutil_wrap_outputstream_binary(self):
        self.fileutil_os_helper("wb", bytearray("1\n2\n3\n"))

    def test_unsupported_tell(self):
        fp = FileUtil.wrap(System.out)
        self.assertRaises(IOError, fp.tell)


class JavaReservedNamesTest(unittest.TestCase):
    "Access to reserved words"

    def test_system_in(self):
        s = System.in
        self.assert_("method" in str(s.read))

    def test_runtime_exec(self):
        e = Runtime.getRuntime().exec
        self.assert_(re.search("method .*exec", str(e)) is not None)

    def test_byte_class(self):
        b = Byte(10)
        self.assert_("java.lang.Byte" in str(b.class))

class Keywords(object):
    pass

Keywords.in = lambda self: "in"
Keywords.exec = lambda self: "exec"
Keywords.class = lambda self: "class"
Keywords.print = lambda self: "print"
Keywords.and = lambda self: "and"
Keywords.as = lambda self: "as"
Keywords.assert = lambda self: "assert"
Keywords.break = lambda self: "break"
Keywords.continue = lambda self: "continue"
Keywords.def = lambda self: "def"
Keywords.del = lambda self: "del"
Keywords.elif = lambda self: "elif"
Keywords.else = lambda self: "else"
Keywords.except = lambda self: "except"
Keywords.finally = lambda self: "finally"
Keywords.from = lambda self: "from"
Keywords.for = lambda self: "for"
Keywords.global = lambda self: "global"
Keywords.if = lambda self: "if"
Keywords.import = lambda self: "import"
Keywords.is = lambda self: "is"
Keywords.lambda = lambda self: "lambda"
Keywords.pass = lambda self: "pass"
Keywords.print = lambda self: "print"
Keywords.raise = lambda self: "raise"
Keywords.return = lambda self: "return"
Keywords.try = lambda self: "try"
Keywords.while = lambda self: "while"
Keywords.with = lambda self: "with"
Keywords.yield = lambda self: "yield"


class PyReservedNamesTest(unittest.TestCase):
    "Access to reserved words"

    def setUp(self):
        self.kws = Keywords()

    def test_in(self):
        self.assertEquals(self.kws.in(), "in")

    def test_exec(self):
        self.assertEquals(self.kws.exec(), "exec")

    def test_class(self):
        self.assertEquals(self.kws.class(), "class")

    def test_print(self):
        self.assertEquals(self.kws.print(), "print")

    def test_and(self):
        self.assertEquals(self.kws.and(), "and")

    def test_as(self):
        self.assertEquals(self.kws.as(), "as")

    def test_assert(self):
        self.assertEquals(self.kws.assert(), "assert")

    def test_break(self):
        self.assertEquals(self.kws.break(), "break")

    def test_continue(self):
        self.assertEquals(self.kws.continue(), "continue")

    def test_def(self):
        self.assertEquals(self.kws.def(), "def")

    def test_del(self):
        self.assertEquals(self.kws.del(), "del")

    def test_elif(self):
        self.assertEquals(self.kws.elif(), "elif")

    def test_else(self):
        self.assertEquals(self.kws.else(), "else")

    def test_except(self):
        self.assertEquals(self.kws.except(), "except")

    def test_finally(self):
        self.assertEquals(self.kws.finally(), "finally")

    def test_from(self):
        self.assertEquals(self.kws.from(), "from")

    def test_for(self):
        self.assertEquals(self.kws.for(), "for")

    def test_global(self):
        self.assertEquals(self.kws.global(), "global")

    def test_if(self):
        self.assertEquals(self.kws.if(), "if")

    def test_import(self):
        self.assertEquals(self.kws.import(), "import")

    def test_is(self):
        self.assertEquals(self.kws.is(), "is")

    def test_lambda(self):
        self.assertEquals(self.kws.lambda(), "lambda")

    def test_pass(self):
        self.assertEquals(self.kws.pass(), "pass")

    def test_print(self):
        self.assertEquals(self.kws.print(), "print")

    def test_raise(self):
        self.assertEquals(self.kws.raise(), "raise")

    def test_return(self):
        self.assertEquals(self.kws.return(), "return")

    def test_try(self):
        self.assertEquals(self.kws.try(), "try")

    def test_while(self):
        self.assertEquals(self.kws.while(), "while")

    def test_with(self):
        self.assertEquals(self.kws.with(), "with")

    def test_yield(self):
        self.assertEquals(self.kws.yield(), "yield")


class ImportTest(unittest.TestCase):
    def test_bad_input_exception(self):
        self.assertRaises(ValueError, __import__, '')

    def test_broken_static_initializer(self):
        self.assertRaises(ExceptionInInitializerError, __import__, "org.python.tests.BadStaticInitializer")

class ColorTest(unittest.TestCase):
    def test_assigning_over_method(self):
        self.assertRaises(TypeError, setattr, Color.RED, "getRGB", 4)

    def test_static_fields(self):
        self.assertEquals(Color(255, 0, 0), Color.RED)
        # The bean accessor for getRed should be active on instances, but the static field red
        # should be visible on the class
        self.assertEquals(255, Color.red.red)
        self.assertEquals(Color(0, 0, 255), Color.blue)

    def test_is_operator(self):
        red = Color.red
        self.assert_(red is red)
        self.assert_(red is Color.red)

class TreePathTest(unittest.TestCase):
    def test_overloading(self):
        treePath = TreePath([1,2,3])
        self.assertEquals(len(treePath.path), 3, "Object[] not passed correctly")
        self.assertEquals(TreePath(treePath.path).path, treePath.path, "Object[] not passed and returned correctly")

class BigNumberTest(unittest.TestCase):
    def test_coerced_bigdecimal(self):
        from javatests import BigDecimalTest
        x = BigDecimal("123.4321")
        y = BigDecimalTest().asBigDecimal()

        self.assertEqual(type(x), type(y), "BigDecimal coerced")
        self.assertEqual(x, y, "coerced BigDecimal not equal to directly created version")

    def test_biginteger_in_long(self):
        '''Checks for #608628, that long can take a BigInteger in its constructor'''
        ns = '10000000000'
        self.assertEquals(ns, str(long(BigInteger(ns))))

class JavaStringTest(unittest.TestCase):
    def test_string_not_iterable(self):
        x = String('test')
        self.assertRaises(TypeError, list, x)

    def test_null_tostring(self):
        # http://bugs.jython.org/issue1819
        nts = NullToString()
        self.assertEqual(repr(nts), '')
        self.assertEqual(str(nts), '')
        self.assertEqual(unicode(nts), '')

class JavaDelegationTest(unittest.TestCase):
    def test_list_delegation(self):
        for c in ArrayList, Vector:
            a = c()
            self.assertRaises(IndexError, a.__getitem__, 0)
            a.add("blah")
            self.assertTrue("blah" in a)
            self.assertEquals(1, len(a))
            n = 0
            for i in a:
                n += 1
                self.assertEquals("blah", i)
            self.assertEquals(1, n)
            self.assertEquals("blah", a[0])
            a[0] = "bleh"
            del a[0]
            self.assertEquals(0, len(a))

    def test_map_delegation(self):
        m = HashMap()
        m["a"] = "b"
        self.assertTrue("a" in m)
        self.assertEquals("b", m["a"])
        n = 0
        for k in m:
            n += 1
            self.assertEquals("a", k)
        self.assertEquals(1, n)
        del m["a"]
        self.assertEquals(0, len(m))

    def test_enumerable_delegation(self):
        tokenizer = StringTokenizer('foo bar')
        self.assertEquals(list(iter(tokenizer)), ['foo', 'bar'])

    def test_vector_delegation(self):
        class X(Runnable):
            pass
        v = Vector()
        v.addElement(1)
        v.indexOf(X())# Compares the Java object in the vector to a Python subclass
        for i in v:
            pass

    def test_comparable_delegation(self):
        first_file = File("a")
        first_date = Date(100)
        for a, b, c in [(first_file, File("b"), File("c")), (first_date, Date(1000), Date())]:
            self.assertTrue(a.compareTo(b) < 0)
            self.assertEquals(-1, cmp(a, b))
            self.assertTrue(a.compareTo(c) < 0)
            self.assertEquals(-1, cmp(a, c))
            self.assertEquals(0, a.compareTo(a))
            self.assertEquals(0, cmp(a, a))
            self.assertTrue(b.compareTo(a) > 0)
            self.assertEquals(1, cmp(b, a))
            self.assertTrue(c.compareTo(b) > 0)
            self.assertEquals(1, cmp(c, b))
            self.assertTrue(a < b)
            self.assertTrue(a <= a)
            self.assertTrue(b > a)
            self.assertTrue(c >= a)
            self.assertTrue(a != b)
            l = [b, c, a]
            self.assertEquals(a, min(l))
            self.assertEquals(c, max(l))
            l.sort()
            self.assertEquals([a, b, c], l)
        # Check that we fall back to the default comparison(class name) instead of using compareTo
        # on non-Comparable types
        self.assertRaises(ClassCastException, first_file.compareTo, first_date)
        self.assertEquals(-1, cmp(first_file, first_date))
        self.assertTrue(first_file < first_date)
        self.assertTrue(first_file <= first_date)
        self.assertTrue(first_date > first_file)
        self.assertTrue(first_date >= first_file)

    def test_equals(self):
        # Test for bug #1338
        a = range(5)

        x = ArrayList()
        x.addAll(a)

        y = Vector()
        y.addAll(a)

        z = ArrayList()
        z.addAll(range(1, 6))

        self.assertTrue(x.equals(y))
        self.assertEquals(x, y)
        self.assertTrue(not (x != y))

        self.assertTrue(not x.equals(z))
        self.assertNotEquals(x, z)
        self.assertTrue(not (x == z))

class SecurityManagerTest(unittest.TestCase):

    def test_nonexistent_import_with_security(self):
        script = test_support.findfile("import_nonexistent.py")
        home = os.path.realpath(sys.prefix)
        if not os.path.commonprefix((home, os.path.realpath(script))) == home:
            # script must lie within python.home for this test to work
            return
        policy = test_support.findfile("python_home.policy")
        self.assertEquals(
            subprocess.call([sys.executable,
                             "-J-Dpython.cachedir.skip=true",
                             "-J-Djava.security.manager",
                             "-J-Djava.security.policy=%s" % policy, script]),
            0)

    def test_import_signal_fails_with_import_error_using_security(self):
        policy = test_support.findfile("python_home.policy")
        with self.assertRaises(subprocess.CalledProcessError) as cm:
            subprocess.check_output(
                [sys.executable,
                 "-J-Dpython.cachedir.skip=true",
                 "-J-Djava.security.manager",
                 "-J-Djava.security.policy=%s" % policy,
                 "-c", "import signal"],
                stderr=subprocess.STDOUT)
        self.assertIn(
            'ImportError: signal module requires sun.misc.Signal, which is not allowed by your security profile',
            cm.exception.output)


class JavaWrapperCustomizationTest(unittest.TestCase):
    def tearDown(self):
        CustomizableMapHolder.clearAdditions()

    def test_adding_item_access(self):
        m = CustomizableMapHolder()
        self.assertRaises(TypeError, operator.getitem, m, "initial")
        CustomizableMapHolder.addGetitem()
        self.assertEquals(m.held["initial"], m["initial"])
        # dict would throw a KeyError here, but Map returns null for a missing key
        self.assertEquals(None, m["nonexistent"])
        self.assertRaises(TypeError, operator.setitem, m, "initial")
        CustomizableMapHolder.addSetitem()
        m["initial"] = 12
        self.assertEquals(12, m["initial"])

    def test_adding_attributes(self):
        m = CustomizableMapHolder()
        self.assertRaises(AttributeError, getattr, m, "initial")
        CustomizableMapHolder.addGetattribute()
        self.assertEquals(7, m.held["initial"], "Existing fields should still be accessible")
        self.assertEquals(7, m.initial)
        self.assertEquals(None, m.nonexistent, "Nonexistent fields should be passed on to the Map")


class JavaMROTest(unittest.TestCase):

    def test_adding_on_interface(self):
        GetitemAdder.addPredefined()
        class UsesInterfaceMethod(FirstPredefinedGetitem):
            pass
        self.assertEquals("key", UsesInterfaceMethod()["key"])

    def test_add_on_mro_conflict(self):
        """Adding same-named methods to Java classes with MRO conflicts produces TypeError"""
        GetitemAdder.addPredefined()
        self.assertRaises(TypeError, __import__, "org.python.tests.mro.ConfusedOnImport")
        self.assertRaises(TypeError, GetitemAdder.addPostdefined)

    def test_diamond_inheritance_of_iterable_and_map(self):
        """Test deeply nested diamond inheritance of Iterable and Map"""
        # http://bugs.jython.org/issue1878. Previously raised a TypeError (MRO conflict).
        from javatests import DiamondIterableMapMRO
        # The MRO places Iterable ahead of Map (so that __iter__ means j.u.Iterator.__iter__).
        # The following used types are empty interfaces and abstract classes matching the 
        # inheritance graph and naming of Clojure/Storm, where the bug was discovered.
        # Match using str - this will still be stable/robust.
        mrostr = str(DiamondIterableMapMRO.__mro__)
        jli = mrostr.find("java.lang.Iterable")
        self.assertGreater(jli, -1, "Iterable must be in the MRO")
        jum = mrostr.find("java.util.Map")
        self.assertGreater(jum, jli, "Map must come later than Iterable")
        # And usable with __iter__ and map functionality
        m = DiamondIterableMapMRO()
        m["abc"] = 42
        m["xyz"] = 47
        self.assertEqual(set(m), set(["abc", "xyz"]))
        self.assertEqual(m["abc"], 42)

    def test_mro_eclipse(self):
        # http://bugs.jython.org/issue2445
        from org.python.tests.mro import EclipseChallengeMRO

    def test_mro_ibmmq(self):
        # http://bugs.jython.org/issue2445
        from org.python.tests.mro import IBMMQChallengeMRO
        t = IBMMQChallengeMRO.mq.jms.MQQueue


def roundtrip_serialization(obj):
    """Returns a deep copy of an object, via serializing it
 
    see http://weblogs.java.net/blog/emcmanus/archive/2007/04/cloning_java_ob.html
    """
    output = ByteArrayOutputStream()
    serializer = CloneOutput(output)
    serializer.writeObject(obj)
    serializer.close()
    input = ByteArrayInputStream(output.toByteArray())
    unserializer = CloneInput(input, serializer) # to get the list of classes seen, in order
    return unserializer.readObject()

class CloneOutput(ObjectOutputStream):
    def __init__(self, output):
        ObjectOutputStream.__init__(self, output)
        self.classQueue = deque()

    def annotateClass(self, c):
        self.classQueue.append(c)

    def annotateProxyClass(self, c):
        self.classQueue.append(c)

class CloneInput(ObjectInputStream):

    def __init__(self, input, output):
        ObjectInputStream.__init__(self, input)
        self.output = output

    def resolveClass(self, obj_stream_class):
        return self.output.classQueue.popleft()

    def resolveProxyClass(self, interfaceNames):
        return self.output.classQueue.popleft()


def find_jython_jars():
    # Uses the same classpath resolution as bin/jython.py
    jython_bin = os.path.normpath(os.path.dirname(sys.executable))

    home = os.path.dirname(jython_bin)
    jython_jar = os.path.join(home, 'jython.jar')
    jython_dev_jar = os.path.join(home, 'jython-dev.jar')

    if os.path.exists(jython_dev_jar):
        # We are running in the development environment.
        # Add -test and -dev JARs to path.
        jython_test_jar = os.path.join(home, 'jython-test.jar')
        jars = [jython_dev_jar,
                os.path.join(home, 'jython-test.jar'),
                os.path.join(home, "javalib", "*")]
    elif not os.path.exists(jython_jar): 
        raise Exception("Cannot find jython jar")
    else:
        # We are running in the deployment environment.
        # Add only the main JAR (not tests) to path.
        jars = [jython_jar]

    return jars


class JavaSource(SimpleJavaFileObject):

    def __init__(self, name, source):
        self._name = name
        self._source = source
        SimpleJavaFileObject.__init__(
            self, 
            URI.create("string:///" + name.replace(".", "/") + JavaFileObject.Kind.SOURCE.extension),
            JavaFileObject.Kind.SOURCE)

    def getName(self):
        return self._name

    def getCharContent(self, ignore):
        return self._source


@unittest.skipIf(ToolProvider.getSystemJavaCompiler() is None,
        "No Java compiler available. Is JAVA_HOME pointing to a JDK?")
def compile_java_source(options, class_name, source):
    """Compiles a single java source "file" contained in the string source
    
    Use options, specifically -d DESTDIR, to control where the class
    file is emitted. Note that we could use forwarding managers to
    avoid tempdirs, but this is overkill here given that we still need
    to run the emitted Java class.
    """
    f = JavaSource(class_name, source)
    compiler = ToolProvider.getSystemJavaCompiler()
    task = compiler.getTask(None, None, None, options, None, [f])
    task.call()


class SerializationTest(unittest.TestCase):

    def test_java_serialization(self):
        date_list = [Date(), Date()]
        self.assertEqual(date_list, roundtrip_serialization(date_list))

    def test_java_serialization_pycode(self):
        def universal_answer():
            return 42

        serialized_code = roundtrip_serialization(universal_answer.func_code)
        self.assertEqual(eval(serialized_code), universal_answer())

    def test_java_serialization_pyfunction(self):
        # Not directly supported due to lack of general utility
        # (globals will usually be in the function object in
        # func_globals), and problems with unserialization
        # vulnerabilities. Users can always subclass from PyFunction
        # for specific cases, as seen in PyCascading
        import new
        def f():
            return 6 * 7 + max(0, 1, 2)
        # However, using the new module, it's possible to create a
        # function with no globals, which means the globals will come
        # from the current context
        g = new.function(f.func_code, {}, "g")
        # But still forbid Java deserialization of this function
        # object. Use pickling or other support instead.
        with self.assertRaises(UnsupportedOperationException):
            roundtrip_serialization(g)

    def test_builtin_names(self):
        import __builtin__
        names = [x for x in dir(__builtin__)]
        self.assertEqual(names, roundtrip_serialization(names))

    @unittest.skipUnless(find_executable('jar'), 'Need the jar command to run')
    def test_proxy_serialization(self):
        # Proxies can be deserializable in a fresh JVM, including being able
        # to "findPython" to get a PySystemState.
        # tempdir gets combined with unicode paths derived from class names,
        # so make it a unicode object.
        tempdir = tempfile.mkdtemp().decode(sys.getfilesystemencoding())
        old_proxy_debug_dir = org.python.core.Options.proxyDebugDirectory
        try:
            # Generate a proxy for Cat class;
            org.python.core.Options.proxyDebugDirectory = tempdir
            from pounce import Cat
            cat = Cat()
            self.assertEqual(cat.whoami(), "Socks")

            # Create a jar file containing the Cat proxy; could use Java to do this; do it the easy way for now
            proxies_jar_path = os.path.join(tempdir, "proxies.jar")
            subprocess.check_call(["jar", "cf", proxies_jar_path, "-C", tempdir,
                                    "org" + os.path.sep])

            # Serialize our cat
            output = ByteArrayOutputStream()
            serializer = CloneOutput(output)
            serializer.writeObject(cat)
            serializer.close()
            cat_path = os.path.join(tempdir, "serialized-cat")
            with open(cat_path, "wb") as f:
                f.write(output.toByteArray())

            # Then in a completely different JVM running
            # ProxyDeserialization, verify we get "meow" printed to
            # stdout, which in turn ensures that PySystemState (and
            # Jython runtime) is initialized for the proxy
            jars = find_jython_jars()
            jars.append(proxies_jar_path)
            classpath = os.pathsep.join(jars)
            cmd = [os.path.join(System.getProperty("java.home"), "bin", "java"),
                   "-Dpython.path=" + os.path.dirname(__file__),
                    "-classpath", classpath,
                    "javatests.ProxyDeserialization",
                    cat_path]
            self.assertEqual(subprocess.check_output(cmd, universal_newlines=True), "meow\n")
        finally:
            org.python.core.Options.proxyDebugDirectory = old_proxy_debug_dir
            shutil.rmtree(tempdir)

    @unittest.skipUnless(find_executable('jar'), 'Need the jar command to run')
    def test_custom_proxymaker(self):
        # Verify custom proxymaker supports direct usage of Python code in Java
        # tempdir gets combined with unicode paths derived from class names,
        # so make it a unicode object.
        tempdir = tempfile.mkdtemp().decode(sys.getfilesystemencoding())
        try:
            SerializableProxies.serialized_path = tempdir
            import bark
            dog = bark.Dog()
            self.assertEqual(dog.whoami(), "Rover")
            self.assertEqual(dog.serialVersionUID, 1)
            self.assertEqual(dog, roundtrip_serialization(dog))

            # Create a jar file containing the org.python.test.Dog proxy
            proxies_jar_path = os.path.join(tempdir, "proxies.jar")
            subprocess.check_call(["jar", "cf", proxies_jar_path, "-C", tempdir,
                                    "org" + os.path.sep])

            # Build a Java class importing Dog
            source = """
import org.python.test.bark.Dog;  // yes, it's that simple

public class BarkTheDog {
    public static void main(String[] args) {
        Dog dog = new Dog();
        try {
            Boolean b = (Boolean)(dog.call());
            if (!b) {
                throw new RuntimeException("Expected site module to be imported");
            }
        }
        catch(Exception e) {
            System.err.println(e);
        }
    }
}
"""
            jars = find_jython_jars()
            jars.append(proxies_jar_path)
            classpath = os.pathsep.join(jars)
            compile_java_source(
                ["-classpath", classpath, "-d", tempdir],
                "BarkTheDog", source)

            # Then in a completely different JVM running our
            # BarkTheDog code, verify we get an appropriate bark
            # message printed to stdout, which in turn ensures that
            # PySystemState (and Jython runtime) is initialized for
            # the proxy
            classpath += os.pathsep + tempdir
            cmd = [os.path.join(System.getProperty("java.home"), "bin", "java"),
                   "-Dpython.path=" + os.path.dirname(__file__),
                   "-classpath", classpath, "BarkTheDog"]
            self.assertRegexpMatches(
                subprocess.check_output(cmd, universal_newlines=True,
                                        stderr=subprocess.STDOUT),
                r"^Class defined on CLASSPATH <type 'org.python.test.bark.Dog'>\n"
                                        "Rover barks 42 times$")
        finally:
            pass
            # print "Will not remove", tempdir
            #shutil.rmtree(tempdir)


class CopyTest(unittest.TestCase):

    def test_copy(self):
        fruits = ArrayList(["apple", "banana"])
        fruits_copy = copy.copy(fruits)
        self.assertEqual(fruits, fruits_copy)
        self.assertNotEqual(id(fruits), id(fruits_copy))

    def test_deepcopy(self):
        items = ArrayList([ArrayList(["apple", "banana"]),
                           ArrayList(["trs80", "vic20"])])
        items_copy = copy.deepcopy(items)
        self.assertEqual(items, items_copy)
        self.assertNotEqual(id(items), id(items_copy))
        self.assertNotEqual(id(items[0]), id(items_copy[0]))
        self.assertNotEqual(id(items[1]), id(items_copy[1]))

    def test_copy_when_not_cloneable(self):
        bdfl = Person("Guido", "von Rossum")
        self.assertRaises(TypeError, copy.copy, bdfl)

        # monkeypatching in a __copy__ should now work
        Person.__copy__ = lambda p: Person(p.firstName, p.lastName)
        copy_bdfl = copy.copy(bdfl)
        self.assertEqual(str(bdfl), str(copy_bdfl))

    def test_copy_when_not_serializable(self):
        bdfl = Person("Guido", "von Rossum")
        self.assertRaises(TypeError, copy.deepcopy, bdfl)

        # monkeypatching in a __deepcopy__ should now work
        Person.__deepcopy__ = lambda p, memo: Person(p.firstName, p.lastName)
        copy_bdfl = copy.deepcopy(bdfl)
        self.assertEqual(str(bdfl), str(copy_bdfl))

    def test_immutable(self):
        abc = String("abc")
        abc_copy = copy.copy(abc)
        self.assertEqual(id(abc), id(abc_copy))

        fruits = ArrayList([String("apple"), String("banana")])
        fruits_copy = copy.copy(fruits)
        self.assertEqual(fruits, fruits_copy)
        self.assertNotEqual(id(fruits), id(fruits_copy))


class UnicodeTest(unittest.TestCase):

    def test_unicode_conversion(self):
        test = unicode(ToUnicode())
        self.assertEqual(type(test), unicode)
        self.assertEqual(test, u"Circle is 360\u00B0")


class BeanPropertyTest(unittest.TestCase):

    def test_issue1833(self):
        class TargetClass(object):
            def _getattribute(self):
                return self.__attribute
            def _setattribute(self, value):
                self.__attribute = value
            attribute = property(_getattribute, _setattribute)

        target = TargetClass()
        test = Issue1833(target=target)
        value = ('bleh', 'blah')
        test.value = value
        self.assertEqual(target.attribute, value)


class WrappedUp(object):
    def __init__(self):
        self.data = list()
    def doit(self):
        self.data.append(42)


class CallableObject(object):
    def __init__(self):
        self.data = list()
    def __call__(self):
        self.data.append(42)


class SingleMethodInterfaceTest(unittest.TestCase):

    def setUp(self):
        self.executor = Executors.newSingleThreadExecutor()

    def tearDown(self):
        self.executor.shutdown()

    def test_function(self):
        x = list()
        def f():
            x.append(42)
        future = self.executor.submit(f)
        future.get()
        self.assertEqual(x, [42])

    @unittest.skip("FIXME: not working; see http://bugs.jython.org/issue2115")
    def test_callable_object(self):
        callable_obj = CallableObject()
        future = self.executor.submit(callable_obj)
        future.get()
        self.assertEqual(callable_obj.data, [42])

    def test_bound_method(self):
        obj = WrappedUp()
        future = self.executor.submit(obj.doit)
        future.get()
        self.assertEqual(obj.data, [42])

    def test_unbound_method(self):
        with self.assertRaises(TypeError) as exc:
            future = self.executor.submit(WrappedUp.doit)  # not a bound method
        self.assertIsInstance(
            exc.exception, TypeError,
            "submit(): 1st arg can't be coerced to java.util.concurrent.Callable, java.lang.Runnable")

    def test_some_noncallable_object(self):
        obj = WrappedUp()
        with self.assertRaises(TypeError) as exc:
            future = self.executor.submit(obj)
        self.assertIsInstance(
            exc.exception, TypeError,
            "submit(): 1st arg can't be coerced to java.util.concurrent.Callable, java.lang.Runnable")


def test_main():
    test_support.run_unittest(
        BeanPropertyTest,
        BeanTest,
        BigNumberTest,
        ColorTest,
        CopyTest,
        IOTest,
        ImportTest,
        InstantiationTest,
        JavaDelegationTest,
        JavaReservedNamesTest,
        JavaStringTest,
        JavaMROTest,
        JavaWrapperCustomizationTest,
        PyReservedNamesTest,
        SecurityManagerTest,
        SerializationTest,
        SysIntegrationTest,
        TreePathTest,
        UnicodeTest,
        SingleMethodInterfaceTest,
        )

if __name__ == "__main__":
    test_main()