github.com/luchsh/agentlib.go@v0.0.0-20221115155834-ffd0caec4d72/jgo/jni_test.go (about)

     1  // Copyright 2022 chuanshenglu@gmail.com
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  // http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.\n\n
    14  
    15  package jgo
    16  
    17  import (
    18  	"fmt"
    19  	"runtime"
    20  	"testing"
    21  
    22  	"github.com/ClarkGuan/jni"
    23  	"github.com/stretchr/testify/assert"
    24  )
    25  
    26  var (
    27  	vm  jni.VM
    28  	env jni.Env // only for the creating thread
    29  	jvm *JavaVM
    30  )
    31  
    32  //  only create the VM once, spec does not support creating multiple VMs
    33  func init() {
    34  	runtime.LockOSThread()
    35  	defer runtime.UnlockOSThread()
    36  	//vm, env = jniCreateJavaVM([]string{"-verbose:gc", "-check:jni", "-Djava.class.path=./testdata"})
    37  	var err error
    38  	jvm, err = Exec([]string{"-verbose:gc", "-check:jni", "-Djava.class.path=./testdata"})
    39  	if err != nil {
    40  		panic(fmt.Errorf("Failed to create JavaVM, err=%v", err))
    41  	}
    42  	vm = jvm.jvm
    43  	env = jvm.jni
    44  }
    45  
    46  func TestGetCreatedJavaVMs(t *testing.T) {
    47  	vms := jniGetCreatedJavaVMs()
    48  	assert.Equal(t, len(vms), 1)
    49  }
    50  
    51  func TestFindClass(t *testing.T) {
    52  	JVM(vm).jniRun(func(env JniEnv) {
    53  		jni := env.raw()
    54  		clazz := jni.FindClass("Ljava/lang/Object;")
    55  		assert.NotZero(t, uintptr(clazz))
    56  	})
    57  }