JavaLookupTests.st
branchcvs_MAIN
changeset 3360 1a8899091305
parent 3359 cb6c15e66bed
child 3412 df11bb428463
--- a/JavaLookupTests.st	Fri Feb 14 14:27:26 2014 +0100
+++ b/JavaLookupTests.st	Wed Jan 28 03:12:08 2015 +0100
@@ -1,6 +1,6 @@
 "
  Copyright (c) 2010-2011 Jan Vrany, Jan Kurs & Marcel Hlopko,
-                         SWING Research Group, Czech Technical University 
+                         SWING Research Group, Czech Technical University
                          in Prague
 
  Permission is hereby granted, free of charge, to any person
@@ -26,6 +26,8 @@
 "
 "{ Package: 'stx:libjava' }"
 
+"{ NameSpace: Smalltalk }"
+
 TestCase subclass:#JavaLookupTests
 	instanceVariableNames:''
 	classVariableNames:''
@@ -38,7 +40,7 @@
 copyright
 "
  Copyright (c) 2010-2011 Jan Vrany, Jan Kurs & Marcel Hlopko,
-                         SWING Research Group, Czech Technical University 
+                         SWING Research Group, Czech Technical University
                          in Prague
 
  Permission is hereby granted, free of charge, to any person
@@ -85,11 +87,11 @@
 !JavaLookupTests class methodsFor:'resources'!
 
 resources
-    ^ Array with: JavaLookupTestsResource.
+    ^ Array with: JavaTestsResource.
 
     "Created: / 11-04-2011 / 19:38:11 / kursjan <kursjan@fit.cvut.cz>"
     "Modified: / 29-08-2011 / 23:35:06 / Jan Kurs <kursjan@fit.cvut.cz>"
-    "Modified: / 29-10-2012 / 07:47:38 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 19-03-2014 / 11:27:12 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
 !JavaLookupTests methodsFor:'running'!
@@ -97,57 +99,134 @@
 setUp
     | md |
 
-    md := self javaTestClass methodDictionary.
-    md copy keysAndValuesDo:[:sel :m|
-        m isProxyMethod ifTrue:[md removeKey: sel]
+    Smalltalk loadPackage: 'stx:libjava/experiments'.
+    self javaTestClasses do:[:each | 
+        md := each methodDictionary.
+        md copy keysAndValuesDo:[:sel :m|
+            m isProxyMethod ifTrue:[md removeKey: sel]
+        ].
     ].
+    self personManipulatorClass theClass constantPool invalidate.
 
     "Created: / 03-12-2012 / 17:50:29 / Jan Vrany <jan.vrany@fit.cvut.cz>"
     "Modified: / 16-12-2012 / 13:35:52 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
+    "Modified: / 19-03-2014 / 13:47:55 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
 !JavaLookupTests methodsFor:'support'!
 
 javaTestClass
-    ^ JAVA cz cvut fit swing methodLookup Object.
+    ^ JAVA stx libjava tests mocks interop Object.
 
     "Created: / 05-09-2011 / 20:13:48 / Jan Kurs <kursjan@fit.cvut.cz>"
     "Modified: / 30-11-2012 / 22:16:24 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
+    "Modified: / 19-03-2014 / 11:28:08 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+javaTestClasses
+    "raise an error: this method should be implemented (TODO)"
+
+    ^ OrderedCollection streamContents:[:s|
+        s
+            nextPut: self javaTestClass;
+            nextPut: JAVA stx libjava tests mocks interop Person theClass.
+    ]
+
+    "Created: / 19-03-2014 / 13:45:42 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 personManipulatorClass
     "Person Manipulator can interact with Person class defined in Java"
-    
-    ^ JAVA cz cvut fit swing methodLookup PersonManipulator.
+
+    ^ JAVA stx libjava tests mocks interop PersonManipulator.
 
     "Created: / 06-09-2011 / 21:48:16 / Jan Kurs <kursjan@fit.cvut.cz>"
     "Modified: / 30-11-2012 / 22:16:19 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
+    "Modified: / 19-03-2014 / 11:28:14 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 setManipulatorClass
     "Person Manipulator can interact with Person class defined in Java"
-    
-    ^ JAVA cz cvut fit swing methodLookup SetManipulator.
+
+    ^ JAVA stx libjava tests mocks interop SetManipulator.
 
     "Created: / 25-09-2011 / 16:41:02 / Jan Kurs <kursjan@fit.cvut.cz>"
     "Modified: / 30-11-2012 / 22:13:34 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
+    "Modified: / 19-03-2014 / 11:28:21 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+! !
+
+!JavaLookupTests methodsFor:'tests - java extensions'!
+
+test_java_extension_01
+
+    Compiler compile: 'fooPerson ^ 0' forClass: JAVA stx libjava tests mocks interop Person theClass.
+    self assert: JAVA stx libjava tests mocks interop Person new fooPerson == 0.
+
+    "Created: / 04-10-2013 / 15:07:52 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 19-03-2014 / 11:28:38 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+test_java_extension_02_1
+
+    Compiler compile: 'fooIPerson1 ^ 1' forClass: JAVA stx libjava tests mocks interop IPerson1 theClass.
+    self assert: JAVA stx libjava tests mocks interop Person new fooIPerson1 == 1.
+
+    "Created: / 04-10-2013 / 15:13:06 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 19-03-2014 / 11:28:48 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+test_java_extension_02_2
+
+    Compiler compile: 'fooIPerson2 ^ 2' forClass: JAVA stx libjava tests mocks interop IPerson2 theClass.
+    self assert: JAVA stx libjava tests mocks interop Person new fooIPerson2 == 2
+
+    "Created: / 04-10-2013 / 15:12:56 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 19-03-2014 / 11:28:57 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+test_java_extension_03
+
+    Compiler compile: 'fooIPersonSuper ^ 3' forClass: JAVA stx libjava tests mocks interop IPersonSuper theClass.
+    self assert: JAVA stx libjava tests mocks interop Person new fooIPersonSuper == 3
+
+    "Created: / 04-10-2013 / 15:11:20 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 19-03-2014 / 11:29:17 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+test_java_extension_04
+
+    Compiler compile: 'fooIPerson ^ 3' forClass: JAVA stx libjava tests mocks interop IPerson1 theClass.
+    Compiler compile: 'fooIPerson ^ 4' forClass: JAVA stx libjava tests mocks interop IPerson2 theClass.
+    self should:[self assert: JAVA stx libjava tests mocks interop Person new fooIPerson] raise: Object ambiguousMessageSignal
+
+    "Created: / 04-10-2013 / 15:43:25 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 19-03-2014 / 11:29:30 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+test_java_extension_05
+
+    Compiler compile: 'fooIPerson1_05 ^ 50' forClass: JAVA stx libjava tests mocks interop IPerson1 theClass.
+    self assert: JAVA stx libjava tests mocks interop Director new fooIPerson1_05 == 50.
+
+    "Created: / 04-10-2013 / 20:09:58 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 19-03-2014 / 11:29:40 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
 !JavaLookupTests methodsFor:'tests - java lookup'!
 
 test3
     "I am not sure with this test :)"
-    self shouldnt: [(Java classForName: 'cz.cvut.fit.swing.methodLookup.Object') new isNumber] raise: Exception.
-    self assert: (Java classForName: 'cz.cvut.fit.swing.methodLookup.Object') new isNumber = false.
+    self shouldnt: [(Java classForName: 'stx.libjava.tests.mocks.interop.Object') new isNumber] raise: Exception.
+    self assert: (Java classForName: 'stx.libjava.tests.mocks.interop.Object') new isNumber = false.
 
     "Created: / 11-04-2011 / 19:43:37 / kursjan <kursjan@fit.cvut.cz>"
 !
 
 testBasic
-    self 
+    self
         shouldnt:[ (Java classForName:'java.lang.Object') new hash ]
         raise:Exception.
-    self 
+    self
         shouldnt:[ (Java classForName:'java.lang.Object') new toString ]
         raise:Exception.
 
@@ -205,7 +284,7 @@
 !
 
 testDoesNotUnderstand
-    self 
+    self
         should:[ (Java classForName:'java.lang.Object') new bafickyBaf ]
         raise:MessageNotUnderstood.
 
@@ -214,8 +293,8 @@
 !
 
 testInterfaceMethodsWithNarrowerReturnType
-    "when a class A implements an interface I, and overridden method has narrower 
-    return type (e.g. method in I should return java.lang.Object, but overridden 
+    "when a class A implements an interface I, and overridden method has narrower
+    return type (e.g. method in I should return java.lang.Object, but overridden
     method in A declares return type of java.lang.String), java compiler generates
     synthetic method with the same name and return type declared in I, which just
     delegates to overridden method. This situation should not be considered
@@ -228,8 +307,8 @@
 
 testMultipleParameters
     "test multiple parameters"
-    
-    self 
+
+    self
         assert: (self javaTestClass new foo) = 0
 
     "Created: / 11-04-2011 / 19:48:54 / kursjan <kursjan@fit.cvut.cz>"
@@ -238,8 +317,8 @@
 
 testMultipleParameters1
     "test multiple parameters"
-    
-    self 
+
+    self
         assert: (self javaTestClass new foo: 1) = 1
 
     "Created: / 11-04-2011 / 19:48:54 / kursjan <kursjan@fit.cvut.cz>"
@@ -248,8 +327,8 @@
 
 testMultipleParameters2
     "test multiple parameters"
-    
-    self 
+
+    self
         assert: (self javaTestClass new foo: 1 and: 2) = 2
 
     "Created: / 11-04-2011 / 19:48:54 / kursjan <kursjan@fit.cvut.cz>"
@@ -275,7 +354,7 @@
 
     self assert: false message:'Solution wanted'
 
-"/    self 
+"/    self
 "/        should:[r := self javaTestClass new overloadedMethod: nil]
 "/        raise: AmbiguousMessage
 
@@ -316,6 +395,93 @@
     "Created: / 11-04-2011 / 20:02:54 / kursjan <kursjan@fit.cvut.cz>"
     "Modified: / 05-09-2011 / 21:37:53 / Jan Kurs <kursjan@fit.cvut.cz>"
     "Modified (format): / 03-12-2012 / 17:45:23 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+testTypeOverloading_02
+    "Tests overloaded method resulution"
+
+    | director |
+
+    director := JAVA stx libjava tests mocks interop Director new.
+    director setTag1: 1.
+    self assert: (director instVarNamed: #tag1) class == JAVA java lang Integer.
+    self assert: ((director instVarNamed: #tag1) instVarNamed: #value) == 11.
+
+    "Created: / 19-03-2014 / 12:39:24 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+testTypeOverloading_03
+    "Tests overloaded method resulution. Scenario:
+
+    1) create an instance of Person
+    2) send it #setTag1: JAVA java lang Object new.
+       This creates a proxy #setTag1: that should call
+       setTag1(Ljava/lang/Object;)V
+    3) create an instance of TheOneAndOnlyBoss
+    4) send it #setTag1: JAVA java lang Object new.
+       This should send TheOneAndOnlyBoss#setTag1(I)V"
+
+    | person tag boss |
+
+    self assert: (Java classNamed:'stx.libjava.tests mocks.interop.TheOneAndOnlyBoss') isNil.
+    person := JAVA stx libjava tests mocks interop Person new.
+    tag := JAVA java lang Object new.
+    person setTag1: tag.
+    self assert: (person instVarNamed: #tag1) == tag.
+
+    boss := JAVA stx libjava tests mocks interop TheOneAndOnlyBoss new.   
+    boss setTag1: 1.
+    self assert: (boss instVarNamed: #tag1) class == JAVA java lang Integer.
+    self assert: ((boss instVarNamed: #tag1) instVarNamed: #value) == 1001.
+
+    "
+    JavaClassReloader unload: (Java classNamed:'stx.libjava.tests mocks.interop.TheOneAndOnlyBoss')
+    "
+
+    "Created: / 19-03-2014 / 13:31:29 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 19-03-2014 / 17:15:19 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+! !
+
+!JavaLookupTests methodsFor:'tests - java lookup - ctors'!
+
+test_ctor01_Integer_a
+    | o |
+
+    o :=  self javaTestClass new: 1.
+    self assert: (o instVarNamed: #eventsTally) == 1.
+    self assert: (o instVarNamed: #events) first = 'Integer Constructor'.
+
+    "Created: / 02-12-2013 / 23:39:11 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+test_ctor01_Integer_b
+    | o |
+
+    o :=  self javaTestClass new: (JAVA java lang Integer new: 1).
+    self assert: (o instVarNamed: #eventsTally) == 1.
+    self assert: (o instVarNamed: #events) first = 'Integer Constructor'.
+
+    "Created: / 02-12-2013 / 23:39:33 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+test_ctor01_String
+    | o |
+
+    o :=  self javaTestClass new: 'String'.
+    self assert: (o instVarNamed: #eventsTally) == 1.
+    self assert: (o instVarNamed: #events) first = 'String Constructor'.
+
+    "Created: / 02-12-2013 / 23:38:25 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+test_ctor01_default
+    | o |
+
+    o :=  self javaTestClass new.
+    self assert: (o instVarNamed: #eventsTally) == 1.
+    self assert: (o instVarNamed: #events) first = 'Default Constructor'.
+
+    "Created: / 02-12-2013 / 23:34:38 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
 !JavaLookupTests methodsFor:'tests - obsolete'!
@@ -402,11 +568,18 @@
     person name: 'Anne'.
     person age: 18.
 
-    self 
-        assert:(self personManipulatorClass new getPersonAge: person) = 18.
+    "/ !!!!!! The three assertions in a row is by purpose !!!!!!
+    "/ Do not remove them. The reason is that with JIT on,
+    "/ the JIT compiler recompiles method each time a reference
+    "/ is resolved. This should cover different code paths.
+
+    self assert:(self personManipulatorClass new getPersonAge: person) = 18.
+    self assert:(self personManipulatorClass new getPersonAge: person) = 18.
+    self assert:(self personManipulatorClass new getPersonAge: person) = 18.
 
     "Created: / 11-04-2011 / 20:02:54 / kursjan <kursjan@fit.cvut.cz>"
     "Created: / 25-09-2011 / 17:27:45 / Jan Kurs <kursjan@fit.cvut.cz>"
+    "Modified (comment): / 20-01-2014 / 10:11:05 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 testPersonManipulator_getName
@@ -417,17 +590,24 @@
     person name: 'Anne'.
     person age: 18.
 
-    self 
-        assert:(self personManipulatorClass new getPersonName: person) = 'Anne'.
+    "/ !!!!!! The three assertions in a row is by purpose !!!!!!
+    "/ Do not remove them. The reason is that with JIT on,
+    "/ the JIT compiler recompiles method each time a reference
+    "/ is resolved. This should cover different code paths.
+
+    self assert:(self personManipulatorClass new getPersonName: person) = 'Anne'.
+    self assert:(self personManipulatorClass new getPersonName: person) = 'Anne'.
+    self assert:(self personManipulatorClass new getPersonName: person) = 'Anne'.
 
     "Created: / 11-04-2011 / 20:02:54 / kursjan <kursjan@fit.cvut.cz>"
     "Created: / 25-09-2011 / 17:27:34 / Jan Kurs <kursjan@fit.cvut.cz>"
+    "Modified: / 20-01-2014 / 10:11:20 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
 !JavaLookupTests class methodsFor:'documentation'!
 
 version_CVS
-    ^ '$Header: /cvs/stx/stx/libjava/JavaLookupTests.st,v 1.3 2013-09-06 00:41:24 vrany Exp $'
+    ^ '$Header: /cvs/stx/stx/libjava/JavaLookupTests.st,v 1.4 2015-01-28 02:10:50 vrany Exp $'
 !
 
 version_HG
@@ -436,6 +616,6 @@
 !
 
 version_SVN
-    ^ '§Id§'
+    ^ 'Id'
 ! !