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 com.methodhead.sitecontext.*;
030: import com.methodhead.aikp.*;
031: import org.apache.cactus.*;
032: import org.apache.struts.action.*;
033: import org.apache.struts.config.*;
034: import org.apache.commons.beanutils.*;
035: import java.io.*;
036:
037: public class TextModuleTest extends JspTestCase {
038:
039: TextModule textModule = null;
040: Page page = null;
041: Panel panel = null;
042: HtmlFragment fragment = null;
043: List list = null;
044:
045: private Page page1_ = null;
046: private Page page2_ = null;
047: private Panel panel1_ = null;
048: private HtmlFragment fragment1_ = null;
049: private HtmlFragment fragment2_ = null;
050: SiteContext siteContext1_ = null;
051:
052: static {
053: TestUtils.initLogger();
054: TestUtils.initDb();
055: }
056:
057: public TextModuleTest(String name) {
058: super (name);
059: }
060:
061: protected void setUp() {
062: //setLogLevel( Level.DEBUG );
063: try {
064: ConnectionSingleton.runBatchUpdate(new FileReader(
065: "webapp/WEB-INF/db/transfer-reset.sql"));
066:
067: siteContext1_ = new SiteContext();
068: siteContext1_.getDomains().add("methodhead.com");
069: siteContext1_.setString("path", "path");
070: siteContext1_.saveNew();
071:
072: panel1_ = new Panel();
073: panel1_.setName("body");
074: panel1_.setModuleClass("com.methodhead.shim.TextModule");
075:
076: page1_ = new Page();
077: page1_.setSiteContext(SiteContext.getDefaultContext());
078: page1_.setString("title", "Page1");
079: page1_.setString("template", "template.jsp");
080: page1_.addPanel(panel1_);
081: page1_.saveNew();
082:
083: page2_ = new Page();
084: page2_.setSiteContext(siteContext1_);
085: page2_.setString("title", "Page2");
086: page2_.setString("template", "template.jsp");
087: page2_.addPanel(panel1_);
088: page2_.saveNew();
089:
090: fragment1_ = new HtmlFragment();
091: fragment1_.setSiteContext(SiteContext.getDefaultContext());
092: fragment1_.setString("name", "Fragment1");
093: fragment1_.setString("value", "This is Fragment1");
094: fragment1_.saveNew();
095:
096: fragment2_ = new HtmlFragment();
097: fragment2_.setSiteContext(SiteContext.getDefaultContext());
098: fragment2_.setString("name", "Fragment2");
099: fragment2_.setString("value", "This is Fragment2");
100: fragment2_.saveNew();
101: } catch (Exception e) {
102: fail(e.getMessage());
103: }
104: }
105:
106: protected void tearDown() {
107: }
108:
109: public void testLoad() {
110: try {
111: TextModule textModule = new TextModule();
112:
113: textModule = new TextModule();
114: textModule.setInt("page_id", page1_.getInt("id"));
115: textModule.setString("panel", "body");
116: textModule.setString("value", "Test text.");
117: textModule.saveNew();
118: textModule = new TextModule();
119: textModule.init(page1_, "body");
120: textModule.load();
121:
122: assertEquals("Test text.", textModule.getString("value"));
123: assertEquals(0, textModule.getInt("htmlfragment_id"));
124: } catch (Exception e) {
125: e.printStackTrace();
126: fail();
127: }
128: }
129:
130: public void testSave() {
131: try {
132: TextModule textModule = new TextModule();
133:
134: textModule = new TextModule();
135: textModule.init(page1_, "body");
136: textModule.create();
137: textModule = new TextModule();
138: textModule.init(page1_, "body");
139: textModule.setString("value", "Updated text.");
140: textModule.save();
141: textModule = new TextModule();
142: textModule.init(page1_, "body");
143: textModule.load();
144:
145: assertEquals("Updated text.", textModule.getString("value"));
146: } catch (Exception e) {
147: e.printStackTrace();
148: fail();
149: }
150: }
151:
152: public void testCreate() {
153: try {
154: TextModule textModule = null;
155: ResultSet rs = null;
156:
157: //
158: // typical create
159: //
160: textModule = new TextModule();
161: textModule.init(page1_, "body");
162: textModule.create();
163: rs = ConnectionSingleton
164: .runQuery("SELECT value,htmlfragment_id FROM shim_text WHERE page_id="
165: + page1_.getInt("id") + " AND panel='body'");
166:
167: assertTrue(rs.next());
168: assertEquals("Insert your text here...", rs
169: .getString("value"));
170: assertEquals(0, rs.getInt("htmlfragment_id"));
171: assertTrue(!rs.next());
172:
173: ConnectionSingleton.close(rs);
174:
175: //
176: // create for an existing page/panel
177: //
178: textModule.setString("value", "Updated text.");
179: textModule.save();
180:
181: textModule = new TextModule();
182: textModule.init(page1_, "body");
183: textModule.create();
184: rs = ConnectionSingleton
185: .runQuery("SELECT value,htmlfragment_id FROM shim_text WHERE page_id="
186: + page1_.getInt("id") + " AND panel='body'");
187:
188: assertTrue(rs.next());
189: assertEquals("Updated text.", rs.getString("value"));
190: assertEquals(0, rs.getInt("htmlfragment_id"));
191: assertTrue(!rs.next());
192:
193: ConnectionSingleton.close(rs);
194: } catch (Exception e) {
195: e.printStackTrace();
196: fail();
197: }
198: }
199:
200: public void testDisplay() {
201: try {
202: TextModule textModule = null;
203: Page page = null;
204: Panel panel = null;
205:
206: //
207: // display without init'ing
208: //
209: try {
210: textModule = new TextModule();
211: textModule.display(request, response, out);
212: fail("TextModule.display() didn't throw an exception.");
213: } catch (Exception e) {
214: // success
215: }
216:
217: panel = new Panel();
218: panel.setName("body");
219: panel.setModuleClass("com.methodhead.shim.TextModule");
220: page = new Page();
221: page.setString("template", "template.jsp");
222: page.addPanel(panel);
223: page.setSiteContext(SiteContext.getDefaultContext());
224: page.saveNew();
225: textModule = new TextModule();
226: textModule.setInt("page_id", page.getInt("id"));
227: textModule.setString("panel", "body");
228: textModule.setString("value", "Test text.");
229: textModule.saveNew();
230: textModule = new TextModule();
231: textModule.init(page, "body");
232: textModule.display(request, response, out);
233: } catch (Exception e) {
234: e.printStackTrace();
235: fail();
236: }
237: }
238:
239: public void endDisplay(WebResponse response) {
240: assertEquals("Test text.\n", response.getText());
241: }
242:
243: public void testDisplayFragment() {
244: try {
245:
246: panel = new Panel();
247: panel.setName("body");
248: panel.setModuleClass("com.methodhead.shim.TextModule");
249:
250: page = new Page();
251: page.setString("template", "template.jsp");
252: page.addPanel(panel);
253: page.setSiteContext(SiteContext.getDefaultContext());
254: page.saveNew();
255:
256: textModule = new TextModule();
257: textModule.setInt("page_id", page.getInt("id"));
258: textModule.setString("panel", "body");
259: textModule.setString("value", "Test text.");
260: textModule.setInt("htmlfragment_id", fragment1_
261: .getInt("id"));
262: textModule.saveNew();
263:
264: textModule = new TextModule();
265: textModule.init(page, "body");
266: textModule.display(request, response, out);
267: } catch (Exception e) {
268: e.printStackTrace();
269: fail();
270: }
271: }
272:
273: public void endDisplayFragment(WebResponse response) {
274: assertEquals("This is Fragment1\n", response.getText());
275: }
276:
277: public void testConfigure() {
278: try {
279: DynaActionForm dynaActionForm = null;
280: ActionForward forward = null;
281: TextModule textModule = null;
282:
283: dynaActionForm = new MockDynaActionForm();
284: dynaActionForm.set("pageid", "666");
285: dynaActionForm.set("panel", "body");
286:
287: textModule = new TextModule();
288: forward = textModule.configure(new ActionMapping(),
289: dynaActionForm, request, response);
290:
291: assertEquals(
292: "/configureTextModuleForm.do?pageid=666&panel=body",
293: forward.getPath());
294: } catch (Exception e) {
295: e.printStackTrace();
296: fail();
297: }
298: }
299:
300: public void testUpdate() {
301: try {
302: TextModule textModule = new TextModule();
303:
304: textModule = new TextModule();
305: textModule.init(page1_, "body");
306: textModule.create();
307: textModule
308: .update("Updated text. And a <a href=\"pg/test\">link</a>");
309: textModule = new TextModule();
310: textModule.init(page1_, "body");
311: textModule.load();
312:
313: assertEquals(
314: "Updated text. And a <a href=\"pg/test\">link</a>",
315: textModule.getString("value"));
316: } catch (Exception e) {
317: e.printStackTrace();
318: fail();
319: }
320: }
321:
322: public void testUpdateFragment() {
323: try {
324: TextModule textModule = new TextModule();
325:
326: textModule = new TextModule();
327: textModule.init(page1_, "body");
328: textModule.setInt("htmlfragment_id", fragment1_
329: .getInt("id"));
330: textModule.create();
331:
332: textModule = new TextModule();
333: textModule.init(page1_, "body");
334: textModule.update("Updated text.");
335:
336: fragment = new HtmlFragment();
337: fragment.load(new IntKey(fragment1_.getInt("id")));
338: assertEquals("Updated text.", fragment.getString("value"));
339: } catch (Exception e) {
340: e.printStackTrace();
341: fail();
342: }
343: }
344:
345: public void testDestroy() {
346: try {
347: TextModule textModule = new TextModule();
348:
349: textModule = new TextModule();
350: textModule.init(page1_, "body");
351: textModule.create();
352:
353: textModule = new TextModule();
354: textModule.init(page1_, "body");
355: textModule.destroy();
356:
357: try {
358: textModule.load("page_id=" + page1_.getInt("id")
359: + " AND panel='body'");
360: fail("Text module still in database.");
361: } catch (Exception e) {
362: // success
363: }
364: } catch (Exception e) {
365: e.printStackTrace();
366: fail();
367: }
368: }
369:
370: public void testCopyTo() {
371: try {
372: //
373: // copy to a new page
374: //
375: textModule = new TextModule();
376: textModule.init(page1_, "body");
377: textModule.create();
378: textModule.setString("value", "test");
379: textModule.setInt("htmlfragment_id", fragment1_
380: .getInt("id"));
381: textModule.save();
382:
383: textModule = new TextModule();
384: textModule.init(page1_, "body");
385: textModule.copyTo(page2_);
386:
387: textModule = new TextModule();
388: textModule.init(page2_, "body");
389: textModule.load();
390:
391: assertEquals("test", textModule.getString("value"));
392: assertEquals(fragment1_.getInt("id"), textModule
393: .getInt("htmlfragment_id"));
394:
395: //
396: // copy to a page with an existing text module
397: //
398: textModule.setString("value", "updatedtest");
399: textModule.setInt("htmlfragment_id", fragment2_
400: .getInt("id"));
401: textModule.save();
402:
403: textModule = new TextModule();
404: textModule.init(page2_, "body");
405: textModule.copyTo(page1_);
406:
407: textModule = new TextModule();
408: textModule.init(page1_, "body");
409: textModule.load();
410:
411: assertEquals("updatedtest", textModule.getString("value"));
412: assertEquals(fragment2_.getInt("id"), textModule
413: .getInt("htmlfragment_id"));
414: } catch (Exception e) {
415: e.printStackTrace();
416: fail();
417: }
418: }
419:
420: public void testGetDependentPages() {
421: try {
422: //
423: // copy to a new page
424: //
425: textModule = new TextModule();
426: textModule.init(page1_, "body");
427: textModule.create();
428: textModule.setInt("htmlfragment_id", fragment1_
429: .getInt("id"));
430: textModule.save();
431:
432: textModule = new TextModule();
433: list = textModule.getDependentPages(fragment1_);
434: assertNotNull(list);
435: assertEquals(1, list.size());
436:
437: page = (Page) list.get(0);
438: assertEquals(page1_.getInt("id"), page.getInt("id"));
439: } catch (Exception e) {
440: e.printStackTrace();
441: fail();
442: }
443: }
444: }
|