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 servletunit.struts.*;
031: import org.apache.struts.action.*;
032: import org.apache.struts.util.*;
033: import org.apache.cactus.*;
034: import com.methodhead.sitecontext.*;
035: import com.methodhead.auth.*;
036: import com.methodhead.*;
037: import com.methodhead.util.*;
038: import org.apache.commons.lang.*;
039:
040: public class ModuleFormTest extends CactusStrutsTestCase {
041:
042: String s = null;
043: LabelValueBean labelValue = null;
044: List list = null;
045: DynaActionForm form = null;
046: Panel panel = null;
047:
048: Page page1_ = null;
049: TextModule textModule1_ = null;
050: HtmlFragment fragment1_ = null;
051: HtmlFragment fragment2_ = null;
052:
053: private void createData() {
054: panel = new Panel();
055: panel.setName("body");
056: panel.setModuleClass("com.methodhead.shim.TextModule");
057:
058: page1_ = new Page();
059: page1_.set("title", "Page1");
060: page1_.set("aliasname", "page1");
061: page1_.setBoolean("hidden", false);
062: page1_.setString("template", "template.jsp");
063: page1_.addPanel(panel);
064: page1_.setSiteContext(SiteContext.getDefaultContext());
065: page1_.saveNew();
066:
067: textModule1_ = new TextModule();
068: textModule1_.init(page1_, "body");
069: textModule1_.create();
070:
071: fragment1_ = new HtmlFragment();
072: fragment1_.setSiteContext(SiteContext.getDefaultContext());
073: fragment1_.setString("name", "Fragment2");
074: fragment1_.setString("value", "This is Fragment2");
075: fragment1_.saveNew();
076:
077: fragment2_ = new HtmlFragment();
078: fragment2_.setSiteContext(SiteContext.getDefaultContext());
079: fragment2_.setString("name", "Fragment1");
080: fragment2_.setString("value", "This is Fragment1");
081: fragment2_.saveNew();
082: }
083:
084: static {
085: TestUtils.initLogger();
086: TestUtils.initDb();
087: }
088:
089: public ModuleFormTest(String name) {
090: super (name);
091: }
092:
093: public void setUp() {
094: try {
095: super .setUp();
096:
097: TestData.createWebappFiles(ServletUtils.getRealFile(
098: request, ""));
099:
100: ConnectionSingleton.runBatchUpdate(new FileReader(
101: "webapp/WEB-INF/db/transfer-reset.sql"));
102:
103: TestData.createUsers();
104: AuthUtil.setUser(request, TestData.user1);
105: SiteContext.setContext(request, SiteContext
106: .getDefaultContext());
107: } catch (Exception e) {
108: fail(e.getMessage());
109: }
110: }
111:
112: public void tearDown() throws Exception {
113: super .tearDown();
114: }
115:
116: public void testReset() {
117: try {
118: createData();
119:
120: //
121: // missing jsp
122: //
123: setRequestPathInfo("/configureTextModuleForm");
124: addRequestParameter("pageid", "1");
125: addRequestParameter("panel", "body");
126: actionPerform();
127:
128: form = (DynaActionForm) getActionForm();
129: list = (List) form.get("fragments");
130: assertEquals(2, list.size());
131:
132: labelValue = (LabelValueBean) list.get(0);
133: assertEquals("Fragment1", labelValue.getLabel());
134: assertEquals("2", labelValue.getValue());
135:
136: labelValue = (LabelValueBean) list.get(1);
137: assertEquals("Fragment2", labelValue.getLabel());
138: assertEquals("1", labelValue.getValue());
139: } catch (Exception e) {
140: e.printStackTrace();
141: fail();
142: }
143: }
144:
145: public void testValidateConfigureNavModuleMissingJsp()
146: throws Exception {
147: createData();
148:
149: //
150: // missing jsp
151: //
152: setRequestPathInfo("/configureNavModule");
153: addRequestParameter("pageid", "1");
154: addRequestParameter("type", "jsp");
155: actionPerform();
156:
157: verifyInputForward();
158: verifyActionErrors(new String[] { "shim.moduleForm.missingJsp" });
159: }
160:
161: public void testValidateConfigureNavModuleInvalidJsp()
162: throws Exception {
163: //
164: // invalid jsp
165: //
166: createData();
167: setRequestPathInfo("/configureNavModule");
168: addRequestParameter("pageid", "1");
169: addRequestParameter("type", "jsp");
170: addRequestParameter("jsp", "test.txt");
171: actionPerform();
172:
173: verifyInputForward();
174: verifyActionErrors(new String[] { "shim.moduleForm.invalidJsp" });
175: }
176:
177: public void testValidateConfigureNavModuleNonExistantJsp()
178: throws Exception {
179: //
180: // non-existant jsp
181: //
182: createData();
183: setRequestPathInfo("/configureNavModule");
184: addRequestParameter("pageid", "1");
185: addRequestParameter("type", "jsp");
186: addRequestParameter("jsp", "invalid.jsp");
187: actionPerform();
188:
189: verifyInputForward();
190: verifyActionErrors(new String[] { "shim.moduleForm.jspDoesNotExist" });
191: }
192:
193: public void testValidateConfigureNavModuleBadLevels()
194: throws Exception {
195: //
196: // bad levels
197: //
198: createData();
199: setRequestPathInfo("/configureNavModule");
200: addRequestParameter("pageid", "1");
201: addRequestParameter("type", "jsp");
202: addRequestParameter("pageid", "1");
203: addRequestParameter("panel", "body");
204: addRequestParameter("type", "folding");
205: addRequestParameter("startlev", "x");
206: addRequestParameter("contextlev", "y");
207: addRequestParameter("depthlev", "z");
208: addRequestParameter("toplev", "a");
209: addRequestParameter("header", "header");
210: addRequestParameter("link", "link");
211: addRequestParameter("curlink", "curlink");
212: addRequestParameter("footer", "footer");
213: actionPerform();
214:
215: verifyInputForward();
216: verifyActionErrors(new String[] {
217: "shim.moduleForm.invalidStartLev",
218: "shim.moduleForm.invalidContextLev",
219: "shim.moduleForm.invalidDepthLev",
220: "shim.moduleForm.invalidTopLev" });
221: }
222:
223: public void testValidateConfigureNavModuleHtmlTooLong()
224: throws Exception {
225: //
226: // html too long
227: //
228: s = StringUtils.repeat("a", 513);
229:
230: createData();
231: setRequestPathInfo("/configureNavModule");
232: addRequestParameter("pageid", "1");
233: addRequestParameter("type", "jsp");
234: addRequestParameter("pageid", "1");
235: addRequestParameter("panel", "body");
236: addRequestParameter("type", "folding");
237: addRequestParameter("startlev", "1");
238: addRequestParameter("contextlev", "1");
239: addRequestParameter("depthlev", "1");
240: addRequestParameter("toplev", "1");
241: addRequestParameter("header", s);
242: addRequestParameter("link", s);
243: addRequestParameter("curlink", s);
244: addRequestParameter("footer", s);
245: actionPerform();
246:
247: verifyInputForward();
248: verifyActionErrors(new String[] {
249: "shim.moduleForm.headerTooLong",
250: "shim.moduleForm.linkTooLong",
251: "shim.moduleForm.curlinkTooLong",
252: "shim.moduleForm.footerTooLong" });
253: }
254:
255: public void testValidateConfigureIncludeModuleMissingJsp()
256: throws Exception {
257: //
258: // missing jsp
259: //
260: setRequestPathInfo("/configureIncludeModule");
261: addRequestParameter("pageid", "1");
262: addRequestParameter("panel", "body");
263: actionPerform();
264:
265: verifyInputForward();
266: verifyActionErrors(new String[] { "shim.moduleForm.missingJsp" });
267: }
268:
269: public void testValidateConfigureIncludeModuleInvalidJsp()
270: throws Exception {
271: //
272: // invalid jsp
273: //
274: setRequestPathInfo("/configureIncludeModule");
275: addRequestParameter("pageid", "1");
276: addRequestParameter("panel", "body");
277: addRequestParameter("jsp", "test.txt");
278: actionPerform();
279:
280: verifyInputForward();
281: verifyActionErrors(new String[] { "shim.moduleForm.invalidJsp" });
282: }
283:
284: public void testValidateConfigureIncludeModuleNonExistantJsp()
285: throws Exception {
286: //
287: // non-existant jsp
288: //
289: setRequestPathInfo("/configureIncludeModule");
290: addRequestParameter("pageid", "1");
291: addRequestParameter("panel", "body");
292: addRequestParameter("jsp", "invalid.jsp");
293: actionPerform();
294:
295: verifyInputForward();
296: verifyActionErrors(new String[] { "shim.moduleForm.jspDoesNotExist" });
297: }
298: }
|