KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > methodhead > shim > ShimUtilsTest


1 /*
2  * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
3  *
4  * This file is part of TransferCM.
5  *
6  * TransferCM is free software; you can redistribute it and/or modify it under the
7  * terms of the GNU General Public License as published by the Free Software
8  * Foundation; either version 2 of the License, or (at your option) any later
9  * version.
10  *
11  * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
18  * Fifth Floor, Boston, MA 02110-1301 USA
19  */

20
21 package com.methodhead.shim;
22
23 import java.util.*;
24 import java.sql.*;
25 import java.io.*;
26 import junit.framework.*;
27 import org.apache.log4j.*;
28 import com.methodhead.persistable.*;
29 import com.methodhead.test.*;
30 import org.apache.cactus.*;
31 import com.methodhead.sitecontext.*;
32 import com.methodhead.*;
33
34 public class ShimUtilsTest extends ServletTestCase {
35
36   SiteMap siteMap = null;
37   SiteMap siteMap2 = null;
38   Link link = null;
39   private Module module = null;
40   private List list = null;
41
42   static {
43     TestUtils.initLogger();
44     TestUtils.initDb();
45   }
46
47   public ShimUtilsTest( String JavaDoc name ) {
48     super( name );
49   }
50
51 /*
52   private SiteContext siteContext1_ = null;
53   private SiteContext siteContext2_ = null;
54
55   private Page page1_ = null;
56   private Page page2_ = null;
57   private Page page3_ = null;
58   private Page page4_ = null;
59   private Page page5_ = null;
60
61   private Link link1_ = null;
62   private Link link2_ = null;
63   private Link link3_ = null;
64   private Link link4_ = null;
65   private Link link5_ = null;
66
67   protected void createData() {
68
69     // Link1
70     // Link2
71     // Link3
72     // Link4
73     // Link5
74     link1_ = new Link();
75     link1_.setTitle( "Link1" );
76     link1_.setPageId( page1_.getInt( "id" ) );
77
78     link2_ = new Link();
79     link2_.setTitle( "Link2" );
80     link2_.setPageId( page2_.getInt( "id" ) );
81
82     link3_ = new Link();
83     link3_.setTitle( "Link3" );
84     link3_.setPageId( page3_.getInt( "id" ) );
85
86     link4_ = new Link();
87     link4_.setTitle( "Link4" );
88     link4_.setPageId( page4_.getInt( "id" ) );
89
90     link5_ = new Link();
91     link5_.setTitle( "Link5" );
92     link5_.setPageId( page5_.getInt( "id" ) );
93
94     link1_.add( link2_ );
95     link2_.add( link3_ );
96     link1_.add( link4_ );
97     link1_.add( link5_ );
98   }
99 */

100
101   protected void setUp() {
102     //setLogLevel( Level.DEBUG );
103
try {
104       ConnectionSingleton.runBatchUpdate( new FileReader( "webapp/WEB-INF/db/transfer-reset.sql" ) );
105
106       session.getServletContext().removeAttribute( ShimGlobals.SITEMAPMAP_KEY );
107
108 /*
109       siteContext1_ = new SiteContext();
110       siteContext1_.saveNew();
111
112       page1_ = new Page();
113       page1_.setSiteContext( SiteContext.getDefaultContext() );
114       page1_.saveNew();
115
116       page2_ = new Page();
117       page2_.setSiteContext( SiteContext.getDefaultContext() );
118       page2_.saveNew();
119
120       page3_ = new Page();
121       page3_.setSiteContext( SiteContext.getDefaultContext() );
122       page3_.saveNew();
123
124       page4_ = new Page();
125       page4_.setSiteContext( SiteContext.getDefaultContext() );
126       page4_.saveNew();
127
128       page5_ = new Page();
129       page5_.setSiteContext( SiteContext.getDefaultContext() );
130       page5_.saveNew();
131
132       siteContext2_ = new SiteContext();
133       siteContext2_.setString( "path", "context2" );
134 */

135     }
136     catch ( Exception JavaDoc e ) {
137       fail( e.getMessage() );
138     }
139   }
140
141   protected void tearDown() {
142   }
143
144   public void testSetSiteMap() {
145     try {
146       TestData.createLinks();
147
148       siteMap = new SiteMap();
149       siteMap.setSiteContext( SiteContext.getDefaultContext() );
150       siteMap.setRoot( TestData.link1 );
151       siteMap.save();
152
153       ShimUtils.setSiteMap( request, siteMap );
154
155       Map map = ( Map )session.getServletContext().getAttribute( ShimGlobals.SITEMAPMAP_KEY );
156
157       assertNotNull( map );
158
159       siteMap2 = ( SiteMap )map.get( SiteContext.getDefaultContext().get( "id" ) );
160
161       assertNotNull( siteMap2 );
162
163       link = ( Link )siteMap2.getRoot();
164
165       assertNotNull( link );
166       assertEquals( TestData.link1, link );
167     }
168     catch ( Exception JavaDoc e ) {
169       e.printStackTrace();
170       fail();
171     }
172   }
173
174   public void testGetSiteMap() {
175     try {
176
177       //
178
// no current site map
179
//
180
try {
181         siteMap2 = ShimUtils.getSiteMap( request );
182       }
183       catch ( Exception JavaDoc e ) {
184         // success
185
}
186
187       TestData.createSiteMap();
188
189       SiteContext.setContext( request, SiteContext.getDefaultContext() );
190
191       siteMap2 = ShimUtils.getSiteMap( request );
192
193       Map map = ( Map )session.getServletContext().getAttribute( ShimGlobals.SITEMAPMAP_KEY );
194
195       assertNotNull( map );
196
197       siteMap2 = ( SiteMap )map.get( SiteContext.getDefaultContext().get( "id" ) );
198
199       assertNotNull( siteMap2 );
200
201       link = ( Link )siteMap2.getRoot();
202
203       assertNotNull( link );
204       assertEquals( TestData.link1.getPageId(), link.getPageId() );
205     }
206     catch ( Exception JavaDoc e ) {
207       e.printStackTrace();
208       fail();
209     }
210   }
211
212   public void testGetModules() {
213     try {
214       list = ShimUtils.getModules( SiteContext.getDefaultContext() );
215
216       assertNotNull( list );
217       assertEquals( 3, list.size() );
218
219       module = ( Module )list.get( 0 );
220       assertNotNull( module );
221       assertEquals( "Text", module.getName() );
222
223       module = ( Module )list.get( 1 );
224       assertNotNull( module );
225       assertEquals( "Navigation", module.getName() );
226
227       module = ( Module )list.get( 2 );
228       assertNotNull( module );
229       assertEquals( "Include", module.getName() );
230     }
231     catch ( Exception JavaDoc e ) {
232       e.printStackTrace();
233       fail();
234     }
235   }
236
237   public void testGetLinkUrl() {
238     try {
239       link = new Link();
240       link.setTitle( "Title" );
241       link.setAlias( "title" );
242       link.setPageId( 666 );
243
244       assertEquals( "title.shtml", ShimUtils.getLinkUrl( link ) );
245     }
246     catch ( Exception JavaDoc e ) {
247       e.printStackTrace();
248       fail();
249     }
250   }
251
252   public void testFileNameToLabel() {
253     assertEquals( "<null>", ShimUtils.fileNameToLabel( null ) );
254     assertEquals( "", ShimUtils.fileNameToLabel( "" ) );
255     assertEquals( "", ShimUtils.fileNameToLabel( " " ) );
256     assertEquals( "", ShimUtils.fileNameToLabel( "\t" ) );
257     assertEquals( "T", ShimUtils.fileNameToLabel( "t" ) );
258     assertEquals( "T", ShimUtils.fileNameToLabel( "T" ) );
259     assertEquals( "T", ShimUtils.fileNameToLabel( "t.txt" ) );
260     assertEquals( "T", ShimUtils.fileNameToLabel( "T.txt" ) );
261     assertEquals( "Test", ShimUtils.fileNameToLabel( "test" ) );
262     assertEquals( "Test", ShimUtils.fileNameToLabel( "test.txt" ) );
263     assertEquals( "Test", ShimUtils.fileNameToLabel( "Test" ) );
264     assertEquals( "Test", ShimUtils.fileNameToLabel( "Test.txt" ) );
265     assertEquals( "My Test", ShimUtils.fileNameToLabel( "myTest.txt" ) );
266     assertEquals( "My Test", ShimUtils.fileNameToLabel( "MyTest.txt" ) );
267     assertEquals( "My Test2 Test", ShimUtils.fileNameToLabel( "MyTest2Test.txt" ) );
268     assertEquals( "M T A", ShimUtils.fileNameToLabel( "MTA.txt" ) );
269     assertEquals( "My Test", ShimUtils.fileNameToLabel( "MyTest." ) );
270     assertEquals( "My Test", ShimUtils.fileNameToLabel( "My Test.txt" ) );
271     assertEquals( "My-test", ShimUtils.fileNameToLabel( "my-test.txt" ) );
272   }
273 }
274
Popular Tags