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.util.*;
024: import java.io.*;
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.util.*;
032: import com.methodhead.*;
033: import org.apache.cactus.*;
034: import org.apache.struts.action.*;
035: import org.apache.struts.config.*;
036: import org.apache.commons.beanutils.*;
037:
038: public class IncludeModuleTest extends JspTestCase {
039:
040: static {
041: TestUtils.initLogger();
042: TestUtils.initDb();
043: }
044:
045: public IncludeModuleTest(String name) {
046: super (name);
047: }
048:
049: private Page page1_ = null;
050: private Page page2_ = null;
051: private Panel panel1_ = null;
052:
053: private IncludeModule includeModule;
054: private Page page = null;
055: private Panel panel = null;
056: private DynaActionForm dynaActionForm = null;
057: private ActionForward forward = null;
058:
059: ResultSet rs = null;
060:
061: protected void setUp() {
062: //setLogLevel( Level.DEBUG );
063: try {
064: TestData.createWebappFiles(ServletUtils.getRealFile(
065: request, ""));
066:
067: ConnectionSingleton.runBatchUpdate(new FileReader(
068: "webapp/WEB-INF/db/transfer-reset.sql"));
069:
070: panel1_ = new Panel();
071: panel1_.setName("body");
072: panel1_.setModuleClass("com.methodhead.shim.IncludeModule");
073:
074: page1_ = new Page();
075: page1_.setSiteContext(SiteContext.getDefaultContext());
076: page1_.setString("title", "Page1");
077: page1_.setString("template", "template.jsp");
078: page1_.addPanel(panel1_);
079: page1_.saveNew();
080:
081: page2_ = new Page();
082: page2_.setSiteContext(SiteContext.getDefaultContext());
083: page2_.setString("title", "Page2");
084: page2_.setString("template", "template.jsp");
085: page2_.addPanel(panel1_);
086: page2_.saveNew();
087:
088: SiteContext.setContext(request, SiteContext
089: .getDefaultContext());
090: } catch (Exception e) {
091: fail(e.getMessage());
092: }
093: }
094:
095: protected void tearDown() {
096: }
097:
098: public void testLoad() {
099: try {
100: includeModule = new IncludeModule();
101: includeModule.setInt("page_id", page1_.getInt("id"));
102: includeModule.setString("panel", "body");
103: includeModule.setString("jsp", "jsp/test.jsp");
104: includeModule.saveNew();
105:
106: includeModule = new IncludeModule();
107: includeModule.init(page1_, "body");
108: includeModule.load();
109:
110: assertEquals("jsp/test.jsp", includeModule.getString("jsp"));
111: } catch (Exception e) {
112: e.printStackTrace();
113: fail();
114: }
115: }
116:
117: public void testSave() {
118: try {
119: IncludeModule includeModule = new IncludeModule();
120:
121: includeModule = new IncludeModule();
122: includeModule.init(page1_, "body");
123: includeModule.create();
124:
125: includeModule = new IncludeModule();
126: includeModule.init(page1_, "body");
127: includeModule.setString("jsp", "jsp/test.jsp");
128: includeModule.save();
129:
130: includeModule = new IncludeModule();
131: includeModule.init(page1_, "body");
132: includeModule.load();
133:
134: assertEquals("jsp/test.jsp", includeModule.getString("jsp"));
135: } catch (Exception e) {
136: e.printStackTrace();
137: fail();
138: }
139: }
140:
141: public void testCreate() {
142: try {
143:
144: //
145: // typical create
146: //
147: includeModule = new IncludeModule();
148: includeModule.init(page1_, "body");
149: includeModule.create();
150:
151: try {
152: includeModule = new IncludeModule();
153: includeModule.load("page_id=" + page1_.getInt("id")
154: + " AND panel='body'");
155: } catch (Exception e) {
156: fail("Couldn't load include module.");
157: }
158:
159: //
160: // create again; create() should check for an existing record and there
161: // should be just one
162: //
163: includeModule = new IncludeModule();
164: includeModule.init(page1_, "body");
165: includeModule.create();
166:
167: rs = ConnectionSingleton
168: .runQuery("SELECT page_id FROM shim_include WHERE page_id="
169: + page1_.getInt("id") + " AND panel='body'");
170: assertTrue(rs.next());
171: assertTrue(!rs.next());
172: } catch (Exception e) {
173: e.printStackTrace();
174: fail();
175: }
176: }
177:
178: public void testDisplay() {
179: try {
180: //
181: // display without init'ing
182: //
183: try {
184: includeModule = new IncludeModule();
185: includeModule.display(request, response, out);
186: fail("IncludeModule.display() didn't throw an exception.");
187: } catch (Exception e) {
188: // success
189: }
190:
191: panel = new Panel();
192: panel.setName("body");
193: panel.setModuleClass("com.methodhead.shim.IncludeModule");
194:
195: page = new Page();
196: page.setString("template", "template.jsp");
197: page.addPanel(panel);
198: page.setSiteContext(SiteContext.getDefaultContext());
199: page.saveNew();
200:
201: includeModule = new IncludeModule();
202: includeModule.init(page, "body");
203: includeModule.setString("jsp", "jsp/test.jsp");
204: includeModule.saveNew();
205:
206: includeModule = new IncludeModule();
207: includeModule.init(page, "body");
208: includeModule.display(request, response, out);
209: } catch (Exception e) {
210: e.printStackTrace();
211: fail();
212: }
213: }
214:
215: public void endDisplay(WebResponse response) {
216: assertEquals("Hello from test.jsp.\n", response.getText());
217: }
218:
219: public void testDisplayException() {
220: try {
221: //
222: // display without init'ing
223: //
224: panel = new Panel();
225: panel.setName("body");
226: panel.setModuleClass("com.methodhead.shim.IncludeModule");
227:
228: page = new Page();
229: page.setString("template", "template.jsp");
230: page.addPanel(panel);
231: page.setSiteContext(SiteContext.getDefaultContext());
232: page.saveNew();
233:
234: includeModule = new IncludeModule();
235: includeModule.init(page, "body");
236: includeModule.setString("jsp", "jsp/invalid.jsp");
237: includeModule.saveNew();
238:
239: includeModule = new IncludeModule();
240: includeModule.init(page, "body");
241: includeModule.display(request, response, out);
242: } catch (Exception e) {
243: e.printStackTrace();
244: fail();
245: }
246: }
247:
248: public void endDisplayException(WebResponse response) {
249: assertTrue(response.getText().startsWith(
250: "org.apache.jasper.JasperException"));
251: }
252:
253: public void testConfigure() {
254: try {
255: dynaActionForm = new MockDynaActionForm();
256: dynaActionForm.set("pageid", "666");
257: dynaActionForm.set("panel", "body");
258:
259: includeModule = new IncludeModule();
260: forward = includeModule.configure(new ActionMapping(),
261: dynaActionForm, request, response);
262:
263: assertEquals(
264: "/configureIncludeModuleForm.do?pageid=666&panel=body",
265: forward.getPath());
266: } catch (Exception e) {
267: e.printStackTrace();
268: fail();
269: }
270: }
271:
272: public void testDestroy() {
273: try {
274: IncludeModule includeModule = new IncludeModule();
275:
276: includeModule = new IncludeModule();
277: includeModule.init(page1_, "body");
278: includeModule.create();
279:
280: includeModule = new IncludeModule();
281: includeModule.init(page1_, "body");
282: includeModule.destroy();
283:
284: try {
285: includeModule.load("page_id=" + page1_.getInt("id")
286: + " AND panel='body'");
287: fail("Module still in database.");
288: } catch (Exception e) {
289: // success
290: }
291: } catch (Exception e) {
292: e.printStackTrace();
293: fail();
294: }
295: }
296:
297: public void testCopyTo() {
298: try {
299: //
300: // copy to a new page
301: //
302: includeModule = new IncludeModule();
303: includeModule.init(page1_, "body");
304: includeModule.create();
305: includeModule.setString("jsp", "test.jsp");
306: includeModule.save();
307:
308: includeModule = new IncludeModule();
309: includeModule.init(page1_, "body");
310: includeModule.copyTo(page2_);
311:
312: includeModule = new IncludeModule();
313: includeModule.init(page2_, "body");
314: includeModule.load();
315:
316: assertEquals("test.jsp", includeModule.getString("jsp"));
317:
318: //
319: // copy to an existing page
320: //
321: includeModule.setString("jsp", "newtest.jsp");
322: includeModule.save();
323:
324: includeModule = new IncludeModule();
325: includeModule.init(page2_, "body");
326: includeModule.copyTo(page1_);
327:
328: includeModule = new IncludeModule();
329: includeModule.init(page1_, "body");
330: includeModule.load();
331:
332: assertEquals("newtest.jsp", includeModule.getString("jsp"));
333: } catch (Exception e) {
334: e.printStackTrace();
335: fail();
336: }
337: }
338: }
|