jni/jni_test.go

319 lines
7.4 KiB
Go

// +build darwin
//go:generate javac test/AClass.java
package jni
import (
"sync"
"testing"
)
var (
vm JVM
vmOnce sync.Once
)
func TestCreateJVM(t *testing.T) {
// Do not try to create more than one JVM
vmOnce.Do(func() {
res := CreateJavaVM()
if res.jvm == nil {
t.Errorf("CreateJavaVM() returned nil")
} else {
vm = res
}
})
}
func TestFindClass(t *testing.T) {
err := Do(vm, func(env Env) error {
cls := FindClass(env, "test/AClass")
if cls == 0 {
t.Errorf("Class is nil")
}
return nil
})
if err != nil {
t.Errorf("Error: %s", err)
}
}
func TestGetMethodID(t *testing.T) {
err := Do(vm, func(env Env) error {
cls := FindClass(env, "test/AClass")
mid := GetMethodID(env, cls, "<init>", "()V")
if mid == nil {
t.Errorf("MethodID is nil")
}
return nil
})
if err != nil {
t.Errorf("Error: %s", err)
}
}
func TestNewObject(t *testing.T) {
err := Do(vm, func(env Env) error {
cls := FindClass(env, "test/AClass")
mid := GetMethodID(env, cls, "<init>", "()V")
inst, err := NewObject(env, cls, mid)
if err != nil {
t.Errorf("NewObject raised exception")
}
if inst == 0 {
t.Errorf("NewObject returned nil")
}
inst = NewGlobalRef(env, inst)
if inst == 0 {
t.Errorf("NewGlobalRef returned nil")
}
return nil
})
if err != nil {
t.Errorf("Error: %s", err)
}
}
func TestIsSameObject(t *testing.T) {
err := Do(vm, func(env Env) error {
cls := FindClass(env, "test/AClass")
mid := GetMethodID(env, cls, "<init>", "()V")
inst, err := NewObject(env, cls, mid)
if !IsSameObject(env, inst, inst) {
t.Errorf("Object is not the same as itself")
}
inst2, err := NewObject(env, cls, mid)
if err != nil {
t.Errorf("Object is nil")
}
if IsSameObject(env, inst, inst2) {
t.Errorf("Different objects are the same")
}
inst2 = NewLocalRef(env, inst)
if !IsSameObject(env, inst, inst2) {
t.Errorf("New local ref is not the same as original object")
}
return nil
})
if err != nil {
t.Errorf("Error: %s", err)
}
}
func TestStaticIntMethod(t *testing.T) {
err := Do(vm, func(env Env) error {
cls := FindClass(env, "test/AClass")
mid := GetMethodID(env, cls, "<init>", "()V")
mid = GetStaticMethodID(env, cls, "GetStaticInt", "()I")
if mid == nil {
t.Errorf("MethodID is nil")
}
res, err := CallStaticIntMethod(env, cls, mid)
if err != nil {
t.Errorf("Method invocation failed")
}
if res != 100 {
t.Errorf("Method returned %d, not expected value of %d.", res, 100)
}
return err
})
if err != nil {
t.Errorf("Error: %s", err)
}
}
func TestIntMethod(t *testing.T) {
err := Do(vm, func(env Env) error {
cls := FindClass(env, "test/AClass")
mid := GetMethodID(env, cls, "<init>", "()V")
inst, err := NewObject(env, cls, mid)
mid = GetMethodID(env, cls, "GetInt", "()I")
if mid == nil {
t.Errorf("MethodID is nil")
}
res, err := CallIntMethod(env, inst, mid)
if err != nil {
t.Errorf("Method invocation failed")
}
if res != 0 {
t.Errorf("Method returned %d, not expected value of %d.", res, 0)
}
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")
mid := GetMethodID(env, cls, "<init>", "()V")
inst, err := NewObject(env, cls, mid)
mid = GetMethodID(env, cls, "IncInt", "()V")
if mid == nil {
t.Errorf("MethodID is nil")
}
err = CallVoidMethod(env, inst, mid)
if err != nil {
t.Errorf("Method invocation failed")
}
mid = GetMethodID(env, cls, "GetInt", "()I")
res, err := CallIntMethod(env, inst, mid)
if err != nil {
t.Errorf("Method invocation falied")
}
if res != 1 {
t.Errorf("Method returned %d, not expected value of %d.", res, 1)
}
return nil
})
if err != nil {
t.Errorf("Error: %s", err)
}
}
func TestVoidMethod2(t *testing.T) {
err := Do(vm, func(env Env) error {
cls := FindClass(env, "test/AClass")
mid := GetMethodID(env, cls, "<init>", "()V")
inst, err := NewObject(env, cls, mid)
mid = GetMethodID(env, cls, "SetInt", "(I)V")
if mid == nil {
t.Errorf("MethodID is nil")
}
err = CallVoidMethod(env, inst, mid, 5)
if err != nil {
t.Errorf("Method invocation failed")
}
mid = GetMethodID(env, cls, "GetInt", "()I")
res, err := CallIntMethod(env, inst, mid)
if err != nil {
t.Errorf("Method invocation falied")
}
if res != 5 {
t.Errorf("Method returned %d, not expected value of %d.", res, 5)
}
return nil
})
if err != nil {
t.Errorf("Error: %s", err)
}
}
func TestVoidMethod3(t *testing.T) {
err := Do(vm, func(env Env) error {
cls := FindClass(env, "test/AClass")
mid := GetMethodID(env, cls, "<init>", "()V")
inst, err := NewObject(env, cls, mid)
mid = GetMethodID(env, cls, "AddInts", "(II)V")
if mid == nil {
t.Errorf("MethodID is nil")
}
err = CallVoidMethod(env, inst, mid, 9, 11)
if err != nil {
t.Errorf("Method invocation failed")
}
mid = GetMethodID(env, cls, "GetInt", "()I")
res, err := CallIntMethod(env, inst, mid)
if err != nil {
t.Errorf("Method invocation falied")
}
if res != 20 {
t.Errorf("Method returned %d, not expected value of %d.", res, 20)
}
return nil
})
if err != nil {
t.Errorf("Error: %s", err)
}
}
func TestStaticVoidMethod(t *testing.T) {
err := Do(vm, func(env Env) error {
cls := FindClass(env, "test/AClass")
mid := GetStaticMethodID(env, cls, "SetStaticInt", "(I)V")
if mid == nil {
t.Errorf("MethodID is nil")
}
err := CallStaticVoidMethod(env, cls, mid, 200)
if err != nil {
t.Errorf("Method invocation failed: %s", err)
}
mid = GetStaticMethodID(env, cls, "GetStaticInt", "()I")
res, err := CallStaticIntMethod(env, cls, mid)
if err != nil {
t.Errorf("Method invocation falied: %s", err)
}
if res != 200 {
t.Errorf("Method returned %d, not expected value of %d.", res, 200)
}
mid = GetStaticMethodID(env, cls, "SetStaticInt", "(I)V")
err = CallStaticVoidMethod(env, cls, mid, 100)
if err != nil {
t.Errorf("Method invocation failed: %s", err)
}
return nil
})
if err != nil {
t.Errorf("Error: %s", err)
}
}
func TestGetObjectClass(t *testing.T) {
err := Do(vm, func(env Env) error {
cls := FindClass(env, "test/AClass")
mid := GetMethodID(env, cls, "<init>", "()V")
inst, _ := NewObject(env, cls, mid)
cls2 := GetObjectClass(env, inst)
if cls2 == 0 {
t.Errorf("GetObjectClass returned nil")
}
if !IsInstanceOf(env, inst, cls2) {
t.Errorf("IsInstanceOf failed")
}
return nil
})
if err != nil {
t.Errorf("Error: %s", err)
}
}
func TestField(t *testing.T) {
err := Do(vm, func(env Env) error {
cls := FindClass(env, "test/AClass")
mid := GetMethodID(env, cls, "<init>", "()V")
inst, _ := NewObject(env, cls, mid)
fid := GetFieldID(env, cls, "intval", "I")
if fid == nil {
t.Errorf("FieldID is nil")
}
res := GetIntField(env, inst, fid)
if res != 0 {
t.Errorf("Method returned %d, not expected value of %d.", res, 0)
}
return nil
})
if err != nil {
t.Errorf("Error: %s", err)
}
}
func TestStaticField(t *testing.T) {
err := Do(vm, func(env Env) error {
cls := FindClass(env, "test/AClass")
fid := GetStaticFieldID(env, cls, "staticintval", "I")
if fid == nil {
t.Errorf("FieldID is nil")
}
res := GetStaticIntField(env, cls, fid)
if res != 100 {
t.Errorf("Method returned %d, not expected value of %d.", res, 100)
}
return nil
})
if err != nil {
t.Errorf("Error: %s", err)
}
}