515 lines
12 KiB
Go
515 lines
12 KiB
Go
// +build !android
|
|
//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 TestStaticBooleanMethod(t *testing.T) {
|
|
err := Do(vm, func(env Env) error {
|
|
cls := FindClass(env, "test/AClass")
|
|
mid := GetStaticMethodID(env, cls, "GetStaticBoolean", "()Z")
|
|
if mid == nil {
|
|
t.Errorf("MethodID is nil")
|
|
}
|
|
setterMid := GetStaticMethodID(env, cls, "SetStaticBoolean", "(Z)V")
|
|
if setterMid == nil {
|
|
t.Errorf("Setter MethodID is nil")
|
|
}
|
|
res, err := CallStaticBooleanMethod(env, cls, mid)
|
|
if err != nil {
|
|
t.Errorf("Method invocation failed")
|
|
}
|
|
if !res {
|
|
t.Errorf("Method returned %v, not expected value of %v.", res, true)
|
|
}
|
|
if err := CallStaticVoidMethod(env, cls, setterMid, FALSE); err != nil {
|
|
t.Errorf("Setter invocation failed")
|
|
}
|
|
res, err = CallStaticBooleanMethod(env, cls, mid)
|
|
if err != nil {
|
|
t.Errorf("Method invocation failed")
|
|
}
|
|
if res {
|
|
t.Errorf("Method returned %v, not expected value of %v.", res, false)
|
|
}
|
|
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 TestBooleanMethod(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, "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, "<init>", "()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, "<init>", "()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, "<init>", "()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, "<init>", "()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, "<init>", "()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, "<init>", "()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")
|
|
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)
|
|
}
|
|
}
|