Skip to content

Commit

Permalink
Fix simple tests and adjust runtime error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
nberth committed Sep 12, 2024
1 parent ea840e3 commit 3cfe83d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 12 deletions.
6 changes: 3 additions & 3 deletions libcob/java.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,22 +81,22 @@ resolve_java (const char *class_name,
cob_free(jni_class_name);
if (!cls) {
cob_runtime_error(_("Java class '%s' not found"), class_name);
return NULL;
cob_hard_failure ();
}

mid = (*env)->GetStaticMethodID(env, cls, method_name, method_signature);
if (!mid) {
cob_runtime_error(_("Java method '%s' with signature '%s' not found in class '%s'"),
method_name, method_signature, class_name);
(*env)->DeleteLocalRef(env, cls);
return NULL;
cob_hard_failure ();
}

handle = (cob_java_handle*)cob_malloc(sizeof(cob_java_handle));
if (!handle) {
cob_runtime_error(_("Memory allocation failed for Java method handle"));
(*env)->DeleteLocalRef(env, cls);
return NULL;
cob_hard_failure ();
}

handle->cls = (*env)->NewGlobalRef(env, cls);
Expand Down
47 changes: 38 additions & 9 deletions tests/testsuite.src/run_java.at
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,26 @@ AT_CHECK([$COBCRUN_DIRECT ./prog], [0],
AT_CLEANUP


AT_SETUP([CALL Java static void (void) (missing)])
AT_SETUP([CALL Java with malformed method name])
AT_KEYWORDS([extensions jni malformed])

AT_SKIP_IF([test "$COB_HAS_JNI" = "no"])

AT_DATA([prog.cob], [
IDENTIFICATION DIVISION.
PROGRAM-ID. prog.
PROCEDURE DIVISION.
CALL "Java.InvalidName"
STOP RUN.
])

AT_CHECK([$COMPILE prog.cob], [1], [],
[prog.cob:5: error: malformed Java method name 'Java.InvalidName', expected format 'Java.ClassName.methodName'
])
AT_CLEANUP


AT_SETUP([CALL Java static void (void) (missing class)])
AT_KEYWORDS([extensions jni])

AT_SKIP_IF([test "$COB_HAS_JNI" = "no"])
Expand All @@ -57,27 +76,37 @@ AT_DATA([prog.cob], [
IDENTIFICATION DIVISION.
PROGRAM-ID. prog.
PROCEDURE DIVISION.
CALL "Java.Test.missingMethod"
CALL "Java.Test.isAMissingClass"
STOP RUN.
])

AT_CHECK([$COMPILE prog.cob], [0], [], [])
# TODO: should fail!
AT_CHECK([$COBCRUN_DIRECT ./prog], [0], [])
AT_CHECK([$COBCRUN_DIRECT ./prog], [1], [],
[libcob: prog.cob:5: error: Java class 'Test' not found
])
AT_CLEANUP

AT_SETUP([CALL Java with malformed method name])
AT_KEYWORDS([extensions jni malformed])

AT_SETUP([CALL Java static void (void) (missing method)])
AT_KEYWORDS([extensions jni])

AT_SKIP_IF([test "$COB_HAS_JNI" = "no"])

AT_DATA([Test.java], [
public class Test {}
])

AT_DATA([prog.cob], [
IDENTIFICATION DIVISION.
PROGRAM-ID. prog.
PROCEDURE DIVISION.
CALL "Java.InvalidName"
CALL "Java.Test.missingMethod"
STOP RUN.
])

AT_CHECK([$COMPILE prog.cob], [1], [], [stderr])
AT_CLEANUP
AT_CHECK([$JAVAC Test.java], [0], [], [])
AT_CHECK([$COMPILE prog.cob], [0], [], [])
AT_CHECK([$COBCRUN_DIRECT ./prog], [1], [],
[libcob: prog.cob:5: error: Java method 'missingMethod' with signature '()V' not found in class 'Test'
])
AT_CLEANUP

0 comments on commit 3cfe83d

Please sign in to comment.