16
16
17
17
package org .springframework .web .servlet .view .freemarker ;
18
18
19
+ import static org .junit .Assert .assertEquals ;
20
+ import static org .junit .Assert .assertNotNull ;
21
+ import static org .junit .Assert .assertTrue ;
22
+ import static org .junit .Assert .fail ;
23
+
24
+ import java .io .FileWriter ;
25
+ import java .io .InputStreamReader ;
19
26
import java .util .HashMap ;
20
27
import java .util .Map ;
21
28
22
29
import javax .servlet .ServletException ;
23
30
import javax .servlet .http .HttpServletResponse ;
24
31
25
- import freemarker .template .Configuration ;
26
- import freemarker .template .Template ;
27
- import freemarker .template .TemplateModel ;
28
- import freemarker .template .SimpleHash ;
29
- import freemarker .template .TemplateException ;
30
- import junit .framework .TestCase ;
31
-
32
+ import org .junit .Before ;
33
+ import org .junit .Test ;
32
34
import org .springframework .beans .TestBean ;
35
+ import org .springframework .core .io .ClassPathResource ;
36
+ import org .springframework .core .io .FileSystemResource ;
33
37
import org .springframework .mock .web .MockHttpServletRequest ;
34
38
import org .springframework .mock .web .MockHttpServletResponse ;
35
39
import org .springframework .mock .web .MockServletContext ;
40
+ import org .springframework .util .FileCopyUtils ;
36
41
import org .springframework .util .StringUtils ;
37
42
import org .springframework .web .context .support .StaticWebApplicationContext ;
38
43
import org .springframework .web .servlet .DispatcherServlet ;
42
47
import org .springframework .web .servlet .theme .FixedThemeResolver ;
43
48
import org .springframework .web .servlet .view .DummyMacroRequestContext ;
44
49
50
+ import freemarker .template .Configuration ;
51
+ import freemarker .template .SimpleHash ;
52
+ import freemarker .template .Template ;
53
+ import freemarker .template .TemplateException ;
54
+
45
55
/**
46
56
* @author Darren Davison
47
57
* @author Juergen Hoeller
48
58
* @since 25.01.2005
49
59
*/
50
- public class FreeMarkerMacroTests extends TestCase {
60
+ public class FreeMarkerMacroTests {
51
61
52
62
private static final String TEMPLATE_FILE = "test.ftl" ;
53
63
54
-
55
64
private StaticWebApplicationContext wac ;
56
65
57
66
private MockHttpServletRequest request ;
@@ -60,14 +69,14 @@ public class FreeMarkerMacroTests extends TestCase {
60
69
61
70
private FreeMarkerConfigurer fc ;
62
71
63
-
72
+ @ Before
64
73
public void setUp () throws Exception {
65
74
wac = new StaticWebApplicationContext ();
66
75
wac .setServletContext (new MockServletContext ());
67
76
68
- //final Template expectedTemplate = new Template();
77
+ // final Template expectedTemplate = new Template();
69
78
fc = new FreeMarkerConfigurer ();
70
- fc .setPreferFileSystemAccess ( false );
79
+ fc .setTemplateLoaderPaths ( new String [] { "classpath:/" , "file://" + System . getProperty ( "java.io.tmpdir" ) } );
71
80
fc .afterPropertiesSet ();
72
81
73
82
wac .getDefaultListableBeanFactory ().registerSingleton ("freeMarkerConfigurer" , fc );
@@ -80,10 +89,12 @@ public void setUp() throws Exception {
80
89
response = new MockHttpServletResponse ();
81
90
}
82
91
92
+ @ Test
83
93
public void testExposeSpringMacroHelpers () throws Exception {
84
94
FreeMarkerView fv = new FreeMarkerView () {
85
95
@ Override
86
- protected void processTemplate (Template template , SimpleHash fmModel , HttpServletResponse response ) throws TemplateException {
96
+ protected void processTemplate (Template template , SimpleHash fmModel , HttpServletResponse response )
97
+ throws TemplateException {
87
98
Map model = fmModel .toMap ();
88
99
assertTrue (model .get (FreeMarkerView .SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE ) instanceof RequestContext );
89
100
RequestContext rc = (RequestContext ) model .get (FreeMarkerView .SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE );
@@ -101,6 +112,7 @@ protected void processTemplate(Template template, SimpleHash fmModel, HttpServle
101
112
fv .render (model , request , response );
102
113
}
103
114
115
+ @ Test
104
116
public void testSpringMacroRequestContextAttributeUsed () {
105
117
final String helperTool = "wrongType" ;
106
118
@@ -119,14 +131,143 @@ protected void processTemplate(Template template, SimpleHash model, HttpServletR
119
131
120
132
try {
121
133
fv .render (model , request , response );
122
- }
123
- catch (Exception ex ) {
134
+ } catch (Exception ex ) {
124
135
assertTrue (ex instanceof ServletException );
125
136
assertTrue (ex .getMessage ().contains (FreeMarkerView .SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE ));
126
137
}
127
138
}
128
139
129
- public void testAllMacros () throws Exception {
140
+ @ Test
141
+ public void testName () throws Exception {
142
+ assertEquals ("Darren" , getMacroOutput ("NAME" ));
143
+ }
144
+
145
+ @ Test
146
+ public void testAge () throws Exception {
147
+ assertEquals ("99" , getMacroOutput ("AGE" ));
148
+ }
149
+
150
+ @ Test
151
+ public void testMessage () throws Exception {
152
+ assertEquals ("Howdy Mundo" , getMacroOutput ("MESSAGE" ));
153
+ }
154
+
155
+ @ Test
156
+ public void testDefaultMessage () throws Exception {
157
+ assertEquals ("hi planet" , getMacroOutput ("DEFAULTMESSAGE" ));
158
+ }
159
+
160
+ @ Test
161
+ public void testMessageArgs () throws Exception {
162
+ assertEquals ("Howdy[World]" , getMacroOutput ("MESSAGEARGS" ));
163
+ }
164
+
165
+ @ Test
166
+ public void testMessageArgsWithDefaultMessage () throws Exception {
167
+ assertEquals ("Hi" , getMacroOutput ("MESSAGEARGSWITHDEFAULTMESSAGE" ));
168
+ }
169
+
170
+ @ Test
171
+ public void testTheme () throws Exception {
172
+ assertEquals ("Howdy! Mundo!" , getMacroOutput ("THEME" ));
173
+ }
174
+
175
+ @ Test
176
+ public void testDefaultTheme () throws Exception {
177
+ assertEquals ("hi! planet!" , getMacroOutput ("DEFAULTTHEME" ));
178
+ }
179
+
180
+ @ Test
181
+ public void testThemeArgs () throws Exception {
182
+ assertEquals ("Howdy![World]" , getMacroOutput ("THEMEARGS" ));
183
+ }
184
+
185
+ @ Test
186
+ public void testThemeArgsWithDefaultMessage () throws Exception {
187
+ assertEquals ("Hi!" , getMacroOutput ("THEMEARGSWITHDEFAULTMESSAGE" ));
188
+ }
189
+
190
+ @ Test
191
+ public void testUrl () throws Exception {
192
+ assertEquals ("/springtest/aftercontext.html" , getMacroOutput ("URL" ));
193
+ }
194
+
195
+ @ Test
196
+ public void testUrlParams () throws Exception {
197
+ assertEquals ("/springtest/aftercontext/bar?spam=bucket" , getMacroOutput ("URLPARAMS" ));
198
+ }
199
+
200
+ @ Test
201
+ public void testForm1 () throws Exception {
202
+ assertEquals ("<input type=\" text\" id=\" name\" name=\" name\" value=\" Darren\" >" , getMacroOutput ("FORM1" ));
203
+ }
204
+
205
+ @ Test
206
+ public void testForm2 () throws Exception {
207
+ assertEquals ("<input type=\" text\" id=\" name\" name=\" name\" value=\" Darren\" class=\" myCssClass\" >" ,
208
+ getMacroOutput ("FORM2" ));
209
+ }
210
+
211
+ @ Test
212
+ public void testForm3 () throws Exception {
213
+ assertEquals ("<textarea id=\" name\" name=\" name\" >Darren</textarea>" , getMacroOutput ("FORM3" ));
214
+ }
215
+
216
+ @ Test
217
+ public void testForm4 () throws Exception {
218
+ assertEquals ("<textarea id=\" name\" name=\" name\" rows=10 cols=30>Darren</textarea>" , getMacroOutput ("FORM4" ));
219
+ }
220
+
221
+ // TODO verify remaining output (fix whitespace)
222
+ @ Test
223
+ public void testForm9 () throws Exception {
224
+ assertEquals ("<input type=\" password\" id=\" name\" name=\" name\" value=\" \" >" , getMacroOutput ("FORM9" ));
225
+ }
226
+
227
+ @ Test
228
+ public void testForm10 () throws Exception {
229
+ assertEquals ("<input type=\" hidden\" id=\" name\" name=\" name\" value=\" Darren\" >" ,
230
+ getMacroOutput ("FORM10" ));
231
+ }
232
+
233
+ @ Test
234
+ public void testForm11 () throws Exception {
235
+ assertEquals ("<input type=\" text\" id=\" name\" name=\" name\" value=\" Darren\" >" , getMacroOutput ("FORM11" ));
236
+ }
237
+
238
+ @ Test
239
+ public void testForm12 () throws Exception {
240
+ assertEquals ("<input type=\" hidden\" id=\" name\" name=\" name\" value=\" Darren\" >" ,
241
+ getMacroOutput ("FORM12" ));
242
+ }
243
+
244
+ @ Test
245
+ public void testForm13 () throws Exception {
246
+ assertEquals ("<input type=\" password\" id=\" name\" name=\" name\" value=\" \" >" , getMacroOutput ("FORM13" ));
247
+ }
248
+
249
+ @ Test
250
+ public void testForm15 () throws Exception {
251
+ String output = getMacroOutput ("FORM15" );
252
+ assertTrue ("Wrong output: " + output , output .startsWith ("<input type=\" hidden\" name=\" _name\" value=\" on\" />" ));
253
+ assertTrue ("Wrong output: " + output , output .contains ("<input type=\" checkbox\" id=\" name\" name=\" name\" />" ));
254
+ }
255
+
256
+ @ Test
257
+ public void testForm16 () throws Exception {
258
+ String output = getMacroOutput ("FORM16" );
259
+ assertTrue ("Wrong output: " + output , output .startsWith ("<input type=\" hidden\" name=\" _jedi\" value=\" on\" />" ));
260
+ assertTrue ("Wrong output: " + output , output .contains ("<input type=\" checkbox\" id=\" jedi\" name=\" jedi\" checked=\" checked\" />" ));
261
+ }
262
+
263
+ private String getMacroOutput (String name ) throws Exception {
264
+
265
+ String macro = fetchMacro (name );
266
+ assertNotNull (macro );
267
+
268
+ FileSystemResource resource = new FileSystemResource (System .getProperty ("java.io.tmpdir" ) + "/tmp.ftl" );
269
+ FileCopyUtils .copy ("<#import \" spring.ftl\" as spring />\n " + macro , new FileWriter (resource .getPath ()));
270
+
130
271
DummyMacroRequestContext rc = new DummyMacroRequestContext (request );
131
272
Map msgMap = new HashMap ();
132
273
msgMap .put ("hello" , "Howdy" );
@@ -153,13 +294,13 @@ public void testAllMacros() throws Exception {
153
294
Map model = new HashMap ();
154
295
model .put ("command" , tb );
155
296
model .put ("springMacroRequestContext" , rc );
156
- model .put ("msgArgs" , new Object [] {"World" });
297
+ model .put ("msgArgs" , new Object [] { "World" });
157
298
model .put ("nameOptionMap" , names );
158
299
model .put ("options" , names .values ());
159
300
160
301
FreeMarkerView view = new FreeMarkerView ();
161
302
view .setBeanName ("myView" );
162
- view .setUrl ("test .ftl" );
303
+ view .setUrl ("tmp .ftl" );
163
304
view .setExposeSpringMacroHelpers (false );
164
305
view .setConfiguration (config );
165
306
view .setServletContext (new MockServletContext ());
@@ -168,36 +309,20 @@ public void testAllMacros() throws Exception {
168
309
169
310
// tokenize output and ignore whitespace
170
311
String output = response .getContentAsString ();
171
- System .out .println (output );
172
- String [] tokens = StringUtils .tokenizeToStringArray (output , "\t \n " );
173
-
174
- for (int i = 0 ; i < tokens .length ; i ++) {
175
- if (tokens [i ].equals ("NAME" )) assertEquals ("Darren" , tokens [i + 1 ]);
176
- if (tokens [i ].equals ("AGE" )) assertEquals ("99" , tokens [i + 1 ]);
177
- if (tokens [i ].equals ("MESSAGE" )) assertEquals ("Howdy Mundo" , tokens [i + 1 ]);
178
- if (tokens [i ].equals ("DEFAULTMESSAGE" )) assertEquals ("hi planet" , tokens [i + 1 ]);
179
- if (tokens [i ].equals ("MESSAGEARGS" )) assertEquals ("Howdy[World]" , tokens [i + 1 ]);
180
- if (tokens [i ].equals ("MESSAGEARGSWITHDEFAULTMESSAGE" )) assertEquals ("Hi" , tokens [i + 1 ]);
181
- if (tokens [i ].equals ("THEME" )) assertEquals ("Howdy! Mundo!" , tokens [i + 1 ]);
182
- if (tokens [i ].equals ("DEFAULTTHEME" )) assertEquals ("hi! planet!" , tokens [i + 1 ]);
183
- if (tokens [i ].equals ("THEMEARGS" )) assertEquals ("Howdy![World]" , tokens [i + 1 ]);
184
- if (tokens [i ].equals ("THEMEARGSWITHDEFAULTMESSAGE" )) assertEquals ("Hi!" , tokens [i + 1 ]);
185
- if (tokens [i ].equals ("URL" )) assertEquals ("/springtest/aftercontext.html" , tokens [i + 1 ]);
186
- if (tokens [i ].equals ("FORM1" )) assertEquals ("<input type=\" text\" id=\" name\" name=\" name\" value=\" Darren\" >" , tokens [i + 1 ]);
187
- if (tokens [i ].equals ("FORM2" )) assertEquals ("<input type=\" text\" id=\" name\" name=\" name\" value=\" Darren\" class=\" myCssClass\" >" , tokens [i + 1 ]);
188
- if (tokens [i ].equals ("FORM3" )) assertEquals ("<textarea id=\" name\" name=\" name\" >Darren</textarea>" , tokens [i + 1 ]);
189
- if (tokens [i ].equals ("FORM4" )) assertEquals ("<textarea id=\" name\" name=\" name\" rows=10 cols=30>Darren</textarea>" , tokens [i + 1 ]);
190
- //TODO verify remaining output (fix whitespace)
191
- if (tokens [i ].equals ("FORM9" )) assertEquals ("<input type=\" password\" id=\" name\" name=\" name\" value=\" \" >" , tokens [i + 1 ]);
192
- if (tokens [i ].equals ("FORM10" )) assertEquals ("<input type=\" hidden\" id=\" name\" name=\" name\" value=\" Darren\" >" , tokens [i + 1 ]);
193
- if (tokens [i ].equals ("FORM11" )) assertEquals ("<input type=\" text\" id=\" name\" name=\" name\" value=\" Darren\" >" , tokens [i + 1 ]);
194
- if (tokens [i ].equals ("FORM12" )) assertEquals ("<input type=\" hidden\" id=\" name\" name=\" name\" value=\" Darren\" >" , tokens [i + 1 ]);
195
- if (tokens [i ].equals ("FORM13" )) assertEquals ("<input type=\" password\" id=\" name\" name=\" name\" value=\" \" >" , tokens [i + 1 ]);
196
- if (tokens [i ].equals ("FORM15" )) assertEquals ("<input type=\" hidden\" name=\" _name\" value=\" on\" />" , tokens [i + 1 ]);
197
- if (tokens [i ].equals ("FORM15" )) assertEquals ("<input type=\" checkbox\" id=\" name\" name=\" name\" />" , tokens [i + 2 ]);
198
- if (tokens [i ].equals ("FORM16" )) assertEquals ("<input type=\" hidden\" name=\" _jedi\" value=\" on\" />" , tokens [i + 1 ]);
199
- if (tokens [i ].equals ("FORM16" )) assertEquals ("<input type=\" checkbox\" id=\" jedi\" name=\" jedi\" checked=\" checked\" />" , tokens [i + 2 ]);
312
+ return output .trim ();
313
+ }
314
+
315
+ private String fetchMacro (String name ) throws Exception {
316
+ ClassPathResource resource = new ClassPathResource ("test.ftl" , getClass ());
317
+ assertTrue (resource .exists ());
318
+ String all = FileCopyUtils .copyToString (new InputStreamReader (resource .getInputStream ()));
319
+ String [] macros = StringUtils .delimitedListToStringArray (all , "\n \n " );
320
+ for (String macro : macros ) {
321
+ if (macro .startsWith (name )) {
322
+ return macro .substring (macro .indexOf ("\n " )).trim ();
323
+ }
200
324
}
325
+ return null ;
201
326
}
202
327
203
328
}
0 commit comments