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 org.apache.cactus.*;
031: import com.methodhead.sitecontext.*;
032: import com.methodhead.*;
033:
034: public class ShimUtilsTest extends ServletTestCase {
035:
036: SiteMap siteMap = null;
037: SiteMap siteMap2 = null;
038: Link link = null;
039: private Module module = null;
040: private List list = null;
041:
042: static {
043: TestUtils.initLogger();
044: TestUtils.initDb();
045: }
046:
047: public ShimUtilsTest(String name) {
048: super (name);
049: }
050:
051: /*
052: private SiteContext siteContext1_ = null;
053: private SiteContext siteContext2_ = null;
054:
055: private Page page1_ = null;
056: private Page page2_ = null;
057: private Page page3_ = null;
058: private Page page4_ = null;
059: private Page page5_ = null;
060:
061: private Link link1_ = null;
062: private Link link2_ = null;
063: private Link link3_ = null;
064: private Link link4_ = null;
065: private Link link5_ = null;
066:
067: protected void createData() {
068:
069: // Link1
070: // Link2
071: // Link3
072: // Link4
073: // Link5
074: link1_ = new Link();
075: link1_.setTitle( "Link1" );
076: link1_.setPageId( page1_.getInt( "id" ) );
077:
078: link2_ = new Link();
079: link2_.setTitle( "Link2" );
080: link2_.setPageId( page2_.getInt( "id" ) );
081:
082: link3_ = new Link();
083: link3_.setTitle( "Link3" );
084: link3_.setPageId( page3_.getInt( "id" ) );
085:
086: link4_ = new Link();
087: link4_.setTitle( "Link4" );
088: link4_.setPageId( page4_.getInt( "id" ) );
089:
090: link5_ = new Link();
091: link5_.setTitle( "Link5" );
092: link5_.setPageId( page5_.getInt( "id" ) );
093:
094: link1_.add( link2_ );
095: link2_.add( link3_ );
096: link1_.add( link4_ );
097: link1_.add( link5_ );
098: }
099: */
100:
101: protected void setUp() {
102: //setLogLevel( Level.DEBUG );
103: try {
104: ConnectionSingleton.runBatchUpdate(new FileReader(
105: "webapp/WEB-INF/db/transfer-reset.sql"));
106:
107: session.getServletContext().removeAttribute(
108: ShimGlobals.SITEMAPMAP_KEY);
109:
110: /*
111: siteContext1_ = new SiteContext();
112: siteContext1_.saveNew();
113:
114: page1_ = new Page();
115: page1_.setSiteContext( SiteContext.getDefaultContext() );
116: page1_.saveNew();
117:
118: page2_ = new Page();
119: page2_.setSiteContext( SiteContext.getDefaultContext() );
120: page2_.saveNew();
121:
122: page3_ = new Page();
123: page3_.setSiteContext( SiteContext.getDefaultContext() );
124: page3_.saveNew();
125:
126: page4_ = new Page();
127: page4_.setSiteContext( SiteContext.getDefaultContext() );
128: page4_.saveNew();
129:
130: page5_ = new Page();
131: page5_.setSiteContext( SiteContext.getDefaultContext() );
132: page5_.saveNew();
133:
134: siteContext2_ = new SiteContext();
135: siteContext2_.setString( "path", "context2" );
136: */
137: } catch (Exception e) {
138: fail(e.getMessage());
139: }
140: }
141:
142: protected void tearDown() {
143: }
144:
145: public void testSetSiteMap() {
146: try {
147: TestData.createLinks();
148:
149: siteMap = new SiteMap();
150: siteMap.setSiteContext(SiteContext.getDefaultContext());
151: siteMap.setRoot(TestData.link1);
152: siteMap.save();
153:
154: ShimUtils.setSiteMap(request, siteMap);
155:
156: Map map = (Map) session.getServletContext().getAttribute(
157: ShimGlobals.SITEMAPMAP_KEY);
158:
159: assertNotNull(map);
160:
161: siteMap2 = (SiteMap) map.get(SiteContext
162: .getDefaultContext().get("id"));
163:
164: assertNotNull(siteMap2);
165:
166: link = (Link) siteMap2.getRoot();
167:
168: assertNotNull(link);
169: assertEquals(TestData.link1, link);
170: } catch (Exception e) {
171: e.printStackTrace();
172: fail();
173: }
174: }
175:
176: public void testGetSiteMap() {
177: try {
178:
179: //
180: // no current site map
181: //
182: try {
183: siteMap2 = ShimUtils.getSiteMap(request);
184: } catch (Exception e) {
185: // success
186: }
187:
188: TestData.createSiteMap();
189:
190: SiteContext.setContext(request, SiteContext
191: .getDefaultContext());
192:
193: siteMap2 = ShimUtils.getSiteMap(request);
194:
195: Map map = (Map) session.getServletContext().getAttribute(
196: ShimGlobals.SITEMAPMAP_KEY);
197:
198: assertNotNull(map);
199:
200: siteMap2 = (SiteMap) map.get(SiteContext
201: .getDefaultContext().get("id"));
202:
203: assertNotNull(siteMap2);
204:
205: link = (Link) siteMap2.getRoot();
206:
207: assertNotNull(link);
208: assertEquals(TestData.link1.getPageId(), link.getPageId());
209: } catch (Exception e) {
210: e.printStackTrace();
211: fail();
212: }
213: }
214:
215: public void testGetModules() {
216: try {
217: list = ShimUtils
218: .getModules(SiteContext.getDefaultContext());
219:
220: assertNotNull(list);
221: assertEquals(3, list.size());
222:
223: module = (Module) list.get(0);
224: assertNotNull(module);
225: assertEquals("Text", module.getName());
226:
227: module = (Module) list.get(1);
228: assertNotNull(module);
229: assertEquals("Navigation", module.getName());
230:
231: module = (Module) list.get(2);
232: assertNotNull(module);
233: assertEquals("Include", module.getName());
234: } catch (Exception e) {
235: e.printStackTrace();
236: fail();
237: }
238: }
239:
240: public void testGetLinkUrl() {
241: try {
242: link = new Link();
243: link.setTitle("Title");
244: link.setAlias("title");
245: link.setPageId(666);
246:
247: assertEquals("title.shtml", ShimUtils.getLinkUrl(link));
248: } catch (Exception e) {
249: e.printStackTrace();
250: fail();
251: }
252: }
253:
254: public void testFileNameToLabel() {
255: assertEquals("<null>", ShimUtils.fileNameToLabel(null));
256: assertEquals("", ShimUtils.fileNameToLabel(""));
257: assertEquals("", ShimUtils.fileNameToLabel(" "));
258: assertEquals("", ShimUtils.fileNameToLabel("\t"));
259: assertEquals("T", ShimUtils.fileNameToLabel("t"));
260: assertEquals("T", ShimUtils.fileNameToLabel("T"));
261: assertEquals("T", ShimUtils.fileNameToLabel("t.txt"));
262: assertEquals("T", ShimUtils.fileNameToLabel("T.txt"));
263: assertEquals("Test", ShimUtils.fileNameToLabel("test"));
264: assertEquals("Test", ShimUtils.fileNameToLabel("test.txt"));
265: assertEquals("Test", ShimUtils.fileNameToLabel("Test"));
266: assertEquals("Test", ShimUtils.fileNameToLabel("Test.txt"));
267: assertEquals("My Test", ShimUtils.fileNameToLabel("myTest.txt"));
268: assertEquals("My Test", ShimUtils.fileNameToLabel("MyTest.txt"));
269: assertEquals("My Test2 Test", ShimUtils
270: .fileNameToLabel("MyTest2Test.txt"));
271: assertEquals("M T A", ShimUtils.fileNameToLabel("MTA.txt"));
272: assertEquals("My Test", ShimUtils.fileNameToLabel("MyTest."));
273: assertEquals("My Test", ShimUtils
274: .fileNameToLabel("My Test.txt"));
275: assertEquals("My-test", ShimUtils
276: .fileNameToLabel("my-test.txt"));
277: }
278: }
|