src/JavaMethodRefTests.st
author hlopkmar
Thu, 12 May 2011 14:28:39 +0000
branchjk_new_structure
changeset 773 ff7dbe5581a3
parent 771 fb8026dda011
child 815 f98e7173ef67
permissions -rw-r--r--
cleaned tests class hierarchy, small fixes

"{ Package: 'stx:libjava' }"

AbstractJavaTestCase subclass:#JavaMethodRefTests
	instanceVariableNames:''
	classVariableNames:''
	poolDictionaries:''
	category:'Languages-Java-Tests-RuntimeConstantPool'
!


!JavaMethodRefTests methodsFor:'permission tests'!

testAccessingPPFromOutside
    | javaClassRef  javaMethodRef  initString  throwedException |

    self enableMockedExceptionThrowing.
    
    [ initString := 'Lstx/libjava/tests/mocks/PublicClass;'.
    javaClassRef := self getClassRefFor: initString.
    javaClassRef owner: (Java classForName: 'java.lang.Object').
    javaMethodRef := JavaMethodRef2 in: nil
                namedAndTyped: (self getName: 'packagePrivateMethod'
                        descriptor: '()Ljava/lang/String;')
                inClassIdentifiedByRef: javaClassRef.
    javaMethodRef resolve. ] on: Error
            do: [:e | throwedException := e ].
    self assertTrue: (throwedException notNil 
                and: [ throwedException messageText = 'IllegalAccessError' ]).
    self disableMockedExceptionThrowing.

    "Created: / 14-04-2011 / 15:10:35 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
    "Modified: / 10-05-2011 / 16:23:09 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
!

testAccessingPPFromSubclass
    | javaClassRef  javaMethodRef  initString  throwedException |

    self enableMockedExceptionThrowing.
    
    [ initString := 'Lstx/libjava/tests/mocks/PublicClass;'.
    javaClassRef := self getClassRefFor: initString.
    javaClassRef 
        owner: (Java classForName: 'stx.libjava.tests.mocks.SubclassOfPublicClass').
    javaMethodRef := JavaMethodRef2 in: nil
                namedAndTyped: (self getName: 'packagePrivateMethod'
                        descriptor: '()Ljava/lang/String;')
                inClassIdentifiedByRef: javaClassRef.
    javaMethodRef resolve. ] on: Error
            do: [:e | throwedException := e ].
    self assertTrue: (throwedException isNil).
    self disableMockedExceptionThrowing.

    "Created: / 14-04-2011 / 15:10:55 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
    "Modified: / 10-05-2011 / 16:23:12 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
!

testAccessingPrivateFromOutside
    | javaClassRef  javaMethodRef  initString  throwedException |

    self enableMockedExceptionThrowing.
    
    [ initString := 'Lstx/libjava/tests/mocks/PublicClass;'.
    javaClassRef := self getClassRefFor: initString.
    javaClassRef owner: (Java classForName: 'java.lang.Object').
    javaMethodRef := JavaMethodRef2 in: nil
                namedAndTyped: (self getName: 'privateMethod'
                        descriptor: '()Ljava/lang/String;')
                inClassIdentifiedByRef: javaClassRef.
    javaMethodRef resolve. ] on: Error
            do: [:e | throwedException := e ].
    self assertTrue: (throwedException notNil 
                and: [ throwedException messageText = 'IllegalAccessError' ]).
    self disableMockedExceptionThrowing.

    "Created: / 13-04-2011 / 14:44:48 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
    "Modified: / 10-05-2011 / 16:23:14 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
!

testAccessingPrivateFromOutsideInNonPublic
    | javaClassRef  javaMethodRef  initString  throwedException |

    self enableMockedExceptionThrowing.
    
    [ initString := 'Lstx/libjava/tests/mocks/NonPublicClass;'.
    javaClassRef := self getClassRefFor: initString.
    javaClassRef owner: (Java classForName: 'java.lang.Object').
    javaMethodRef := JavaMethodRef2        in: nil
                namedAndTyped: (self getName: 'privateMethod'
                        descriptor: '()Ljava/lang/String;')
                inClassIdentifiedByRef: javaClassRef.
    javaMethodRef resolve. ] on: Error
            do: [:e | throwedException := e ].
    self assertTrue: (throwedException notNil 
                and: [ throwedException messageText = 'IllegalAccessError' ]).
    self disableMockedExceptionThrowing.

    "Created: / 13-04-2011 / 14:47:44 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
    "Modified: / 10-05-2011 / 16:23:17 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
!

testAccessingPrivateFromSubclass
    | javaClassRef  javaMethodRef  initString  throwedException |

    self enableMockedExceptionThrowing.
    
    [ initString := 'Lstx/libjava/tests/mocks/PublicClass;'.
    javaClassRef := self getClassRefFor: initString.
    javaClassRef owner: (Java classForName: 'java.lang.SubclassOfPublicClass').
    javaMethodRef := JavaMethodRef2               in: nil
                namedAndTyped: (self getName: 'privateMethod'
                        descriptor: '()Ljava/lang/String;')
                inClassIdentifiedByRef: javaClassRef.
    javaMethodRef resolve. ] on: Error
            do: [:e | throwedException := e ].
    self assertTrue: (throwedException notNil 
                and: [ throwedException messageText = 'IllegalAccessError' ]).
    self disableMockedExceptionThrowing.

    "Created: / 13-04-2011 / 14:49:32 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
    "Modified: / 10-05-2011 / 16:23:19 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
!

testAccessingProtectedFromOutside
    | javaClassRef  javaMethodRef  initString  throwedException |

    self enableMockedExceptionThrowing.
    
    [ initString := 'Lstx/libjava/tests/mocks/PublicClass;'.
    javaClassRef := self getClassRefFor: initString.
    javaClassRef owner: (Java classForName: 'java.lang.Object').
    javaMethodRef := JavaMethodRef2                      in: nil
                namedAndTyped: (self getName: 'protectedMethod'
                        descriptor: '()Ljava/lang/String;')
                inClassIdentifiedByRef: javaClassRef.
    javaMethodRef resolve. ] on: Error
            do: [:e | throwedException := e ].
    self assertTrue: (throwedException notNil 
                and: [ throwedException messageText = 'IllegalAccessError' ]).
    self disableMockedExceptionThrowing.

    "Created: / 13-04-2011 / 14:44:48 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
    "Modified: / 10-05-2011 / 16:23:21 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
!

testAccessingProtectedFromOutsideInNonPublic
    | javaClassRef  javaMethodRef  initString  throwedException |

    self enableMockedExceptionThrowing.
    
    [ initString := 'Lstx/libjava/tests/mocks/NonPublicClass;'.
    javaClassRef := self getClassRefFor: initString.
    javaClassRef owner: (Java classForName: 'java.lang.Object').
    javaMethodRef := JavaMethodRef2                             in: nil
                namedAndTyped: (self getName: 'protectedMethod'
                        descriptor: '()Ljava/lang/String;')
                inClassIdentifiedByRef: javaClassRef.
    javaMethodRef resolve. ] on: Error
            do: [:e | throwedException := e ].
    self assertTrue: (throwedException notNil 
                and: [ throwedException messageText = 'IllegalAccessError' ]).
    self disableMockedExceptionThrowing.

    "Created: / 13-04-2011 / 14:47:54 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
    "Modified: / 10-05-2011 / 16:23:22 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
!

testAccessingProtectedFromPackage
    | javaClassRef  javaMethodRef  initString  throwedException |

    self enableMockedExceptionThrowing.
    
    [ initString := 'Lstx/libjava/tests/mocks/PublicClass;'.
    javaClassRef := self getClassRefFor: initString.
    javaClassRef owner: (Java classForName: 'stx.libjava.tests.mocks.Crate').
    javaMethodRef := JavaMethodRef2                                    in: nil
                namedAndTyped: (self getName: 'protectedMethod'
                        descriptor: '()Ljava/lang/String;')
                inClassIdentifiedByRef: javaClassRef.
    javaMethodRef resolve. ] on: Error
            do: [:e | throwedException := e ].
    self assertTrue: (throwedException isNil).
    self disableMockedExceptionThrowing.

    "Created: / 14-04-2011 / 15:09:41 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
    "Modified: / 10-05-2011 / 16:23:24 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
!

testAccessingProtectedFromSubclass
    | javaClassRef  javaMethodRef  initString  throwedException |

    self enableMockedExceptionThrowing.
    
    [ initString := 'Lstx/libjava/tests/mocks/PublicClass;'.
    javaClassRef := self getClassRefFor: initString.
    javaClassRef 
        owner: (Java classForName: 'stx.libjava.tests.mocks.SubclassOfPublicClass').
    javaMethodRef := JavaMethodRef2 in: nil
                namedAndTyped: (self getName: 'protectedMethod'
                        descriptor: '()Ljava/lang/String;')
                inClassIdentifiedByRef: javaClassRef.
    javaMethodRef resolve. ] on: Error
            do: [:e | throwedException := e ].
    self assertTrue: (throwedException isNil).
    self disableMockedExceptionThrowing.

    "Created: / 13-04-2011 / 14:49:23 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
    "Modified: / 10-05-2011 / 16:23:27 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
!

testAccessingPublic
    | javaClassRef  javaMethodRef  initString |

    self enableMockedExceptionThrowing.
    self shouldnt: 
            [ initString := 'Lstx/libjava/tests/mocks/PublicClass;'.
            javaClassRef := self getClassRefFor: initString.
            javaClassRef owner: (Java classForName: 'java.lang.Object').
            javaMethodRef := JavaMethodRef2 in: nil
                        namedAndTyped: (self getName: 'publicMethod'
                                descriptor: '()Ljava/lang/String;')
                        inClassIdentifiedByRef: javaClassRef.
            javaMethodRef resolve. ]
        raise: Error.
    self disableMockedExceptionThrowing.

    "Created: / 13-04-2011 / 14:44:13 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
    "Modified: / 10-05-2011 / 16:23:31 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
!

testAccessingPublicFromOutside
    | javaClassRef  javaMethodRef  initString |

    self enableMockedExceptionThrowing.
    self shouldnt: 
            [ initString := 'Lstx/libjava/tests/mocks/PublicClass;'.
            javaClassRef := self getClassRefFor: initString.
            javaClassRef owner: (Java classForName: 'java.lang.Object').
            javaMethodRef := JavaMethodRef2        in: nil
                        namedAndTyped: (self getName: 'publicMethod'
                                descriptor: '()Ljava/lang/String;')
                        inClassIdentifiedByRef: javaClassRef.
            javaMethodRef resolve. ]
        raise: Error.
    self disableMockedExceptionThrowing.

    "Created: / 13-04-2011 / 14:44:31 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
    "Modified: / 10-05-2011 / 16:23:33 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
!

testAccessingPublicFromOutsideInNonPublic
    | javaClassRef  javaMethodRef  initString  throwedException |

    self enableMockedExceptionThrowing.
    
    [ initString := 'Lstx/libjava/tests/mocks/NonPublicClass;'.
    javaClassRef := self getClassRefFor: initString.
    javaClassRef owner: (Java classForName: 'java.lang.Object').
    javaMethodRef := JavaMethodRef2                       in: nil
                namedAndTyped: (self getName: 'publicMethod'
                        descriptor: '()Ljava/lang/String;')
                inClassIdentifiedByRef: javaClassRef.
    javaMethodRef resolve. ] on: Error
            do: [:e | throwedException := e ].
    self assertTrue: (throwedException notNil 
                and: [ throwedException messageText = 'IllegalAccessError' ]).
    self disableMockedExceptionThrowing.

    "Created: / 13-04-2011 / 14:48:05 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
    "Modified: / 10-05-2011 / 16:23:35 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
!

testAccessingPublicFromSubclass
    | javaClassRef  javaMethodRef  initString  throwedException |

    self enableMockedExceptionThrowing.
    
    [ initString := 'Lstx/libjava/tests/mocks/PublicClass;'.
    javaClassRef := self getClassRefFor: initString.
    javaClassRef owner: (Java classForName: 'java.lang.SubclassOfPublicClass').
    javaMethodRef := JavaMethodRef2                              in: nil
                namedAndTyped: (self getName: 'publicMethod'
                        descriptor: '()Ljava/lang/String;')
                inClassIdentifiedByRef: javaClassRef.
    javaMethodRef resolve. ] on: Error
            do: [:e | throwedException := e ].
    self assertTrue: (throwedException isNil).
    self disableMockedExceptionThrowing.

    "Created: / 13-04-2011 / 14:49:11 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
    "Modified: / 10-05-2011 / 16:23:36 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
! !

!JavaMethodRefTests methodsFor:'resolving static tests'!

testCorrectStaticResolving
    | javaClassRef  initString  javaMethodRef  expectedResult  result |

    initString := 'Lstx/libjava/tests/mocks/PublicClass;'.
    javaClassRef := self getClassRefFor: initString.
    javaClassRef owner: (Java classForName: 'java.lang.Object').
    javaMethodRef := JavaMethodRef2 in: nil
                namedAndTyped: (self getName: 'publicStaticMethod'
                        descriptor: '()Ljava/lang/String;')
                inClassIdentifiedByRef: javaClassRef.
    result := javaMethodRef resolveStatic.
    expectedResult := (Java 
                classForName: 'stx.libjava.tests.mocks.PublicClass') methodDictionary 
                at: #'publicStaticMethod()Ljava/lang/String;'.
    self assertTrue: (result = expectedResult).

    "Created: / 28-04-2011 / 22:46:53 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
    "Modified: / 10-05-2011 / 16:23:40 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
! !

!JavaMethodRefTests methodsFor:'resolving tests'!

testCorrectInstanceCreation
    | javaClassRef  initString  javaMethodRef |

    initString := 'Ljava/lang/String;'.
    javaClassRef := self getClassRefFor: initString.
    javaMethodRef := JavaMethodRef2 in: nil
                namedAndTyped: (self getName: '<init>' descriptor: '()V')
                inClassIdentifiedByRef: javaClassRef.
    self assertTrue: (javaMethodRef isResolved not).
    self assertTrue: (javaMethodRef valueCache isNil).
    self assertTrue: (javaMethodRef name = '<init>').
    self assertTrue: (javaMethodRef descriptor = '()V').
    self assertTrue: (javaMethodRef classRef name = 'Ljava/lang/String;').

    "Created: / 08-04-2011 / 14:01:41 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
    "Modified: / 10-05-2011 / 16:23:44 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
!

testCorrectResolving
    | javaClassRef  initString  javaMethodRef  expectedResult  result |

    initString := 'Ljava/lang/String;'.
    javaClassRef := self getClassRefFor: initString.
    javaClassRef owner: (Java classForName: 'java.lang.Object').
    javaMethodRef := JavaMethodRef2 in: nil
                namedAndTyped: (self getName: '<init>' descriptor: '()V')
                inClassIdentifiedByRef: javaClassRef.
    result := javaMethodRef resolve.
    expectedResult := (Java classForName: 'java.lang.String') methodDictionary 
                at: #'<init>()V'.
    self assertTrue: (result = expectedResult).

    "Created: / 08-04-2011 / 14:07:57 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
    "Modified: / 10-05-2011 / 16:23:48 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
!

testInvalidation
    | javaClassRef  javaMethodRef |

    javaClassRef := self getClassRefFor: 'Ljava/lang/String;'.
    javaClassRef owner: (Java classForName: 'java.lang.Object').
    javaMethodRef := JavaMethodRef2 in: nil
                namedAndTyped: (self getName: '<init>' descriptor: '()V')
                inClassIdentifiedByRef: javaClassRef.
    self assertTrue: (javaMethodRef isResolved not).
    javaMethodRef resolve.
    self assertTrue: (javaMethodRef isResolved).
    self assertTrue: (javaMethodRef classRef isResolved).
    javaMethodRef invalidate.
    self assertTrue: (javaMethodRef isResolved not).
    self assertTrue: (javaMethodRef classRef isResolved not).

    "Created: / 08-04-2011 / 14:09:06 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
    "Modified: / 10-05-2011 / 16:23:51 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
!

testInvalidationForClassNegative
    | javaClassRef  javaMethodRef |

    javaClassRef := self getClassRefFor: 'Ljava/lang/String;'.
    javaClassRef owner: (Java classForName: 'java.lang.Object').
    javaMethodRef := JavaMethodRef2        in: nil
                namedAndTyped: (self getName: '<init>' descriptor: '()V')
                inClassIdentifiedByRef: javaClassRef.
    self assertTrue: (javaMethodRef isResolved not).
    javaMethodRef resolve.
    self assertTrue: (javaMethodRef isResolved).
    javaMethodRef invalidateForClass: 'Ljava/lang/Object;'.
    self assertTrue: (javaMethodRef isResolved).

    "Created: / 08-04-2011 / 16:23:06 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
    "Modified: / 10-05-2011 / 16:23:53 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
!

testInvalidationForClassPositive
    | javaClassRef  javaMethodRef |

    javaClassRef := self getClassRefFor: 'Ljava/lang/String;'.
    javaClassRef owner: (Java classForName: 'java.lang.Object').
    javaMethodRef := JavaMethodRef2               in: nil
                namedAndTyped: (self getName: '<init>' descriptor: '()V')
                inClassIdentifiedByRef: javaClassRef.
    self assertTrue: (javaMethodRef isResolved not).
    javaMethodRef resolve.
    self assertTrue: (javaMethodRef isResolved).
    javaMethodRef invalidateForClass: 'Ljava/lang/String;'.
    self assertTrue: (javaMethodRef isResolved not).

    "Created: / 08-04-2011 / 16:23:19 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
    "Modified: / 10-05-2011 / 16:23:55 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
!

testResolving
    | javaClassRef  javaMethodRef |

    javaClassRef := self getClassRefFor: 'Ljava/lang/String;'.
    javaClassRef owner: (Java classForName: 'java.lang.Object').
    javaMethodRef := JavaMethodRef2                      in: nil
                namedAndTyped: (self getName: '<init>' descriptor: '()V')
                inClassIdentifiedByRef: javaClassRef.
    self assertTrue: (javaMethodRef isResolved not).
    javaMethodRef resolve.
    self assertTrue: (javaClassRef isResolved).
    self assertTrue: (javaMethodRef isResolved).

    "Created: / 08-04-2011 / 14:04:01 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
    "Modified: / 10-05-2011 / 16:23:57 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
! !

!JavaMethodRefTests class methodsFor:'documentation'!

version_SVN
    ^ '$Id$'
! !