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 junit.framework.*;
026: import org.apache.log4j.*;
027: import com.methodhead.persistable.*;
028: import com.methodhead.test.*;
029: import servletunit.struts.*;
030: import org.apache.struts.action.*;
031: import org.apache.cactus.*;
032: import com.methodhead.sitecontext.*;
033: import com.methodhead.auth.*;
034: import com.methodhead.*;
035: import com.methodhead.util.*;
036: import java.io.*;
037: import org.apache.struts.util.*;
038:
039: public class PageFormTest extends CactusStrutsTestCase {
040:
041: private Panel panel = null;
042:
043: private Page page1_ = null;
044: private Page page2_ = null;
045: private Link link1_ = null;
046: private Link link2_ = null;
047:
048: DynaActionForm dynaForm = null;
049: List list = null;
050: LabelValueBean labelValueBean = null;
051:
052: static {
053: TestUtils.initLogger();
054: TestUtils.initDb();
055: }
056:
057: public PageFormTest(String name) {
058: super (name);
059: }
060:
061: public void setUp() {
062: try {
063: super .setUp();
064:
065: TestData.createWebappFiles(ServletUtils.getRealFile(
066: request, ""));
067:
068: ConnectionSingleton.runBatchUpdate(new FileReader(
069: "webapp/WEB-INF/db/transfer-reset.sql"));
070:
071: SiteContext.setContext(request, SiteContext
072: .getDefaultContext());
073:
074: panel = new Panel();
075: panel.setName("body");
076: panel.setModuleClass("com.methodhead.shim.MockModule");
077:
078: page1_ = new Page();
079: page1_.set("title", "Page1");
080: page1_.set("aliasname", "page1");
081: page1_.set("template", "template.jsp");
082: page1_.addPanel(panel);
083: page1_.setSiteContext(SiteContext.getDefaultContext());
084: page1_.saveNew();
085:
086: page2_ = new Page();
087: page2_.set("title", "Page2");
088: page2_.set("aliasname", "page2");
089: page2_.set("template", "template.jsp");
090: page2_.addPanel(panel);
091: page2_.setSiteContext(SiteContext.getDefaultContext());
092: page2_.saveNew();
093:
094: TestData.createUsers();
095: AuthUtil.setUser(request, TestData.user1);
096:
097: getSession().setAttribute(SiteContext.SITECONTEXT_KEY,
098: SiteContext.getDefaultContext());
099: getSession().setAttribute(ShimGlobals.SITEMAPTREE_KEY,
100: new SiteMapTree());
101:
102: link1_ = new Link();
103: link1_.setPageId(1);
104: link1_.setTitle("Page1");
105:
106: link2_ = new Link();
107: link2_.setPageId(2);
108: link2_.setTitle("Page2");
109:
110: link1_.add(link2_);
111:
112: SiteMap siteMap = ShimUtils.getSiteMap(request);
113: siteMap.setRoot(link1_);
114: siteMap.save();
115:
116: SiteMapTree tree = (SiteMapTree) getSession().getAttribute(
117: ShimGlobals.SITEMAPTREE_KEY);
118: tree.build(siteMap);
119: } catch (Exception e) {
120: fail(e.getMessage());
121: }
122: }
123:
124: public void tearDown() throws Exception {
125: super .tearDown();
126: }
127:
128: public void testReset() {
129: //
130: // the /newPage action should be enough to test with
131: //
132: setRequestPathInfo("/newPageForm");
133: actionPerform();
134:
135: dynaForm = (DynaActionForm) getActionForm();
136: list = (List) dynaForm.get("templates");
137:
138: assertNotNull(list);
139: assertEquals(5, list.size());
140:
141: //
142: // labels should be upcased, values should be the file names
143: //
144: labelValueBean = (LabelValueBean) list.get(0);
145: assertEquals("Select...", labelValueBean.getLabel());
146: assertEquals("", labelValueBean.getValue());
147:
148: labelValueBean = (LabelValueBean) list.get(1);
149: assertEquals("Template", labelValueBean.getLabel());
150: assertEquals("template.jsp", labelValueBean.getValue());
151:
152: labelValueBean = (LabelValueBean) list.get(2);
153: assertEquals("Template2", labelValueBean.getLabel());
154: assertEquals("Template2.jsp", labelValueBean.getValue());
155:
156: labelValueBean = (LabelValueBean) list.get(3);
157: assertEquals("Template3", labelValueBean.getLabel());
158: assertEquals("template3.jsp", labelValueBean.getValue());
159:
160: labelValueBean = (LabelValueBean) list.get(4);
161: assertEquals("Template4", labelValueBean.getLabel());
162: assertEquals("template4.jsp", labelValueBean.getValue());
163: }
164:
165: public void testValidateInvalidAlias() throws Exception {
166: //
167: // save a new page with an invalid alias
168: //
169: setRequestPathInfo("/configurePage");
170: addRequestParameter("id", "");
171: addRequestParameter("title", "Test");
172: addRequestParameter("alias", "Test Alias");
173: addRequestParameter("template", "template.jsp");
174: actionPerform();
175:
176: verifyInputForward();
177: verifyActionErrors(new String[] { "shim.pageForm.invalidAlias" });
178: }
179:
180: public void testValidateAliasUsed() throws Exception {
181: //
182: // save a new page with an existing alias
183: //
184: setRequestPathInfo("/configurePage");
185: addRequestParameter("id", "");
186: addRequestParameter("title", "Test");
187: addRequestParameter("alias", "page1");
188: addRequestParameter("template", "template.jsp");
189: actionPerform();
190:
191: verifyInputForward();
192: verifyActionErrors(new String[] { "shim.pageForm.aliasUsed" });
193: }
194:
195: public void testValidateAliasUsed2() throws Exception {
196: //
197: // save a new page with another page's alias
198: //
199: setRequestPathInfo("/configurePage");
200: addRequestParameter("id", "" + page1_.getInt("id"));
201: addRequestParameter("title", "Test");
202: addRequestParameter("alias", "page2");
203: addRequestParameter("template", "template.jsp");
204: actionPerform();
205:
206: verifyInputForward();
207: verifyActionErrors(new String[] { "shim.pageForm.aliasUsed" });
208: }
209:
210: public void testValidateMissingTemplateOrCopy() throws Exception {
211: //
212: // save a new page without a template or page to copy from
213: //
214: setRequestPathInfo("/configurePage");
215: addRequestParameter("id", "");
216: addRequestParameter("title", "Test");
217: addRequestParameter("alias", "test");
218: addRequestParameter("template", "");
219: addRequestParameter("copyfrom", "");
220: actionPerform();
221:
222: verifyInputForward();
223: verifyActionErrors(new String[] { "shim.pageForm.missingTemplateOrCopy" });
224: }
225:
226: public void testValidateQuotesInMetaKeywords() throws Exception {
227: //
228: // save a new page with quotes in the meta tags
229: //
230: setRequestPathInfo("/configurePage");
231: addRequestParameter("id", "");
232: addRequestParameter("title", "Test");
233: addRequestParameter("alias", "test");
234: addRequestParameter("template", "template.jsp");
235: addRequestParameter("metadescription", "test\"test");
236: addRequestParameter("metakeywords", "test\"test");
237: actionPerform();
238:
239: verifyInputForward();
240: verifyActionErrors(new String[] {
241: "shim.pageForm.quotesInMetaDescription",
242: "shim.pageForm.quotesInMetaKeywords" });
243: }
244:
245: public void testValidate() throws Exception {
246: //
247: // a valid save
248: //
249: setRequestPathInfo("/configurePage");
250: addRequestParameter("id", "" + page1_.getInt("id"));
251: addRequestParameter("title", "Test");
252: addRequestParameter("alias", "page1alias");
253: addRequestParameter("metadescription", "");
254: addRequestParameter("metakeywords", "");
255: addRequestParameter("template", "template.jsp");
256: actionPerform();
257:
258: verifyNoActionErrors();
259: }
260: }
|