experiments/ClassReloadingTests.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Tue, 29 Jan 2013 15:15:51 +0000
branchdevelopment
changeset 2006 c0598cab5f15
parent 1923 10990021296a
child 2069 75d40b7b986f
permissions -rw-r--r--
Commit fix: fixed content of Smalltalk extensions containers. Somehow, bare Java class were written to extension containers for Java classes.

"
 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:#ClassReloadingTests
	instanceVariableNames:'testClassLoader'
	classVariableNames:''
	poolDictionaries:''
	category:'Languages-Java-Tests-ClassReloading'
!

!ClassReloadingTests 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>"
! !

!ClassReloadingTests class methodsFor:'resources'!

resources

  ^ Array 
        with: JavaInitializedResource.

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

!ClassReloadingTests methodsFor:'private'!

compile: sourceCode

    ^ JavaCompiler compile: sourceCode.

    "Created: / 16-12-2012 / 17:02:41 / Marcel Hlopko <marcel.hlopko@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.
    ] ifFalse:[
        JavaVM registry registerClass: aClassOrArray.
        JavaVM registry classNamed: aClassOrArray name
    ].

    "Created: / 16-12-2012 / 17:02:04 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
    "Modified: / 04-01-2013 / 14:54:58 / 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>"
! !

!ClassReloadingTests 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>"
! !

!ClassReloadingTests 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>"
! !

!ClassReloadingTests class methodsFor:'documentation'!

version_HG

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

version_SVN
    ^ '§Id::                                                                                                                        §'
! !