-
Notifications
You must be signed in to change notification settings - Fork 134
Expand file tree
/
Copy pathAbstractClassGeneratorTest.java
More file actions
183 lines (162 loc) · 7.24 KB
/
Copy pathAbstractClassGeneratorTest.java
File metadata and controls
183 lines (162 loc) · 7.24 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
/*
* Copyright 2013-2025 chronicle.software; SPDX-License-Identifier: Apache-2.0
*/
package net.openhft.chronicle.wire;
import net.openhft.chronicle.bytes.UpdateInterceptor;
import net.openhft.chronicle.core.Jvm;
import net.openhft.chronicle.wire.utils.SourceCodeFormatter;
import org.junit.jupiter.api.Test;
import java.io.StringWriter;
import java.lang.reflect.Method;
import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.function.Consumer;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assumptions.*;
// Generator for simple classes based on SimpleMetaData
class SimpleClassGenerator extends AbstractClassGenerator<SimpleMetaData> {
// Constructor initializing generator with default metadata and setting Callable as its interface
SimpleClassGenerator() {
super(new SimpleMetaData());
metaData().interfaces().add(Callable.class);
}
// Overridden method to generate the content for a method
@Override
protected void generateMethod(Method method, StringBuilder params, List<String> paramList, SourceCodeFormatter mainCode) {
// Ensure the method's name is "call"
assertEquals("call", method.getName());
// Add the generated source code line
withLineNumber(mainCode)
.append("return \"").append(metaData().message).append("\";\n");
}
}
// Generator for UI classes based on SimpleMetaData
class UIClassGenerator extends AbstractClassGenerator<SimpleMetaData> {
// Constructor initializing generator with default metadata and setting Consumer as its interface
UIClassGenerator() {
super(new SimpleMetaData());
metaData().interfaces().add(Consumer.class);
}
// Overridden method to generate constructors for the class
@Override
protected void generateConstructors(SourceCodeFormatter mainCode) {
super.generateConstructors(mainCode);
mainCode.append("public ").append(className()).append("(").append(nameForClass(UpdateInterceptor.class)).append(" updateInterceptor) {\n")
.append("this.updateInterceptor = updateInterceptor;\n")
.append("}\n");
}
// Overridden method to generate the content for a method
@Override
protected void generateMethod(Method method, StringBuilder params, List<String> paramList, SourceCodeFormatter mainCode) {
// Ensure the method's name is "accept"
assertEquals("accept", method.getName());
// Add the generated source code line
withLineNumber(mainCode)
.append("((").append(nameForClass(MyTypes.class)).append(")").append(params).append(").text().append('-').append(\"").append(metaData().message).append("\");\n");
}
}
// Metadata specific for SimpleClassGenerator
class SimpleMetaData extends AbstractClassGenerator.MetaData<SimpleMetaData> {
public String message; // Message to be used in generated methods
}
// Tests for the AbstractClassGenerator's functionality
class AbstractClassGeneratorTest extends WireTestCommon {
// Test case to validate the SimpleClassGenerator's functionality
@Test
void simpleGenerator() throws Exception {
assumeFalse(Jvm.maxDirectMemory() == 0);
doTest("Hello World");
doTest("Bye now");
doTest("The time is " + LocalDateTime.now());
}
// Helper method to perform the test with a given message
private void doTest(String message) throws Exception {
SimpleClassGenerator scg = new SimpleClassGenerator();
scg.metaData()
.packageName(Jvm.getPackageName(getClass()))
.baseClassName("ACGT")
.message = message;
Class<Callable<String>> aClass = scg.acquireClass(getClass().getClassLoader());
Callable<String> callable = aClass.getDeclaredConstructor().newInstance();
// break point on the next line to be able to debug the generated class.
String call = callable.call();
assertEquals(message, call);
}
// Test Cases
// Test case to validate the interceptor's functionality in the UIClassGenerator
@Test
void useInterceptor() throws Exception {
assumeFalse(Jvm.maxDirectMemory() == 0);
// StringWriter to capture the interceptor's output
StringWriter sw = new StringWriter();
// Define an interceptor for method updates
UpdateInterceptor ui = (methodName, t) -> {
sw.append(methodName).append(": ").append(String.valueOf(t));
boolean block = !((MyTypes) t).text().toString().equals("block");
sw.append("return: ").append(String.valueOf(block)).append("\n\n");
return block;
};
// Test the interceptor with various messages
doTest(ui, "Hello World");
doTest(ui, "block");
String theTimeIs = "The time is " + LocalDateTime.now();
doTest(ui, theTimeIs);
assertEquals("accept: !net.openhft.chronicle.wire.MyTypes {\n" +
" text: Hello World,\n" +
" flag: false,\n" +
" b: 0,\n" +
" s: 0,\n" +
" ch: \"\\0\",\n" +
" i: 0,\n" +
" f: 0.0,\n" +
" d: 0.0,\n" +
" l: 0\n" +
"}\n" +
"return: true\n" +
"\n" +
"accept: !net.openhft.chronicle.wire.MyTypes {\n" +
" text: block,\n" +
" flag: false,\n" +
" b: 0,\n" +
" s: 0,\n" +
" ch: \"\\0\",\n" +
" i: 0,\n" +
" f: 0.0,\n" +
" d: 0.0,\n" +
" l: 0\n" +
"}\n" +
"return: false\n" +
"\n" +
"accept: !net.openhft.chronicle.wire.MyTypes {\n" +
" text: \"" + theTimeIs + "\",\n" +
" flag: false,\n" +
" b: 0,\n" +
" s: 0,\n" +
" ch: \"\\0\",\n" +
" i: 0,\n" +
" f: 0.0,\n" +
" d: 0.0,\n" +
" l: 0\n" +
"}\n" +
"return: true\n" +
"\n", sw.toString());
}
// Helper method to test UIClassGenerator with a given message and update interceptor
private void doTest(UpdateInterceptor ui, String message) throws Exception {
UIClassGenerator scg = new UIClassGenerator();
scg.metaData()
.packageName(Jvm.getPackageName(getClass()))
.baseClassName("ACGTUI")
.useUpdateInterceptor(true)
.message = message;
Class<Consumer<MyTypes>> aClass = scg.acquireClass(getClass().getClassLoader());
Consumer<MyTypes> callable = aClass.getDeclaredConstructor(UpdateInterceptor.class).newInstance(ui);
// break point on the next line to be able to debug the generated class.
MyTypes mt = new MyTypes().text(message);
callable.accept(mt);
// Define expected output based on input message and assert against the actual output
String expected = message.equals("block") ? message : (message + '-' + message);
assertEquals(expected, mt.text().toString());
}
}