001: /*
002: * Copyright (c) 2002-2006 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.components.template;
006:
007: import java.io.BufferedInputStream;
008: import java.io.ByteArrayInputStream;
009: import java.io.File;
010: import java.io.InputStream;
011: import java.net.URL;
012: import java.util.Map;
013:
014: import javax.servlet.ServletContext;
015: import javax.servlet.http.HttpServlet;
016:
017: import com.mockobjects.servlet.MockServletContext;
018: import com.opensymphony.util.ClassLoaderUtil;
019: import com.opensymphony.webwork.components.template.BaseTemplateEngine;
020: import com.opensymphony.webwork.components.template.Template;
021: import com.opensymphony.webwork.components.template.TemplateEngine;
022: import com.opensymphony.webwork.components.template.TemplateRenderingContext;
023: import com.opensymphony.webwork.views.JspSupportServlet;
024:
025: import junit.framework.TestCase;
026:
027: /**
028: * Test case for BaseTemplateEngine.
029: *
030: * @author tm_jee
031: * @version $Date: 2007-04-30 06:46:50 +0200 (Mon, 30 Apr 2007) $ $Id: BaseTemplateEngineTest.java 2910 2007-04-30 04:46:50Z tm_jee $
032: */
033: public class BaseTemplateEngineTest extends TestCase {
034:
035: public void testGetThemePropsThroughFileSystem() throws Exception {
036:
037: URL dummyResourceUrl = getClass().getResource(
038: "dummy.properties");
039: File dummyResourceFile = new File(dummyResourceUrl.getFile());
040: String themePropertiesDir = dummyResourceFile.getParent();
041:
042: System.out.println("dummy resource url=" + dummyResourceUrl);
043: System.out.println("resource file=" + dummyResourceFile);
044: System.out
045: .println("theme properties dir=" + themePropertiesDir);
046:
047: assertTrue(dummyResourceFile.exists());
048: assertNotNull(themePropertiesDir);
049:
050: Template template = new Template(themePropertiesDir, "theme1",
051: "template1");
052:
053: TemplateEngine templateEngine = new InnerBaseTemplateEngine(
054: "themeThroughFileSystem.properties");
055: Map propertiesMap = templateEngine.getThemeProps(template);
056:
057: assertNotNull(propertiesMap);
058: assertTrue(propertiesMap.size() > 0);
059:
060: }
061:
062: public void testGetThemePropsThroughClasspath() throws Exception {
063:
064: Template template = new Template(
065: "com/opensymphony/webwork/components/template",
066: "theme1", "template2");
067: TemplateEngine templateEngine = new InnerBaseTemplateEngine(
068: "themeThroughClassPath.properties");
069: Map propertiesMap = templateEngine.getThemeProps(template);
070:
071: assertNotNull(propertiesMap);
072: assertTrue(propertiesMap.size() > 0);
073: }
074:
075: public void testGetPropsThroughWebAppResource() throws Exception {
076:
077: final MockServletContext mockServletContext = new MockServletContext() {
078: public InputStream getResourceAsStream(String resource) {
079: if ("/template/myTheme/theme.properties"
080: .equals(resource)) {
081: return ClassLoaderUtil
082: .getResourceAsStream(
083: "com/opensymphony/webwork/components/template/dummyWithContents.properties",
084: BaseTemplateEngineTest.class);
085: }
086: return null;
087: }
088: };
089:
090: JspSupportServlet.jspSupportServlet = new JspSupportServlet() {
091: public ServletContext getServletContext() {
092: return mockServletContext;
093: }
094: };
095:
096: Template template = new Template("template", "myTheme",
097: "myComponent");
098: TemplateEngine templateEngine = new InnerBaseTemplateEngine(
099: "theme.properties");
100: Map propertiesMap = templateEngine.getThemeProps(template);
101: assertNotNull(propertiesMap);
102: assertEquals(propertiesMap.size(), 3);
103: assertEquals(propertiesMap.get("one"), "one");
104: assertEquals(propertiesMap.get("two"), "two");
105: assertEquals(propertiesMap.get("parent"), "css_xhtml");
106: }
107:
108: private boolean wentThroughWebAppPath = false;
109:
110: public void testGetPropsThroughWebAppResourceByPassingClassPathResource()
111: throws Exception {
112:
113: final MockServletContext mockServletContext = new MockServletContext() {
114: public InputStream getResourceAsStream(String resource) {
115: if ("/template/myTheme/dummy.properties"
116: .equals(resource)) {
117: wentThroughWebAppPath = true;
118: return ClassLoaderUtil
119: .getResourceAsStream(
120: "com/opensymphony/webwork/components/template/dummy.properties",
121: BaseTemplateEngineTest.class);
122: }
123: return null;
124: }
125: };
126:
127: JspSupportServlet.jspSupportServlet = new JspSupportServlet() {
128: public ServletContext getServletContext() {
129: return mockServletContext;
130: }
131: };
132:
133: wentThroughWebAppPath = false;
134:
135: Template template = new Template("template", "myTheme",
136: "myComponent");
137: TemplateEngine templateEngine = new InnerBaseTemplateEngine(
138: "dummy.properties");
139: Map propertiesMap = templateEngine.getThemeProps(template);
140:
141: assertNotNull(propertiesMap);
142: assertTrue(wentThroughWebAppPath);
143: }
144:
145: public void testGetPropsThroughClassPathResourceByPassingWebAppResource()
146: throws Exception {
147: JspSupportServlet.jspSupportServlet = null;
148: wentThroughWebAppPath = false;
149:
150: Template template = new Template("template", "myTheme",
151: "myComponent");
152: TemplateEngine templateEngine = new InnerBaseTemplateEngine(
153: "dummy.properties");
154: Map propertiesMap = templateEngine.getThemeProps(template);
155:
156: assertNotNull(propertiesMap);
157: assertFalse(wentThroughWebAppPath);
158: }
159:
160: protected void setUp() throws Exception {
161: }
162:
163: protected void tearDown() throws Exception {
164: JspSupportServlet.jspSupportServlet = null;
165: wentThroughWebAppPath = false;
166: }
167:
168: public class InnerBaseTemplateEngine extends BaseTemplateEngine {
169:
170: private String themePropertiesFileName;
171:
172: public InnerBaseTemplateEngine(String themePropertiesFileName) {
173: this .themePropertiesFileName = themePropertiesFileName;
174: }
175:
176: protected String getSuffix() {
177: return "ftl";
178: }
179:
180: public void renderTemplate(
181: TemplateRenderingContext templateContext)
182: throws Exception {
183: }
184:
185: protected String getThemePropertiesFileName() {
186: return this.themePropertiesFileName;
187: }
188: }
189: }
|