diff --git a/jni_test.go b/jni_test.go index 1685fb7..8301719 100644 --- a/jni_test.go +++ b/jni_test.go @@ -9,7 +9,7 @@ import ( ) var ( - vm JVM + vm JVM vmOnce sync.Once ) @@ -145,6 +145,167 @@ func TestIntMethod(t *testing.T) { } } +func TestBooleanMethod(t *testing.T) { + err := Do(vm, func(env Env) error { + cls := FindClass(env, "test/AClass") + mid := GetMethodID(env, cls, "", "()V") + inst, err := NewObject(env, cls, mid) + mid = GetMethodID(env, cls, "GetBoolean", "()Z") + if mid == nil { + t.Errorf("MethodID is nil") + } + res, err := CallBooleanMethod(env, inst, mid) + if err != nil { + t.Errorf("Method invocation failed") + } + if !res { + t.Errorf("Method returned %v, not expected value of %v.", res, true) + } + return nil + }) + if err != nil { + t.Errorf("Error: %s", err) + } +} + +func TestByteMethod(t *testing.T) { + err := Do(vm, func(env Env) error { + cls := FindClass(env, "test/AClass") + mid := GetMethodID(env, cls, "", "()V") + inst, err := NewObject(env, cls, mid) + mid = GetMethodID(env, cls, "GetByte", "()B") + if mid == nil { + t.Errorf("MethodID is nil") + } + res, err := CallByteMethod(env, inst, mid) + if err != nil { + t.Errorf("Method invocation failed") + } + if res != 127 { + t.Errorf("Method returned %d, not expected value of %d.", res, 127) + } + return nil + }) + if err != nil { + t.Errorf("Error: %s", err) + } +} + +func TestCharMethod(t *testing.T) { + err := Do(vm, func(env Env) error { + cls := FindClass(env, "test/AClass") + mid := GetMethodID(env, cls, "", "()V") + inst, err := NewObject(env, cls, mid) + mid = GetMethodID(env, cls, "GetChar", "()C") + if mid == nil { + t.Errorf("MethodID is nil") + } + res, err := CallCharMethod(env, inst, mid) + if err != nil { + t.Errorf("Method invocation failed") + } + if res != 65432 { + t.Errorf("Method returned %d, not expected value of %d.", res, 65432) + } + return nil + }) + if err != nil { + t.Errorf("Error: %s", err) + } +} + +func TestShortMethod(t *testing.T) { + err := Do(vm, func(env Env) error { + cls := FindClass(env, "test/AClass") + mid := GetMethodID(env, cls, "", "()V") + inst, err := NewObject(env, cls, mid) + mid = GetMethodID(env, cls, "GetShort", "()S") + if mid == nil { + t.Errorf("MethodID is nil") + } + res, err := CallShortMethod(env, inst, mid) + if err != nil { + t.Errorf("Method invocation failed") + } + if res != 512 { + t.Errorf("Method returned %d, not expected value of %d.", res, 512) + } + return nil + }) + if err != nil { + t.Errorf("Error: %s", err) + } +} + +func TestLongMethod(t *testing.T) { + err := Do(vm, func(env Env) error { + cls := FindClass(env, "test/AClass") + mid := GetMethodID(env, cls, "", "()V") + inst, err := NewObject(env, cls, mid) + mid = GetMethodID(env, cls, "GetLong", "()J") + if mid == nil { + t.Errorf("MethodID is nil") + } + res, err := CallLongMethod(env, inst, mid) + if err != nil { + t.Errorf("Method invocation failed") + } + if res != 1<<33 { + t.Errorf("Method returned %d, not expected value of %d.", res, 1<<33) + } + return nil + }) + if err != nil { + t.Errorf("Error: %s", err) + } +} + +func TestFloatMethod(t *testing.T) { + err := Do(vm, func(env Env) error { + cls := FindClass(env, "test/AClass") + mid := GetMethodID(env, cls, "", "()V") + inst, err := NewObject(env, cls, mid) + mid = GetMethodID(env, cls, "GetFloat", "()F") + if mid == nil { + t.Errorf("MethodID is nil") + } + res, err := CallFloatMethod(env, inst, mid) + if err != nil { + t.Errorf("Method invocation failed") + } + if res != 4.321 { + t.Errorf("Method returned %f, not expected value of %f.", res, 4.321) + } + return nil + }) + if err != nil { + t.Errorf("Error: %s", err) + } +} + +func TestDoubleMethod(t *testing.T) { + err := Do(vm, func(env Env) error { + cls := FindClass(env, "test/AClass") + mid := GetMethodID(env, cls, "", "()V") + inst, err := NewObject(env, cls, mid) + mid = GetMethodID(env, cls, "GetDouble", "()D") + if mid == nil { + t.Errorf("MethodID is nil") + } + res, err := CallDoubleMethod(env, inst, mid) + if err != nil { + t.Errorf("Method invocation failed") + } + if res != 5.4321 { + t.Errorf("Method returned %f, not expected value of %f.", res, 5.4321) + } + return nil + }) + if err != nil { + t.Errorf("Error: %s", err) + } +} + func TestVoidMethod(t *testing.T) { err := Do(vm, func(env Env) error { cls := FindClass(env, "test/AClass") diff --git a/test/AClass.class b/test/AClass.class index b7a71a5..608a39e 100644 Binary files a/test/AClass.class and b/test/AClass.class differ diff --git a/test/AClass.java b/test/AClass.java index 4ea82d6..f5a8fd9 100644 --- a/test/AClass.java +++ b/test/AClass.java @@ -21,6 +21,34 @@ public class AClass { return intval; } + public boolean GetBoolean() { + return true; + } + + public byte GetByte() { + return 127; + } + + public char GetChar() { + return 65432; + } + + public short GetShort() { + return 512; + } + + public long GetLong() { + return 1L<<33; + } + + public float GetFloat() { + return 4.321f; + } + + public double GetDouble() { + return 5.4321; + } + public void SetInt(int val) { intval = val; }