KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.regex.*;
26 import java.io.*;
27 import junit.framework.*;
28 import org.apache.log4j.*;
29 import com.methodhead.sitecontext.*;
30 import com.methodhead.persistable.*;
31 import com.methodhead.test.*;
32 import com.meterware.httpunit.*;
33
34 public class ShimFilterTest
35 extends
36   TestCase {
37
38   String JavaDoc filterTestUrl = null;
39   SiteContext siteContext1 = null;
40   SiteContext siteContext2 = null;
41
42   static {
43     TestUtils.initLogger();
44     TestUtils.initDb();
45     TestUtils.setLogLevel( Level.DEBUG );
46   }
47
48   public ShimFilterTest( String JavaDoc name ) {
49     super( name );
50   }
51
52   void createSiteContexts() throws Exception JavaDoc {
53
54     //
55
// parse the domain out of our test url
56
//
57
Matcher matcher = Pattern.compile( "^http:\\/\\/([^:/]+).*" ).matcher( filterTestUrl );
58
59     if ( !matcher.matches() ) {
60       throw new RuntimeException JavaDoc( "Couldn't match domain in filterTestUrl \"" + filterTestUrl + "\"" );
61     }
62     
63     String JavaDoc domain = matcher.group( 1 );
64
65     //
66
// site.com
67
//
68
siteContext1 = new SiteContext();
69     siteContext1.getDomains().add( domain );
70     siteContext1.saveNew();
71
72     //
73
// site.com/subsite
74
//
75
siteContext2 = new SiteContext();
76     siteContext2.getDomains().add( domain );
77     siteContext2.setString( "path", "subsite" );
78     siteContext2.saveNew();
79   }
80
81   void createPages() throws Exception JavaDoc {
82
83     Page pg = null;
84     Link link = new Link();
85     SiteMap siteMap = new SiteMap();
86
87     //
88
// site.com
89
//
90
pg = new Page();
91     pg.setSiteContext( siteContext1 );
92     pg.setString( "title", "Page1" );
93     pg.setString( "aliasname", "page1" );
94     pg.setString( "template", "template.jsp" );
95     pg.setBoolean( "hidden", false );
96     pg.saveNew();
97
98     link.setAlias( pg.getString( "aliasname" ) );
99     link.setTitle( pg.getString( "title" ) );
100     link.setHidden( false );
101     link.setPageId( pg.getInt( "id" ) );
102
103     siteMap.setSiteContext( siteContext1 );
104     siteMap.setRoot( link );
105     siteMap.save();
106
107     //
108
// site.com/subsite
109
//
110
pg = new Page();
111     pg.setSiteContext( siteContext2 );
112     pg.setString( "title", "Page2" );
113     pg.setString( "aliasname", "page2" );
114     pg.setString( "template", "template.jsp" );
115     pg.setBoolean( "hidden", false );
116     pg.saveNew();
117
118     link.setAlias( pg.getString( "aliasname" ) );
119     link.setTitle( pg.getString( "title" ) );
120     link.setHidden( false );
121     link.setPageId( pg.getInt( "id" ) );
122
123     siteMap.setSiteContext( siteContext2 );
124     siteMap.setRoot( link );
125     siteMap.save();
126   }
127
128   protected void setUp() throws Exception JavaDoc {
129     //setLogLevel( Level.DEBUG );
130
ConnectionSingleton.runBatchUpdate( new FileReader( "webapp/WEB-INF/db/transfer-reset.sql" ) );
131
132     //
133
// get our test url; this is set in build.properties
134
//
135
filterTestUrl = System.getProperty( "filtertest.url" );
136
137     //
138
// clear any loaded site maps
139
//
140
WebConversation conversation = new WebConversation();
141     WebResponse response = conversation.getResponse( filterTestUrl + "/util.do?action=clearSiteMaps" );
142   }
143
144   protected void tearDown() {
145   }
146
147   public void testDomain() throws Exception JavaDoc {
148     createSiteContexts();
149     createPages();
150
151     WebConversation conversation = new WebConversation();
152     
153     WebResponse response = conversation.getResponse( filterTestUrl + "" );
154
155     assertEquals(
156       "\n" +
157       "<head>\n" +
158       "<base HREF=\"" + filterTestUrl + "\">\n" +
159       "<title>Page1</title>\n" +
160       "</head>\n" +
161       "<body>\n" +
162       "This is /WEB-INF/resources/1/templates/template.jsp.</body>\n" +
163       "\n",
164       response.getText() );
165   }
166
167   public void testDomainTrailingSlash() throws Exception JavaDoc {
168     createSiteContexts();
169     createPages();
170
171     WebConversation conversation = new WebConversation();
172     
173     WebResponse response = conversation.getResponse( filterTestUrl + "/" );
174
175     assertEquals(
176       "\n" +
177       "<head>\n" +
178       "<base HREF=\"" + filterTestUrl + "\">\n" +
179       "<title>Page1</title>\n" +
180       "</head>\n" +
181       "<body>\n" +
182       "This is /WEB-INF/resources/1/templates/template.jsp.</body>\n" +
183       "\n",
184       response.getText() );
185   }
186
187   public void testDomainShimPage() throws Exception JavaDoc {
188     createSiteContexts();
189     createPages();
190
191     WebConversation conversation = new WebConversation();
192     
193     WebResponse response = conversation.getResponse( filterTestUrl + "/page1.shtml" );
194
195     assertEquals(
196       "\n" +
197       "<head>\n" +
198       "<base HREF=\"" + filterTestUrl + "\">\n" +
199       "<title>Page1</title>\n" +
200       "</head>\n" +
201       "<body>\n" +
202       "This is /WEB-INF/resources/1/templates/template.jsp.</body>\n" +
203       "\n",
204       response.getText() );
205   }
206
207   public void testDomainSubsite() throws Exception JavaDoc {
208     createSiteContexts();
209     createPages();
210
211     WebConversation conversation = new WebConversation();
212     
213     WebResponse response = conversation.getResponse( filterTestUrl + "/subsite" );
214
215     assertEquals(
216       "\n" +
217       "<head>\n" +
218       "<base HREF=\"" + filterTestUrl + "subsite/\">\n" +
219       "<title>Page2</title>\n" +
220       "</head>\n" +
221       "<body>\n" +
222       "This is /WEB-INF/resources/2/templates/template.jsp.</body>\n" +
223       "\n",
224       response.getText() );
225   }
226
227   public void testDomainSubsiteTrailingSlash() throws Exception JavaDoc {
228     createSiteContexts();
229     createPages();
230
231     WebConversation conversation = new WebConversation();
232     
233     WebResponse response = conversation.getResponse( filterTestUrl + "subsite/" );
234
235     assertEquals(
236       "\n" +
237       "<head>\n" +
238       "<base HREF=\"" + filterTestUrl + "subsite/\">\n" +
239       "<title>Page2</title>\n" +
240       "</head>\n" +
241       "<body>\n" +
242       "This is /WEB-INF/resources/2/templates/template.jsp.</body>\n" +
243       "\n",
244       response.getText() );
245   }
246
247   public void testDomainSubsiteShimPage() throws Exception JavaDoc {
248     createSiteContexts();
249     createPages();
250
251     WebConversation conversation = new WebConversation();
252     
253     WebResponse response = conversation.getResponse( filterTestUrl + "/subsite/page2.shtml" );
254
255     assertEquals(
256       "\n" +
257       "<head>\n" +
258       "<base HREF=\"" + filterTestUrl + "subsite/\">\n" +
259       "<title>Page2</title>\n" +
260       "</head>\n" +
261       "<body>\n" +
262       "This is /WEB-INF/resources/2/templates/template.jsp.</body>\n" +
263       "\n",
264       response.getText() );
265   }
266
267   public void testDomainEmptySite() throws Exception JavaDoc {
268     createSiteContexts();
269
270     WebConversation conversation = new WebConversation();
271     
272     WebResponse response = conversation.getResponse( filterTestUrl + "" );
273
274     assertEquals( "This site is under construction.\n", response.getText() );
275   }
276
277   public void testDomainPageNotFound() throws Exception JavaDoc {
278     createSiteContexts();
279     createPages();
280
281     WebConversation conversation = new WebConversation();
282     
283     WebResponse response = conversation.getResponse( filterTestUrl + "/invalid.shtml" );
284
285     assertEquals( "Page not found.\n", response.getText() );
286   }
287
288   public void testDomainSubsiteEmptySite() throws Exception JavaDoc {
289     createSiteContexts();
290
291     WebConversation conversation = new WebConversation();
292     
293     WebResponse response = conversation.getResponse( filterTestUrl + "/subsite" );
294
295     assertEquals( "This site is under construction.\n", response.getText() );
296   }
297
298   public void testDomainSubsitePageNotFound() throws Exception JavaDoc {
299     createSiteContexts();
300
301     WebConversation conversation = new WebConversation();
302     
303     WebResponse response = conversation.getResponse( filterTestUrl + "/subsite/invalid.shtml" );
304
305     assertEquals( "Page not found.\n", response.getText() );
306   }
307
308   public void testForward() throws Exception JavaDoc {
309     createSiteContexts();
310
311     WebConversation conversation = new WebConversation();
312     
313     WebResponse response = conversation.getResponse( filterTestUrl + "/forward.do" );
314
315     assertEquals( "This is TestAction. Site context id is 1.\n", response.getText() );
316   }
317
318   public void testForwardModule() throws Exception JavaDoc {
319     createSiteContexts();
320
321     WebConversation conversation = new WebConversation();
322     
323     WebResponse response = conversation.getResponse( filterTestUrl + "/module/forward.do" );
324
325     assertEquals( "This is TestModuleAction. Site context id is 1.\n", response.getText() );
326   }
327
328   public void testForwardSubsite() throws Exception JavaDoc {
329     createSiteContexts();
330
331     WebConversation conversation = new WebConversation();
332     
333     WebResponse response = conversation.getResponse( filterTestUrl + "/subsite/forward.do" );
334
335     assertEquals( "This is TestAction. Site context id is 2.\n", response.getText() );
336   }
337
338   public void testForwardSubsiteModule() throws Exception JavaDoc {
339     createSiteContexts();
340
341     WebConversation conversation = new WebConversation();
342     
343     WebResponse response = conversation.getResponse( filterTestUrl + "/subsite/module/forward.do" );
344
345     assertEquals( "This is TestModuleAction. Site context id is 2.\n", response.getText() );
346   }
347
348 }
349
Popular Tags