-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy pathdefacto.test.js
More file actions
107 lines (91 loc) · 3.92 KB
/
defacto.test.js
File metadata and controls
107 lines (91 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// In the defacto case, the developer sets asyncOptions, but specifies the defacto standard behavior.
import { beforeAll, describe, expect, test } from "vitest";
import { java } from "../testHelpers";
describe("defacto", () => {
beforeAll(async () => {
await new Promise((resolve) => {
const api = Object.keys(java).filter((key) => typeof java[key] === "function");
expect(api.includes("isJvmCreated"), "Expected `isJvmCreated` to be present, but it is NOT.").toBeTruthy();
expect(java.isJvmCreated()).toBeFalsy();
java.asyncOptions = {
syncSuffix: "Sync",
asyncSuffix: "",
};
function before() {
expect(java.isJvmCreated()).toBeFalsy();
}
function after() {
expect(java.isJvmCreated()).toBeTruthy();
}
java.registerClient(before, after);
java.registerClient(undefined, after);
java.registerClient(before, undefined);
java.ensureJvm(function (err) {
expect(err).toBeFalsy();
expect(java.isJvmCreated()).toBeTruthy();
// Verify that ensureJvm is idempotent
java.ensureJvm(function (err) {
expect(err).toBeFalsy();
resolve();
});
});
});
});
test("api", () => {
const arrayList = java.newInstanceSync("java.util.ArrayList");
expect(arrayList).toBeTruthy();
expect(java.instanceOf(arrayList, "java.util.ArrayList")).toBeTruthy();
expect(typeof arrayList.addSync !== "undefined", "Expected `addSync` to be present, but it is NOT.").toBeTruthy();
expect(typeof arrayList.add !== "undefined", "Expected `add` to be present, but it is NOT.").toBeTruthy();
expect(
typeof arrayList.addPromise === "undefined",
"Expected `addPromise` to NOT be present, but it is."
).toBeTruthy();
});
test("importClass", () => {
// Note: java.import executes javascript code in src-node/nodeJavaBridge that makes sync calls to java classes.
const ArrayList = java.import("java.util.ArrayList");
expect(ArrayList).toBeTruthy();
const arrayList = new ArrayList();
expect(arrayList).toBeTruthy();
expect(arrayList.sizeSync()).toBe(0);
});
test("staticAPI", () => {
const String = java.import("java.lang.String");
expect(String).toBeTruthy();
const api = Object.keys(String).filter((key) => typeof String[key] === "function");
expect(api.includes("format"), "Expected `format` to be present, but it is NOT.").toBeTruthy();
expect(api.includes("formatSync"), "Expected `formatSync` to be present, but it is NOT.").toBeTruthy();
expect(api.includes("formatAsync"), "Expected `formatAsync` to NOT be present, but it is.").toBeFalsy();
expect(api.includes("formatPromise"), "Expected `formatPromise` to NOT be present, but it is.").toBeFalsy();
expect(api.includes("formatundefined"), "Expected `formatundefined` to NOT be present, but it is.").toBeFalsy();
});
test("syncCalls", () => {
const arrayList = java.newInstanceSync("java.util.ArrayList");
arrayList.addSync("hello");
arrayList.addSync("world");
expect(arrayList.sizeSync()).toBe(2);
});
test("staticSyncCalls", () => {
// Note: java.import executes javascript code in src-node/nodeJavaBridge that makes sync calls to java classes.
// Among other things, java.import creates Sync functions for static methods.
const String = java.import("java.lang.String");
expect(String.formatSync("%s--%s", "hello", "world")).toBe("hello--world");
});
test("asyncCalls", async () => {
await new Promise((resolve) => {
const arrayList = java.newInstanceSync("java.util.ArrayList");
arrayList.add("hello", function (err) {
expect(err).toBeFalsy();
arrayList.add("world", function (err) {
expect(err).toBeFalsy();
arrayList.size(function (err, size) {
expect(err).toBeFalsy();
expect(size).toBe(2);
resolve();
});
});
});
});
});
});