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.auth.*;
032: import com.methodhead.aikp.*;
033: import com.methodhead.*;
034: import servletunit.struts.*;
035: import org.apache.struts.action.*;
036: import org.apache.struts.util.*;
037: import org.apache.cactus.*;
038:
039: public class HtmlFragmentActionTest extends CactusStrutsTestCase {
040:
041: private HtmlFragment fragment = null;
042: private List list = null;
043: private LabelValueBean labelValue = null;
044: private DynaActionForm form = null;
045: private Panel panel = null;
046:
047: private SiteContext siteContext1_ = null;
048: private HtmlFragment fragment1_ = null;
049: private HtmlFragment fragment2_ = null;
050: private Page page1_ = null;
051: private TextModule textModule1_ = null;
052:
053: private void createData() {
054:
055: fragment1_ = new HtmlFragment();
056: fragment1_.setSiteContext(siteContext1_);
057: fragment1_.setString("name", "Fragment1");
058: fragment1_.setString("value", "Value1");
059: fragment1_.saveNew();
060:
061: fragment2_ = new HtmlFragment();
062: fragment2_.setSiteContext(siteContext1_);
063: fragment2_.setString("name", "Fragment2");
064: fragment2_.setString("value", "Value2");
065: fragment2_.saveNew();
066:
067: panel = new Panel();
068: panel.setName("body");
069: panel.setModuleClass("com.methodhead.shim.TextModule");
070:
071: page1_ = new Page();
072: page1_.set("title", "Page1");
073: page1_.set("aliasname", "page1");
074: page1_.setBoolean("hidden", false);
075: page1_.setString("template", "template.jsp");
076: page1_.addPanel(panel);
077: page1_.setSiteContext(siteContext1_);
078: page1_.saveNew();
079:
080: textModule1_ = new TextModule();
081: textModule1_.init(page1_, "body");
082: textModule1_.create();
083:
084: textModule1_.setString("value", "Test text.");
085: textModule1_.setInt("htmlfragment_id", fragment1_.getInt("id"));
086: textModule1_.save();
087: }
088:
089: static {
090: TestUtils.initLogger();
091: TestUtils.initDb();
092: }
093:
094: public HtmlFragmentActionTest(String name) {
095: super (name);
096: }
097:
098: public void setUp() {
099: try {
100: super .setUp();
101:
102: ConnectionSingleton.runBatchUpdate(new FileReader(
103: "webapp/WEB-INF/db/transfer-reset.sql"));
104:
105: siteContext1_ = new SiteContext();
106: siteContext1_.getDomains().add("methodhead.com");
107: siteContext1_.saveNew();
108:
109: TestData.createUsers();
110: AuthUtil.setUser(request, TestData.user1);
111: SiteContext.setContext(request, siteContext1_);
112: } catch (Exception e) {
113: fail(e.getMessage());
114: }
115: }
116:
117: public void tearDown() throws Exception {
118: super .tearDown();
119: }
120:
121: public void testDoSaveNew() {
122: try {
123: createData();
124:
125: setRequestPathInfo("/htmlFragment");
126: addRequestParameter("action", "saveNew");
127: addRequestParameter("name", "Name");
128: addRequestParameter("value", "Value");
129: actionPerform();
130:
131: verifyForwardPath("/htmlFragment.do?action=list");
132:
133: fragment = new HtmlFragment();
134: fragment.load(new IntKey(3));
135: assertEquals(1, fragment.getInt("sitecontext_id"));
136: assertEquals("Name", fragment.getString("name"));
137: assertEquals("Value", fragment.getString("value"));
138: } catch (Exception e) {
139: e.printStackTrace();
140: fail();
141: }
142: }
143:
144: public void testDoList() {
145: try {
146: createData();
147:
148: setRequestPathInfo("/htmlFragment");
149: addRequestParameter("action", "list");
150: actionPerform();
151:
152: verifyForward("list");
153:
154: form = (DynaActionForm) getActionForm();
155:
156: assertEquals("edit", form.get("action"));
157:
158: list = (List) form.get("list");
159: assertEquals(2, list.size());
160:
161: labelValue = (LabelValueBean) list.get(0);
162: assertEquals("Fragment1", labelValue.getLabel());
163: assertEquals("1", labelValue.getValue());
164:
165: labelValue = (LabelValueBean) list.get(1);
166: assertEquals("Fragment2", labelValue.getLabel());
167: assertEquals("2", labelValue.getValue());
168: } catch (Exception e) {
169: e.printStackTrace();
170: fail();
171: }
172: }
173:
174: public void testDoCancel() {
175: try {
176: createData();
177:
178: setRequestPathInfo("/htmlFragment");
179: addRequestParameter("action", "save");
180: addRequestParameter("cancel", "Cancel");
181: actionPerform();
182:
183: verifyForwardPath("/htmlFragment.do?action=list");
184: } catch (Exception e) {
185: e.printStackTrace();
186: fail();
187: }
188: }
189:
190: public void testDoSave() {
191: try {
192: createData();
193:
194: setRequestPathInfo("/htmlFragment");
195: addRequestParameter("action", "save");
196: addRequestParameter("id", "1");
197: addRequestParameter("name", "Name");
198: addRequestParameter("value", "Value");
199: actionPerform();
200:
201: verifyForwardPath("/htmlFragment.do?action=list");
202: } catch (Exception e) {
203: e.printStackTrace();
204: fail();
205: }
206: }
207:
208: public void testDoConfirm() {
209: try {
210: createData();
211:
212: setRequestPathInfo("/htmlFragment");
213: addRequestParameter("action", "save");
214: addRequestParameter("delete", "Delete");
215: addRequestParameter("id", "1");
216: actionPerform();
217:
218: verifyInputForward();
219:
220: verifyActionMessages(new String[] { "shim.htmlFragment.fragmentInUse" });
221: } catch (Exception e) {
222: e.printStackTrace();
223: fail();
224: }
225: }
226: }
|