Skip to content

Commit c2e62f0

Browse files
author
David Syer
committed
Add ignorable log file to .gitignore
1 parent 4f96edd commit c2e62f0

File tree

6 files changed

+204
-48
lines changed

6 files changed

+204
-48
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ bin
88
build.sh
99
integration-repo
1010
ivy-cache
11+
jxl.log
1112
jmx.log
1213
org.springframework.jdbc/derby.log
1314
org.springframework.spring-parent/.classpath

org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/support/RequestContext.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import org.springframework.web.context.WebApplicationContext;
4242
import org.springframework.web.servlet.LocaleResolver;
4343
import org.springframework.web.util.HtmlUtils;
44+
import org.springframework.web.util.UriTemplate;
4445
import org.springframework.web.util.UrlPathHelper;
4546
import org.springframework.web.util.WebUtils;
4647

@@ -408,6 +409,23 @@ public String getContextUrl(String relativeUrl) {
408409
return url;
409410
}
410411

412+
/**
413+
* Return a context-aware URl for the given relative URL with placeholders (named keys with braces <code>{}</code>).
414+
* @param relativeUrl the relative URL part
415+
* @param a map of parameters to insert as placeholders in the url
416+
* @return a URL that points back to the server with an absolute path
417+
* (also URL-encoded accordingly)
418+
*/
419+
public String getContextUrl(String relativeUrl, Map<String,?> params) {
420+
String url = getContextPath() + relativeUrl;
421+
UriTemplate template = new UriTemplate(url);
422+
url = template.expand(params).toASCIIString();
423+
if (this.response != null) {
424+
url = this.response.encodeURL(url);
425+
}
426+
return url;
427+
}
428+
411429
/**
412430
* Return the request URI of the original request, that is, the invoked URL
413431
* without parameters. This is particularly useful as HTML form action target,

org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/view/freemarker/spring.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
* Takes a relative URL and makes it absolute from the server root by
8787
* adding the context root for the web application.
8888
-->
89-
<#macro url relativeUrl>${springMacroRequestContext.getContextUrl(relativeUrl)}</#macro>
89+
<#macro url relativeUrl extra...><#if extra?? && extra?size!=0>${springMacroRequestContext.getContextUrl(relativeUrl,extra)}<#else>${springMacroRequestContext.getContextUrl(relativeUrl)}</#if></#macro>
9090

9191
<#--
9292
* bind

org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/DummyMacroRequestContext.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import org.springframework.web.servlet.support.BindStatus;
2424
import org.springframework.web.servlet.support.RequestContext;
25+
import org.springframework.web.util.UriTemplate;
2526

2627
/**
2728
* Dummy request context used for VTL and FTL macro tests.
@@ -134,6 +135,14 @@ public String getContextUrl(String relativeUrl) {
134135
return getContextPath() + relativeUrl;
135136
}
136137

138+
/**
139+
* @see org.springframework.web.servlet.support.RequestContext#getContextUrl(String, Map)
140+
*/
141+
public String getContextUrl(String relativeUrl, Map<String,String> params) {
142+
UriTemplate template = new UriTemplate(relativeUrl);
143+
return getContextPath() + template.expand(params).toASCIIString();
144+
}
145+
137146
/**
138147
* @see org.springframework.web.servlet.support.RequestContext#getBindStatus(String)
139148
*/

org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/freemarker/FreeMarkerMacroTests.java

Lines changed: 172 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,28 @@
1616

1717
package 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;
1926
import java.util.HashMap;
2027
import java.util.Map;
2128

2229
import javax.servlet.ServletException;
2330
import 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;
3234
import org.springframework.beans.TestBean;
35+
import org.springframework.core.io.ClassPathResource;
36+
import org.springframework.core.io.FileSystemResource;
3337
import org.springframework.mock.web.MockHttpServletRequest;
3438
import org.springframework.mock.web.MockHttpServletResponse;
3539
import org.springframework.mock.web.MockServletContext;
40+
import org.springframework.util.FileCopyUtils;
3641
import org.springframework.util.StringUtils;
3742
import org.springframework.web.context.support.StaticWebApplicationContext;
3843
import org.springframework.web.servlet.DispatcherServlet;
@@ -42,16 +47,20 @@
4247
import org.springframework.web.servlet.theme.FixedThemeResolver;
4348
import 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
}

org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/freemarker/test.ftl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ THEMEARGSWITHDEFAULTMESSAGE
3636
URL
3737
<@spring.url "/aftercontext.html"/>
3838

39+
URLPARAMS
40+
<@spring.url relativeUrl="/aftercontext/{foo}?spam={spam}" foo="bar" spam="bucket"/>
41+
3942
FORM1
4043
<@spring.formInput "command.name", ""/>
4144

0 commit comments

Comments
 (0)