-
Notifications
You must be signed in to change notification settings - Fork 290
Expand file tree
/
Copy pathasyncSuffixSyncDefault.test.ts
More file actions
150 lines (128 loc) · 5.29 KB
/
asyncSuffixSyncDefault.test.ts
File metadata and controls
150 lines (128 loc) · 5.29 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Use "Async" for the asyncSuffix, and "" for the syncSuffix.
import { beforeAll, describe, expect, test } from "vitest";
import { getJava } from "../testHelpers";
import { Java } from "../java";
describe("asyncSuffixSyncDefault", () => {
let java!: Java;
beforeAll(async () => {
let beforeCalled = false;
let afterCalled = false;
java = await getJava(
{
syncSuffix: "",
asyncSuffix: "Async",
ifReadOnlySuffix: "_alt",
},
{
beforeInit: async (java) => {
function before(callback: () => void): void {
beforeCalled = true;
java.classpath.push("test/");
expect(java.isJvmCreated()).toBeFalsy();
callback();
}
function after(callback: () => void): void {
afterCalled = true;
expect(java.isJvmCreated()).toBeTruthy();
callback();
}
java.registerClient(before, after);
await new Promise<void>((resolve) => {
java.ensureJvm((err) => {
expect(err).toBeFalsy();
expect(java.isJvmCreated()).toBeTruthy();
resolve();
});
});
},
}
);
expect(beforeCalled).toBeTruthy();
expect(afterCalled).toBeTruthy();
});
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<void>((resolve) => {
arrayList.addAsync("hello", (err: Error | undefined) => {
expect(err).toBeUndefined();
arrayList.addAsync("world", (err: Error | undefined) => {
expect(err).toBeUndefined();
arrayList.sizeAsync((err: Error | undefined, size: number | undefined) => {
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");
});
});