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.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 ShimFilterTest extends TestCase {
035:
036: String filterTestUrl = null;
037: SiteContext siteContext1 = null;
038: SiteContext siteContext2 = null;
039:
040: static {
041: TestUtils.initLogger();
042: TestUtils.initDb();
043: TestUtils.setLogLevel(Level.DEBUG);
044: }
045:
046: public ShimFilterTest(String name) {
047: super (name);
048: }
049:
050: void createSiteContexts() throws Exception {
051:
052: //
053: // parse the domain out of our test url
054: //
055: Matcher matcher = Pattern.compile("^http:\\/\\/([^:/]+).*")
056: .matcher(filterTestUrl);
057:
058: if (!matcher.matches()) {
059: throw new RuntimeException(
060: "Couldn't match domain in filterTestUrl \""
061: + filterTestUrl + "\"");
062: }
063:
064: String domain = matcher.group(1);
065:
066: //
067: // site.com
068: //
069: siteContext1 = new SiteContext();
070: siteContext1.getDomains().add(domain);
071: siteContext1.saveNew();
072:
073: //
074: // site.com/subsite
075: //
076: siteContext2 = new SiteContext();
077: siteContext2.getDomains().add(domain);
078: siteContext2.setString("path", "subsite");
079: siteContext2.saveNew();
080: }
081:
082: void createPages() throws Exception {
083:
084: Page pg = null;
085: Link link = new Link();
086: SiteMap siteMap = new SiteMap();
087:
088: //
089: // site.com
090: //
091: pg = new Page();
092: pg.setSiteContext(siteContext1);
093: pg.setString("title", "Page1");
094: pg.setString("aliasname", "page1");
095: pg.setString("template", "template.jsp");
096: pg.setBoolean("hidden", false);
097: pg.saveNew();
098:
099: link.setAlias(pg.getString("aliasname"));
100: link.setTitle(pg.getString("title"));
101: link.setHidden(false);
102: link.setPageId(pg.getInt("id"));
103:
104: siteMap.setSiteContext(siteContext1);
105: siteMap.setRoot(link);
106: siteMap.save();
107:
108: //
109: // site.com/subsite
110: //
111: pg = new Page();
112: pg.setSiteContext(siteContext2);
113: pg.setString("title", "Page2");
114: pg.setString("aliasname", "page2");
115: pg.setString("template", "template.jsp");
116: pg.setBoolean("hidden", false);
117: pg.saveNew();
118:
119: link.setAlias(pg.getString("aliasname"));
120: link.setTitle(pg.getString("title"));
121: link.setHidden(false);
122: link.setPageId(pg.getInt("id"));
123:
124: siteMap.setSiteContext(siteContext2);
125: siteMap.setRoot(link);
126: siteMap.save();
127: }
128:
129: protected void setUp() throws Exception {
130: //setLogLevel( Level.DEBUG );
131: ConnectionSingleton.runBatchUpdate(new FileReader(
132: "webapp/WEB-INF/db/transfer-reset.sql"));
133:
134: //
135: // get our test url; this is set in build.properties
136: //
137: filterTestUrl = System.getProperty("filtertest.url");
138:
139: //
140: // clear any loaded site maps
141: //
142: WebConversation conversation = new WebConversation();
143: WebResponse response = conversation.getResponse(filterTestUrl
144: + "/util.do?action=clearSiteMaps");
145: }
146:
147: protected void tearDown() {
148: }
149:
150: public void testDomain() throws Exception {
151: createSiteContexts();
152: createPages();
153:
154: WebConversation conversation = new WebConversation();
155:
156: WebResponse response = conversation.getResponse(filterTestUrl
157: + "");
158:
159: assertEquals(
160: "\n"
161: + "<head>\n"
162: + "<base href=\""
163: + filterTestUrl
164: + "\">\n"
165: + "<title>Page1</title>\n"
166: + "</head>\n"
167: + "<body>\n"
168: + "This is /WEB-INF/resources/1/templates/template.jsp.</body>\n"
169: + "\n", response.getText());
170: }
171:
172: public void testDomainTrailingSlash() throws Exception {
173: createSiteContexts();
174: createPages();
175:
176: WebConversation conversation = new WebConversation();
177:
178: WebResponse response = conversation.getResponse(filterTestUrl
179: + "/");
180:
181: assertEquals(
182: "\n"
183: + "<head>\n"
184: + "<base href=\""
185: + filterTestUrl
186: + "\">\n"
187: + "<title>Page1</title>\n"
188: + "</head>\n"
189: + "<body>\n"
190: + "This is /WEB-INF/resources/1/templates/template.jsp.</body>\n"
191: + "\n", response.getText());
192: }
193:
194: public void testDomainShimPage() throws Exception {
195: createSiteContexts();
196: createPages();
197:
198: WebConversation conversation = new WebConversation();
199:
200: WebResponse response = conversation.getResponse(filterTestUrl
201: + "/page1.shtml");
202:
203: assertEquals(
204: "\n"
205: + "<head>\n"
206: + "<base href=\""
207: + filterTestUrl
208: + "\">\n"
209: + "<title>Page1</title>\n"
210: + "</head>\n"
211: + "<body>\n"
212: + "This is /WEB-INF/resources/1/templates/template.jsp.</body>\n"
213: + "\n", response.getText());
214: }
215:
216: public void testDomainSubsite() throws Exception {
217: createSiteContexts();
218: createPages();
219:
220: WebConversation conversation = new WebConversation();
221:
222: WebResponse response = conversation.getResponse(filterTestUrl
223: + "/subsite");
224:
225: assertEquals(
226: "\n"
227: + "<head>\n"
228: + "<base href=\""
229: + filterTestUrl
230: + "subsite/\">\n"
231: + "<title>Page2</title>\n"
232: + "</head>\n"
233: + "<body>\n"
234: + "This is /WEB-INF/resources/2/templates/template.jsp.</body>\n"
235: + "\n", response.getText());
236: }
237:
238: public void testDomainSubsiteTrailingSlash() throws Exception {
239: createSiteContexts();
240: createPages();
241:
242: WebConversation conversation = new WebConversation();
243:
244: WebResponse response = conversation.getResponse(filterTestUrl
245: + "subsite/");
246:
247: assertEquals(
248: "\n"
249: + "<head>\n"
250: + "<base href=\""
251: + filterTestUrl
252: + "subsite/\">\n"
253: + "<title>Page2</title>\n"
254: + "</head>\n"
255: + "<body>\n"
256: + "This is /WEB-INF/resources/2/templates/template.jsp.</body>\n"
257: + "\n", response.getText());
258: }
259:
260: public void testDomainSubsiteShimPage() throws Exception {
261: createSiteContexts();
262: createPages();
263:
264: WebConversation conversation = new WebConversation();
265:
266: WebResponse response = conversation.getResponse(filterTestUrl
267: + "/subsite/page2.shtml");
268:
269: assertEquals(
270: "\n"
271: + "<head>\n"
272: + "<base href=\""
273: + filterTestUrl
274: + "subsite/\">\n"
275: + "<title>Page2</title>\n"
276: + "</head>\n"
277: + "<body>\n"
278: + "This is /WEB-INF/resources/2/templates/template.jsp.</body>\n"
279: + "\n", response.getText());
280: }
281:
282: public void testDomainEmptySite() throws Exception {
283: createSiteContexts();
284:
285: WebConversation conversation = new WebConversation();
286:
287: WebResponse response = conversation.getResponse(filterTestUrl
288: + "");
289:
290: assertEquals("This site is under construction.\n", response
291: .getText());
292: }
293:
294: public void testDomainPageNotFound() throws Exception {
295: createSiteContexts();
296: createPages();
297:
298: WebConversation conversation = new WebConversation();
299:
300: WebResponse response = conversation.getResponse(filterTestUrl
301: + "/invalid.shtml");
302:
303: assertEquals("Page not found.\n", response.getText());
304: }
305:
306: public void testDomainSubsiteEmptySite() throws Exception {
307: createSiteContexts();
308:
309: WebConversation conversation = new WebConversation();
310:
311: WebResponse response = conversation.getResponse(filterTestUrl
312: + "/subsite");
313:
314: assertEquals("This site is under construction.\n", response
315: .getText());
316: }
317:
318: public void testDomainSubsitePageNotFound() throws Exception {
319: createSiteContexts();
320:
321: WebConversation conversation = new WebConversation();
322:
323: WebResponse response = conversation.getResponse(filterTestUrl
324: + "/subsite/invalid.shtml");
325:
326: assertEquals("Page not found.\n", response.getText());
327: }
328:
329: public void testForward() throws Exception {
330: createSiteContexts();
331:
332: WebConversation conversation = new WebConversation();
333:
334: WebResponse response = conversation.getResponse(filterTestUrl
335: + "/forward.do");
336:
337: assertEquals("This is TestAction. Site context id is 1.\n",
338: response.getText());
339: }
340:
341: public void testForwardModule() throws Exception {
342: createSiteContexts();
343:
344: WebConversation conversation = new WebConversation();
345:
346: WebResponse response = conversation.getResponse(filterTestUrl
347: + "/module/forward.do");
348:
349: assertEquals(
350: "This is TestModuleAction. Site context id is 1.\n",
351: response.getText());
352: }
353:
354: public void testForwardSubsite() throws Exception {
355: createSiteContexts();
356:
357: WebConversation conversation = new WebConversation();
358:
359: WebResponse response = conversation.getResponse(filterTestUrl
360: + "/subsite/forward.do");
361:
362: assertEquals("This is TestAction. Site context id is 2.\n",
363: response.getText());
364: }
365:
366: public void testForwardSubsiteModule() throws Exception {
367: createSiteContexts();
368:
369: WebConversation conversation = new WebConversation();
370:
371: WebResponse response = conversation.getResponse(filterTestUrl
372: + "/subsite/module/forward.do");
373:
374: assertEquals(
375: "This is TestModuleAction. Site context id is 2.\n",
376: response.getText());
377: }
378:
379: }
|