src/JavaMethodRefTests.st
author vranyj1
Sun, 01 May 2011 12:52:23 +0000
branchjk_new_structure
changeset 761 43e017ec7958
parent 758 be8e84381ce0
child 764 3c1d3f2d29a5
permissions -rw-r--r--
Merged with /branches/jk

"{ Package: 'stx:libjava' }"

JavaRuntimeConstantPoolTests 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 := JavaClassRef2 for: initString.
    javaClassRef owner: (Java classForName: 'java.lang.Object').
    javaMethodRef := JavaMethodRef2 
                namedAndTyped: (JavaNameAndType2 name: '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>"
!

testAccessingPPFromSubclass
    | javaClassRef  javaMethodRef  initString  throwedException |

    self enableMockedExceptionThrowing.
    
    [ initString := 'Lstx/libjava/tests/mocks/PublicClass;'.
    javaClassRef := JavaClassRef2 for: initString.
    javaClassRef 
        owner: (Java classForName: 'stx.libjava.tests.mocks.SubclassOfPublicClass').
    javaMethodRef := JavaMethodRef2 
                namedAndTyped: (JavaNameAndType2 name: '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>"
!

testAccessingPrivateFromOutside
    | javaClassRef  javaMethodRef  initString  throwedException |

    self enableMockedExceptionThrowing.
    
    [ initString := 'Lstx/libjava/tests/mocks/PublicClass;'.
    javaClassRef := JavaClassRef2 for: initString.
    javaClassRef owner: (Java classForName: 'java.lang.Object').
    javaMethodRef := JavaMethodRef2 
                namedAndTyped: (JavaNameAndType2 name: '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: / 13-04-2011 / 23:09:19 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
!

testAccessingPrivateFromOutsideInNonPublic
    | javaClassRef  javaMethodRef  initString  throwedException |

    self enableMockedExceptionThrowing.
    
    [ initString := 'Lstx/libjava/tests/mocks/NonPublicClass;'.
    javaClassRef := JavaClassRef2 for: initString.
    javaClassRef owner: (Java classForName: 'java.lang.Object').
    javaMethodRef := JavaMethodRef2 
                namedAndTyped: (JavaNameAndType2 name: '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: / 14-04-2011 / 14:11:08 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
!

testAccessingPrivateFromSubclass
    | javaClassRef  javaMethodRef  initString  throwedException |

    self enableMockedExceptionThrowing.
    
    [ initString := 'Lstx/libjava/tests/mocks/PublicClass;'.
    javaClassRef := JavaClassRef2 for: initString.
    javaClassRef owner: (Java classForName: 'java.lang.SubclassOfPublicClass').
    javaMethodRef := JavaMethodRef2 
                namedAndTyped: (JavaNameAndType2 name: '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: / 14-04-2011 / 00:03:40 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
!

testAccessingProtectedFromOutside
    | javaClassRef  javaMethodRef  initString  throwedException |

    self enableMockedExceptionThrowing.
    
    [ initString := 'Lstx/libjava/tests/mocks/PublicClass;'.
    javaClassRef := JavaClassRef2 for: initString.
    javaClassRef owner: (Java classForName: 'java.lang.Object').
    javaMethodRef := JavaMethodRef2 
                namedAndTyped: (JavaNameAndType2 name: '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: / 14-04-2011 / 14:12:31 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
!

testAccessingProtectedFromOutsideInNonPublic
    | javaClassRef  javaMethodRef  initString  throwedException |

    self enableMockedExceptionThrowing.
    
    [ initString := 'Lstx/libjava/tests/mocks/NonPublicClass;'.
    javaClassRef := JavaClassRef2 for: initString.
    javaClassRef owner: (Java classForName: 'java.lang.Object').
    javaMethodRef := JavaMethodRef2 
                namedAndTyped: (JavaNameAndType2 name: '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: / 14-04-2011 / 14:11:15 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
!

testAccessingProtectedFromPackage
    | javaClassRef  javaMethodRef  initString  throwedException |

    self enableMockedExceptionThrowing.
    
    [ initString := 'Lstx/libjava/tests/mocks/PublicClass;'.
    javaClassRef := JavaClassRef2 for: initString.
    javaClassRef owner: (Java classForName: 'stx.libjava.tests.mocks.Crate').
    javaMethodRef := JavaMethodRef2 
                namedAndTyped: (JavaNameAndType2 name: '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>"
!

testAccessingProtectedFromSubclass
    | javaClassRef  javaMethodRef  initString  throwedException |

    self enableMockedExceptionThrowing.
    
    [ initString := 'Lstx/libjava/tests/mocks/PublicClass;'.
    javaClassRef := JavaClassRef2 for: initString.
    javaClassRef owner: (Java classForName: 'stx.libjava.tests.mocks.SubclassOfPublicClass').
    javaMethodRef := JavaMethodRef2 
                namedAndTyped: (JavaNameAndType2 name: '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: / 13-04-2011 / 23:18:36 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
!

testAccessingPublic
    | javaClassRef javaMethodRef initString |

    self enableMockedExceptionThrowing.
    self shouldnt: 
            [ initString := 'Lstx/libjava/tests/mocks/PublicClass;'.
            javaClassRef := JavaClassRef2 for: initString.
            javaClassRef owner: (Java classForName: 'java.lang.Object').
            javaMethodRef := JavaMethodRef2 
                namedAndTyped: (JavaNameAndType2 name: '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>"
!

testAccessingPublicFromOutside
    | javaClassRef  javaMethodRef  initString |

    self enableMockedExceptionThrowing.
    self shouldnt: 
            [ initString := 'Lstx/libjava/tests/mocks/PublicClass;'.
            javaClassRef := JavaClassRef2 for: initString.
            javaClassRef owner: (Java classForName: 'java.lang.Object').
            javaMethodRef := JavaMethodRef2 
                        namedAndTyped: (JavaNameAndType2 name: '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>"
!

testAccessingPublicFromOutsideInNonPublic
    | javaClassRef  javaMethodRef  initString  throwedException |

    self enableMockedExceptionThrowing.
    
    [ initString := 'Lstx/libjava/tests/mocks/NonPublicClass;'.
    javaClassRef := JavaClassRef2 for: initString.
    javaClassRef owner: (Java classForName: 'java.lang.Object').
    javaMethodRef := JavaMethodRef2 
                namedAndTyped: (JavaNameAndType2 name: '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: / 13-04-2011 / 23:04:57 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
!

testAccessingPublicFromSubclass
    | javaClassRef  javaMethodRef  initString  throwedException |

    self enableMockedExceptionThrowing.
    
    [ initString := 'Lstx/libjava/tests/mocks/PublicClass;'.
    javaClassRef := JavaClassRef2 for: initString.
    javaClassRef owner: (Java classForName: 'java.lang.SubclassOfPublicClass').
    javaMethodRef := JavaMethodRef2 
                namedAndTyped: (JavaNameAndType2 name: '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>"
! !

!JavaMethodRefTests methodsFor:'resolving static tests'!

testCorrectStaticResolving
    | javaClassRef  initString  javaMethodRef  expectedResult  result |

    initString := 'Lstx/libjava/tests/mocks/PublicClass;'.
    javaClassRef := JavaClassRef2 for: initString.
    javaClassRef owner: (Java classForName: 'java.lang.Object').
    javaMethodRef := JavaMethodRef2 
                namedAndTyped: (JavaNameAndType2 name: '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>"
! !

!JavaMethodRefTests methodsFor:'resolving tests'!

testCorrectInstanceCreation
    | javaClassRef  initString  javaMethodRef |

    initString := 'Ljava/lang/String;'.
    javaClassRef := JavaClassRef2 for: initString.
    javaMethodRef := JavaMethodRef2 
                namedAndTyped: (JavaNameAndType2 name: '<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: / 08-04-2011 / 16:25:32 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
!

testCorrectResolving
    | javaClassRef  initString  javaMethodRef  expectedResult  result |

    initString := 'Ljava/lang/String;'.
    javaClassRef := JavaClassRef2 for: initString.
    javaClassRef owner: (Java classForName: 'java.lang.Object').
    javaMethodRef := JavaMethodRef2 
                namedAndTyped: (JavaNameAndType2 name: '<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: / 11-04-2011 / 20:34:42 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
!

testInvalidation
    | javaClassRef  javaMethodRef |

    javaClassRef := JavaClassRef2 for: 'Ljava/lang/String;'.
    javaClassRef owner: (Java classForName: 'java.lang.Object').
    javaMethodRef := JavaMethodRef2 
                namedAndTyped: (JavaNameAndType2 name: '<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: / 13-04-2011 / 12:22:30 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
!

testInvalidationForClassNegative
    | javaClassRef  javaMethodRef |

    javaClassRef := JavaClassRef2 for: 'Ljava/lang/String;'.
    javaClassRef owner: (Java classForName: 'java.lang.Object').
    javaMethodRef := JavaMethodRef2 
                namedAndTyped: (JavaNameAndType2 name: '<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: / 11-04-2011 / 20:41:52 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
!

testInvalidationForClassPositive
    | javaClassRef  javaMethodRef |

    javaClassRef := JavaClassRef2 for: 'Ljava/lang/String;'.
    javaClassRef owner: (Java classForName: 'java.lang.Object').
    javaMethodRef := JavaMethodRef2 
                namedAndTyped: (JavaNameAndType2 name: '<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: / 11-04-2011 / 20:41:49 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
!

testResolving
    | javaClassRef  javaMethodRef |

    javaClassRef := JavaClassRef2 for: 'Ljava/lang/String;'.
    javaClassRef owner: (Java classForName: 'java.lang.Object').
    javaMethodRef := JavaMethodRef2 
                namedAndTyped: (JavaNameAndType2 name: '<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: / 11-04-2011 / 20:41:45 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
! !

!JavaMethodRefTests class methodsFor:'documentation'!

version_SVN
    ^ '$Id$'
! !