001: /*
002: * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
003: *
004: * This file is part of TransferCM.
005: *
006: * TransferCM is free software; you can redistribute it and/or modify it under the
007: * terms of the GNU General Public License as published by the Free Software
008: * Foundation; either version 2 of the License, or (at your option) any later
009: * version.
010: *
011: * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
012: * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
013: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
014: * details.
015: *
016: * You should have received a copy of the GNU General Public License along with
017: * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
018: * Fifth Floor, Boston, MA 02110-1301 USA
019: */
020:
021: package com.methodhead.shim;
022:
023: import java.io.*;
024: import java.util.*;
025: import java.sql.*;
026: import junit.framework.*;
027: import org.apache.log4j.*;
028: import com.methodhead.persistable.*;
029: import com.methodhead.test.*;
030: import com.methodhead.sitecontext.*;
031: import com.methodhead.*;
032: import com.methodhead.util.*;
033: import org.apache.cactus.*;
034:
035: public class TemplateTest extends ServletTestCase {
036:
037: static {
038: TestUtils.initLogger();
039: TestUtils.initDb();
040: }
041:
042: public TemplateTest(String name) {
043: super (name);
044: }
045:
046: File templateBase_ = null;
047: File template_ = null;
048: SiteContext siteContext1_ = null;
049: SiteContext siteContext2_ = null;
050:
051: protected void setUp() {
052: //setLogLevel( Level.DEBUG );
053: try {
054: ConnectionSingleton.runBatchUpdate(new FileReader(
055: "webapp/WEB-INF/db/transfer-reset.sql"));
056:
057: siteContext1_ = new SiteContext();
058: siteContext1_.setString("path", "foo");
059: siteContext1_.saveNew();
060:
061: siteContext2_ = new SiteContext();
062: siteContext2_.setString("path", "foo");
063: siteContext2_.saveNew();
064: } catch (Exception e) {
065: fail(e.getMessage());
066: }
067: }
068:
069: protected void tearDown() {
070: }
071:
072: public void testGetSiteContext() {
073: try {
074: Template template = null;
075: SiteContext siteContext = null;
076:
077: template = new Template();
078: siteContext = SiteContext.getDefaultContext();
079:
080: //
081: // get site context
082: //
083: try {
084: template.getSiteContext();
085: fail("Template.getSiteContext() did not throw an exception.");
086: } catch (ShimException e) {
087: // success
088: }
089:
090: template.setSiteContext(siteContext);
091:
092: assertNotNull(template.getSiteContext());
093: assertEquals(siteContext, template.getSiteContext());
094: } catch (Exception e) {
095: e.printStackTrace();
096: fail();
097: }
098: }
099:
100: public void testGetTemplateList() throws Exception {
101: Template t = null;
102: List l = null;
103:
104: //
105: // get the list for the default context
106: //
107: t = new Template();
108: t.setSiteContext(SiteContext.getDefaultContext());
109: l = t.getTemplateList(request);
110:
111: assertNotNull(l);
112: assertEquals(4, l.size());
113: assertEquals("template.jsp", l.get(0));
114: assertEquals("Template2.jsp", l.get(1));
115: assertEquals("template3.jsp", l.get(2));
116: assertEquals("template4.jsp", l.get(3));
117:
118: //
119: // get the list for a specific context (should be id=1)
120: //
121: t = new Template();
122: t.setSiteContext(siteContext1_);
123: l = t.getTemplateList(request);
124:
125: assertNotNull(l);
126: assertEquals(2, l.size());
127: assertEquals("template.jsp", l.get(0));
128:
129: //
130: // get the list for a specific context with no templates (should be id=2)
131: //
132: t = new Template();
133: t.setSiteContext(siteContext2_);
134: l = t.getTemplateList(request);
135:
136: assertNotNull(l);
137: assertEquals(0, l.size());
138: }
139:
140: public void testLoad() {
141: try {
142: Template t = null;
143: PanelConfig panelConfig = null;
144: Map map = null;
145:
146: TestData.createWebappFiles(ServletUtils.getRealFile(
147: request, ""));
148:
149: //
150: // try to load with an invalid template
151: //
152: try {
153: t = new Template();
154: t.setSiteContext(SiteContext.getDefaultContext());
155: t.load(request, null);
156: fail("load() did not throw an exception.");
157: } catch (ShimException e) {
158: // success
159: }
160:
161: //
162: // try to load with an invalid template
163: //
164: try {
165: t = new Template();
166: t.setSiteContext(SiteContext.getDefaultContext());
167: t.load(request, "");
168: fail("load() did not throw an exception.");
169: } catch (ShimException e) {
170: // success
171: }
172:
173: //
174: // load for the default context
175: //
176: t = new Template();
177: t.setSiteContext(SiteContext.getDefaultContext());
178: t.load(request, "template.jsp");
179:
180: assertEquals("template.jsp", t.getFileName());
181: assertNull(request.getAttribute(ShimGlobals.PANELMAP_KEY));
182:
183: map = t.getPanelConfigs();
184:
185: assertNotNull(map);
186: assertEquals(1, map.keySet().size());
187:
188: panelConfig = (PanelConfig) map.get("body");
189:
190: assertNotNull(panelConfig);
191: assertEquals(ShimGlobals.DEFAULT_MODULE, panelConfig
192: .getDefaultModule());
193:
194: //
195: // load for a particular context
196: //
197: t = new Template();
198: t.setSiteContext(siteContext1_);
199: t.load(request, "template.jsp");
200:
201: assertEquals("template.jsp", t.getFileName());
202: assertNull(request.getAttribute(ShimGlobals.PANELMAP_KEY));
203:
204: map = t.getPanelConfigs();
205:
206: assertNotNull(map);
207: assertEquals(2, map.keySet().size());
208:
209: panelConfig = (PanelConfig) map.get("body");
210:
211: assertNotNull(panelConfig);
212: assertEquals(ShimGlobals.DEFAULT_MODULE, panelConfig
213: .getDefaultModule());
214:
215: panelConfig = (PanelConfig) map.get("footer");
216:
217: assertNotNull(panelConfig);
218: assertEquals("some.Module", panelConfig.getDefaultModule());
219: } catch (Exception e) {
220: e.printStackTrace();
221: fail();
222: }
223: }
224:
225: public void testInclude() {
226: try {
227: Template template = null;
228: SiteContext siteContext = null;
229:
230: //
231: // include a template from the default site context
232: //
233: template = new Template();
234: template.setSiteContext(SiteContext.getDefaultContext());
235: template.include(request, response, "template3.jsp");
236:
237: //
238: // include a template from a specific site context
239: //
240: siteContext = new SiteContext();
241: siteContext.setInt("id", 1);
242: template.setSiteContext(siteContext);
243: template.include(request, response, "template2.jsp");
244: } catch (Exception e) {
245: e.printStackTrace();
246: fail();
247: }
248: }
249:
250: public void endInclude(WebResponse response) {
251: assertEquals("This is a template.\nThis is also a template.\n",
252: response.getText());
253: }
254: }
|