JavaExceptionTests.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Wed, 19 Mar 2014 17:44:46 +0000
changeset 3041 7a326f9f8aad
parent 3040 ff90519ce3ac
child 3067 4bbacafb9592
permissions -rw-r--r--
Bug fix in interop with respect to overloaded methods in subclasses. This commit fixes bug in Smalltalk-to-Java lookup. When a proxy was compiled for the very first time and only one matching candidate was found, no argument check was performed and that method was called directly. This is wrong, because later on a new subclass which overloads that could be loaded and in this case we have to search again for candidates and select correct one. This commit fixes this problem by: * Always compiling guard that ensures dynamic proxy recompilation if argument type(s) change. * When recompiling method, a new set of candidates is looked up, based on receiver's class (before cached list of candidates were used to select matching method from) * When installing proxy, always install into receiver's class.

"
 COPYRIGHT (c) 1996-2011 by Claus Gittinger

 New code and modifications done at SWING Research Group [1]:

 COPYRIGHT (c) 2010-2011 by Jan Vrany, Jan Kurs and Marcel Hlopko
                            SWING Research Group, Czech Technical University in Prague

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.

 [1] Code written at SWING Research Group contains a signature
     of one of the above copright owners. For exact set of such code,
     see the differences between this version and version stx:libjava
     as of 1.9.2010
"
"{ Package: 'stx:libjava' }"

TestCase subclass:#JavaExceptionTests
	instanceVariableNames:'signal'
	classVariableNames:''
	poolDictionaries:''
	category:'Languages-Java-Tests'
!

!JavaExceptionTests class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 1996-2011 by Claus Gittinger

 New code and modifications done at SWING Research Group [1]:

 COPYRIGHT (c) 2010-2011 by Jan Vrany, Jan Kurs and Marcel Hlopko
                            SWING Research Group, Czech Technical University in Prague

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.

 [1] Code written at SWING Research Group contains a signature
     of one of the above copright owners. For exact set of such code,
     see the differences between this version and version stx:libjava
     as of 1.9.2010

"
! !

!JavaExceptionTests class methodsFor:'accessing'!

resources

  ^ Array 
        with: JavaInitializedResource 
        with: JavaLibrariesResource
        with: JavaTestsResource

    "Created: / 30-03-2012 / 13:38:08 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!JavaExceptionTests methodsFor:'callbacks'!

call: trhower with: aBoolean 
    aBoolean ifTrue: [ signal raise ].

    "Created: / 03-04-2012 / 17:33:00 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 18-11-2012 / 18:04:47 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
!

throw_me: aBoolean

    aBoolean ifTrue:[signal raise].

    "Created: / 03-04-2012 / 17:31:26 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!JavaExceptionTests methodsFor:'error handling (interop)'!

doesNotUnderstand:aMessage

    | method  selector class args|
    selector := aMessage selector.
    args := aMessage arguments.
    class := self class.

    JavaLookup isNil ifTrue:[
        (Smalltalk loadPackage: 'stx:libjava/experiments') ifFalse:[
            self error: 'You should load package stx:libjava/experiments if you want some interop - still experimental' mayProceed: true.
            ^nil                        
        ]        
    ].

    method := JavaLookup instance lookupMethodForSelector: selector
                directedTo: class
                for: self
                withArguments: args
                from: thisContext sender sender
                ilc: nil.

    method isNil ifTrue:[
        ^super doesNotUnderstand:aMessage
    ] ifFalse:[
        ^ method valueWithReceiver: self arguments: args
    ].

    "Created: / 06-09-2011 / 22:16:26 / Jan Kurs <kursjan@fit.cvut.cz>"
    "Modified: / 15-12-2011 / 23:42:48 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!JavaExceptionTests methodsFor:'running'!

setUp
    signal := Signal new.

    "Created: / 03-04-2012 / 17:30:33 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!JavaExceptionTests methodsFor:'tests'!

test_01a
    "
    Scenario (method activation stack, last called at bottom)
        1) ST method, handles IllegalArgumentException
        2) Java method, throws IllegalArgumentException.
    "

    | thrower caught |
    thrower := JAVA stx libjava tests lang ExceptionTests new.
    [ 
        thrower throw_me: true.
        caught := false.
        
    ] on: JAVA java lang IllegalArgumentException do: [:ex|
        caught := true.
    ].

    self assert: caught == true.
    self assert: (JavaVM enteredMonitorsOfProcess: Processor activeProcess) isEmptyOrNil.
    self assert: (JavaVM acquiredMonitorsOfProcess: Processor activeProcess) isEmptyOrNil.

    "Created: / 18-03-2012 / 11:06:23 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

test_01b
    "
     Scenario (method activation stack, last called at bottom)
        1) ST method, handles IllegalArgumentException
        2) Java method, does not IllegalArgumentException."
    
    | thrower  caught |
    thrower := JAVA stx libjava tests lang ExceptionTests new.
    [
        thrower throw_me: false.
        caught := false.
    ] on: JAVA java lang IllegalArgumentException do: [:ex | 
        caught := true. 
    ].
    self assert: caught == false.
    self assert: (JavaVM enteredMonitorsOfProcess: Processor activeProcess) 
                isEmptyOrNil.
    self assert: (JavaVM acquiredMonitorsOfProcess: Processor activeProcess) 
                isEmptyOrNil.

    "Created: / 18-03-2012 / 21:49:09 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 18-11-2012 / 18:17:22 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
    "Modified (format): / 29-11-2012 / 22:54:46 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

test_01c
    "
    Scenario (method activation stack, last called at bottom)
        1) ST method, handles IllegalArgumentException
        2) Java method, does not IllegalArgumentException.
    "

    | thrower caught |
    thrower := JAVA stx libjava tests lang ExceptionTests new.
    [ 
        thrower throw_me: true.
        caught := false.
        
    ] on: JAVA java lang Throwable do: [:ex|
        caught := true.
    ].

    self assert: caught == true.
    self assert: (JavaVM enteredMonitorsOfProcess: Processor activeProcess) isEmptyOrNil.
    self assert: (JavaVM acquiredMonitorsOfProcess: Processor activeProcess) isEmptyOrNil.

    "Created: / 18-03-2012 / 22:11:24 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

test_01d
    "
    Scenario (method activation stack, last called at bottom)
        1) ST method, handles IllegalArgumentException
        2) Java method, does not IllegalArgumentException.
    "

    | thrower caught |
    thrower := JAVA stx libjava tests lang ExceptionTests new.
    [
        [ 
            thrower throw_me: true.
            caught := false.
            
        ] on: JAVA java lang ArrayIndexOutOfBoundsException do: [:ex|
            caught := true.
        ].
    ] on: JavaUnhandledExceptionError do:[
        caught := 123.
    ].

    self assert: caught == 123.
    self assert: (JavaVM enteredMonitorsOfProcess: Processor activeProcess) isEmptyOrNil.
    self assert: (JavaVM acquiredMonitorsOfProcess: Processor activeProcess) isEmptyOrNil.

    "Created: / 18-03-2012 / 22:12:06 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

test_01e
    "
    Scenario (method activation stack, last called at bottom)
        1) ST method, handles IllegalArgumentException
        2) Java method, throws IllegalArgumentException.
    "

    | thrower caught |
    thrower := JAVA stx libjava tests lang ExceptionTests new.
    [ 
        thrower call: true.
        caught := false.
        
    ] on: JAVA java lang IllegalArgumentException do: [:ex|
        caught := true.
    ].

    self assert: caught == true.
    self assert: (JavaVM enteredMonitorsOfProcess: Processor activeProcess) isEmptyOrNil.
    self assert: (JavaVM acquiredMonitorsOfProcess: Processor activeProcess) isEmptyOrNil.

    "Created: / 03-04-2012 / 14:44:01 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

test_03a
    "
     Scenario (method activation stack, last called at bottom)
        1) ST method, handles IllegalArgumentException
        2) Java method with finally
        3) Java method, throws IllegalArgumentException."
    
    | thrower  caught |
    thrower := JAVA stx libjava tests lang ExceptionTests new.
    [
        thrower test_03: true.
        caught := false.
    ] on: JAVA java lang IllegalArgumentException do: [:ex | caught := true. ].
    self assert: caught == true.
    self assert: (thrower instVarNamed: #token) == 3.
    self assert: (JavaVM enteredMonitorsOfProcess: Processor activeProcess) 
                isEmptyOrNil.
    self assert: (JavaVM acquiredMonitorsOfProcess: Processor activeProcess) 
                isEmptyOrNil.

    "Created: / 03-04-2012 / 15:39:08 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified (comment): / 18-11-2012 / 16:50:50 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
!

test_04a
    "
     Scenario (method activation stack, last called at bottom)
        1) ST method, handles 'signal'
        2) Java method with finally
        3) Smalltak method, throws 'signal'"
    
    | thrower  caught |
    thrower := JAVA stx libjava tests lang ExceptionTests new.
    [
        thrower test_04: self with: true.
        caught := false.
    ] on: signal do: [:ex | caught := true. ].
    self assert: caught == true.
    self assert: (thrower instVarNamed: #token) == 3.
    self assert: (JavaVM enteredMonitorsOfProcess: Processor activeProcess) 
                isEmptyOrNil.
    self assert: (JavaVM acquiredMonitorsOfProcess: Processor activeProcess) 
                isEmptyOrNil.

    "Created: / 03-04-2012 / 17:30:10 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 18-11-2012 / 18:04:42 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
! !

!JavaExceptionTests class methodsFor:'documentation'!

version_CVS
    ^ '$Header: /cvs/stx/stx/libjava/JavaExceptionTests.st,v 1.3 2013-09-06 00:41:23 vrany Exp $'
!

version_HG

    ^ '$Changeset: <not expanded> $'
!

version_SVN
    ^ 'Id'
! !