# PkgInvoker.test --
#
#	Tests the tcl.lang.reflect.PkgInvoker class.
#
# Copyright (c) 1997 by Sun Microsystems, Inc.
#
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
# RCS: @(#) $Id: PkgInvoker.test,v 1.5 1999/05/16 00:21:51 dejong Exp $

# Note: This test file must be sourced from the parent directory
#       or else the defs file may not be properly loaded.

if {[string compare test [info procs test]] == 1} then {source defs}

setupJavaPackage

proc forName {clsName} {
    return [java::call Class forName $clsName]
}

set PkgInvoker tcl.lang.reflect.PkgInvoker

test PkgInvoker-1.1 {getPkgInvoker} {
    set x1 [java::call $PkgInvoker getPkgInvoker [forName pkg1.MyClass]]
    set x2 [java::call $PkgInvoker getPkgInvoker [forName pkg1.MyClass]]
    string compare $x1 $x2
} 0

test PkgInvoker-1.2 {getPkgInvoker} {
    set x1 [java::call $PkgInvoker getPkgInvoker [forName pkg2.MyClass]]
    set x2 [java::call $PkgInvoker getPkgInvoker [forName java.lang.Object]]
    string compare $x1 $x2
} 0

test PkgInvoker-1.3 {getPkgInvoker} {
    [java::new pkg1.MyClass] someMethod
} 1234

test PkgInvoker-1.4 {getPkgInvoker} {
    catch {java::new pkg2.MyClass}
} {0}

test PkgInvoker-1.5 {getPkgInvoker} {
    list [catch {[java::new pkg2.MyClass] someMethod} msg] $msg
} {1 {java.lang.IllegalAccessException: pkg2/MyClass}}

test PkgInvoker-1.6 {getPkgInvoker} {
    list [catch {java::new pkg2.ProtectedClass} msg] $msg
} {1 {java.lang.IllegalAccessException: pkg2/ProtectedClass}}


foreach CLS {pkg1.MyClass pkg1.ProtectedClass} {

    test PkgInvoker-$CLS-2.1 {invokeConstructor} {
	[java::new $CLS] getFoo
    } 4321

    test PkgInvoker-$CLS-2.2 {invokeConstructor} {
	[java::new "$CLS int" 2233] getFoo
    } 2233

    test PkgInvoker-$CLS-2.3 {invokeConstructor} {
	[java::new "$CLS short" 3344] getFoo
    } 3344

    test PkgInvoker-$CLS-2.4 {invokeMethod} {
	[java::new $CLS] someMethod
    } 1234
    test PkgInvoker-$CLS-2.5 {invokeMethod} {
	[java::new $CLS] someProtectedMethod
    } 4321

    test PkgInvoker-$CLS-2.6 {getField} {
	java::field [java::new $CLS] foo
    } 4321
    test PkgInvoker-$CLS-2.7 {getField} {
	java::field [java::new $CLS] pfoo
    } 5432
    test PkgInvoker-$CLS-2.8 {getField} {
	java::field [java::new $CLS] sstring
    } "can get"
    test PkgInvoker-$CLS-2.9 {getField} {
	java::field [java::new $CLS] psstring
    } "protected can get"
    test PkgInvoker-$CLS-2.10 {getField} {
	java::field $CLS sstring
    } "can get"
    test PkgInvoker-$CLS-2.11 {getField} {
	java::field $CLS psstring
    } "protected can get"

    test PkgInvoker-$CLS-2.12 {setField} {
	set x [java::new $CLS]
	java::field $x foo 2233
	java::field $x foo
    } 2233
    test PkgInvoker-$CLS-2.13 {setField} {
	set x [java::new $CLS]
	java::field $x pfoo 3344
	java::field $x pfoo
    } 3344
    test PkgInvoker-$CLS-2.14 {setField} {
	set x [java::new $CLS]
	java::field $x sstring "yo yo ma"
	java::field $x sstring
    } {yo yo ma}
    test PkgInvoker-$CLS-2.15 {setField} {
	set x [java::new $CLS]
	java::field $x psstring yadiyadida
	java::field $x psstring
    } yadiyadida
    test PkgInvoker-$CLS-2.16 {setField} {
	java::field $CLS sstring "yo yo ma"
	java::field $CLS sstring
    } {yo yo ma}
    test PkgInvoker-$CLS-2.17 {setField} {
	java::field $CLS psstring yadiyadida
	java::field $CLS psstring
    } yadiyadida

    test PkgInvoker-$CLS-2.18 {get/set property} {
	set x [java::new $CLS]
	java::prop $x foo
    } 4321

    test PkgInvoker-$CLS-2.19 {get/set property} {
	set x [java::new $CLS]
	java::prop $x foo 4455
	java::prop $x foo
    } 4455
}

