KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > servlet > view > freemarker > FreeMarkerViewTests


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.web.servlet.view.freemarker;
18
19 import java.io.IOException JavaDoc;
20 import java.io.StringReader JavaDoc;
21 import java.io.Writer JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Locale JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import javax.servlet.http.HttpServletResponse JavaDoc;
27
28 import freemarker.template.Configuration;
29 import freemarker.template.Template;
30 import freemarker.template.TemplateException;
31 import junit.framework.TestCase;
32 import org.easymock.MockControl;
33
34 import org.springframework.context.ApplicationContextException;
35 import org.springframework.mock.web.MockHttpServletRequest;
36 import org.springframework.mock.web.MockHttpServletResponse;
37 import org.springframework.web.context.WebApplicationContext;
38 import org.springframework.web.servlet.DispatcherServlet;
39 import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver;
40
41 /**
42  * @author Juergen Hoeller
43  * @since 14.03.2004
44  */

45 public class FreeMarkerViewTests extends TestCase {
46
47     public void testNoFreemarkerConfig() {
48         FreeMarkerView fv = new FreeMarkerView();
49
50         MockControl wmc = MockControl.createControl(WebApplicationContext.class);
51         WebApplicationContext wac = (WebApplicationContext) wmc.getMock();
52         wac.getBeansOfType(FreeMarkerConfig.class, true, false);
53         wmc.setReturnValue(new HashMap JavaDoc());
54         wac.getParentBeanFactory();
55         wmc.setReturnValue(null);
56         wmc.replay();
57
58         fv.setUrl("anythingButNull");
59         try {
60             fv.setApplicationContext(wac);
61             fail();
62         }
63         catch (ApplicationContextException ex) {
64             // Check there's a helpful error message
65
assertTrue(ex.getMessage().indexOf("FreeMarkerConfig") != -1);
66         }
67
68         wmc.verify();
69     }
70
71     public void testNoTemplateName() throws Exception JavaDoc {
72         FreeMarkerView fv = new FreeMarkerView();
73         try {
74             fv.afterPropertiesSet();
75             fail("Should have thrown IllegalArgumentException");
76         }
77         catch (IllegalArgumentException JavaDoc ex) {
78             // Check there's a helpful error message
79
assertTrue(ex.getMessage().indexOf("url") != -1);
80         }
81     }
82
83     public void testValidTemplateName() throws Exception JavaDoc {
84         FreeMarkerView fv = new FreeMarkerView();
85
86         MockControl wmc = MockControl.createControl(WebApplicationContext.class);
87         WebApplicationContext wac = (WebApplicationContext) wmc.getMock();
88         wac.getBeansOfType(FreeMarkerConfig.class, true, false);
89         Map JavaDoc configs = new HashMap JavaDoc();
90         FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
91         configurer.setConfiguration(new TestConfiguration());
92         configs.put("freemarkerConfig", configurer);
93         wmc.setReturnValue(configs);
94         wac.getParentBeanFactory();
95         wmc.setReturnValue(null);
96         wmc.replay();
97
98         fv.setUrl("templateName");
99         fv.setApplicationContext(wac);
100
101         MockHttpServletRequest request = new MockHttpServletRequest();
102         request.addPreferredLocale(Locale.US);
103         request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
104         HttpServletResponse JavaDoc response = new MockHttpServletResponse();
105
106         Map JavaDoc model = new HashMap JavaDoc();
107         model.put("myattr", "myvalue");
108         fv.render(model, request, response);
109
110         wmc.verify();
111     }
112
113
114     private class TestConfiguration extends Configuration {
115
116         public Template getTemplate(String JavaDoc name, final Locale JavaDoc locale) throws IOException JavaDoc {
117             assertEquals("templateName", name);
118             return new Template(name, new StringReader JavaDoc("test")) {
119                 public void process(Object JavaDoc model, Writer JavaDoc writer) throws TemplateException, IOException JavaDoc {
120                     assertEquals(Locale.US, locale);
121                     assertTrue(model instanceof Map JavaDoc);
122                     Map JavaDoc modelMap = (Map JavaDoc) model;
123                     assertEquals("myvalue", modelMap.get("myattr"));
124                 }
125             };
126         }
127
128     }
129
130 }
131
Popular Tags