Skip to content

Commit de58019

Browse files
committed
Add testing of RegisterNative
* Add Java Class with native function in java/testing * Add callback function in jnigi_testing_c_callback.go * Add script testing.sh to compile java and run tests
1 parent a85984a commit de58019

File tree

7 files changed

+76
-2
lines changed

7 files changed

+76
-2
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
java/test/out/*

Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ RUN go mod download && go mod verify
1818

1919
COPY . .
2020

21-
CMD go test
21+
CMD ./testing.sh
2222

2323

java/test/javac.sh

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
3+
cd $(dirname $0)
4+
javac -d out -cp src/ src/local/*

java/test/src/local/JnigiTesting.java

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package local;
2+
3+
public class JnigiTesting {
4+
public native java.lang.String Greet(java.lang.String x);
5+
}

jnigi_test.go

+27-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ package jnigi
66

77
import (
88
"github.com/stretchr/testify/assert"
9+
"os"
10+
"path/filepath"
911
"runtime"
1012
"strings"
1113
"testing"
@@ -28,6 +30,7 @@ func TestAll(t *testing.T) {
2830
PTestPushPopLocalFrame(t)
2931
PTestHandleException(t)
3032
PTestCast(t)
33+
PTestRegisterNative(t)
3134
PTestDestroy(t)
3235
}
3336

@@ -39,7 +42,8 @@ func PTestInit(t *testing.T) {
3942
t.Fatal(err)
4043
}
4144
runtime.LockOSThread()
42-
jvm2, e2, err := CreateJVM(NewJVMInitArgs(false, true, DEFAULT_VERSION, []string{"-Xcheck:jni"}))
45+
cwd, _ := os.Getwd()
46+
jvm2, e2, err := CreateJVM(NewJVMInitArgs(false, true, DEFAULT_VERSION, []string{"-Xcheck:jni", "-Djava.class.path=" + filepath.Join(cwd, "java/test/out")}))
4347
if err != nil {
4448
t.Fatal(err)
4549
}
@@ -514,6 +518,28 @@ func PTestCast(t *testing.T) {
514518
}
515519
}
516520

521+
func PTestRegisterNative(t *testing.T) {
522+
if err := env.RegisterNative("local/JnigiTesting", "Greet", ObjectType("java/lang/String"), []interface{}{ObjectType("java/lang/String")}, c_go_callback_Greet); err != nil {
523+
t.Fatal(err)
524+
}
525+
objRef, err := env.NewObject("local/JnigiTesting")
526+
if err != nil {
527+
t.Fatal(err)
528+
}
529+
nameRef, err := env.NewObject("java/lang/String", []byte("World"))
530+
if err != nil {
531+
t.Fatal(err)
532+
}
533+
strRef := NewObjectRef("java/lang/String")
534+
if err := objRef.CallMethod(env, "Greet", strRef, nameRef); err != nil {
535+
t.Fatal(err)
536+
}
537+
goStr := toGoStr(t, strRef)
538+
if !assert.Equal(t, "Hello World!", goStr) {
539+
t.Fail()
540+
}
541+
}
542+
517543
func toGoStr(t *testing.T, o *ObjectRef) string {
518544
var goBytes []byte
519545
if err := o.CallMethod(env, "getBytes", &goBytes); err != nil {

jnigi_test_c_callback.go

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//go:build cgo_testing
2+
3+
package jnigi
4+
5+
import (
6+
"unsafe"
7+
)
8+
9+
/*
10+
#include<stdint.h>
11+
12+
extern uintptr_t go_callback_Greet(void *env, uintptr_t obj, uintptr_t arg_0 );
13+
14+
*/
15+
import "C"
16+
17+
//export go_callback_Greet
18+
func go_callback_Greet(jenv unsafe.Pointer, jobj uintptr, arg_0 uintptr) uintptr {
19+
env := WrapEnv(jenv)
20+
env.ExceptionHandler = ThrowableToStringExceptionHandler
21+
22+
strArgRef := WrapJObject(arg_0, "java/lang/String", false)
23+
var goBytes []byte
24+
if err := strArgRef.CallMethod(env, "getBytes", &goBytes); err != nil {
25+
panic(err)
26+
}
27+
retRef, err := env.NewObject("java/lang/String", []byte("Hello "+string(goBytes)+"!"))
28+
if err != nil {
29+
panic(err)
30+
}
31+
return uintptr(retRef.JObject())
32+
}
33+
34+
var c_go_callback_Greet = C.go_callback_Greet

testing.sh

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
3+
java/test/javac.sh && \
4+
go test -v --tags=cgo_testing

0 commit comments

Comments
 (0)