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.test.*;
030: import com.methodhead.sitecontext.*;
031:
032: public class PageTest extends TestCase {
033:
034: static {
035: TestUtils.initDb();
036: TestUtils.initLogger();
037: }
038:
039: public PageTest(String name) {
040: super (name);
041: }
042:
043: private Page page = null;
044: private Page page2 = null;
045: private Panel panel = null;
046:
047: SiteContext siteContext1_ = null;
048: SiteContext siteContext2_ = null;
049: Page page1_ = null;
050: Page page2_ = null;
051: Page page3_ = null;
052:
053: protected void createData() {
054: Panel panel = null;
055:
056: siteContext1_ = new SiteContext();
057: siteContext1_.set("path", "sitecontext1");
058: siteContext1_.saveNew();
059:
060: panel = new Panel();
061: panel.setName("body");
062: panel.setModuleClass("com.methodhead.shim.MockModule");
063:
064: page1_ = new Page();
065: page1_.set("title", "Page1");
066: page1_.set("alttitle", "altPage1");
067: page1_.set("aliasname", "page1");
068: page1_.set("template", "template.jsp");
069: page1_.set("metadescription", "metadescription");
070: page1_.set("metakeywords", "metakeywords");
071: page1_.addPanel(panel);
072: page1_.setSiteContext(siteContext1_);
073: page1_.saveNew();
074:
075: siteContext2_ = new SiteContext();
076: siteContext2_.set("path", "sitecontext2");
077: siteContext2_.saveNew();
078:
079: panel = new Panel();
080: panel.setName("body");
081: panel.setModuleClass("com.methodhead.shim.MockModule");
082:
083: page2_ = new Page();
084: page2_.set("title", "Page2");
085: page2_.set("aliasname", "page2");
086: page2_.set("template", "template.jsp");
087: page2_.addPanel(panel);
088: page2_.setSiteContext(siteContext2_);
089: page2_.saveNew();
090:
091: panel = new Panel();
092: panel.setName("body");
093: panel.setModuleClass("com.methodhead.shim.MockModule");
094:
095: page3_ = new Page();
096: page3_.set("title", "Page3");
097: page3_.set("aliasname", "page3");
098: page3_.set("template", "template.jsp");
099: page3_.addPanel(panel);
100: page3_.setSiteContext(siteContext2_);
101: page3_.saveNew();
102: }
103:
104: protected void setUp() {
105: //setLogLevel( Level.DEBUG );
106: try {
107: ConnectionSingleton.runBatchUpdate(new FileReader(
108: "webapp/WEB-INF/db/transfer-reset.sql"));
109: } catch (Exception e) {
110: fail(e.getMessage());
111: }
112: }
113:
114: protected void tearDown() {
115: }
116:
117: public void testToString() {
118: try {
119: page = new Page();
120: assertEquals("", page.toString());
121:
122: page.setString("title", "Page1");
123: assertEquals("Page1", page.toString());
124: } catch (Exception e) {
125: e.printStackTrace();
126: fail();
127: }
128: }
129:
130: public void testGetSiteContext() {
131: try {
132: Page page = null;
133: SiteContext siteContext = null;
134:
135: page = new Page();
136: siteContext = SiteContext.getDefaultContext();
137:
138: //
139: // get site context
140: //
141: try {
142: page.getSiteContext();
143: fail("Page.getSiteContext() did not throw an exception.");
144: } catch (ShimException e) {
145: // success
146: }
147:
148: page.setSiteContext(siteContext);
149:
150: assertNotNull(page.getSiteContext());
151: assertEquals(siteContext, page.getSiteContext());
152: } catch (Exception e) {
153: e.printStackTrace();
154: fail();
155: }
156: }
157:
158: public void testAddPanel() {
159: try {
160: Page page = null;
161: Panel panel = null;
162: Map panelMap = null;
163:
164: //
165: // add a single panel
166: //
167: page = new Page();
168: panel = new Panel();
169: panel.setName("body");
170: panel.setModuleClass("com.methodhead.shim.MockModule");
171: page.addPanel(panel);
172: panelMap = page.getPanels();
173:
174: assertNotNull(panelMap);
175: assertEquals(1, panelMap.keySet().size());
176:
177: panel = (Panel) panelMap.get("body");
178:
179: assertNotNull(panel);
180: assertEquals("body", panel.getName());
181: assertEquals("com.methodhead.shim.MockModule", panel
182: .getModuleClass());
183:
184: //
185: // add the same panel again
186: //
187: try {
188: page.addPanel(panel);
189: fail("Page.addPanel() did not through an exception.");
190: } catch (ShimException e) {
191: // success
192: }
193: } catch (Exception e) {
194: e.printStackTrace();
195: fail();
196: }
197: }
198:
199: public void testSaveNew() {
200: try {
201: Page page = null;
202: Panel panel = null;
203: ResultSet rs = null;
204:
205: //
206: // save page for default site context
207: //
208: page = new Page();
209: page.setString("title", "Test");
210: page.setString("template", "template.jsp");
211: page.setString("aliasname", "test");
212: panel = new Panel();
213: panel.setName("body");
214: panel.setModuleClass("com.methodhead.shim.MockModule");
215: page.addPanel(panel);
216: page.setSiteContext(SiteContext.getDefaultContext());
217: page.saveNew();
218: rs = ConnectionSingleton
219: .runQuery("SELECT sitecontext_id FROM shim_page WHERE id="
220: + page.getInt("id"));
221:
222: assertTrue(rs.next());
223: assertEquals(0, rs.getInt("sitecontext_id"));
224: assertTrue(!rs.next());
225:
226: ConnectionSingleton.close(rs);
227: rs = ConnectionSingleton
228: .runQuery("SELECT name,module FROM shim_panel WHERE page_id="
229: + page.getInt("id"));
230:
231: assertTrue(rs.next());
232: assertEquals("body", rs.getString("name"));
233: assertEquals("com.methodhead.shim.MockModule", rs
234: .getString("module"));
235: assertTrue(!rs.next());
236:
237: ConnectionSingleton.close(rs);
238: } catch (Exception e) {
239: e.printStackTrace();
240: fail();
241: }
242: }
243:
244: public void testLoad() {
245: try {
246: createData();
247:
248: //
249: // load for the wrong site context
250: //
251: try {
252: page = new Page();
253: page.setSiteContext(SiteContext.getDefaultContext());
254: page.load(page1_.getKey());
255: fail("No exception thrown.");
256: } catch (Exception e) {
257: }
258:
259: //
260: // typical
261: //
262: page = new Page();
263: page.setSiteContext(siteContext1_);
264: page.load(page1_.getKey());
265:
266: assertEquals(page1_.getString("title"), page
267: .getString("title"));
268: assertEquals(page1_.getString("aliasname"), page
269: .getString("aliasname"));
270: assertEquals(1, page.getPanels().keySet().size());
271:
272: panel = (Panel) page.getPanels().get("body");
273:
274: assertEquals("com.methodhead.shim.MockModule", panel
275: .getModuleClass());
276: } catch (Exception e) {
277: e.printStackTrace();
278: fail();
279: }
280: }
281:
282: public void testSave() {
283: try {
284: Page page = null;
285: Panel panel = null;
286: createData();
287:
288: //
289: // save page for default site context
290: //
291: page = new Page();
292: page.setSiteContext(siteContext1_);
293: page.load(page1_.getKey());
294: page.setString("title", "Page2");
295: page.setString("aliasname", "page2");
296: panel = (Panel) page.getPanels().get("body");
297: panel.setName("body");
298: panel.setModuleClass("com.methodhead.shim.FooModule");
299: page.save();
300: page = new Page();
301: page.setSiteContext(siteContext1_);
302: page.load(page1_.getKey());
303:
304: assertEquals("Page2", page.getString("title"));
305: assertEquals("page2", page.getString("aliasname"));
306:
307: panel = (Panel) page.getPanels().get("body");
308:
309: assertEquals("com.methodhead.shim.FooModule", panel
310: .getModuleClass());
311: } catch (Exception e) {
312: e.printStackTrace();
313: fail();
314: }
315: }
316:
317: public void testDelete() {
318: try {
319: Page page = null;
320: ResultSet rs = null;
321: createData();
322:
323: //
324: // save page for default site context
325: //
326: MockModule.setDestroyed(false);
327: page = new Page();
328: page.setSiteContext(siteContext1_);
329: page.loadFull(page1_.getKey());
330: page.delete();
331: rs = ConnectionSingleton
332: .runQuery("SELECT title FROM shim_page WHERE id="
333: + page1_.getInt("id"));
334:
335: assertTrue(!rs.next());
336:
337: ConnectionSingleton.close(rs);
338: rs = ConnectionSingleton
339: .runQuery("SELECT name FROM shim_panel WHERE page_id="
340: + page1_.getInt("id"));
341:
342: assertTrue(!rs.next());
343:
344: assertTrue(MockModule.isDestroyed());
345:
346: ConnectionSingleton.close(rs);
347: } catch (Exception e) {
348: e.printStackTrace();
349: fail();
350: }
351: }
352:
353: public void testLoadFull() {
354: try {
355: Page page = null;
356: Panel panel = null;
357: createData();
358:
359: //
360: // save page for default site context
361: //
362: page = new Page();
363: page.setSiteContext(siteContext1_);
364: page.loadFull(page1_.getKey());
365:
366: assertEquals(page1_.getString("title"), page
367: .getString("title"));
368: assertEquals(page1_.getString("aliasname"), page
369: .getString("aliasname"));
370: assertEquals(1, page.getPanels().keySet().size());
371:
372: panel = (Panel) page.getPanels().get("body");
373:
374: assertNotNull(panel.getModule());
375: assertEquals("com.methodhead.shim.MockModule", panel
376: .getModule().getClass().getName());
377: } catch (Exception e) {
378: e.printStackTrace();
379: fail();
380: }
381: }
382:
383: public void testCompareTo() {
384: try {
385: Page page1 = new Page();
386: page1.setString("title", "page1");
387:
388: Page page2 = new Page();
389: page2.setString("title", "Page2");
390:
391: Page page3 = new Page();
392: page3.setString("title", "Page3");
393:
394: assertTrue(page1.compareTo(null) > 0);
395: assertTrue(page1.compareTo("foo") > 0);
396: assertTrue(page1.compareTo(page2) < 0);
397: assertEquals(0, page1.compareTo(page1));
398: assertTrue(page3.compareTo(page1) > 0);
399: } catch (Exception e) {
400: e.printStackTrace();
401: fail();
402: }
403: }
404:
405: public void testLoadAll() {
406: try {
407: Page page = null;
408: Panel panel = null;
409: List l = null;
410:
411: createData();
412:
413: //
414: // save page for default site context
415: //
416: page = new Page();
417: page.setSiteContext(siteContext1_);
418: l = page.loadAll();
419:
420: assertNotNull(l);
421: assertEquals(1, l.size());
422:
423: page = (Page) l.get(0);
424:
425: assertEquals(page1_.getInt("id"), page.getInt("id"));
426: assertEquals(siteContext1_, page.getSiteContext());
427:
428: page.setSiteContext(siteContext2_);
429: l = page.loadAll();
430:
431: assertNotNull(l);
432: assertEquals(2, l.size());
433:
434: page = (Page) l.get(0);
435:
436: assertEquals(page2_.getInt("id"), page.getInt("id"));
437: assertEquals(siteContext2_, page.getSiteContext());
438:
439: page = (Page) l.get(1);
440:
441: assertEquals(page3_.getInt("id"), page.getInt("id"));
442: assertEquals(siteContext2_, page.getSiteContext());
443: } catch (Exception e) {
444: e.printStackTrace();
445: fail();
446: }
447: }
448:
449: public void testLoadForAlias() {
450: try {
451: createData();
452:
453: page = new Page();
454: page.setSiteContext(siteContext1_);
455: page.loadForAlias("page1");
456:
457: assertEquals(page1_.getString("title"), page
458: .getString("title"));
459: assertEquals(page1_.getString("aliasname"), page
460: .getString("aliasname"));
461: assertEquals(1, page.getPanels().keySet().size());
462:
463: panel = (Panel) page.getPanels().get("body");
464:
465: assertEquals("com.methodhead.shim.MockModule", panel
466: .getModuleClass());
467: } catch (Exception e) {
468: e.printStackTrace();
469: fail();
470: }
471: }
472:
473: public void testSetDefaultAliasName() {
474: try {
475: createData();
476:
477: page = new Page();
478: page.setSiteContext(siteContext1_);
479:
480: try {
481: page.setString("title", "");
482: page.setDefaultAliasName();
483: fail("Exception not thrown.");
484: } catch (Exception e) {
485: }
486:
487: page.setString("title", "Test");
488: page.setDefaultAliasName();
489: assertEquals("test", page.getString("aliasname"));
490:
491: page.setString("title", "Test Page");
492: page.setDefaultAliasName();
493: assertEquals("testpage", page.getString("aliasname"));
494:
495: page.setString("title", "Test$Page!");
496: page.setDefaultAliasName();
497: assertEquals("testpage", page.getString("aliasname"));
498:
499: page.setString("title", "Page1");
500: page.setDefaultAliasName();
501: assertEquals("page12", page.getString("aliasname"));
502:
503: page1_.setDefaultAliasName();
504: assertEquals("page1", page1_.getString("aliasname"));
505: } catch (Exception e) {
506: e.printStackTrace();
507: fail();
508: }
509: }
510:
511: public void testLoadFullForAlias() {
512: try {
513: Page page = null;
514: Panel panel = null;
515: createData();
516:
517: //
518: // save page for default site context
519: //
520: page = new Page();
521: page.setSiteContext(siteContext1_);
522: page.loadFullForAlias("page1");
523:
524: assertEquals(page1_.getString("title"), page
525: .getString("title"));
526: assertEquals(page1_.getString("aliasname"), page
527: .getString("aliasname"));
528: assertEquals(1, page.getPanels().keySet().size());
529:
530: panel = (Panel) page.getPanels().get("body");
531:
532: assertNotNull(panel.getModule());
533: assertEquals("com.methodhead.shim.MockModule", panel
534: .getModule().getClass().getName());
535: } catch (Exception e) {
536: e.printStackTrace();
537: fail();
538: }
539: }
540:
541: public void testCopy() {
542: try {
543: createData();
544: MockModule.setCopied(false);
545:
546: page = new Page();
547: page.setSiteContext(siteContext1_);
548: page.loadFull(page1_.getKey());
549:
550: page2 = page.copy();
551: assertNotNull(page2);
552: assertEquals(4, page2.getInt("id"));
553: assertEquals("Page1", page2.getString("title"));
554: assertEquals("altPage1", page2.getString("alttitle"));
555: assertEquals("page12", page2.getString("aliasname"));
556: assertEquals("metadescription", page2
557: .getString("metadescription"));
558: assertEquals("metakeywords", page2
559: .getString("metakeywords"));
560:
561: panel = (Panel) page2.getPanels().get("body");
562: assertNotNull(panel);
563: assertEquals("body", panel.getName());
564: assertEquals("com.methodhead.shim.MockModule", panel
565: .getModuleClass());
566:
567: assertTrue(MockModule.isCopied());
568: } catch (Exception e) {
569: e.printStackTrace();
570: fail();
571: }
572: }
573: }
|