experiments/JavaSingleClassReloadingTests.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Wed, 24 Apr 2013 00:07:32 +0100
branchdevelopment
changeset 2549 6ef03f1baa82
parent 2533 5e9a3673a297
child 2664 2b63c6b7f040
permissions -rw-r--r--
Bugfix in natives (array reflection). Caused by semi-automatic refactoring of natives.

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

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

!JavaSingleClassReloadingTests 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: #test_overloads_01b / 11-04-2013 / 13:35:15 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
    "Modified: #test_03a / 14-04-2013 / 15:22:18 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
    "Modified: #test_03b / 14-04-2013 / 15:23:25 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
! !

!JavaSingleClassReloadingTests methodsFor:'interfaces'!

testAddingInterface
    OperatingSystem getLoginName = 'm' ifTrue:[
        self assert: false message: 'implement me!!'
    ]

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

!JavaSingleClassReloadingTests methodsFor:'tests - fields'!

test_fields_00
    "
    1) compile simple class with one field
    2) change type of the field from int to Object

    "

    | jclass1 jinst1 jclass2 jinst2 callerClass |

    jclass1 := self compileAndRegister:'
public class test_01 { 
    public String foo = 1;
    public int bar() {
        return 1;
    }
}'.
    jinst1 := jclass1 new.

    jclass2 := self compileAndRegister:'
public class test_01 { 
    public Object foo = new Object();
    public int bar() {
        return 2;
    }
}'.
    jinst2 := jclass2 new.

    callerClass := self compileAndRegister:'
public class CallerClass {
    public static Object getFoo(test_01 inst) {
        return inst.foo;
    }
}'.

    self assert: jclass1 ~~ jclass2.

    self assert: jclass1 fields size == 1.
    self assert: jclass1 fields anElement descriptor = 'Ljava/lang/String;'.
    self assert: jinst1 bar == 1.

    self assert: jclass2 fields size == 1.
    self assert: jclass2 fields anElement descriptor = 'Ljava/lang/Object;'.
    self assert: jinst2 bar == 2.

    self should:   [ callerClass getFoo: jinst1 ] raise: Error.
    self shouldnt: [ callerClass getFoo: jinst2 ] raise: Error.

    "Created: / 09-04-2013 / 15:00:51 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 09-04-2013 / 16:17:26 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

test_fields_01a
    "
    1) compile simple class with one field
    2) change type of the field from String to Object

    "

    | jclass1 jinst1 jclass2 jinst2 callerClass |

    jclass1 := self compileAndRegister:'
public class test_01 { 
    public String foo = "xxx";
    public int bar() {
        return 1;
    }
}'.
    jinst1 := jclass1 new.

    jclass2 := self compileAndRegister:'
public class test_01 { 
    public Object foo = new Object();
    public int bar() {
        return 2;
    }
}'.
    jinst2 := jclass2 new.

    callerClass := self compileAndRegister:'
public class CallerClass {
    public static Object getFoo(test_01 inst) {
        return inst.foo;
    }
}'.

    self assert: jclass1 ~~ jclass2.

    self assert: jclass1 fields size == 1.
    self assert: jclass1 fields anElement descriptor = 'Ljava/lang/String;'.
    self assert: jinst1 bar == 1.

    self assert: jclass2 fields size == 1.
    self assert: jclass2 fields anElement descriptor = 'Ljava/lang/Object;'.
    self assert: jinst2 bar == 2.

    self should:   [ callerClass getFoo: jinst1 ] raise: Error.
    self shouldnt: [ callerClass getFoo: jinst2 ] raise: Error.

    "Created: / 09-04-2013 / 15:24:13 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

test_fields_01b
    "
    1) compile simple class with one field
    2) change type of the field from String to Object

    "

    | jclass1 jinst1 jclass2 jinst2 callerClass |

    jclass1 := self compileAndRegister:'
public class test_01 { 
    public String foo = "XXX";
    public int bar() {
        return 1;
    }
}'.
    jinst1 := jclass1 new.

    callerClass := self compileAndRegister:'
public class CallerClass {
    public static Object getFoo(test_01 inst) {
        return inst.foo;
    }
}'.

    jclass2 := self compileAndRegister:'
public class test_01 { 
    public Object foo = new Object();
    public int bar() {
        return 2;
    }
}'.
    jinst2 := jclass2 new.



    self assert: jclass1 ~~ jclass2.

    self assert: jclass1 fields size == 1.
    self assert: jclass1 fields anElement descriptor = 'Ljava/lang/String;'.
    self assert: jinst1 bar == 1.

    self assert: jclass2 fields size == 1.
    self assert: jclass2 fields anElement descriptor = 'Ljava/lang/Object;'.
    self assert: jinst2 bar == 2.

    self shouldnt: [ callerClass getFoo: jinst1 ] raise: Error.
    self should:   [ callerClass getFoo: jinst2 ] raise: Error.

    "Created: / 09-04-2013 / 15:25:10 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

test_fields_02a
    "
    1) compile simple class with one public field
    2) add second field after the first one
    3) create caller class that accesses the public field
    4) check that caller class accesses the right field
       on both old and new instance
    "

    | jclass1 jinst1 jclass2 jinst2 callerClass |

    jclass1 := self compileAndRegister:'
public class test_fields_02a { 
    public String foo = "foo";
}'.
    jinst1 := jclass1 new.


    jclass2 := self compileAndRegister:'
public class test_fields_02a { 
    public String foo = "foo+bar";
    public String bar = "foo+bar";
}'.
    jinst2 := jclass2 new.

    callerClass := self compileAndRegister:'
public class test_fields_02a_caller {
    public static String getFoo(test_fields_02a x) {
        return x.foo;
    }
}'.

    self assert: jclass2 fields size == 2.

    self assert: (callerClass getFoo: jinst1) = 'foo'.   
    self assert: (callerClass getFoo: jinst2) = 'foo+bar'.

    "Created: / 09-04-2013 / 15:32:20 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

test_fields_02b
    "
    1) compile simple class with one public field
    2) add second field after the first one
    3) create caller class that accesses the public field
    4) check that caller class accesses the right field
       on both old and new instance
    "

    | jclass1 jinst1 jclass2 jinst2 callerClass |

    jclass1 := self compileAndRegister:'
public class test_fields_02b { 
    public String foo = "foo";
}'.
    jinst1 := jclass1 new.

    callerClass := self compileAndRegister:'
public class test_fields_02b_caller {
    public static String getFoo(test_fields_02b x) {
        return x.foo;
    }
}'.

    jclass2 := self compileAndRegister:'
public class test_fields_02b { 
    public String foo = "foo+bar";
    public String bar = "foo+bar";
}'.
    jinst2 := jclass2 new.

    self assert: jclass2 fields size == 2.

    self assert: (callerClass getFoo: jinst1) = 'foo'.   
    self assert: (callerClass getFoo: jinst2) = 'foo+bar'.

    "Created: / 09-04-2013 / 15:30:49 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

test_fields_03a
    "
    1) compile simple class with one public field
    2) add second field before the first one
    3) create caller class that accesses the public field
    4) check that caller class accesses the right field
       on both old and new instance
    "

    | jclass1 jinst1 jclass2 jinst2 callerClass |

    jclass1 := self compileAndRegister:'
public class test_fields_03a { 
    public String foo = "foo";
}'.
    jinst1 := jclass1 new.


    jclass2 := self compileAndRegister:'
public class test_fields_03a { 
    public String bar = "foo+bar";
    public String foo = "foo+bar";
}'.
    jinst2 := jclass2 new.

    callerClass := self compileAndRegister:'
public class test_fields_03a_caller {
    public static String getFoo(test_fields_03a x) {
        return x.foo;
    }
}'.

    self assert: jclass2 fields size == 2.

    self assert: (callerClass getFoo: jinst1) = 'foo'.   
    self assert: (callerClass getFoo: jinst2) = 'foo+bar'.

    "Created: / 09-04-2013 / 15:33:39 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

test_fields_03b
    "
    1) compile simple class with one public field
    2) add second field before the first one
    3) create caller class that accesses the public field
    4) check that caller class accesses the right field
       on both old and new instance
    "

    | jclass1 jinst1 jclass2 jinst2 callerClass |

    jclass1 := self compileAndRegister:'
public class test_fields_03b { 
    public String foo = "foo";
}'.
    jinst1 := jclass1 new.

    callerClass := self compileAndRegister:'
public class test_fields_03b_caller {
    public static String getFoo(test_fields_03b x) {
        return x.foo;
    }
}'.

    jclass2 := self compileAndRegister:'
public class test_fields_03b { 
    public String bar = "foo+bar";
    public String foo = "foo+bar";
}'.
    jinst2 := jclass2 new.



    self assert: jclass2 fields size == 2.

    self assert: (callerClass getFoo: jinst1) = 'foo'.   
    self assert: (callerClass getFoo: jinst2) = 'foo+bar'.

    "Created: / 09-04-2013 / 15:34:11 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

test_fields_03c
    "
    1) compile simple class with one public field
    2) add second field before the first one
    3) create caller class that accesses the new field
    4) check that caller class accesses the new field
       on new instance
    5) check that caller class accessing new field on
       old instances throws exception
    "

    | jclass1 jinst1 jclass2 jinst2 callerClass |

    jclass1 := self compileAndRegister:'
public class test_fields_03c { 
    public String foo = "foo";
}'.
    jinst1 := jclass1 new.


    jclass2 := self compileAndRegister:'
public class test_fields_03c { 
    public String bar = "foo+bar";
    public String foo = "foo+bar";
}'.
    jinst2 := jclass2 new.

    callerClass := self compileAndRegister:'
public class test_fields_03c_caller {
    public static String getBar(test_fields_03c x) {
        return x.bar;
    }
}'.

    self assert: jclass2 fields size == 2.

    self assert: (callerClass getBar: jinst2) = 'foo+bar'.
    self should: [ callerClass getBar: jinst1 ] raise: Error.

    "Created: / 09-04-2013 / 15:53:51 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

test_fields_04a
    "
    1) compile simple class with two public fields
    2) remove first field
    3) create caller class that accesses the second field
    4) check that caller class accesses the right field
       on both old and new instances
    "

    | jclass1 jinst1 jclass2 jinst2 callerClass |

    jclass1 := self compileAndRegister:'
public class test_fields_04a { 
    public String foo = "foo";
    public String bar = "bar+foo";
}'.
    jinst1 := jclass1 new.

    jclass2 := self compileAndRegister:'
public class test_fields_04a { 
    public String bar = "bar";
}'.
    jinst2 := jclass2 new.

    callerClass := self compileAndRegister:'
public class test_fields_04a_caller {
    public static String getBar(test_fields_04a x) {
        return x.bar;
    }
}'.

    self assert: jclass2 fields size == 1.

    self assert: (callerClass getBar: jinst1) = 'bar+foo'.   
    self assert: (callerClass getBar: jinst2) = 'bar'.

    "Created: / 09-04-2013 / 15:46:54 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

test_fields_04b
    "
    1) compile simple class with two public fields
    2) remove first field
    3) create caller class that accesses the second field
    4) check that caller class accesses the right field
       on both old and new instances
    "

    | jclass1 jinst1 jclass2 jinst2 callerClass |

    jclass1 := self compileAndRegister:'
public class test_fields_04b { 
    public String foo = "foo";
    public String bar = "bar+foo";
}'.
    jinst1 := jclass1 new.

    callerClass := self compileAndRegister:'
public class test_fields_04b_caller {
    public static String getBar(test_fields_04b x) {
        return x.bar;
    }
}'.

    jclass2 := self compileAndRegister:'
public class test_fields_04b { 
    public String bar = "bar";
}'.
    jinst2 := jclass2 new.



    self assert: jclass2 fields size == 1.

    self assert: (callerClass getBar: jinst1) = 'bar+foo'.   
    self assert: (callerClass getBar: jinst2) = 'bar'.

    "Created: / 09-04-2013 / 15:45:38 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

test_fields_04c
    "
    1) compile simple class with two public fields
    2) create caller class that accesses the first field
    3) remove first field
    4) check that caller class accesses the first field
       on old instances
    5) check that accessing the first field on new instances
       raises exception
    "

    | jclass1 jinst1 jclass2 jinst2 callerClass |

    jclass1 := self compileAndRegister:'
public class test_fields_04c { 
    public String foo = "foo";
    public String bar = "bar+foo";
}'.
    jinst1 := jclass1 new.

    callerClass := self compileAndRegister:'
public class test_fields_04c_caller {
    public static String getFoo(test_fields_04c x) {
        return x.foo;
    }
}'.

    jclass2 := self compileAndRegister:'
public class test_fields_04c { 
    public String bar = "bar";
}'.
    jinst2 := jclass2 new.



    self assert: jclass2 fields size == 1.

    self assert: (callerClass getFoo: jinst1) = 'bar+foo'.   
    self should: [ callerClass getFoo: jinst2 ] raise: Error.

    "Created: / 09-04-2013 / 15:57:38 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

test_fields_05a
    "
    1) compile simple class with one field
    2) change visibility of the field
    3) accessing the field should raise exception
    "

    | jclass1 jinst1 jclass2 jinst2 callerClass |

    jclass1 := self compileAndRegister:'
public class test_fields_05a { 
    public String foo = "public_foo";
}'.
    jinst1 := jclass1 new.

    jclass2 := self compileAndRegister:'
public class test_fields_05a { 
    private String foo = "private_foo";
}'.
    jinst2 := jclass2 new.

    callerClass := self compileAndRegister:'
public class CallerClass {
    public static Object getFoo(test_fields_05a inst) {
        return inst.foo;
    }
}'.

    self assert: (callerClass getFoo: jinst1) = 'public_foo'.
    self should:   [ callerClass getFoo: jinst2 ] raise: Error.

    "Created: / 09-04-2013 / 16:16:06 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 11-04-2013 / 10:40:26 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
!

test_fields_05b
    "
    1) compile simple class with private field
    2) change visibility of the field to public
    3) accessing the field should not raise exception anymore
    "

    | jclass1 jinst1 jclass2 jinst2 callerClass |

    jclass1 := self compileAndRegister:'
public class test_fields_05b { 
    private String foo = "private_foo";
}'.
    jinst1 := jclass1 new.

    jclass2 := self compileAndRegister:'
public class test_fields_05b { 
    public String foo = "public_foo";
}'.
    jinst2 := jclass2 new.

    callerClass := self compileAndRegister:'
public class CallerClass {
    public static Object getFoo(test_fields_05b inst) {
        return inst.foo;
    }
}'.

    self should: [ callerClass getFoo: jinst1 ] raise: Error.    
    self assert: (callerClass getFoo: jinst2) = 'public_foo'.

    "Created: / 11-04-2013 / 10:41:52 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
! !

!JavaSingleClassReloadingTests methodsFor:'tests - hierarchy'!

test_hierarchy_00

    "
    1) compile a parent and a child class, parent having a method
    2) change parent's method
    3) assert the method is changed in child
    "

    | jclass1_A jclass1_B jinst1_B jclass_caller jinst2_B jclass2_B |

    jclass1_A := self compileAndRegister:'
public class test_hierarchy_00_A { 
    public int foo() {
        return 1;
    }
}'.

     jclass1_B := self compileAndRegister:'
public class test_hierarchy_00_B extends test_hierarchy_00_A { 
}'.

    jclass_caller :=  self compileAndRegister:'
public class test_hierarchy_00_caller  {
    public int call(test_hierarchy_00_B b) {
        return b.foo();
    }
}'.

    jinst1_B := jclass1_B new.

    self assert: (jclass_caller new call: jinst1_B) == 1.

    jclass2_B := self compileAndRegister:'
public class test_hierarchy_00_B extends test_hierarchy_00_A { 
  public int foo() {
        return 2;
    }
}'.

    jinst2_B := jclass2_B new.

    self assert: (jclass_caller new call: jinst1_B) == 2.
    self assert: (jclass_caller new call: jinst2_B) == 2.

    "Created: / 09-04-2013 / 16:09:24 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 11-04-2013 / 11:24:33 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
!

test_hierarchy_01a

    "
    1) compile a parent and a child class
    2) add field to parent
    3) assert the field is present in new instances of child
    "

    | jclass1_A jclass1_B jinst1_B jclass_caller jinst2_B jclass2_B |

    jclass1_A := self compileAndRegister:'
public class test_hierarchy_01_A {}'.

     jclass1_B := self compileAndRegister:'
public class test_hierarchy_01_B extends test_hierarchy_01_A {}'.

    jinst1_B := jclass1_B new.    

    jclass2_B := self compileAndRegister:'
public class test_hierarchy_01_A {
    public int foo = 1;
}'.

    jclass_caller :=  self compileAndRegister:'
public class test_hierarchy_01_caller  {
    public int call(test_hierarchy_01_B b) {
        return b.foo;
    }
}'.

    jinst2_B := jclass2_B new.

    self should: [ jclass_caller new call: jinst1_B ] raise: Error.
    self assert: (jclass_caller new call: jinst2_B) == 1.

    "Created: / 11-04-2013 / 11:37:21 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
!

test_hierarchy_02a

    "
    1) compile a parent and a child class, parent having a method
    2) change parent's method
    3) assert the method is changed in child
    "

    | jclass1_A jclass1_B jclass1_C jinst1_C jclass_caller jinst2_C jclass2_B |

    jclass1_A := self compileAndRegister:'
public class test_hierarchy_02_A { 
    public int foo() {
        return 1;
    }
}'.

     jclass1_B := self compileAndRegister:'
public class test_hierarchy_02_B extends test_hierarchy_02_A {}'.

     jclass1_C := self compileAndRegister:'
public class test_hierarchy_02_C extends test_hierarchy_02_B {
    public int foo() {
        return super.foo();
    }
}'.

    jclass_caller :=  self compileAndRegister:'
public class test_hierarchy_02_caller  {
    public int call(test_hierarchy_02_C c) {
        return c.foo();
    }
}'.

    jinst1_C := jclass1_C new.

    self assert: (jclass_caller new call: jinst1_C) == 1.

    jclass2_B := self compileAndRegister:'
public class test_hierarchy_02_B extends test_hierarchy_02_A { 
  public int foo() {
        return 2;
    }
}'.

    jinst2_C := jclass1_C new.

    self assert: (jclass_caller new call: jinst1_C) == 2.
    self assert: (jclass_caller new call: jinst2_C) == 2.

    "Created: / 11-04-2013 / 11:07:26 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
!

test_hierarchy_02b

    "
    1) compile a parent and a child class, parent having a method
    2) change parent's method
    3) assert the method is changed in child
    "

    | jclass1_A jclass1_B jclass1_C jinst1_C jclass_caller jinst2_C jclass2_B |

    jclass1_A := self compileAndRegister:'
public class test_hierarchy_02_A { 
    public int foo() {
        return 1;
    }

    public int bar() {
        return 10;
    }
}'.

     jclass1_B := self compileAndRegister:'
public class test_hierarchy_02_B extends test_hierarchy_02_A {}'.

     jclass1_C := self compileAndRegister:'
public class test_hierarchy_02_C extends test_hierarchy_02_B {
    public int foo() {
        return super.foo();
    }

    public int bar() {
        return super.bar();
    }
}'.

    jclass_caller :=  self compileAndRegister:'
public class test_hierarchy_02_caller  {
    public int callFoo(test_hierarchy_02_C c) {
        return c.foo();
    }

    public int callBar(test_hierarchy_02_C c) {
        return c.bar();
    }
}'.

    jinst1_C := jclass1_C new.

    self assert: (jclass_caller new callFoo: jinst1_C) == 1.
    self assert: (jclass_caller new callBar: jinst1_C) == 10.

    jclass2_B := self compileAndRegister:'
public class test_hierarchy_02_B extends test_hierarchy_02_A { 
    public int bar() {
        return 20;
    }        
}'.

    jinst2_C := jclass1_C new.

    self assert: (jclass_caller new callFoo: jinst1_C) == 1.
    self assert: (jclass_caller new callFoo: jinst2_C) == 1.
    self assert: (jclass_caller new callBar: jinst1_C) == 20.
    self assert: (jclass_caller new callBar: jinst2_C) == 20.

    "Created: / 11-04-2013 / 11:07:49 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
!

test_hierarchy_03a

    "
    1) compile a parent and a child class, parent having a constructor
    2) change parent's constructor
    3) assert new instances of child use new constructor
    "

    | jclass1_A jclass1_B jclass1_C jinst1_C jclass_caller jinst2_C jclass2_B |

    jclass1_A := self compileAndRegister:'
public class test_hierarchy_03_A {
    protected int foo;

    public test_hierarchy_03_A(int foo) {
        this.foo = foo;
    }

    public int getFoo() {
        return foo;
    }
}'.

     jclass1_B := self compileAndRegister:'
public class test_hierarchy_03_B extends test_hierarchy_03_A {}'.

     jclass1_C := self compileAndRegister:'
public class test_hierarchy_03_C extends test_hierarchy_03_B {
    public test_hierarchy_03_C(int foo) {
        super(foo);
    }    
}'.

    jclass_caller :=  self compileAndRegister:'
public class test_hierarchy_03_caller  {
    public int getFoo(test_hierarchy_03_C c) {
        return c.getFoo();
    }
}'.

    jinst1_C := jclass1_C new: 1.

    self assert: (jclass_caller new getFoo: jinst1_C) == 1.

    jclass2_B := self compileAndRegister:'
public class test_hierarchy_03_B extends test_hierarchy_03_A { 
    public test_hierarchy_03_B(int foo) {
        this.foo = 2 * foo;
    }        
}'.

    jinst2_C := jclass1_C new: 2.

    self assert: (jclass_caller new getFoo: jinst1_C) == 1.
    self assert: (jclass_caller new getFoo: jinst2_C) == 4.

    "Created: / 11-04-2013 / 11:14:12 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
!

test_hierarchy_04a

    "
    1) compile a parent and a child class, parent having a field
    2) change child's superclass to parent2, having different field
    3) assert new instances of child have new field
    "

    | jclass1_A1 jclass1_A2 jclass1_B jclass2_B jinst1_B jinst2_B jclass_caller |

    jclass1_A1 := self compileAndRegister:'
public class test_hierarchy_04_A1 {
    public int foo = 1;
}'.

    jclass1_A2 := self compileAndRegister:'
public class test_hierarchy_04_A2 {
    public String foo = "foo";
}'.

     jclass1_B := self compileAndRegister:'
public class test_hierarchy_04_B extends test_hierarchy_04_A1 {}'.     

    jclass_caller :=  self compileAndRegister:'
public class test_hierarchy_04_caller  {
    public Object getFoo(test_hierarchy_04_B c) {
        return c.foo;
    }
}'.

    jinst1_B := jclass1_B new.

    self assert: (jclass_caller new getFoo: jinst1_B) == 1.

    jclass2_B := self compileAndRegister:'
public class test_hierarchy_04_B extends test_hierarchy_04_A2 {}'.

    jinst2_B := jclass1_B new.

    self assert: (jclass_caller new getFoo: jinst1_B) = 1.
    self assert: (jclass_caller new getFoo: jinst2_B) = 'foo'.

    "Created: / 11-04-2013 / 13:16:03 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
! !

!JavaSingleClassReloadingTests methodsFor:'tests - 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>"
!

test_03a
    "
    1) compile simple class with one public method that returns 10
    2) call that method indirectly, check that it returns 10
    3) change it to private method
    4) calling method should raise exception
    "

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

    jclass2 := self compileAndRegister:'
public class test_03 { 
    private static int foo() { 
        return 20; 
    }
}'.
    self should: [ caller new qux ] raise: Error.

    "Created: / 11-04-2013 / 10:44:18 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
    "Modified: / 14-04-2013 / 15:22:18 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
!

test_03b
    "
    1) compile simple class with one private method that returns 10
    2) call that method indirectly, check that it raises exception
    3) change it to public method
    4) calling method should not raise exception anymore
    "

    | jclass1 jclass2 caller |
    self compileAndRegister:'
public class test_03 { 
    public static int foo() {
        return 10; 
    }
}'.
    caller := self compileAndRegister:'
public class test_03_caller {
    public int qux() {
        return test_03.foo();
    }
}
'.
    jclass1 := self compileAndRegister:'
public class test_03 { 
    private static int foo() {
        return 10; 
    }
}'.

    self should: [ caller new qux ] raise: Error.

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

    "Created: / 11-04-2013 / 10:46:31 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
    "Modified: / 14-04-2013 / 15:23:25 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
! !

!JavaSingleClassReloadingTests methodsFor:'tests - overloads'!

test_overloads_00a
    "
    1) compile simple class with one method taking Object arg
    3) compile caller class passing in String
    4) recompile simple class, overload the method with one taking String arg
    5) assert caller class invokes the String method
    "

    | jclass1 jclass2 callerClass jinst1 jinst2 |
    jclass1 := self compileAndRegister:'
public class test_overloads_00 { 
    public int foo(Object o) { 
        return 1; 
    }
}'.    

    callerClass := self compileAndRegister:'
public class CallerClass { 
    public int call(test_overloads_00 o) { 
        return o.foo("foo"); 
    }
}'.
    jinst1 := jclass1 new.

    self assert: (callerClass new call: jinst1) = 1.

    jclass2 := self compileAndRegister:'
public class test_overloads_00 { 
    public int foo(Object o) { 
        return 1; 
    }

    public int foo(String s) { 
        return 2; 
    }
}'. 
    jinst2 := jclass2 new.

    self assert: (callerClass new call: jinst1) = 2.
    self assert: (callerClass new call: jinst2) = 2.

    "Created: / 11-04-2013 / 13:29:44 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
!

test_overloads_00b
    "
    1) compile simple class with two methods taking Object and String arg
    3) compile caller class passing in String
    4) recompile simple class, remove overloaded method taking String arg
    5) assert caller class invokes the Object method
    "

    | jclass1 jclass2 callerClass jinst1 jinst2 |
    jclass1 := self compileAndRegister:'
public class test_overloads_00 { 
    public int foo(Object o) { 
        return 1; 
    }

    public int foo(String s) { 
        return 2; 
    }
}'.    

    callerClass := self compileAndRegister:'
public class CallerClass { 
    public int call(test_overloads_00 o) { 
        return o.foo("foo"); 
    }
}'.
    jinst1 := jclass1 new.

    self assert: (callerClass new call: jinst1) = 2.

    jclass2 := self compileAndRegister:'
public class test_overloads_00 { 
    public int foo(Object o) { 
        return 1; 
    }
}'. 
    jinst2 := jclass2 new.

    self assert: (callerClass new call: jinst1) = 1.
    self assert: (callerClass new call: jinst2) = 1.

    "Created: / 11-04-2013 / 13:30:38 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
!

test_overloads_01a
    "
    1) compile simple class with one method taking Object arg
    3) compile caller class passing in String
    4) recompile simple class, overload the method with one taking String arg
    5) assert caller class invokes the String method
    "

    | jclass1A jclass1B jclass2A callerClass jinst1 jinst2 |
    jclass1A := self compileAndRegister:'
public class test_overloads_01_A { 
    public int foo(Object o) { 
        return 1; 
    }
}'.    

    jclass1B := self compileAndRegister:'
public class test_overloads_01_B extends test_overloads_01_A {
    public int foo(String foo) {
        return super.foo(foo) + 10;
    }
}'.

    callerClass := self compileAndRegister:'
public class CallerClass { 
    public int call(test_overloads_01_B o) { 
        return o.foo("foo"); 
    }
}'.
    jinst1 := jclass1B new.

    self assert: (callerClass new call: jinst1) = 11.

    jclass2A := self compileAndRegister:'
public class test_overloads_01_A { 
    public int foo(Object o) { 
        return 1; 
    }

    public int foo(String s) { 
        return 2; 
    }
}'. 
    jinst2 := jclass2A new.

    self assert: (callerClass new call: jinst1) = 12.
    self assert: (callerClass new call: jinst2) = 12.

    "Created: / 11-04-2013 / 13:34:59 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
!

test_overloads_01b
    "
    1) compile simple class with two overloaded methods taking Object and String arg
    3) compile caller class passing in String
    4) recompile simple class, remove overloaded method aking String arg
    5) assert caller class invokes the Object method
    "

    | jclass1A jclass1B jclass2A callerClass jinst1 jinst2 |
    jclass1A := self compileAndRegister:'
public class test_overloads_01_A { 
    public int foo(Object o) { 
        return 1; 
    }    

    public int foo(String s) { 
        return 2; 
    }
}'.    

    jclass1B := self compileAndRegister:'
public class test_overloads_01_B extends test_overloads_01_A {
    public int foo(String foo) {
        return super.foo(foo) + 10;
    }
}'.

    callerClass := self compileAndRegister:'
public class CallerClass { 
    public int call(test_overloads_01_B o) { 
        return o.foo("foo"); 
    }
}'.
    jinst1 := jclass1B new.

    self assert: (callerClass new call: jinst1) = 12.

    jclass2A := self compileAndRegister:'
public class test_overloads_01_A { 
    public int foo(Object o) { 
        return 1; 
    } 
}'. 
    jinst2 := jclass2A new.

    self assert: (callerClass new call: jinst1) = 11.
    self assert: (callerClass new call: jinst2) = 11.

    "Created: / 11-04-2013 / 13:35:15 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
! !

!JavaSingleClassReloadingTests methodsFor:'tests - restart'!

test_restart_01
    "
    This is a tricky test. It compiles a class with errorneous 
    method anr run it. It raises JavaUnresolvedCompilationProblem which
    is catched and, while the errorneous method is still running (i.e.,
    it is still on the stack) it fixes the code. Then it restarts
    the context of errorneous method and checks, whether a new
    (fixed) code is executed. This requires some support in the VM."

    | source1 source2 inst gotError retval |
    source1 := '
    public class Foo {
        public int foo() {
            return true;
        }
    }
'.

    source2 := '
    public class Foo {
        public int foo() {
            return 100;
        }
    }
'.


    JavaCompiler compile: source1 register: true notifying: nil.

    gotError := false.
    inst := JAVA Foo new.

    [
        retval := inst foo.
    ] on: JavaUnresolvedCompilationError do:[:ex|
        gotError ifFalse:[
            "First error - fix the method"
            self assert: ex suspendedContext sender selector == #'foo()I'.
            gotError := true.
            JavaCompiler compile: source2 register: true notifying: nil.
            ObjectMemory debugBreakPoint3.
            ex suspendedContext sender unwindAndRestart.
        ] ifTrue:[
            self assert: false. "/ did not executed new code...
        ].
    ].
    self assert: retval == 100.

    "Created: / 19-04-2013 / 10:28:20 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!JavaSingleClassReloadingTests class methodsFor:'documentation'!

version_CVS
    ^ '$Header: /cvs/stx/stx/libjava/experiments/SingleClassReloadingTests.st,v 1.2 2013-02-25 11:15:34 vrany Exp $'
!

version_HG

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

version_SVN
    ^ '§Id::                                                                                                                        §'
! !