experiments/JavaClassReloadingTests.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Fri, 06 Sep 2013 02:45:44 +0200
changeset 2678 c865275e48a7
child 2731 13f5be2bf83b
permissions -rw-r--r--
Updated to match Mercurial revision a00302fe5083 with two tweaks: - JavaConstants: conditional compilation of __ACX_MULTIVERS flag (not present in eXept's stc/librun) - JavaNativeMethod: disabled call to __jbindnative() (not present in eXept's librun)

"
 Copyright (c) 2010-2011 Jan Vrany, Jan Kurs & Marcel Hlopko,
                         SWING Research Group, Czech Technical University 
                         in Prague

 Permission is hereby granted, free of charge, to any person
 obtaining a copy of this software and associated documentation
 files (the 'Software'), to deal in the Software without
 restriction, including without limitation the rights to use,
 copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the
 Software is furnished to do so, subject to the following
 conditions:

 The above copyright notice and this permission notice shall be
 included in all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.
"
"{ Package: 'stx:libjava/experiments' }"

TestCase subclass:#JavaClassReloadingTests
	instanceVariableNames:'testClassLoader'
	classVariableNames:''
	poolDictionaries:''
	category:'Languages-Java-Tests-ClassReloading'
!

!JavaClassReloadingTests class methodsFor:'documentation'!

copyright
"
 Copyright (c) 2010-2011 Jan Vrany, Jan Kurs & Marcel Hlopko,
                         SWING Research Group, Czech Technical University 
                         in Prague

 Permission is hereby granted, free of charge, to any person
 obtaining a copy of this software and associated documentation
 files (the 'Software'), to deal in the Software without
 restriction, including without limitation the rights to use,
 copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the
 Software is furnished to do so, subject to the following
 conditions:

 The above copyright notice and this permission notice shall be
 included in all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.

"
!

history

    "Created: #resources / 21-12-2012 / 18:09:27 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
! !

!JavaClassReloadingTests class methodsFor:'resources'!

resources

  ^ Array 
        with: JavaInitializedResource.

    "Created: / 21-12-2012 / 18:09:27 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
! !

!JavaClassReloadingTests methodsFor:'private'!

compile: sourceCode
    | classes |

    classes := JavaCompiler compile: sourceCode.
"/    classes do:[:cls|cls classLoader: testClassLoader] .
    ^classes

    "Created: / 16-12-2012 / 17:02:41 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
    "Modified: / 09-04-2013 / 22:39:06 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

compileAndRegister: sourceCode
    |compiledClasses|

    compiledClasses := self compile: sourceCode.
    ^ self register: compiledClasses.

    "Created: / 16-12-2012 / 16:59:19 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
!

performTest

    JavaClassReader classLoaderQuerySignal answer: testClassLoader do:[
        super performTest
    ].

    "Created: / 16-12-2012 / 16:35:01 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
!

register: aClassOrArray

    ^aClassOrArray isArray ifTrue:[
        JavaVM registry registerClasses: aClassOrArray .
        JavaVM registry classNamed: aClassOrArray anElement name loader: aClassOrArray anElement classLoader
    ] ifFalse:[
        JavaVM registry registerClass: aClassOrArray.
        JavaVM registry classNamed: aClassOrArray name loader: aClassOrArray classLoader
    ].

    "Created: / 16-12-2012 / 17:02:04 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
    "Modified: / 09-04-2013 / 22:27:26 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

testInfrastructure

    |compiledClass|

    self assert: (JavaClassReader classLoaderQuerySignal query = testClassLoader ) 
        message: 'tests should run in testClassLoader sandbox'.
    compiledClass := self compileAndRegister: '
public class HelloSun {

    public String hello() {
        return "helloooo";
    }

}
'.
    self assert: compiledClass notNil message: 'Java compiler should be working for these tests to run'.
    self assert: compiledClass classLoader = testClassLoader message: 'compiled classes should have testClassLoader as their defining cl'.
    self assert: (JavaVM registry getClassesDefinedBy: testClassLoader) size = 1 message: 'only classes from these tests should use testClassLoader'.
    self assert: (JavaVM registry getClassesDefinedBy: testClassLoader) anElement == compiledClass message: 'compiled classes should be registered in testClassLoader'.

    "Created: / 16-12-2012 / 16:38:17 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
! !

!JavaClassReloadingTests methodsFor:'running'!

setUp
    testClassLoader := JAVA java lang ClassLoader new.

    "Created: / 16-12-2012 / 16:32:32 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
!

tearDown
    JavaVM registry unregisterClassLoader: testClassLoader.

    "Created: / 16-12-2012 / 16:33:31 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
! !

!JavaClassReloadingTests methodsFor:'test helpers'!

callerClass
    | callerClass |
    callerClass := (JavaCompiler 
            compile: '
package classReloadingTests;
public class Caller {
    public String callFooToString(Object c) {
        return c.toString();
    }   
}
') anElement.
    callerClass 
        ifNil: [ self error: 'callerClass is expected to be not nil in these tests' ].
    ^ callerClass.

    "Created: / 06-12-2012 / 21:41:35 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
    "Modified: / 18-12-2012 / 14:00:12 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
    "Modified: / 04-01-2013 / 14:51:47 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

compileAndRegisterChangingClassParent

    self compileAndRegister: '
package classReloadingTests;
public class ChangingClassParent {
    public String foo() {
        return "parent";
    }

    public String toString() {
        return "" + foo();
    }   
}'.

    "Created: / 18-12-2012 / 13:47:32 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
! !

!JavaClassReloadingTests class methodsFor:'documentation'!

version_CVS
    ^ '$Header: /cvs/stx/stx/libjava/experiments/JavaClassReloadingTests.st,v 1.1 2013-09-06 00:44:04 vrany Exp $'
!

version_HG

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

version_SVN
    ^ '§Id::                                                                                                                        §'
! !