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.sql.*;
025: import java.io.*;
026: import junit.framework.*;
027: import org.apache.log4j.*;
028: import com.methodhead.persistable.*;
029: import com.methodhead.auth.*;
030: import com.methodhead.test.*;
031: import com.methodhead.sitecontext.*;
032: import com.methodhead.*;
033: import com.methodhead.util.*;
034: import servletunit.struts.*;
035: import org.apache.struts.action.*;
036:
037: public class ModuleActionTest extends CactusStrutsTestCase {
038:
039: private Page page1_ = null;
040: private Page page2_ = null;
041: private Page page3_ = null;
042: private HtmlFragment fragment1_ = null;
043:
044: private Panel panel = null;
045: private NavModule navModule = null;
046: private TextModule textModule = null;
047: private IncludeModule includeModule = null;
048:
049: static {
050: TestUtils.initLogger();
051: TestUtils.initDb();
052: }
053:
054: public ModuleActionTest(String name) {
055: super (name);
056: }
057:
058: public void setUp() {
059: //setLogLevel( Level.DEBUG );
060: try {
061: super .setUp();
062:
063: TestData.createWebappFiles(ServletUtils.getRealFile(
064: request, ""));
065:
066: //
067: // reset the database
068: //
069: ConnectionSingleton.runBatchUpdate(new FileReader(
070: "webapp/WEB-INF/db/transfer-reset.sql"));
071:
072: fragment1_ = new HtmlFragment();
073: fragment1_.setSiteContext(SiteContext.getDefaultContext());
074: fragment1_.setString("name", "Fragment1");
075: fragment1_.setString("value", "This is Fragment1");
076: fragment1_.saveNew();
077:
078: //
079: // create text test data
080: //
081: panel = new Panel();
082: panel.setName("body");
083: panel.setModuleClass("com.methodhead.shim.TextModule");
084:
085: page1_ = new Page();
086: page1_.setString("template", "template.jsp");
087: page1_.addPanel(panel);
088: page1_.setSiteContext(SiteContext.getDefaultContext());
089: page1_.saveNew();
090:
091: textModule = new TextModule();
092: textModule.init(page1_, "body");
093: textModule.create();
094:
095: textModule.setString("value", "Test text.");
096: textModule.save();
097:
098: //
099: // create nav test data
100: //
101: panel = new Panel();
102: panel.setName("body");
103: panel.setModuleClass("com.methodhead.shim.NavModule");
104:
105: page2_ = new Page();
106: page2_.setString("template", "template.jsp");
107: page2_.addPanel(panel);
108: page2_.setSiteContext(SiteContext.getDefaultContext());
109: page2_.saveNew();
110:
111: navModule = new NavModule();
112: navModule.init(page2_, "body");
113: navModule.create();
114:
115: navModule.setString("type", NavModule.TYPE_TEXTSEPARATED);
116: navModule.setString("separator", " | ");
117: navModule.save();
118:
119: //
120: // create include test data
121: //
122: panel = new Panel();
123: panel.setName("body");
124: panel.setModuleClass("com.methodhead.shim.IncludeModule");
125:
126: page3_ = new Page();
127: page3_.setString("template", "template.jsp");
128: page3_.addPanel(panel);
129: page3_.setSiteContext(SiteContext.getDefaultContext());
130: page3_.saveNew();
131:
132: includeModule = new IncludeModule();
133: includeModule.init(page3_, "body");
134: includeModule.create();
135:
136: includeModule.setString("jsp", "jsp/test.jsp");
137: includeModule.save();
138:
139: //
140: // login an admin
141: //
142: TestData.createUsers();
143: AuthUtil.setUser(getRequest(), TestData.user1);
144:
145: //
146: // set site context
147: //
148: SiteContext.setContext(request, SiteContext
149: .getDefaultContext());
150: } catch (Exception e) {
151: fail(e.getMessage());
152: }
153: }
154:
155: public void tearDown() throws Exception {
156: super .tearDown();
157: }
158:
159: public void testConfigureTextModuleForm() throws Exception {
160: DynaActionForm form = null;
161:
162: setRequestPathInfo("/configureTextModuleForm");
163: addRequestParameter("pageid", "" + page1_.getInt("id"));
164: addRequestParameter("panel", "body");
165: actionPerform();
166:
167: verifyForward("form");
168:
169: form = (DynaActionForm) getActionForm();
170:
171: assertEquals("" + page1_.getInt("id"), form.get("pageid"));
172: assertEquals("body", form.get("panel"));
173: assertEquals("Test text.", form.get("text"));
174: assertEquals("", form.get("usefragment"));
175: }
176:
177: public void testConfigureTextModuleFormUsingFragment()
178: throws Exception {
179: DynaActionForm form = null;
180:
181: //
182: // using fragment
183: //
184: textModule.setInt("htmlfragment_id", fragment1_.getInt("id"));
185: textModule.save();
186:
187: setRequestPathInfo("/configureTextModuleForm");
188: addRequestParameter("pageid", "" + page1_.getInt("id"));
189: addRequestParameter("panel", "body");
190: actionPerform();
191:
192: verifyForward("form");
193:
194: form = (DynaActionForm) getActionForm();
195:
196: assertEquals("" + page1_.getInt("id"), form.get("pageid"));
197: assertEquals("body", form.get("panel"));
198: assertEquals("Test text.", form.get("text"));
199: assertEquals("yes", form.get("usefragment"));
200: assertEquals("1", form.get("fragment"));
201: }
202:
203: public void testConfigureTextModule() {
204: try {
205: TextModule textModule = null;
206:
207: setRequestPathInfo("/configureTextModule");
208: addRequestParameter("pageid", "" + page1_.getInt("id"));
209: addRequestParameter("panel", "body");
210: addRequestParameter("text", "Updated text.");
211: addRequestParameter("usefragment", "");
212: addRequestParameter("fragment", "1");
213: actionPerform();
214:
215: verifyForwardPath("/editPage.do?id=" + page1_.getInt("id"));
216:
217: textModule = new TextModule();
218: textModule.init(page1_, "body");
219: textModule.load();
220:
221: assertEquals("Updated text.", textModule.getString("value"));
222: assertEquals(0, textModule.getInt("htmlfragment_id"));
223: } catch (Exception e) {
224: e.printStackTrace();
225: fail();
226: }
227: }
228:
229: public void testConfigureTextModuleUseFragment() {
230: try {
231: TextModule textModule = null;
232:
233: setRequestPathInfo("/configureTextModule");
234: addRequestParameter("pageid", "" + page1_.getInt("id"));
235: addRequestParameter("panel", "body");
236: addRequestParameter("text", "Updated text.");
237: addRequestParameter("usefragment", "yes");
238: addRequestParameter("fragment", "1");
239: actionPerform();
240:
241: verifyForwardPath("/editPage.do?id=" + page1_.getInt("id"));
242:
243: textModule = new TextModule();
244: textModule.init(page1_, "body");
245: textModule.load();
246:
247: assertEquals("Updated text.", textModule.getString("value"));
248: assertEquals(fragment1_.getInt("id"), textModule
249: .getInt("htmlfragment_id"));
250: } catch (Exception e) {
251: e.printStackTrace();
252: fail();
253: }
254: }
255:
256: public void testConfigureNavModuleForm() {
257: try {
258: DynaActionForm form = null;
259:
260: setRequestPathInfo("/configureNavModuleForm");
261: addRequestParameter("pageid", "" + page2_.getInt("id"));
262: addRequestParameter("panel", "body");
263: actionPerform();
264:
265: verifyForward("form");
266:
267: form = (DynaActionForm) getActionForm();
268:
269: assertEquals("" + page2_.getInt("id"), form.get("pageid"));
270: assertEquals("body", form.get("panel"));
271: assertEquals("textseparated", form.get("type"));
272: assertEquals(" | ", form.get("separator"));
273: assertEquals("", form.get("hideroot"));
274: assertEquals("1", form.get("startlev"));
275: assertEquals("1", form.get("contextlev"));
276: assertEquals("1", form.get("depthlev"));
277: assertEquals("1", form.get("toplev"));
278: assertEquals("", form.get("header"));
279: assertEquals("", form.get("link"));
280: assertEquals("", form.get("curlink"));
281: assertEquals("", form.get("footer"));
282: } catch (Exception e) {
283: e.printStackTrace();
284: fail();
285: }
286: }
287:
288: public void testConfigureNavModule() throws Exception {
289: navModule = new NavModule();
290: navModule.init(page2_, "body");
291: navModule.create();
292:
293: //
294: // jsp
295: //
296: setRequestPathInfo("/configureNavModule");
297: addRequestParameter("pageid", "" + page2_.getInt("id"));
298: addRequestParameter("panel", "body");
299: addRequestParameter("type", "jsp");
300: addRequestParameter("separator", "");
301: addRequestParameter("jsp", "jsp/nav.jsp");
302: addRequestParameter("header", "");
303: addRequestParameter("link", "");
304: addRequestParameter("curlink", "");
305: addRequestParameter("footer", "");
306: actionPerform();
307:
308: verifyNoActionErrors();
309: verifyForwardPath("/editPage.do?id=" + page2_.getInt("id"));
310:
311: navModule = new NavModule();
312: navModule.init(page2_, "body");
313: navModule.load();
314:
315: assertEquals(NavModule.TYPE_JSP, navModule.getString("type"));
316: assertEquals("jsp/nav.jsp", navModule.getString("jsp"));
317: }
318:
319: public void testConfigureNavModuleTextSeparated() throws Exception {
320: //
321: // text separated
322: //
323: setRequestPathInfo("/configureNavModule");
324: addRequestParameter("pageid", "" + page2_.getInt("id"));
325: addRequestParameter("panel", "body");
326: addRequestParameter("type", "textseparated");
327: addRequestParameter("separator", " - ");
328: addRequestParameter("hideroot", "yes");
329: addRequestParameter("jsp", "");
330: addRequestParameter("header", "HEADER");
331: addRequestParameter("link", "LINK");
332: addRequestParameter("curlink", "CURLINK");
333: addRequestParameter("footer", "FOOTER");
334: actionPerform();
335:
336: verifyForwardPath("/editPage.do?id=" + page2_.getInt("id"));
337:
338: navModule = new NavModule();
339: navModule.init(page2_, "body");
340: navModule.load();
341:
342: assertEquals(NavModule.TYPE_TEXTSEPARATED, navModule
343: .getString("type"));
344: assertEquals(" - ", navModule.getString("separator"));
345: assertEquals("HEADER", navModule.getString("header"));
346: assertEquals("FOOTER", navModule.getString("footer"));
347: assertEquals("LINK", navModule.getString("link"));
348: assertEquals("CURLINK", navModule.getString("curlink"));
349: assertEquals(true, navModule.getBoolean("hideroot"));
350: }
351:
352: public void testConfigureNavModuleFolding() throws Exception {
353: //
354: // folding
355: //
356: clearRequestParameters();
357: setRequestPathInfo("/configureNavModule");
358: addRequestParameter("pageid", "" + page2_.getInt("id"));
359: addRequestParameter("panel", "body");
360: addRequestParameter("type", "folding");
361: addRequestParameter("startlev", "2");
362: addRequestParameter("contextlev", "2");
363: addRequestParameter("depthlev", "2");
364: addRequestParameter("toplev", "2");
365: addRequestParameter("header", "HEADER");
366: addRequestParameter("link", "LINK");
367: addRequestParameter("curlink", "CURLINK");
368: addRequestParameter("footer", "FOOTER");
369: actionPerform();
370:
371: verifyForwardPath("/editPage.do?id=" + page2_.getInt("id"));
372:
373: navModule = new NavModule();
374: navModule.init(page2_, "body");
375: navModule.load();
376:
377: assertEquals(NavModule.TYPE_FOLDING, navModule
378: .getString("type"));
379: assertEquals(2, navModule.getInt("startlev"));
380: assertEquals(2, navModule.getInt("contextlev"));
381: assertEquals(2, navModule.getInt("depthlev"));
382: assertEquals(2, navModule.getInt("toplev"));
383: assertEquals("HEADER", navModule.getString("header"));
384: assertEquals("LINK", navModule.getString("link"));
385: assertEquals("CURLINK", navModule.getString("curlink"));
386: assertEquals("FOOTER", navModule.getString("footer"));
387: }
388:
389: public void testConfigureIncludeModuleForm() {
390: try {
391: DynaActionForm form = null;
392:
393: setRequestPathInfo("/configureIncludeModuleForm");
394: addRequestParameter("pageid", "" + page3_.getInt("id"));
395: addRequestParameter("panel", "body");
396: actionPerform();
397:
398: verifyForward("form");
399:
400: form = (DynaActionForm) getActionForm();
401:
402: assertEquals("" + page3_.getInt("id"), form.get("pageid"));
403: assertEquals("body", form.get("panel"));
404: assertEquals("jsp/test.jsp", form.get("jsp"));
405: } catch (Exception e) {
406: e.printStackTrace();
407: fail();
408: }
409: }
410:
411: public void testConfigureIncludeModule() {
412: try {
413: IncludeModule includeModule = null;
414:
415: setRequestPathInfo("/configureIncludeModule");
416: addRequestParameter("pageid", "" + page3_.getInt("id"));
417: addRequestParameter("panel", "body");
418: addRequestParameter("jsp", "jsp/test2.jsp");
419: actionPerform();
420:
421: verifyForwardPath("/editPage.do?id=" + page3_.getInt("id"));
422:
423: includeModule = new IncludeModule();
424: includeModule.init(page3_, "body");
425: includeModule.load();
426:
427: assertEquals("jsp/test2.jsp", includeModule
428: .getString("jsp"));
429: } catch (Exception e) {
430: e.printStackTrace();
431: fail();
432: }
433: }
434: }
|