Debian Codegraph @

jte-jsp-converter/src/test/java/gg/jte/convert/jsp/JspToJteConverterTest.java

raw
1 package gg.jte.convert.jsp;
2
3 import gg.jte.convert.IoUtils;
4 import org.junit.jupiter.api.Test;
5 import org.junit.jupiter.api.io.TempDir;
6
7 import java.io.IOException;
8 import java.io.UncheckedIOException;
9 import java.nio.file.Files;
10 import java.nio.file.Path;
11 import java.nio.file.Paths;
12 import java.util.ArrayList;
13 import java.util.List;
14 import java.util.function.Consumer;
15 import java.util.stream.Stream;
16
17 import static org.assertj.core.api.Assertions.assertThat;
18 import static org.assertj.core.api.Assertions.catchThrowable;
19
20
21 class JspToJteConverterTest {
22 @TempDir
23 Path tempDir;
24
25 String usecase;
26 Path jspRoot;
27 Path jteRoot;
28
29 String[] notConvertedTags;
30
31 List<Consumer<Converter>> setups = new ArrayList<>();
32
33 @Test
34 void simpleTag() {
35 givenUsecase("simpleTag");
36 whenJspTagIsConverted("simple.tag", "tag/simple.jte");
37 thenConversionIsAsExpected();
38 }
39
40 @Test
41 void simpleTag_nullPrefix() {
42 givenSetup(c -> c.setPrefix(null));
43 givenUsecase("simpleTag");
44 whenJspTagIsConverted("simple.tag", "tag/simple.jte");
45 // No npe!
46 }
47
48 @Test
49 void simpleTagWithArrayParameter() {
50 givenUsecase("simpleTagWithArrayParameter");
51 whenJspTagIsConverted("simple.tag", "tag/simple.jte");
52 thenConversionIsAsExpected();
53 }
54
55 @Test
56 void simpleTagWithGenericParameter() {
57 givenUsecase("simpleTagWithGenericParameter");
58 whenJspTagIsConverted("simple.tag", "tag/simple.jte");
59 thenConversionIsAsExpected();
60 }
61
62 @Test
63 void simpleTagWithCommentBetweenParams() {
64 givenUsecase("simpleTagWithCommentBetweenParams");
65 whenJspTagIsConverted("simple.tag", "tag/simple.jte");
66 thenConversionIsAsExpected();
67 }
68
69 @Test
70 void simpleTagWithTwoOutputsAfterEachOther() {
71 givenUsecase("simpleTagWithTwoOutputsAfterEachOther");
72 whenJspTagIsConverted("simple.tag", "tag/simple.jte");
73 thenConversionIsAsExpected();
74 }
75
76 @Test
77 void simpleTagWithIfStatement() {
78 givenUsecase("simpleTagWithIfStatement");
79 whenJspTagIsConverted("simple.tag", "tag/simple.jte");
80 thenConversionIsAsExpected();
81 }
82
83 @Test
84 void simpleTagWithChooseStatement() {
85 givenUsecase("simpleTagWithChooseStatement");
86 whenJspTagIsConverted("simple.tag", "tag/simple.jte");
87 thenConversionIsAsExpected();
88 }
89
90 @Test
91 void simpleTagWithVariable() {
92 givenUsecase("simpleTagWithVariable");
93 whenJspTagIsConverted("simple.tag", "tag/simple.jte");
94 thenConversionIsAsExpected();
95 }
96
97 @Test
98 void simpleTagWithForEach() {
99 givenUsecase("simpleTagWithForEach");
100 whenJspTagIsConverted("simple.tag", "tag/simple.jte");
101 thenConversionIsAsExpected();
102 }
103
104 @Test
105 void simpleTagWithForEachAndVarStatus() {
106 givenUsecase("simpleTagWithForEachAndVarStatus");
107 whenJspTagIsConverted("simple.tag", "tag/simple.jte");
108 thenConversionIsAsExpected();
109 }
110
111 @Test
112 void simpleTagWithFormatMessage() {
113 givenUsecase("simpleTagWithFormatMessage");
114 whenJspTagIsConverted("simple.tag", "tag/simple.jte");
115 thenConversionIsAsExpected();
116 }
117
118 @Test
119 void simpleTagWithUsages() {
120 givenUsecase("simpleTagWithUsages");
121 whenJspTagIsConverted("my/simple.tag", "tag/my/simple.jte");
122 thenConversionIsAsExpected();
123 }
124
125 @Test
126 void simpleJspWithAlreadyConvertedTag() {
127 givenUsecase("simpleJspWithAlreadyConvertedTag");
128 whenJspTagIsConverted("usage.jsp", "usage.jte");
129 thenConversionIsAsExpected();
130 }
131
132 @Test
133 void jspWithAlreadyConvertedTagParametersOnSeparateLines() {
134 givenUsecase("jspWithAlreadyConvertedTagParametersOnSeparateLines");
135 whenJspTagIsConverted("usage.jsp", "usage.jte");
136 thenConversionIsAsExpected();
137 }
138
139 @Test
140 void simpleTagWithSetProperty() {
141 givenUsecase("simpleTagWithSetProperty");
142 whenJspTagIsConverted("simple.tag", "tag/simple.jte");
143 thenConversionIsAsExpected();
144 }
145
146 @Test
147 void simpleTagWithNotYetConvertedTag() {
148 givenUsecase("simpleTagWithNotYetConvertedTag");
149 Throwable throwable = catchThrowable(() -> whenJspTagIsConverted("my/simple.tag", "tag/my/simple.jte"));
150 assertThat(throwable).isInstanceOf(UnsupportedOperationException.class).hasMessage("The tag <my:simple-dependency/> is used by this tag and not converted to jte yet. You should convert <my:simple-dependency/> first. If this is a tag that should be always converted by hand, implement getNotConvertedTags() and add it there.");
151 }
152
153 @Test
154 void simpleTagWithNotYetConvertedTags() {
155 givenUsecase("simpleTagWithNotYetConvertedTags");
156 Throwable throwable = catchThrowable(() -> whenJspTagIsConverted("my/simple.tag", "tag/my/simple.jte"));
157 assertThat(throwable).isInstanceOf(UnsupportedOperationException.class).hasMessage(
158 """
159 The tag <my:simple-dependency1/> is used by this tag and not converted to jte yet. You should convert <my:simple-dependency1/> first. If this is a tag that should be always converted by hand, implement getNotConvertedTags() and add it there.
160 The tag <my:simple-dependency2/> is used by this tag and not converted to jte yet. You should convert <my:simple-dependency2/> first. If this is a tag that should be always converted by hand, implement getNotConvertedTags() and add it there.\
161 """
162 );
163 }
164
165 @Test
166 void simpleTagWithNotYetConvertedTag_allowed() {
167 notConvertedTags = new String[]{"my:simple-dependency"};
168
169 givenUsecase("simpleTagWithNotYetConvertedTag");
170 whenJspTagIsConverted("my/simple.tag", "tag/my/simple.jte");
171 thenConversionIsAsExpected();
172 }
173
174 @Test
175 void simpleTagWithInclude_fails() {
176 givenUsecase("simpleTagWithInclude");
177
178 Throwable throwable = catchThrowable(() -> whenJspTagIsConverted("simple.tag", "tag/simple.jte"));
179
180 assertThat(throwable).isInstanceOf(UnsupportedOperationException.class).hasMessage("Includes are not supported. You should convert it to a tag first, or suppress with addInlinedInclude(\"/WEB-INF/import.jsp.inc\")");
181 }
182
183 @Test
184 void simpleTagWithInclude_inlined() {
185 givenSetup(c -> c.addInlinedInclude("/WEB-INF/import.jsp.inc"));
186 givenUsecase("simpleTagWithInclude");
187
188 whenJspTagIsConverted("simple.tag", "tag/simple.jte");
189
190 thenConversionIsAsExpected();
191 }
192
193 @Test
194 void simpleTagWithManualConversion() {
195 notConvertedTags = new String[]{"fmt:formatDate"};
196 givenUsecase("simpleTagWithManualConversion");
197 whenJspTagIsConverted("simple.tag", "tag/simple.jte");
198 thenConversionIsAsExpected();
199 }
200
201 @Test
202 void simpleTagWithBody1() {
203 givenUsecase("simpleTagWithBody1");
204 whenJspTagIsConverted("simple.tag", "tag/simple.jte");
205 thenConversionIsAsExpected();
206 }
207
208 @Test
209 void simpleTagWithBody2() {
210 givenUsecase("simpleTagWithBody2");
211 whenJspTagIsConverted("simple.tag", "tag/simple.jte");
212 thenConversionIsAsExpected();
213 }
214
215 @Test
216 void simpleTagWithBody3() {
217 givenUsecase("simpleTagWithBody3");
218 whenJspTagIsConverted("simple.tag", "tag/simple.jte");
219 thenConversionIsAsExpected();
220 }
221
222 @Test
223 void simpleTagWithBodyUsages() {
224 givenUsecase("simpleTagWithBodyUsages");
225 whenJspTagIsConverted("my/simple.tag", "tag/my/simple.jte");
226 thenConversionIsAsExpected();
227 }
228
229 @Test
230 void simpleTagWithUsagesNoParams() {
231 givenUsecase("simpleTagWithUsagesNoParams");
232 whenJspTagIsConverted("my/simple.tag", "tag/my/simple.jte");
233 thenConversionIsAsExpected();
234 }
235
236 @Test
237 void simpleTagWithComment() {
238 givenUsecase("simpleTagWithComment");
239 whenJspTagIsConverted("simple.tag", "tag/simple.jte");
240 thenConversionIsAsExpected();
241 }
242
243 void givenUsecase(String usecase) {
244 jspRoot = tempDir.resolve("jsp");
245 jteRoot = tempDir.resolve("jte");
246
247 IoUtils.copyDirectory(Paths.get("testdata", usecase, "before"), tempDir);
248
249 this.usecase = usecase;
250 }
251
252 void givenSetup(Consumer<Converter> setup) {
253 setups.add(setup);
254 }
255
256 void whenJspTagIsConverted(String jspTag, String jteTag) {
257 JspToJteConverter converter = new MyConverter(jteTag);
258 converter.convertTag(jspTag, c -> {
259 c.setPrefix("@import static example.JteContext.*\n");
260 setups.forEach(s -> s.accept(c));
261 });
262 }
263
264 private void thenConversionIsAsExpected() {
265 Path expected = Paths.get("testdata", usecase, "after");
266 Path actual = tempDir;
267
268 try (Stream<Path> stream = Files.walk(expected)) {
269 stream.filter(p -> !Files.isDirectory(p) && !p.getFileName().toString().startsWith(".")).forEach(expectedFile -> {
270 Path actualFile = actual.resolve(expected.relativize(expectedFile));
271 assertThat(actualFile).exists();
272 assertThat(actualFile).hasSameTextualContentAs(expectedFile);
273 });
274 } catch (IOException e){
275 throw new UncheckedIOException(e);
276 }
277 }
278
279 private class MyConverter extends JspToJteConverter {
280
281 private final String jteTag;
282
283 public MyConverter(String jteTag) {
284 super(jspRoot, jteRoot, "my:jte");
285 this.jteTag = jteTag;
286 }
287
288 @Override
289 protected String[] getNotConvertedTags() {
290 return notConvertedTags;
291 }
292
293 @Override
294 public String suggestJteFile(String jspFile) {
295 return jteTag;
296 }
297 }
298 }