11'''Tests subclassing Java classes in Python'''
22import os
33import sys
4+ import threading
45import unittest
56
67from test import test_support
78
89from java .lang import (Boolean , Class , ClassLoader , Comparable ,Integer , Object , Runnable , String ,
9- Thread , ThreadGroup , UnsupportedOperationException )
10+ Thread , ThreadGroup , InterruptedException , UnsupportedOperationException )
1011from java .util import AbstractList , ArrayList , Date , Hashtable , HashSet , Vector
1112from java .util .concurrent import Callable , Executors
1213
1516from javax .swing .table import AbstractTableModel
1617
1718from org .python .tests import BeanInterface , Callbacker , Coercions , OwnMethodCaller
18- from javatests import InheritanceA , InheritanceB , InheritanceC , InheritanceD
19+ from javatests import (
20+ InheritanceA , InheritanceB , InheritanceC , InheritanceD ,
21+ ExtendedInterface , UseExtendedInterface )
1922
2023
2124class InterfaceTest (unittest .TestCase ):
@@ -555,6 +558,73 @@ def everyOther(self):
555558 self .assertE (e .buildParent (), "D" , InheritanceD , E )
556559
557560
561+ class ChooseCorrectToJavaTest (unittest .TestCase ):
562+
563+ # Verifies fix for http://bugs.jython.org/issue1795
564+ #
565+ # Note that we use threading.Thread because we have imported
566+ # java.lang.Thread as Thread
567+
568+ def test_extended_thread (self ):
569+
570+ class ExtendedThread (threading .Thread , ExtendedInterface ):
571+ def returnSomething (self ):
572+ return "yo yo yo"
573+
574+ result = [None ]
575+ def f (r ):
576+ r [0 ] = 47
577+
578+ t = ExtendedThread (target = f , args = (result ,))
579+ self .assertEqual (
580+ UseExtendedInterface ().countWords (t ),
581+ 3 )
582+
583+ # Also verify that t still works as a regular thread
584+ t .start ()
585+ t .join ()
586+ self .assertFalse (t .isAlive ())
587+ self .assertEqual (result [0 ], 47 )
588+
589+ def test_interruption (self ):
590+ # based on this code http://www.jython.org/jythonbook/en/1.0/Concurrency.html#interruption,
591+ # which demonstrates __tojava__ works properly
592+
593+ class ExtendedThread (threading .Thread , ExtendedInterface ):
594+ def returnSomething (self ):
595+ return "yo yo yo"
596+
597+ def wait_until_interrupted (cv ):
598+ with cv :
599+ while not Thread .currentThread ().isInterrupted ():
600+ try :
601+ # this condition variable is never notified, so will only
602+ # succeed if interrupted
603+ cv .wait ()
604+ except InterruptedException , e :
605+ break
606+
607+ unfair_condition = threading .Condition ()
608+ threads = [
609+ ExtendedThread (
610+ name = "thread #%d" % i ,
611+ target = wait_until_interrupted ,
612+ args = (unfair_condition ,))
613+ for i in xrange (5 )]
614+
615+ for thread in threads :
616+ thread .start ()
617+ for thread in threads :
618+ Thread .interrupt (thread )
619+ for thread in threads :
620+ thread .join (5 )
621+
622+ # this assertion only succeeds if threads terminated because
623+ # they were interrupted
624+ for thread in threads :
625+ self .assertFalse (thread .isAlive ())
626+
627+
558628def test_main ():
559629 test_support .run_unittest (
560630 InterfaceTest ,
@@ -566,7 +636,8 @@ def test_main():
566636 MetaClassTest ,
567637 AbstractMethodTest ,
568638 SuperIsSuperTest ,
569- HierarchyTest )
639+ HierarchyTest ,
640+ ChooseCorrectToJavaTest )
570641
571642
572643if __name__ == '__main__' :
0 commit comments