1
1
/*
2
- * Copyright 2002-2019 the original author or authors.
2
+ * Copyright 2002-2022 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
16
16
17
17
package org .springframework .web .servlet .view .freemarker ;
18
18
19
- import java .io .FileWriter ;
19
+ import java .io .IOException ;
20
20
import java .io .InputStreamReader ;
21
+ import java .nio .file .Files ;
22
+ import java .nio .file .Path ;
21
23
import java .util .HashMap ;
22
24
import java .util .Map ;
23
25
24
- import javax .servlet .ServletContext ;
25
26
import javax .servlet .ServletException ;
26
27
import javax .servlet .http .HttpServletResponse ;
27
28
34
35
35
36
import org .springframework .beans .testfixture .beans .TestBean ;
36
37
import org .springframework .core .io .ClassPathResource ;
37
- import org .springframework .core .io .FileSystemResource ;
38
38
import org .springframework .util .FileCopyUtils ;
39
39
import org .springframework .util .StringUtils ;
40
40
import org .springframework .web .context .support .StaticWebApplicationContext ;
49
49
import org .springframework .web .testfixture .servlet .MockHttpServletResponse ;
50
50
import org .springframework .web .testfixture .servlet .MockServletContext ;
51
51
52
+ import static java .nio .charset .StandardCharsets .UTF_8 ;
52
53
import static org .assertj .core .api .Assertions .assertThat ;
53
54
54
55
/**
@@ -61,35 +62,34 @@ public class FreeMarkerMacroTests {
61
62
62
63
private static final String TEMPLATE_FILE = "test.ftl" ;
63
64
64
- private StaticWebApplicationContext wac ;
65
+ private final StaticWebApplicationContext wac = new StaticWebApplicationContext () ;
65
66
66
- private MockHttpServletRequest request ;
67
+ private final MockServletContext servletContext = new MockServletContext () ;
67
68
68
- private MockHttpServletResponse response ;
69
+ private final MockHttpServletRequest request = new MockHttpServletRequest () ;
69
70
70
- private FreeMarkerConfigurer fc ;
71
+ private final MockHttpServletResponse response = new MockHttpServletResponse ();
72
+
73
+ private final FreeMarkerConfigurer fc = new FreeMarkerConfigurer ();
74
+
75
+ private Path templateLoaderPath ;
71
76
72
77
73
78
@ BeforeEach
74
79
public void setUp () throws Exception {
75
- ServletContext sc = new MockServletContext ();
76
- wac = new StaticWebApplicationContext ();
77
- wac .setServletContext (sc );
78
-
79
- // final Template expectedTemplate = new Template();
80
- fc = new FreeMarkerConfigurer ();
81
- fc .setTemplateLoaderPaths ("classpath:/" , "file://" + System .getProperty ("java.io.tmpdir" ));
82
- fc .setServletContext (sc );
80
+ this .templateLoaderPath = Files .createTempDirectory ("servlet-" ).toAbsolutePath ();
81
+
82
+ fc .setTemplateLoaderPaths ("classpath:/" , "file://" + this .templateLoaderPath );
83
+ fc .setServletContext (servletContext );
83
84
fc .afterPropertiesSet ();
84
85
86
+ wac .setServletContext (servletContext );
85
87
wac .getDefaultListableBeanFactory ().registerSingleton ("freeMarkerConfigurer" , fc );
86
88
wac .refresh ();
87
89
88
- request = new MockHttpServletRequest ();
89
90
request .setAttribute (DispatcherServlet .WEB_APPLICATION_CONTEXT_ATTRIBUTE , wac );
90
91
request .setAttribute (DispatcherServlet .LOCALE_RESOLVER_ATTRIBUTE , new AcceptHeaderLocaleResolver ());
91
92
request .setAttribute (DispatcherServlet .THEME_RESOLVER_ATTRIBUTE , new FixedThemeResolver ());
92
- response = new MockHttpServletResponse ();
93
93
}
94
94
95
95
@@ -203,12 +203,12 @@ public void testUrlParams() throws Exception {
203
203
204
204
@ Test
205
205
public void testForm1 () throws Exception {
206
- assertThat (getMacroOutput ("FORM1" )).isEqualTo ("<input type=\" text\" id=\" name\" name=\" name\" value=\" Darren\" >" );
206
+ assertThat (getMacroOutput ("FORM1" )).isEqualTo ("<input type=\" text\" id=\" name\" name=\" name\" value=\" Darren\" >" );
207
207
}
208
208
209
209
@ Test
210
210
public void testForm2 () throws Exception {
211
- assertThat (getMacroOutput ("FORM2" )).isEqualTo ("<input type=\" text\" id=\" name\" name=\" name\" value=\" Darren\" class=\" myCssClass\" >" );
211
+ assertThat (getMacroOutput ("FORM2" )).isEqualTo ("<input type=\" text\" id=\" name\" name=\" name\" value=\" Darren\" class=\" myCssClass\" >" );
212
212
}
213
213
214
214
@ Test
@@ -225,27 +225,27 @@ public void testForm4() throws Exception {
225
225
226
226
@ Test
227
227
public void testForm9 () throws Exception {
228
- assertThat (getMacroOutput ("FORM9" )).isEqualTo ("<input type=\" password\" id=\" name\" name=\" name\" value=\" \" >" );
228
+ assertThat (getMacroOutput ("FORM9" )).isEqualTo ("<input type=\" password\" id=\" name\" name=\" name\" value=\" \" >" );
229
229
}
230
230
231
231
@ Test
232
232
public void testForm10 () throws Exception {
233
- assertThat (getMacroOutput ("FORM10" )).isEqualTo ("<input type=\" hidden\" id=\" name\" name=\" name\" value=\" Darren\" >" );
233
+ assertThat (getMacroOutput ("FORM10" )).isEqualTo ("<input type=\" hidden\" id=\" name\" name=\" name\" value=\" Darren\" >" );
234
234
}
235
235
236
236
@ Test
237
237
public void testForm11 () throws Exception {
238
- assertThat (getMacroOutput ("FORM11" )).isEqualTo ("<input type=\" text\" id=\" name\" name=\" name\" value=\" Darren\" >" );
238
+ assertThat (getMacroOutput ("FORM11" )).isEqualTo ("<input type=\" text\" id=\" name\" name=\" name\" value=\" Darren\" >" );
239
239
}
240
240
241
241
@ Test
242
242
public void testForm12 () throws Exception {
243
- assertThat (getMacroOutput ("FORM12" )).isEqualTo ("<input type=\" hidden\" id=\" name\" name=\" name\" value=\" Darren\" >" );
243
+ assertThat (getMacroOutput ("FORM12" )).isEqualTo ("<input type=\" hidden\" id=\" name\" name=\" name\" value=\" Darren\" >" );
244
244
}
245
245
246
246
@ Test
247
247
public void testForm13 () throws Exception {
248
- assertThat (getMacroOutput ("FORM13" )).isEqualTo ("<input type=\" password\" id=\" name\" name=\" name\" value=\" \" >" );
248
+ assertThat (getMacroOutput ("FORM13" )).isEqualTo ("<input type=\" password\" id=\" name\" name=\" name\" value=\" \" >" );
249
249
}
250
250
251
251
@ Test
@@ -266,7 +266,7 @@ public void testForm16() throws Exception {
266
266
267
267
@ Test
268
268
public void testForm17 () throws Exception {
269
- assertThat (getMacroOutput ("FORM17" )).isEqualTo ("<input type=\" text\" id=\" spouses0.name\" name=\" spouses[0].name\" value=\" Fred\" >" );
269
+ assertThat (getMacroOutput ("FORM17" )).isEqualTo ("<input type=\" text\" id=\" spouses0.name\" name=\" spouses[0].name\" value=\" Fred\" >" );
270
270
}
271
271
272
272
@ Test
@@ -282,9 +282,7 @@ public void testForm18() throws Exception {
282
282
private String getMacroOutput (String name ) throws Exception {
283
283
String macro = fetchMacro (name );
284
284
assertThat (macro ).isNotNull ();
285
-
286
- FileSystemResource resource = new FileSystemResource (System .getProperty ("java.io.tmpdir" ) + "/tmp.ftl" );
287
- FileCopyUtils .copy ("<#import \" spring.ftl\" as spring />\n " + macro , new FileWriter (resource .getPath ()));
285
+ storeTemplateInTempDir (macro );
288
286
289
287
DummyMacroRequestContext rc = new DummyMacroRequestContext (request );
290
288
Map <String , String > msgMap = new HashMap <>();
@@ -324,28 +322,39 @@ private String getMacroOutput(String name) throws Exception {
324
322
view .setUrl ("tmp.ftl" );
325
323
view .setExposeSpringMacroHelpers (false );
326
324
view .setConfiguration (config );
327
- view .setServletContext (new MockServletContext () );
325
+ view .setServletContext (servletContext );
328
326
329
327
view .render (model , request , response );
330
328
331
- // tokenize output and ignore whitespace
332
- String output = response .getContentAsString ();
333
- output = output .replace ("\r \n " , "\n " );
334
- return output .trim ();
329
+ return getOutput ();
335
330
}
336
331
337
- private String fetchMacro (String name ) throws Exception {
338
- ClassPathResource resource = new ClassPathResource ("test.ftl" , getClass ());
339
- assertThat (resource .exists ()).isTrue ();
340
- String all = FileCopyUtils .copyToString (new InputStreamReader (resource .getInputStream ()));
341
- all = all .replace ("\r \n " , "\n " );
342
- String [] macros = StringUtils .delimitedListToStringArray (all , "\n \n " );
343
- for (String macro : macros ) {
332
+ private static String fetchMacro (String name ) throws Exception {
333
+ for (String macro : loadMacros ()) {
344
334
if (macro .startsWith (name )) {
345
335
return macro .substring (macro .indexOf ("\n " )).trim ();
346
336
}
347
337
}
348
338
return null ;
349
339
}
350
340
341
+ private static String [] loadMacros () throws IOException {
342
+ ClassPathResource resource = new ClassPathResource ("test.ftl" , FreeMarkerMacroTests .class );
343
+ assertThat (resource .exists ()).isTrue ();
344
+ String all = FileCopyUtils .copyToString (new InputStreamReader (resource .getInputStream ()));
345
+ all = all .replace ("\r \n " , "\n " );
346
+ return StringUtils .delimitedListToStringArray (all , "\n \n " );
347
+ }
348
+
349
+ private void storeTemplateInTempDir (String macro ) throws IOException {
350
+ Files .write (this .templateLoaderPath .resolve ("tmp.ftl" ),
351
+ ("<#import \" spring.ftl\" as spring />\n " + macro ).getBytes (UTF_8 ));
352
+ }
353
+
354
+ private String getOutput () throws IOException {
355
+ String output = response .getContentAsString ();
356
+ output = output .replace ("\r \n " , "\n " ).replaceAll (" +" ," " );
357
+ return output .trim ();
358
+ }
359
+
351
360
}
0 commit comments