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.sitecontext;
022:
023: import java.util.*;
024: import java.sql.*;
025: import java.util.regex.*;
026: import java.io.*;
027: import junit.framework.*;
028: import org.apache.log4j.*;
029: import com.methodhead.sitecontext.*;
030: import com.methodhead.persistable.*;
031: import com.methodhead.test.*;
032: import com.meterware.httpunit.*;
033:
034: public class SiteContextFilterTest extends TestCase {
035:
036: String filterTestUrl = null;
037: SiteContext siteContext1 = null;
038: SiteContext siteContext2 = null;
039: String domain = null;
040:
041: static {
042: TestUtils.initLogger();
043: TestUtils.initDb();
044: //TestUtils.setLogLevel( Level.DEBUG );
045: }
046:
047: public SiteContextFilterTest(String name) {
048: super (name);
049: }
050:
051: void createSiteContexts() throws Exception {
052:
053: //
054: // parse the domain out of our test url
055: //
056: Matcher matcher = Pattern.compile("^http:\\/\\/([^:/]+).*")
057: .matcher(filterTestUrl);
058:
059: if (!matcher.matches()) {
060: throw new RuntimeException(
061: "Couldn't match domain in filterTestUrl \""
062: + filterTestUrl + "\"");
063: }
064:
065: domain = matcher.group(1);
066:
067: //
068: // site.com
069: //
070: siteContext1 = new SiteContext();
071: siteContext1.getDomains().add(domain);
072: siteContext1.saveNew();
073:
074: //
075: // site.com/subsite
076: //
077: siteContext2 = new SiteContext();
078: siteContext2.getDomains().add(domain);
079: siteContext2.setString("path", "subsite");
080: siteContext2.saveNew();
081: }
082:
083: protected void setUp() throws Exception {
084: //setLogLevel( Level.DEBUG );
085: ConnectionSingleton.runBatchUpdate(new FileReader(
086: "webapp/WEB-INF/db/transfer-reset.sql"));
087:
088: //
089: // get our test url; this is set in build.properties
090: //
091: filterTestUrl = System.getProperty("filtertest.url");
092: }
093:
094: protected void tearDown() {
095: }
096:
097: public void testInvalidDomain() throws Exception {
098: WebConversation conversation = new WebConversation();
099:
100: WebResponse response = conversation.getResponse(filterTestUrl
101: + "/test.txt");
102:
103: assertEquals("This site is under construction.\n", response
104: .getText());
105: }
106:
107: public void testApproot() throws Exception {
108: createSiteContexts();
109:
110: WebConversation conversation = new WebConversation();
111:
112: WebResponse response = conversation.getResponse(filterTestUrl
113: + "/approot/test.txt");
114:
115: assertEquals("This is /test.txt.\n", response.getText());
116: }
117:
118: public void testDomainFileName() throws Exception {
119: createSiteContexts();
120:
121: WebConversation conversation = new WebConversation();
122:
123: WebResponse response = conversation.getResponse(filterTestUrl
124: + "/test.txt");
125:
126: assertEquals("This is /1/test.txt.\n", response.getText());
127: }
128:
129: public void testDomainSubdirFileName() throws Exception {
130: createSiteContexts();
131:
132: WebConversation conversation = new WebConversation();
133:
134: WebResponse response = conversation.getResponse(filterTestUrl
135: + "/subdir/test.txt");
136:
137: assertEquals("This is /1/subdir/test.txt.\n", response
138: .getText());
139: }
140:
141: public void testDomainStrutsAction() throws Exception {
142: createSiteContexts();
143:
144: WebConversation conversation = new WebConversation();
145:
146: WebResponse response = conversation.getResponse(filterTestUrl
147: + "/action.do");
148:
149: assertEquals("This is TestAction. Site context id is 1.\n",
150: response.getText());
151: }
152:
153: public void testDomainModuleStrutsAction() throws Exception {
154: createSiteContexts();
155:
156: WebConversation conversation = new WebConversation();
157:
158: WebResponse response = conversation.getResponse(filterTestUrl
159: + "/module/action.do");
160:
161: assertEquals(
162: "This is TestModuleAction. Site context id is 1.\n",
163: response.getText());
164: }
165:
166: public void testDomainSubsiteFileName() throws Exception {
167: createSiteContexts();
168:
169: WebConversation conversation = new WebConversation();
170:
171: WebResponse response = conversation.getResponse(filterTestUrl
172: + "/subsite/test.txt");
173:
174: assertEquals("This is /2/test.txt.\n", response.getText());
175: }
176:
177: public void testDomainSubsiteSubdirFileName() throws Exception {
178: createSiteContexts();
179:
180: WebConversation conversation = new WebConversation();
181:
182: WebResponse response = conversation.getResponse(filterTestUrl
183: + "/subsite/subdir/test.txt");
184:
185: assertEquals("This is /2/subdir/test.txt.\n", response
186: .getText());
187: }
188:
189: public void testDomainSubsiteStrutsAction() throws Exception {
190: createSiteContexts();
191:
192: WebConversation conversation = new WebConversation();
193:
194: WebResponse response = conversation.getResponse(filterTestUrl
195: + "/subsite/action.do");
196:
197: assertEquals("This is TestAction. Site context id is 2.\n",
198: response.getText());
199: }
200:
201: public void testDomainSubsiteModuleStrutsAction() throws Exception {
202: createSiteContexts();
203:
204: WebConversation conversation = new WebConversation();
205:
206: WebResponse response = conversation.getResponse(filterTestUrl
207: + "/subsite/module/action.do");
208:
209: assertEquals(
210: "This is TestModuleAction. Site context id is 2.\n",
211: response.getText());
212: }
213:
214: }
|