Skip to content

Commit

Permalink
[jnigen] Use check:jni for tests and fix warnings (#1077)
Browse files Browse the repository at this point in the history
  • Loading branch information
HosseinYousefi authored Apr 8, 2024
1 parent bd96c0a commit 26594f2
Show file tree
Hide file tree
Showing 16 changed files with 27 additions and 14 deletions.
6 changes: 6 additions & 0 deletions pkgs/jni/src/dartjni.c
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,8 @@ JniExceptionDetails getExceptionDetails(jthrowable exception) {
JniExceptionDetails details;
details.message = (*jniEnv)->CallObjectMethod(
jniEnv, exception, exceptionMethods.toStringMethod);
// No exception is thrown from toString.
(*jniEnv)->ExceptionClear(jniEnv);
jobject buffer =
(*jniEnv)->NewObject(jniEnv, exceptionMethods.byteArrayOutputStreamClass,
exceptionMethods.byteArrayOutputStreamCtor);
Expand All @@ -529,8 +531,12 @@ JniExceptionDetails getExceptionDetails(jthrowable exception) {
exceptionMethods.printStreamCtor, buffer);
(*jniEnv)->CallVoidMethod(
jniEnv, exception, exceptionMethods.printStackTraceMethod, printStream);
// No exception is thrown from printStackTrace.
(*jniEnv)->ExceptionClear(jniEnv);
details.stacktrace = (*jniEnv)->CallObjectMethod(
jniEnv, buffer, exceptionMethods.toStringMethod);
// No exception is thrown from toString.
(*jniEnv)->ExceptionClear(jniEnv);
details.message = to_global_ref(details.message);
details.stacktrace = to_global_ref(details.stacktrace);
return details;
Expand Down
2 changes: 1 addition & 1 deletion pkgs/jni/test/boxed_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ void main() {
// Don't forget to initialize JNI.
if (!Platform.isAndroid) {
checkDylibIsUpToDate();
Jni.spawnIfNotExists(dylibDir: "build/jni_libs", jvmOptions: ["-Xmx128m"]);
spawnJvm();
}
run(testRunner: test);
}
Expand Down
3 changes: 1 addition & 2 deletions pkgs/jni/test/exception_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ void main() {
Jni.spawn(dylibDir: "wrong_dir");
} on HelperNotFoundError catch (_) {
// stderr.write("\n$_\n");
Jni.spawnIfNotExists(
dylibDir: "build/jni_libs", jvmOptions: ["-Xmx128m"]);
spawnJvm();
caught = true;
} on JniVmExistsError {
stderr.writeln('cannot verify: HelperNotFoundError thrown');
Expand Down
2 changes: 1 addition & 1 deletion pkgs/jni/test/global_env_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ void main() {

if (!Platform.isAndroid) {
checkDylibIsUpToDate();
Jni.spawnIfNotExists(dylibDir: "build/jni_libs", jvmOptions: ["-Xmx128m"]);
spawnJvm();
}
run(testRunner: test);
}
Expand Down
2 changes: 1 addition & 1 deletion pkgs/jni/test/isolate_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ void main() {
// Don't forget to initialize JNI.
if (!Platform.isAndroid) {
checkDylibIsUpToDate();
Jni.spawnIfNotExists(dylibDir: "build/jni_libs", jvmOptions: ["-Xmx128m"]);
spawnJvm();
}
run(testRunner: test);
}
Expand Down
2 changes: 1 addition & 1 deletion pkgs/jni/test/jarray_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ void main() {
// Don't forget to initialize JNI.
if (!Platform.isAndroid) {
checkDylibIsUpToDate();
Jni.spawnIfNotExists(dylibDir: "build/jni_libs", jvmOptions: ["-Xmx128m"]);
spawnJvm();
}
run(testRunner: test);
}
Expand Down
2 changes: 1 addition & 1 deletion pkgs/jni/test/jbyte_buffer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ void main() {
// Don't forget to initialize JNI.
if (!Platform.isAndroid) {
checkDylibIsUpToDate();
Jni.spawnIfNotExists(dylibDir: "build/jni_libs", jvmOptions: ["-Xmx128m"]);
spawnJvm();
}
run(testRunner: test);
}
Expand Down
2 changes: 1 addition & 1 deletion pkgs/jni/test/jlist_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ void main() {
// Don't forget to initialize JNI.
if (!Platform.isAndroid) {
checkDylibIsUpToDate();
Jni.spawnIfNotExists(dylibDir: "build/jni_libs", jvmOptions: ["-Xmx128m"]);
spawnJvm();
}
run(testRunner: test);
}
Expand Down
2 changes: 1 addition & 1 deletion pkgs/jni/test/jmap_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import 'test_util/test_util.dart';
void main() {
// Don't forget to initialize JNI.
if (!Platform.isAndroid) {
Jni.spawnIfNotExists(dylibDir: "build/jni_libs", jvmOptions: ["-Xmx128m"]);
spawnJvm();
}
run(testRunner: test);
}
Expand Down
2 changes: 1 addition & 1 deletion pkgs/jni/test/jobject_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ void main() {
// Don't forget to initialize JNI.
if (!Platform.isAndroid) {
checkDylibIsUpToDate();
Jni.spawnIfNotExists(dylibDir: "build/jni_libs", jvmOptions: ["-Xmx128m"]);
spawnJvm();
}
run(testRunner: test);
}
Expand Down
2 changes: 1 addition & 1 deletion pkgs/jni/test/jset_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ void main() {
// Don't forget to initialize JNI.
if (!Platform.isAndroid) {
checkDylibIsUpToDate();
Jni.spawnIfNotExists(dylibDir: "build/jni_libs", jvmOptions: ["-Xmx128m"]);
spawnJvm();
}
run(testRunner: test);
}
Expand Down
2 changes: 1 addition & 1 deletion pkgs/jni/test/jstring_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ void main() {
// Don't forget to initialize JNI.
if (!Platform.isAndroid) {
checkDylibIsUpToDate();
Jni.spawnIfNotExists(dylibDir: "build/jni_libs", jvmOptions: ["-Xmx128m"]);
spawnJvm();
}
run(testRunner: test);
}
Expand Down
2 changes: 1 addition & 1 deletion pkgs/jni/test/load_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void doGC() {
void main() {
if (!Platform.isAndroid) {
checkDylibIsUpToDate();
Jni.spawnIfNotExists(dylibDir: "build/jni_libs", jvmOptions: ["-Xmx128m"]);
spawnJvm();
}
run(testRunner: test);
}
Expand Down
6 changes: 6 additions & 0 deletions pkgs/jni/test/test_util/test_util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import 'dart:io';

import 'package:jni/jni.dart';
import 'package:jni/src/build_util/build_util.dart';

typedef TestCaseCallback = void Function();
Expand Down Expand Up @@ -34,3 +35,8 @@ void checkDylibIsUpToDate() {
exit(1);
}
}

void spawnJvm() {
Jni.spawnIfNotExists(
dylibDir: "build/jni_libs", jvmOptions: ["-Xmx128m", "-Xcheck:jni"]);
}
2 changes: 1 addition & 1 deletion pkgs/jni/test/type_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ final class $FType extends JObjType<F> {
void main() {
if (!Platform.isAndroid) {
checkDylibIsUpToDate();
Jni.spawnIfNotExists(dylibDir: "build/jni_libs", jvmOptions: ["-Xmx128m"]);
spawnJvm();
}
run(testRunner: test);
}
Expand Down
2 changes: 2 additions & 0 deletions pkgs/jnigen/test/test_util/bindings_test_setup.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ Future<void> bindingsTestSetup() async {
tempClassDir.path,
...jacksonJars,
kotlinTestJar,
], jvmOptions: [
"-Xcheck:jni",
]);
}
}
Expand Down

0 comments on commit 26594f2

Please sign in to comment.