-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy pathdynamicProxy.test.ts
More file actions
276 lines (216 loc) · 7.41 KB
/
Copy pathdynamicProxy.test.ts
File metadata and controls
276 lines (216 loc) · 7.41 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import { beforeAll, describe, expect, test } from "vitest";
import { Java, JavaObject } from "../java";
import { expectJavaError, getJava } from "../testHelpers";
describe("Dynamic Proxy", () => {
let java!: Java;
beforeAll(async () => {
java = await getJava();
});
test("0 Arguments", () => {
let callCount = 0;
const myProxy = java.newProxy("RunInterface$Interface0Arg", {
run: function () {
callCount++;
},
});
const runInterface = java.newInstanceSync("RunInterface");
runInterface.run0ArgsSync(myProxy);
expect(callCount).toBe(2);
});
test("1 Arguments", () => {
let runData = "";
const myProxy = java.newProxy("RunInterface$Interface1Arg", {
run: (str: string) => {
runData += str;
},
});
const runInterface = java.newInstanceSync("RunInterface");
runInterface.run1ArgsSync(myProxy);
expect(runData).toBe("test1test1");
});
test("1 Arguments with return data", () => {
const myProxy = java.newProxy("RunInterface$InterfaceWithReturn", {
run: (i: number) => {
return i + 1;
},
});
const runInterface = java.newInstanceSync("RunInterface");
const result = runInterface.runWithReturnSync(myProxy);
expect(result).toBe(43);
});
test("Listener test", () => {
let runData = "";
const myProxy = java.newProxy("ListenerInterface", {
onEvent: (_list: JavaObject, _runtime: JavaObject) => {
runData = "onEvent";
},
});
const listenerTester = java.newInstanceSync("ListenerTester");
listenerTester.setListenerSync(myProxy);
listenerTester.raiseEventSync();
expect(runData).toBe("onEvent");
});
test("thread", async () => {
await new Promise<void>((resolve, reject) => {
let callCount = 0;
const myProxy = java.newProxy("java.lang.Runnable", {
run: function () {
callCount++;
},
});
const thread = java.newInstanceSync("java.lang.Thread", myProxy);
thread.startSync();
let timeout = 50;
function waitForThread(): void {
if (callCount === 1) {
return resolve();
}
timeout--;
if (timeout < 0) {
return reject(new Error("Timeout"));
}
setTimeout(waitForThread, 100);
}
waitForThread();
});
});
test("thread issue #143", async () => {
await new Promise<void>((resolve) => {
const myProxy = java.newProxy("RunInterface$InterfaceWithReturn", {
run: (i: number) => {
return i - 1;
},
});
const runInterface = java.newInstanceSync("RunInterface");
runInterface.runInAnotherThread(myProxy, (err: Error | undefined, result: number) => {
expect(err).toBeUndefined();
expect(result).toBe(45);
resolve();
});
});
});
test("java equals()", () => {
const myProxy = java.newProxy("RunInterface$InterfaceWithReturn", {});
const runInterface = java.newInstanceSync("RunInterface");
const result = runInterface.runEqualsSync(myProxy);
expect(result).toBe(false);
});
test("java equals() same instance", () => {
const myProxy = java.newProxy("RunInterface$InterfaceWithReturn", {});
const runInterface = java.newInstanceSync("RunInterface");
runInterface.setInstanceSync(myProxy);
const result = runInterface.runEqualsInstanceSync(myProxy);
expect(result).toBe(true);
});
test("java equals() different instance", () => {
const myProxy = java.newProxy("RunInterface$InterfaceWithReturn", {});
const myProxy2 = java.newProxy("RunInterface$InterfaceWithReturn", {});
const runInterface = java.newInstanceSync("RunInterface");
runInterface.setInstanceSync(myProxy);
const result = runInterface.runEqualsInstanceSync(myProxy2);
expect(result).toBe(false);
});
test("js equals()", () => {
const myProxy = java.newProxy("RunInterface$InterfaceWithReturn", {
equals: (_obj: JavaObject) => {
return true;
},
});
const runInterface = java.newInstanceSync("RunInterface");
const result = runInterface.runEqualsSync(myProxy);
expect(result).toBe(true);
});
test("java hashCode()", () => {
const myProxy = java.newProxy("RunInterface$InterfaceWithReturn", {});
const runInterface = java.newInstanceSync("RunInterface");
const result = runInterface.runHashCodeSync(myProxy);
const result2 = runInterface.runHashCodeSync(myProxy);
const systemHashCode = java.callStaticMethodSync("java.lang.System", "identityHashCode", myProxy);
expect(result).toBe(result2);
expect(result).toBe(systemHashCode);
});
test("js hashCode()", () => {
const myProxy = java.newProxy("RunInterface$InterfaceWithReturn", {
hashCode: function () {
return 1234;
},
});
const runInterface = java.newInstanceSync("RunInterface");
const result = runInterface.runHashCodeSync(myProxy);
expect(result).toBe(1234);
});
test("java toString()", () => {
const myProxy = java.newProxy("RunInterface$InterfaceWithReturn", {});
const runInterface = java.newInstanceSync("RunInterface");
const result = runInterface.runToStringSync(myProxy);
expect(result).toBe("[object Object]");
});
test("js toString()", () => {
const myProxy = java.newProxy("RunInterface$InterfaceWithReturn", {
toString: () => {
return "myRunInterface";
},
});
const runInterface = java.newInstanceSync("RunInterface");
const result = runInterface.runToStringSync(myProxy);
expect(result).toBe("myRunInterface");
});
test("js string error", () => {
const myProxy = java.newProxy("RunInterface$InterfaceWithReturn", {
run: (_i: JavaObject) => {
// eslint-disable-next-line @typescript-eslint/only-throw-error
throw "myError";
},
});
const runInterface = java.newInstanceSync("RunInterface");
try {
runInterface.runWithReturnSync(myProxy);
throw new Error("Exception was not thrown");
} catch (e) {
expectJavaError(e);
expect(e.cause.getClassSync().getNameSync()).toBe("java.lang.RuntimeException");
expect(e.message).toMatch(/Caused by: node\.NodeJsException:.*myError/);
}
});
test("js Error", () => {
const myProxy = java.newProxy("RunInterface$InterfaceWithReturn", {
run: (_i: JavaObject) => {
throw new Error("newError");
},
});
const runInterface = java.newInstanceSync("RunInterface");
try {
runInterface.runWithReturnSync(myProxy);
throw new Error("Exception was not thrown");
} catch (e) {
expectJavaError(e);
expect(e.cause.getClassSync().getNameSync()).toBe("java.lang.RuntimeException");
expect(e.message).toMatch(/Caused by: node\.NodeJsException:.*newError/);
}
});
test("invocationHandler", () => {
const myProxy = java.newProxy("RunInterface$InterfaceWithReturn", {
run: (i: number) => {
return i + 2;
},
});
const result = myProxy.invocationHandler.run(42);
expect(result).toBe(44);
});
test("unref", () => {
const myProxy = java.newProxy("RunInterface$InterfaceWithReturn", {
run: (i: number) => {
return i + 1;
},
});
myProxy.unref();
try {
myProxy.invocationHandler.run(42);
} catch (e) {
expectJavaError(e);
expect(e.message).toBe("dynamicProxyData has been destroyed or corrupted");
}
// call again
myProxy.unref();
});
});