# This file tests ReflectObject.test.
#
# This file contains a collection of tests for one or more of the Tcl
# built-in commands.  Sourcing this file into Tcl runs the tests and
# generates output for errors.  No output means no errors were found.
#
# 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: ReflectObject.test,v 1.2 1998/10/29 23:23:32 hylands 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

set o [java::new tests.JavaTest]
test ReflectObject-1.1 {cmdProc} {
    list [catch {$o} msg] $msg
} "1 {wrong # args: should be \"$o ?-noconvert? signature ?arg arg ...?\"}"

test ReflectObject-1.2 {cmdProc} {
    list [catch {$o -noconvert} msg] $msg
} "1 {wrong # args: should be \"$o ?-noconvert? signature ?arg arg ...?\"}"

test ReflectObject-1.3 {cmdProc} {
    set o [java::new tests.JavaTest]
    list [catch {[$o -noconvert imethod] toString} msg] $msg
} {0 6}

test ReflectObject-1.4 {cmdProc} {
    set o [java::new tests.JavaTest]
    list [catch {[$o -n imethod] toString} msg] $msg
} {0 6}

test ReflectObject-1.5 {cmdProc} {
    set o [java::new tests.JavaTest]
    list [catch {[$o - imethod] toString} msg] $msg
} {1 {no such method "-" in class tests.JavaTest}}

test ReflectObject-1.6 {cmdProc} {
    set o [java::new tests.JavaTest]
    list [catch {$o imethod} msg] $msg
} {0 6}

test ReflectObject-1.7 {cmdProc} {
    set x [java::new tests.ConvertTest]
    string compare [$x nullMethod] [java::null]
} 0

test ReflectObject-1.8 {cmdProc} {
    set x [java::new tests.ConvertTest]
    $x voidMethod
} ""

test ReflectObject-1.9 {cmdProc} {
    set x [java::new tests.ConvertTest]
    $x emptyStringMethod
} ""

test ReflectObject-1.10 {cmdProc} {
    set s1 [java::new {java.lang.String java.lang.String} "hello"]
    set s2 [$s1 -n toString]
    string compare $s1 $s2 
} {0}

test ReflectObject-2.1 {isValid flag} {
    set x [java::new tests.JavaTest]
    java::call tests.ConvertTest staticTestParam $x
} 6

test ReflectObject-2.2 {isValid flag} {
    set x [java::new tests.JavaTest]
    catch {rename foo ""}
    rename $x foo
    java::call tests.ConvertTest staticTestParam $x
} 6

test ReflectObject-2.3 {isValid flag} {
    set x [java::new tests.JavaTest]
    rename $x ""
    java::call tests.ConvertTest staticTestParam $x
} 4

test ReflectObject-2.4 {isValid flag} {
    set x [java::new tests.JavaTest]
    set k $x
    unset x
    java::call tests.ConvertTest staticTestParam $k
} 6

test ReflectObject-2.5 {isValid flag} {
    set x [java::new tests.JavaTest]
    set k $x
    rename $x ""
    unset x
    java::call tests.ConvertTest staticTestParam $k
} 4

test ReflectObject-2.6 {isValid flag} {
    set x [java::new tests.JavaTest]
    java::info class [format %s $x]
} tests.JavaTest

test ReflectObject-2.7 {isValid flag} {
    set x [java::new tests.JavaTest]
    rename $x ""
    catch {java::field $x -gorp-} msg
    string match "unknown class or object*" $msg
} 1

test ReflectObject-3.0 {object type} {
    catch {unset results}
    set good [java::new tests.DownReference]
    lappend results [java::info class $good]
    lappend results [$good getSecret]
    set bad [java::call tests.DownReference newInstance]
    lappend results [java::info class $bad]
    lappend results [catch {$bad getSecret} err]
    lappend results $err
} {tests.DownReference X123 java.lang.Object 1 {no such method "getSecret" in class java.lang.Object}}


test ReflectObject-3.1 {object compare} {
    # Note that this test fails with JDK1.2beta4
    catch {unset results}
    set c [java::new tests.Compare]
    set r1 [$c get1]
    set r2 [$c get2]
    set r3 [$c get3]
    lappend results [java::info class $r1]
    lappend results [java::info class $r2]
    lappend results [java::info class $r3]
    lappend results [string compare $r1 $r2]
    lappend results [string compare $r2 $r3]
    lappend results [string compare $r1 $r3]
    lappend results [$c compare $r1 $r2]
    lappend results [$c compare $r2 $r3]
} {java.lang.Object java.util.Dictionary java.util.Hashtable -1 -1 -1 1 1}


test ReflectObject-3.2 {object compare} {
    catch {unset results}
    set c [java::new tests.Compare]
    set r1 [$c get1]
    set r2 [$c get2]
    set r3 [$c get3]
    lappend results [java::isnull $r1]
    lappend results [java::isnull $r2]
    lappend results [java::isnull $r3]
    $c empty
    set r4 [$c get1]
    set r5 [$c get2]
    set r6 [$c get3]
    lappend results [java::isnull $r4]
    lappend results [java::isnull $r5]
    lappend results [java::isnull $r6]
    lappend results [$c compare $r1 $r4]
    lappend results [$c compare $r2 $r5]
    lappend results [$c compare $r2 $r6]
} {0 0 0 1 1 1 0 0 0}




