experiments/SingleClassReloadingTests.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Fri, 25 Jan 2013 17:57:06 +0000
branchrefactoring-vmdata
changeset 2002 ef3da336a6c9
parent 1916 bfd8ca240746
child 2069 75d40b7b986f
permissions -rw-r--r--
Merged ad1a490462ed and 1949478fd05e

"
 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' }"

ClassReloadingTests subclass:#SingleClassReloadingTests
	instanceVariableNames:''
	classVariableNames:''
	poolDictionaries:''
	category:'Languages-Java-Tests-ClassReloading'
!

!SingleClassReloadingTests 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: #testAddingInterface / 16-12-2012 / 17:40:40 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
    "Modified (comment): #test_00b / 18-12-2012 / 12:02:24 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
    "Modified: #test_02c / 18-12-2012 / 12:07:59 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
    "Modified (comment): #testChangingSignatureOfMethod / 18-12-2012 / 14:08:36 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
    "Modified (comment): #testModifyingMethod / 18-12-2012 / 14:09:49 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
    "Modified (comment): #testRemovingMethod / 18-12-2012 / 14:11:56 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
    "Modified (comment): #testAddingMethod / 18-12-2012 / 14:12:06 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
! !

!SingleClassReloadingTests methodsFor:'interfaces'!

testAddingInterface
    self assert: false message: 'implement me!!'.

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

!SingleClassReloadingTests methodsFor:'methods'!

testAddingMethod    
    "    
    1) compile a class inheriting from ChangingClassParent.
    2) instantiate inheriting class.
    3) assert calling foo on subclass uses method from superclass'.
    4) recompile inheriting class with overridden foo method.
    5) assert overridden method is used.
    "
   | caller inst |
   caller := self callerClass new.
   self compileAndRegisterChangingClassParent.

    self compileAndRegister: '
package classReloadingTests;
public class ChangingClass extends ChangingClassParent {}
'.
     
    inst := JAVA classReloadingTests ChangingClass new.
    self assert: (caller callFooToString: inst) = 'parent'.        

    self compileAndRegister: '
package classReloadingTests;
public class ChangingClass extends ChangingClassParent {
    public String foo() {
        return "child";
    }
}'.
    self assert: (caller callFooToString: inst) = 'child'.

    "Created: / 06-12-2012 / 21:52:02 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
    "Modified (comment): / 18-12-2012 / 14:12:06 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
!

testChangingSignatureOfMethod
    "
    1) compile a class with foo method returning string.    
    2) instantiate the class.    
    3) assert method returns string.
    4) recompile class with foo method returning int.
    5) assert new method is used.
    "

    | caller inst |
    caller := self callerClass new.

    self compileAndRegister: '
package classReloadingTests;
public class ChangingClass {
    public String toString() {
        return "" + foo();
    }

    public String foo() {
        return "child";
    }
}
'.
    inst := JAVA classReloadingTests ChangingClass new.
    self assert: (caller callFooToString: inst) = 'child'.        

    self compileAndRegister: '
package classReloadingTests;
public class ChangingClass {
    public String toString() {
        return "" + foo();
    }

    public int foo() {
        return 5;
    }
}'.
    self assert: (caller callFooToString: inst) = '5'.

    "Created: / 06-12-2012 / 21:50:53 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
    "Modified (comment): / 18-12-2012 / 14:08:36 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
!

testModifyingMethod
    "
    1) compile a class with foo method.    
    2) instantiate the class.    
    3) assert method returns string.
    4) recompile class with foo method returning different string.
    5) assert new method is used.
    "

    | caller inst |
    caller := self callerClass new.

    self compileAndRegister: '
package classReloadingTests;
public class ChangingClass {
    public String toString() {
        return "" + foo();
    }

    public String foo() {
        return "child";
    }
}
'.
    inst := JAVA classReloadingTests ChangingClass new.
    self assert: (caller callFooToString: inst) = 'child'.        

    self compileAndRegister: '
package classReloadingTests;
public class ChangingClass {
    public String toString() {
        return "" + foo();
    }

    public String foo() {
        return "modified child";
    }
}'.
        self assert: (caller callFooToString: inst) = 'modified child'.

    "Created: / 06-12-2012 / 21:51:14 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
    "Modified (comment): / 18-12-2012 / 14:09:48 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
!

testRemovingMethod   
  "    
    1) compile a class inheriting from ChangingClassParent, which overrides foo method.
    2) instantiate inheriting class.
    3) assert calling foo on subclass uses overridden method'.
    4) recompile inheriting class without foo method.
    5) assert parent method is used.
    "
   | caller inst |
   caller := self callerClass new.
   self compileAndRegisterChangingClassParent.

    self compileAndRegister: '
package classReloadingTests;
public class ChangingClass extends ChangingClassParent {
    public String foo() {
        return "child";
    }
}'.

    inst := JAVA classReloadingTests ChangingClass new.
    self assert: (caller callFooToString: inst) = 'child'.

    self compileAndRegister: '
package classReloadingTests;
public class ChangingClass extends ChangingClassParent {}'.
    self assert: (caller callFooToString: inst) = 'parent'.

    "Created: / 16-12-2012 / 16:01:58 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
    "Modified (comment): / 18-12-2012 / 14:11:56 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
!

test_00
    "
    1) compile simple class with no method
    3) recompile same source again.
    4) check that class does not change
    "

    | jclass1 jclass2 |
    jclass1 := self compileAndRegister:'
public class test_01 { 
    public int foo() { 
        return 10; 
    }
}'.
    self assert: jclass1 new foo == 10.

    jclass2 := self compileAndRegister:'
public class test_01 { 
    public int foo() { 
        return 10; 
    }
}'.

    self assert: jclass1 == jclass2. "/only method update, so reloaded class should be the same
    self assert: (JavaVM registry getClassesDefinedBy: testClassLoader) size = 1 message: 'only classes from these tests should use testClassLoader'.
    self assert: (JavaVM registry getClassesDefinedBy: testClassLoader) anElement == jclass1 message: 'compiled classes should be registered in testClassLoader'.

    "Created: / 16-12-2012 / 23:51:37 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

test_00b
    "
    1) compile simple class with no method but in package
    3) recompile same source again.
    4) check that class does not change
    "

    | jclass1 jclass2 |
    jclass1 := self compileAndRegister:'
package zork;
public class test_01 { 
    public int foo() { 
        return 10; 
    }
}'.
    self assert: jclass1 new foo == 10.

    jclass2 := self compileAndRegister:'
package zork;
public class test_01 { 
    public int foo() { 
        return 10; 
    }
}'.

    self assert: jclass1 == jclass2. "/only method update, so reloaded class should be the same
    self assert: (JavaVM registry getClassesDefinedBy: testClassLoader) size = 1 message: 'only classes from these tests should use testClassLoader'.
    self assert: (JavaVM registry getClassesDefinedBy: testClassLoader) anElement == jclass1 message: 'compiled classes should be registered in testClassLoader'.

    "Created: / 16-12-2012 / 23:56:58 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified (comment): / 18-12-2012 / 12:02:24 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
!

test_01
    "
    1) compile simple class with one method return 10
    2) call that method, check that it returns 10
    3) recompile to return 20
    4) call that method, check that it returns 20
    "

    | jclass1 jclass2 |
    jclass1 := self compileAndRegister:'
public class test_01 { 
    public int foo() { 
        return 10; 
    }
}'.
    self assert: jclass1 new foo == 10.

    jclass2 := self compileAndRegister:'
public class test_01 { 
    public int foo() { 
        return 20; 
    }
}'.

    self assert: jclass2 new foo == 20.
    self assert: jclass1 == jclass2. "/only method update, so reloaded class should be the same

    "Created: / 16-12-2012 / 23:48:37 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

test_02a
    "
    1) compile simple class with one method return 10
    2) call that method indirectly, check that it returns 10
    3) recompile to return 20
    4) call that method indirectly, check that it returns 20
    "

    | jclass1 jclass2 caller |
    jclass1 := self compileAndRegister:'
public class test_02 { 
    public int foo() { 
        return 10; 
    }
}'.
    caller := self compileAndRegister:'
public class test_02_caller {
    public int qux(test_02 t) {
        return t.foo();
    }
}
'.
    self assert: (caller new qux: (jclass1 new)) == 10.

    jclass2 := self compileAndRegister:'
public class test_02 { 
    public int foo() { 
        return 20; 
    }
}'.
    self assert: (caller new qux: (jclass2 new)) == 20.

    "Created: / 17-12-2012 / 00:02:22 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

test_02b
    "
    1) compile simple class with one method return 10
    2) call that method indirectly using fresh instance (NEW), 
       check that it returns 10
    3) recompile to return 20
    4) call that method indirectly, check that it returns 20
    "

    | jclass1 jclass2 caller |
    jclass1 := self compileAndRegister:'
public class test_02 { 
    public int foo() { 
        return 10; 
    }
}'.
    caller := self compileAndRegister:'
public class test_02_caller {
    public int qux() {
        test_02 t = new test_02();
        return t.foo();
    }
}
'.
    self assert: (caller new qux) == 10.

    jclass2 := self compileAndRegister:'
public class test_02 { 
    public int foo() { 
        return 20; 
    }
}'.
    self assert: (caller new qux) == 20.

    "Created: / 17-12-2012 / 00:04:19 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

test_02c
    "
    Same as 02a, but with static methods
    "

    | jclass1 jclass2 caller |
    jclass1 := self compileAndRegister:'
public class test_02 { 
    public static int foo() {
        return 10; 
    }
}'.
    caller := self compileAndRegister:'
public class test_02_caller {
    public int qux() {
        return test_02.foo();
    }
}
'.
    self assert: (caller new qux) == 10.

    jclass2 := self compileAndRegister:'
public class test_02 { 
    public static int foo() { 
        return 20; 
    }
}'.
    self assert: (caller new qux) == 20.

    "Created: / 17-12-2012 / 00:03:54 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 18-12-2012 / 12:07:59 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
! !

!SingleClassReloadingTests class methodsFor:'documentation'!

version_HG

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

version_SVN
    ^ '§Id::                                                                                                                        §'
! !