-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientCallablesTest.java
More file actions
309 lines (268 loc) · 11.7 KB
/
ClientCallablesTest.java
File metadata and controls
309 lines (268 loc) · 11.7 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*-
* #%L
* Json Migration Helper
* %%
* Copyright (C) 2025 Flowing Code
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
package com.flowingcode.vaadin.jsonmigration;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import com.vaadin.flow.component.ClientCallable;
import com.vaadin.flow.component.Component;
import elemental.json.JsonValue;
import java.lang.reflect.Method;
import org.junit.Test;
public abstract class ClientCallablesTest {
protected abstract <T extends Component> Class<? extends T> instrumentClass(Class<T> clazz);
private static final String MESSAGE = "instrumented method is not annotated with @ClientCallable";
/**
* Finds the test method declared in the instrumented class that is annotated
* with @ClientCallable.
*
* @param c the instrumented callable instance
* @return the Method if found, null otherwise
*/
protected static Method getClientCallableTestMethod(Class<? extends Component> clazz) {
for (Method method : clazz.getDeclaredMethods()) {
if ("test".equals(method.getName()) && method.isAnnotationPresent(ClientCallable.class)) {
return method;
}
}
return null;
}
/**
* Invokes the test method on the instrumented instance via reflection.
*
* @param instrumented the instrumented instance
* @param args the arguments to pass to the test method
* @return the result of the method invocation
* @throws Exception if invocation fails
*/
private Object invokeTestMethod(BaseClientCallable instrumented, Object... args)
throws Exception {
Method testMethod = getClientCallableTestMethod(instrumented.getClass());
assertTrue(MESSAGE, testMethod != null);
return testMethod.invoke(instrumented, args);
}
protected abstract Object createJsonNull();
protected abstract Object createJsonBoolean();
protected abstract Object createJsonNumber();
protected abstract Object createJsonString();
protected abstract Object createJsonArray();
protected abstract Object createJsonObject();
protected abstract Object createArrayOfJsonObject();
@Test
public void test__V() throws Exception {
ClientCallable__V instrumented =
instrumentClass(ClientCallable__V.class).getDeclaredConstructor().newInstance();
invokeTestMethod(instrumented);
assertTrue(instrumented.hasBeenTraced());
}
@Test
public void test_Z__V() throws Exception {
ClientCallable_Z__V instrumented =
instrumentClass(ClientCallable_Z__V.class).getDeclaredConstructor().newInstance();
invokeTestMethod(instrumented, true);
assertTrue(instrumented.hasBeenTraced());
}
@Test
public void test_I__V() throws Exception {
ClientCallable_I__V instrumented =
instrumentClass(ClientCallable_I__V.class).getDeclaredConstructor().newInstance();
invokeTestMethod(instrumented, 42);
assertTrue(instrumented.hasBeenTraced());
}
@Test
public void test_D__V() throws Exception {
ClientCallable_D__V instrumented =
instrumentClass(ClientCallable_D__V.class).getDeclaredConstructor().newInstance();
invokeTestMethod(instrumented, 3.14);
assertTrue(instrumented.hasBeenTraced());
}
@Test
public void test_String__V() throws Exception {
ClientCallable_String__V instrumented =
instrumentClass(ClientCallable_String__V.class).getDeclaredConstructor().newInstance();
invokeTestMethod(instrumented, "test");
assertTrue(instrumented.hasBeenTraced());
}
@Test
public void test__Z() throws Exception {
ClientCallable__Z instrumented =
instrumentClass(ClientCallable__Z.class).getDeclaredConstructor().newInstance();
ClientCallable__Z nonInstrumented = new ClientCallable__Z();
boolean result = (boolean) invokeTestMethod(instrumented);
assertTrue(instrumented.hasBeenTraced());
assertEquals(nonInstrumented.test(), result);
}
@Test
public void test__I() throws Exception {
ClientCallable__I instrumented =
instrumentClass(ClientCallable__I.class).getDeclaredConstructor().newInstance();
ClientCallable__I nonInstrumented = new ClientCallable__I();
int result = (int) invokeTestMethod(instrumented);
assertTrue(instrumented.hasBeenTraced());
assertEquals(nonInstrumented.test(), result);
}
@Test
public void test__D() throws Exception {
ClientCallable__D instrumented =
instrumentClass(ClientCallable__D.class).getDeclaredConstructor().newInstance();
ClientCallable__D nonInstrumented = new ClientCallable__D();
double result = (double) invokeTestMethod(instrumented);
assertTrue(instrumented.hasBeenTraced());
assertEquals(nonInstrumented.test(), result, 0.0);
}
@Test
public void test__Integer() throws Exception {
ClientCallable__Integer instrumented =
instrumentClass(ClientCallable__Integer.class).getDeclaredConstructor().newInstance();
ClientCallable__Integer nonInstrumented = new ClientCallable__Integer();
Integer result = (Integer) invokeTestMethod(instrumented);
assertTrue(instrumented.hasBeenTraced());
assertEquals(nonInstrumented.test(), result);
}
@Test
public void test__JsonValue() throws Exception {
ClientCallable__JsonValue instrumented =
instrumentClass(ClientCallable__JsonValue.class).getDeclaredConstructor().newInstance();
ClientCallable__JsonValue nonInstrumented = new ClientCallable__JsonValue();
JsonValue result = (JsonValue) invokeTestMethod(instrumented);
assertTrue(instrumented.hasBeenTraced());
assertEquals(nonInstrumented.test().toJson(), result.toJson());
}
@Test
public void test__JsonBoolean() throws Exception {
ClientCallable__JsonBoolean instrumented =
instrumentClass(ClientCallable__JsonBoolean.class).getDeclaredConstructor().newInstance();
ClientCallable__JsonBoolean nonInstrumented = new ClientCallable__JsonBoolean();
JsonValue result = (JsonValue) invokeTestMethod(instrumented);
assertTrue(instrumented.hasBeenTraced());
assertEquals(nonInstrumented.test().toJson(), result.toJson());
}
@Test
public void test__JsonNumber() throws Exception {
ClientCallable__JsonNumber instrumented =
instrumentClass(ClientCallable__JsonNumber.class).getDeclaredConstructor().newInstance();
ClientCallable__JsonNumber nonInstrumented = new ClientCallable__JsonNumber();
JsonValue result = (JsonValue) invokeTestMethod(instrumented);
assertTrue(instrumented.hasBeenTraced());
assertEquals(nonInstrumented.test().toJson(), result.toJson());
}
@Test
public void test__JsonString() throws Exception {
ClientCallable__JsonString instrumented =
instrumentClass(ClientCallable__JsonString.class).getDeclaredConstructor().newInstance();
ClientCallable__JsonString nonInstrumented = new ClientCallable__JsonString();
JsonValue result = (JsonValue) invokeTestMethod(instrumented);
assertTrue(instrumented.hasBeenTraced());
assertEquals(nonInstrumented.test().toJson(), result.toJson());
}
@Test
public void test__JsonNull() throws Exception {
ClientCallable__JsonNull instrumented =
instrumentClass(ClientCallable__JsonNull.class).getDeclaredConstructor().newInstance();
ClientCallable__JsonNull nonInstrumented = new ClientCallable__JsonNull();
JsonValue result = (JsonValue) invokeTestMethod(instrumented);
assertTrue(instrumented.hasBeenTraced());
assertEquals(nonInstrumented.test().toJson(), result.toJson());
}
@Test
public void test__JsonArray() throws Exception {
ClientCallable__JsonArray instrumented =
instrumentClass(ClientCallable__JsonArray.class).getDeclaredConstructor().newInstance();
ClientCallable__JsonArray nonInstrumented = new ClientCallable__JsonArray();
JsonValue result = (JsonValue) invokeTestMethod(instrumented);
assertTrue(instrumented.hasBeenTraced());
assertEquals(nonInstrumented.test().toJson(), result.toJson());
}
@Test
public void test__JsonObject() throws Exception {
ClientCallable__JsonObject instrumented =
instrumentClass(ClientCallable__JsonObject.class).getDeclaredConstructor().newInstance();
ClientCallable__JsonObject nonInstrumented = new ClientCallable__JsonObject();
JsonValue result = (JsonValue) invokeTestMethod(instrumented);
assertTrue(instrumented.hasBeenTraced());
assertEquals(nonInstrumented.test().toJson(), result.toJson());
}
@Test
public void test_JsonValue__V() throws Exception {
ClientCallable_JsonValue__V instrumented =
instrumentClass(ClientCallable_JsonValue__V.class).getDeclaredConstructor().newInstance();
invokeTestMethod(instrumented, createJsonNull());
assertTrue(instrumented.hasBeenTraced());
}
@Test
public void test_JsonBoolean__V() throws Exception {
ClientCallable_JsonBoolean__V instrumented =
instrumentClass(ClientCallable_JsonBoolean__V.class).getDeclaredConstructor().newInstance();
invokeTestMethod(instrumented, createJsonBoolean());
assertTrue(instrumented.hasBeenTraced());
}
@Test
public void test_JsonNumber__V() throws Exception {
ClientCallable_JsonNumber__V instrumented =
instrumentClass(ClientCallable_JsonNumber__V.class).getDeclaredConstructor().newInstance();
invokeTestMethod(instrumented, createJsonNumber());
assertTrue(instrumented.hasBeenTraced());
}
@Test
public void test_JsonString__V() throws Exception {
ClientCallable_JsonString__V instrumented =
instrumentClass(ClientCallable_JsonString__V.class).getDeclaredConstructor().newInstance();
invokeTestMethod(instrumented, createJsonString());
assertTrue(instrumented.hasBeenTraced());
}
@Test
public void test_JsonNull__V() throws Exception {
ClientCallable_JsonNull__V instrumented =
instrumentClass(ClientCallable_JsonNull__V.class).getDeclaredConstructor().newInstance();
invokeTestMethod(instrumented, createJsonNull());
assertTrue(instrumented.hasBeenTraced());
}
@Test
public void test_JsonArray__V() throws Exception {
ClientCallable_JsonArray__V instrumented =
instrumentClass(ClientCallable_JsonArray__V.class).getDeclaredConstructor().newInstance();
invokeTestMethod(instrumented, createJsonArray());
assertTrue(instrumented.hasBeenTraced());
}
@Test
public void test_JsonObject__V() throws Exception {
ClientCallable_JsonObject__V instrumented =
instrumentClass(ClientCallable_JsonObject__V.class).getDeclaredConstructor().newInstance();
invokeTestMethod(instrumented, createJsonObject());
assertTrue(instrumented.hasBeenTraced());
}
@Test
public void test_ArrayOfJsonObject__V() throws Exception {
ClientCallable_ArrayOfJsonObject__V instrumented =
instrumentClass(ClientCallable_ArrayOfJsonObject__V.class)
.getDeclaredConstructor()
.newInstance();
invokeTestMethod(instrumented, createArrayOfJsonObject());
assertTrue(instrumented.hasBeenTraced());
}
@Test
public void test_JsonObjectArgs__V() throws Exception {
ClientCallable_JsonObjectVarargs__V instrumented =
instrumentClass(ClientCallable_JsonObjectVarargs__V.class)
.getDeclaredConstructor()
.newInstance();
invokeTestMethod(instrumented, createArrayOfJsonObject());
assertTrue(instrumented.hasBeenTraced());
}
}