Skip to content

Commit b7d3cb6

Browse files
committed
add test_call_https_java
1 parent 5cc83ce commit b7d3cb6

File tree

3 files changed

+49
-22
lines changed

3 files changed

+49
-22
lines changed

src/call_java.c

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
#include <jni.h>
22
#include <sqlite3ext.h>
33
#include <string.h>
4+
#include <stdio.h>
5+
#include <stdlib.h>
6+
#include <stdbool.h>
47

5-
JavaVM* gJvm = NULL; // Stored from JNI_OnLoad
8+
JavaVM* gJvm = NULL;
69

710
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
811
gJvm = vm;
@@ -12,7 +15,7 @@ JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
1215
JNIEnv* getEnv() {
1316
JNIEnv* env;
1417
if ((*gJvm)->GetEnv(gJvm, (void**)&env, JNI_VERSION_1_6) != JNI_OK) {
15-
if ((*gJvm)->AttachCurrentThread(gJvm, &env, NULL) != 0) {
18+
if ((*gJvm)->AttachCurrentThread(gJvm, (void**)&env, NULL) != 0) {
1619
return NULL;
1720
}
1821
}
@@ -21,19 +24,34 @@ JNIEnv* getEnv() {
2124

2225
const char* call_https_java(JNIEnv* env) {
2326
jclass cls = (*env)->FindClass(env, "HttpsCaller");
27+
if ((*env)->ExceptionCheck(env)) {
28+
(*env)->ExceptionDescribe(env);
29+
(*env)->ExceptionClear(env);
30+
return "Class not found (exception)";
31+
}
2432
if (!cls) return "Class not found";
2533

2634
jmethodID mid = (*env)->GetStaticMethodID(env, cls, "callHttps", "()Ljava/lang/String;");
35+
if ((*env)->ExceptionCheck(env)) {
36+
(*env)->ExceptionDescribe(env);
37+
(*env)->ExceptionClear(env);
38+
return "Method not found (exception)";
39+
}
2740
if (!mid) return "Method not found";
2841

2942
jstring result = (jstring)(*env)->CallStaticObjectMethod(env, cls, mid);
43+
if ((*env)->ExceptionCheck(env)) {
44+
(*env)->ExceptionDescribe(env);
45+
(*env)->ExceptionClear(env);
46+
return "Java method threw exception";
47+
}
3048
if (!result) return "Java method returned null";
3149

3250
const char* str = (*env)->GetStringUTFChars(env, result, 0);
51+
if (!str) return "Failed to get UTF chars";
3352

3453
char* copied = strdup(str);
35-
3654
(*env)->ReleaseStringUTFChars(env, result, str);
3755

38-
return copied;
56+
return copied ? copied : "Memory allocation failed";
3957
}

src/network.c

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,6 @@
1313
#include "utils.h"
1414
#include "curl/curl.h"
1515
#include "cloudsync_private.h"
16-
#include <stdio.h>
17-
#include <android/log.h>
18-
#include <jni.h>
19-
#include <stdlib.h>
20-
#include "call_java.h"
2116

2217
#define CLOUDSYNC_ENDPOINT_PREFIX "v1/cloudsync"
2318
#define CLOUDSYNC_ENDPOINT_UPLOAD "upload"
@@ -459,18 +454,6 @@ void cloudsync_network_init (sqlite3_context *context, int argc, sqlite3_value *
459454
// save site_id string representation: 01957493c6c07e14803727e969f1d2cc
460455
cloudsync_uuid_v7_stringify(site_id, data->site_id, false);
461456

462-
JNIEnv* env = getEnv();
463-
if (!env) {
464-
sqlite3_result_error(context, "Could not get JNIEnv", -1);
465-
return;
466-
}
467-
468-
const char* javaResult = call_https_java(env);
469-
if (javaResult) {
470-
printf("Got from Java: %s\n", javaResult);
471-
__android_log_print(ANDROID_LOG_INFO, "MyNativeCode", "Got from Java: %s", javaResult);
472-
free((void*)javaResult);
473-
}
474457
// connection string is something like:
475458
// https://UUID.g5.sqlite.cloud:443/chinook.sqlite?apikey=hWDanFolRT9WDK0p54lufNrIyfgLZgtMw6tb6fbPmpo
476459
// or https://UUID.g5.sqlite.cloud:443/chinook.sqlite

test/unit.c

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
#include "dbutils.h"
2424
#include "cloudsync.h"
2525
#include "cloudsync_private.h"
26+
#include <jni.h>
27+
#include "call_java.h"
2628

2729
// declared only if macro CLOUDSYNC_UNITTEST is defined
2830
extern char *OUT_OF_MEMORY_BUFFER;
@@ -3273,6 +3275,29 @@ bool do_test_alter(int nclients, int alter_version, bool print_result, bool clea
32733275
return result;
32743276
}
32753277

3278+
bool test_call_https_java(void) {
3279+
JNIEnv* env = getEnv();
3280+
if (!env) {
3281+
printf("Could not get JNIEnv\n");
3282+
return false;
3283+
}
3284+
3285+
const char* javaResult = call_https_java(env);
3286+
if (javaResult) {
3287+
printf("Got from Java: %s\n", javaResult);
3288+
if (strcmp(javaResult, "Memory allocation failed") != 0 &&
3289+
strcmp(javaResult, "Class not found") != 0 &&
3290+
strcmp(javaResult, "Method not found") != 0 &&
3291+
strstr(javaResult, "exception") == NULL) {
3292+
free((void*)javaResult);
3293+
}
3294+
return true;
3295+
} else {
3296+
printf("call_https_java returned NULL\n");
3297+
return false;
3298+
}
3299+
}
3300+
32763301
// MARK: -
32773302

32783303
int test_report(const char *description, bool result){
@@ -3341,7 +3366,8 @@ int main(int argc, const char * argv[]) {
33413366
result += test_report("Test Alter Table 1:", do_test_alter(3, 1, print_result, cleanup_databases));
33423367
result += test_report("Test Alter Table 2:", do_test_alter(3, 2, print_result, cleanup_databases));
33433368
result += test_report("Test Alter Table 3:", do_test_alter(3, 3, print_result, cleanup_databases));
3344-
3369+
result += test_report("JNI call_https_java Test:", test_call_https_java());
3370+
33453371
finalize:
33463372
printf("\n");
33473373
if (rc != SQLITE_OK) printf("%s (%d)\n", (db) ? sqlite3_errmsg(db) : "N/A", rc);

0 commit comments

Comments
 (0)