-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy pathasyncSuffixSyncDefault.test.js
More file actions
134 lines (114 loc) · 4.79 KB
/
asyncSuffixSyncDefault.test.js
File metadata and controls
134 lines (114 loc) · 4.79 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Use "Async" for the asyncSuffix, and "" for the syncSuffix.
import { beforeAll, describe, expect, test } from "vitest";
import { java } from "../testHelpers";
describe("asyncSuffixSyncDefault", () => {
beforeAll(async () => {
await new Promise((resolve) => {
java.asyncOptions = {
syncSuffix: "",
asyncSuffix: "Async",
ifReadOnlySuffix: "_alt",
};
function before(callback) {
java.classpath.push("test/");
expect(java.isJvmCreated()).toBeFalsy();
callback();
}
function after(callback) {
expect(java.isJvmCreated()).toBeTruthy();
callback();
}
java.registerClient(before, after);
java.ensureJvm(function (err) {
expect(err).toBeNull();
expect(java.isJvmCreated()).toBeTruthy();
resolve();
});
});
});
test("api", () => {
const arrayList = java.newInstanceSync("java.util.ArrayList");
expect(arrayList).toBeTruthy();
expect(java.instanceOf(arrayList, "java.util.ArrayList")).toBeTruthy();
expect(typeof arrayList.addAsync !== "undefined", "Expected `addAsync` 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.size()).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("formatAsync"), "Expected `formatAsync` to be present, but it is NOT.").toBeTruthy();
expect(!api.includes("formatSync"), "Expected `formatSync` to NOT be present, but it is.").toBeTruthy();
expect(!api.includes("formatPromise"), "Expected `formatPromise` to NOT be present, but it is.").toBeTruthy();
expect(!api.includes("formatundefined"), "Expected `formatundefined` to NOT be present, but it is.").toBeTruthy();
});
test("syncCalls", () => {
const arrayList = java.newInstanceSync("java.util.ArrayList");
arrayList.add("hello");
arrayList.add("world");
expect(arrayList.size()).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.format("%s--%s", "hello", "world")).toBe("hello--world");
});
test("asyncCalls", async () => {
const arrayList = java.newInstanceSync("java.util.ArrayList");
await new Promise((resolve) => {
arrayList.addAsync("hello", function (err) {
expect(err).toBeUndefined();
arrayList.addAsync("world", function (err) {
expect(err).toBeUndefined();
arrayList.sizeAsync(function (err, size) {
expect(err).toBeUndefined();
expect(size).toBe(2);
resolve();
});
});
});
});
});
// See testUnusableMethodName.js for the purpose of these last two tests.
// In that test, Test.name_alt() is an async method.
// In this test, it is a sync method.
test("unusableMethodNameThrows", () => {
const Test = java.import("Test");
expect(Test).toBeTruthy();
expect(() => Test.name()).toThrowError(TypeError);
});
test("alternateMethodNameWorks", () => {
const Test = java.import("Test");
expect(Test).toBeTruthy();
expect(Test.name_alt()).toBe("name");
expect(Test.caller_alt()).toBe("caller");
expect(Test.arguments_alt()).toBe("arguments");
});
test("reservedFieldName", () => {
const TestEnum = java.import("Test$Enum");
expect(TestEnum).toBeTruthy();
// 'foo' and 'bar' are valid enum names
expect(TestEnum.foo.toString()).toBe("foo");
expect(TestEnum.bar.toString()).toBe("bar");
// TestEnum.name is actually the name of the proxy constructor function.
expect(TestEnum.name).toBe("javaClassConstructorProxy");
// Instead we need to access TestEnum.name_alt
expect(TestEnum.name_alt.toString()).toBe("name");
expect(TestEnum.caller_alt.toString()).toBe("caller");
expect(TestEnum.arguments_alt.toString()).toBe("arguments");
});
});