KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > methodhead > sitecontext > SiteContextFilterTest


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.sitecontext;
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 SiteContextFilterTest
35 extends
36   TestCase {
37
38   String JavaDoc filterTestUrl = null;
39   SiteContext siteContext1 = null;
40   SiteContext siteContext2 = null;
41   String JavaDoc domain = null;
42
43   static {
44     TestUtils.initLogger();
45     TestUtils.initDb();
46     //TestUtils.setLogLevel( Level.DEBUG );
47
}
48
49   public SiteContextFilterTest( String JavaDoc name ) {
50     super( name );
51   }
52
53   void createSiteContexts() throws Exception JavaDoc {
54
55     //
56
// parse the domain out of our test url
57
//
58
Matcher matcher = Pattern.compile( "^http:\\/\\/([^:/]+).*" ).matcher( filterTestUrl );
59
60     if ( !matcher.matches() ) {
61       throw new RuntimeException JavaDoc( "Couldn't match domain in filterTestUrl \"" + filterTestUrl + "\"" );
62     }
63     
64     domain = matcher.group( 1 );
65
66     //
67
// site.com
68
//
69
siteContext1 = new SiteContext();
70     siteContext1.getDomains().add( domain );
71     siteContext1.saveNew();
72
73     //
74
// site.com/subsite
75
//
76
siteContext2 = new SiteContext();
77     siteContext2.getDomains().add( domain );
78     siteContext2.setString( "path", "subsite" );
79     siteContext2.saveNew();
80   }
81
82   protected void setUp() throws Exception JavaDoc {
83     //setLogLevel( Level.DEBUG );
84
ConnectionSingleton.runBatchUpdate( new FileReader( "webapp/WEB-INF/db/transfer-reset.sql" ) );
85
86     //
87
// get our test url; this is set in build.properties
88
//
89
filterTestUrl = System.getProperty( "filtertest.url" );
90   }
91
92   protected void tearDown() {
93   }
94
95   public void testInvalidDomain() throws Exception JavaDoc {
96     WebConversation conversation = new WebConversation();
97     
98     WebResponse response = conversation.getResponse( filterTestUrl + "/test.txt" );
99
100     assertEquals( "This site is under construction.\n", response.getText() );
101   }
102
103   public void testApproot() throws Exception JavaDoc {
104     createSiteContexts();
105
106     WebConversation conversation = new WebConversation();
107     
108     WebResponse response = conversation.getResponse( filterTestUrl + "/approot/test.txt" );
109
110     assertEquals( "This is /test.txt.\n", response.getText() );
111   }
112
113   public void testDomainFileName() throws Exception JavaDoc {
114     createSiteContexts();
115
116     WebConversation conversation = new WebConversation();
117     
118     WebResponse response = conversation.getResponse( filterTestUrl + "/test.txt" );
119
120     assertEquals( "This is /1/test.txt.\n", response.getText() );
121   }
122
123   public void testDomainSubdirFileName() throws Exception JavaDoc {
124     createSiteContexts();
125
126     WebConversation conversation = new WebConversation();
127     
128     WebResponse response = conversation.getResponse( filterTestUrl + "/subdir/test.txt" );
129
130     assertEquals( "This is /1/subdir/test.txt.\n", response.getText() );
131   }
132
133   public void testDomainStrutsAction() throws Exception JavaDoc {
134     createSiteContexts();
135
136     WebConversation conversation = new WebConversation();
137     
138     WebResponse response = conversation.getResponse( filterTestUrl + "/action.do" );
139
140     assertEquals( "This is TestAction. Site context id is 1.\n", response.getText() );
141   }
142
143   public void testDomainModuleStrutsAction() throws Exception JavaDoc {
144     createSiteContexts();
145
146     WebConversation conversation = new WebConversation();
147     
148     WebResponse response = conversation.getResponse( filterTestUrl + "/module/action.do" );
149
150     assertEquals( "This is TestModuleAction. Site context id is 1.\n", response.getText() );
151   }
152
153   public void testDomainSubsiteFileName() throws Exception JavaDoc {
154     createSiteContexts();
155
156     WebConversation conversation = new WebConversation();
157     
158     WebResponse response = conversation.getResponse( filterTestUrl + "/subsite/test.txt" );
159
160     assertEquals( "This is /2/test.txt.\n", response.getText() );
161   }
162
163   public void testDomainSubsiteSubdirFileName() throws Exception JavaDoc {
164     createSiteContexts();
165
166     WebConversation conversation = new WebConversation();
167     
168     WebResponse response = conversation.getResponse( filterTestUrl + "/subsite/subdir/test.txt" );
169
170     assertEquals( "This is /2/subdir/test.txt.\n", response.getText() );
171   }
172
173   public void testDomainSubsiteStrutsAction() throws Exception JavaDoc {
174     createSiteContexts();
175
176     WebConversation conversation = new WebConversation();
177     
178     WebResponse response = conversation.getResponse( filterTestUrl + "/subsite/action.do" );
179
180     assertEquals( "This is TestAction. Site context id is 2.\n", response.getText() );
181   }
182
183   public void testDomainSubsiteModuleStrutsAction() throws Exception JavaDoc {
184     createSiteContexts();
185
186     WebConversation conversation = new WebConversation();
187     
188     WebResponse response = conversation.getResponse( filterTestUrl + "/subsite/module/action.do" );
189
190     assertEquals( "This is TestModuleAction. Site context id is 2.\n", response.getText() );
191   }
192
193 }
194
Popular Tags