1616
1717package org .springframework .web .servlet .view .freemarker ;
1818
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 ;
1926import java .util .HashMap ;
2027import java .util .Map ;
2128
2229import javax .servlet .ServletException ;
2330import javax .servlet .http .HttpServletResponse ;
2431
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 ;
3234import org .springframework .beans .TestBean ;
35+ import org .springframework .core .io .ClassPathResource ;
36+ import org .springframework .core .io .FileSystemResource ;
3337import org .springframework .mock .web .MockHttpServletRequest ;
3438import org .springframework .mock .web .MockHttpServletResponse ;
3539import org .springframework .mock .web .MockServletContext ;
40+ import org .springframework .util .FileCopyUtils ;
3641import org .springframework .util .StringUtils ;
3742import org .springframework .web .context .support .StaticWebApplicationContext ;
3843import org .springframework .web .servlet .DispatcherServlet ;
4247import org .springframework .web .servlet .theme .FixedThemeResolver ;
4348import org .springframework .web .servlet .view .DummyMacroRequestContext ;
4449
50+ import freemarker .template .Configuration ;
51+ import freemarker .template .SimpleHash ;
52+ import freemarker .template .Template ;
53+ import freemarker .template .TemplateException ;
54+
4555/**
4656 * @author Darren Davison
4757 * @author Juergen Hoeller
4858 * @since 25.01.2005
4959 */
50- public class FreeMarkerMacroTests extends TestCase {
60+ public class FreeMarkerMacroTests {
5161
5262 private static final String TEMPLATE_FILE = "test.ftl" ;
5363
54-
5564 private StaticWebApplicationContext wac ;
5665
5766 private MockHttpServletRequest request ;
@@ -60,14 +69,14 @@ public class FreeMarkerMacroTests extends TestCase {
6069
6170 private FreeMarkerConfigurer fc ;
6271
63-
72+ @ Before
6473 public void setUp () throws Exception {
6574 wac = new StaticWebApplicationContext ();
6675 wac .setServletContext (new MockServletContext ());
6776
68- //final Template expectedTemplate = new Template();
77+ // final Template expectedTemplate = new Template();
6978 fc = new FreeMarkerConfigurer ();
70- fc .setPreferFileSystemAccess ( false );
79+ fc .setTemplateLoaderPaths ( new String [] { "classpath:/" , "file://" + System . getProperty ( "java.io.tmpdir" ) } );
7180 fc .afterPropertiesSet ();
7281
7382 wac .getDefaultListableBeanFactory ().registerSingleton ("freeMarkerConfigurer" , fc );
@@ -80,10 +89,12 @@ public void setUp() throws Exception {
8089 response = new MockHttpServletResponse ();
8190 }
8291
92+ @ Test
8393 public void testExposeSpringMacroHelpers () throws Exception {
8494 FreeMarkerView fv = new FreeMarkerView () {
8595 @ 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 {
8798 Map model = fmModel .toMap ();
8899 assertTrue (model .get (FreeMarkerView .SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE ) instanceof RequestContext );
89100 RequestContext rc = (RequestContext ) model .get (FreeMarkerView .SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE );
@@ -101,6 +112,7 @@ protected void processTemplate(Template template, SimpleHash fmModel, HttpServle
101112 fv .render (model , request , response );
102113 }
103114
115+ @ Test
104116 public void testSpringMacroRequestContextAttributeUsed () {
105117 final String helperTool = "wrongType" ;
106118
@@ -119,14 +131,143 @@ protected void processTemplate(Template template, SimpleHash model, HttpServletR
119131
120132 try {
121133 fv .render (model , request , response );
122- }
123- catch (Exception ex ) {
134+ } catch (Exception ex ) {
124135 assertTrue (ex instanceof ServletException );
125136 assertTrue (ex .getMessage ().contains (FreeMarkerView .SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE ));
126137 }
127138 }
128139
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+
130271 DummyMacroRequestContext rc = new DummyMacroRequestContext (request );
131272 Map msgMap = new HashMap ();
132273 msgMap .put ("hello" , "Howdy" );
@@ -153,13 +294,13 @@ public void testAllMacros() throws Exception {
153294 Map model = new HashMap ();
154295 model .put ("command" , tb );
155296 model .put ("springMacroRequestContext" , rc );
156- model .put ("msgArgs" , new Object [] {"World" });
297+ model .put ("msgArgs" , new Object [] { "World" });
157298 model .put ("nameOptionMap" , names );
158299 model .put ("options" , names .values ());
159300
160301 FreeMarkerView view = new FreeMarkerView ();
161302 view .setBeanName ("myView" );
162- view .setUrl ("test .ftl" );
303+ view .setUrl ("tmp .ftl" );
163304 view .setExposeSpringMacroHelpers (false );
164305 view .setConfiguration (config );
165306 view .setServletContext (new MockServletContext ());
@@ -168,36 +309,20 @@ public void testAllMacros() throws Exception {
168309
169310 // tokenize output and ignore whitespace
170311 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+ }
200324 }
325+ return null ;
201326 }
202327
203328}
0 commit comments