1 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 filterTestUrl = null; 39 SiteContext siteContext1 = null; 40 SiteContext siteContext2 = null; 41 String domain = null; 42 43 static { 44 TestUtils.initLogger(); 45 TestUtils.initDb(); 46 } 48 49 public SiteContextFilterTest( String name ) { 50 super( name ); 51 } 52 53 void createSiteContexts() throws Exception { 54 55 Matcher matcher = Pattern.compile( "^http:\\/\\/([^:/]+).*" ).matcher( filterTestUrl ); 59 60 if ( !matcher.matches() ) { 61 throw new RuntimeException ( "Couldn't match domain in filterTestUrl \"" + filterTestUrl + "\"" ); 62 } 63 64 domain = matcher.group( 1 ); 65 66 siteContext1 = new SiteContext(); 70 siteContext1.getDomains().add( domain ); 71 siteContext1.saveNew(); 72 73 siteContext2 = new SiteContext(); 77 siteContext2.getDomains().add( domain ); 78 siteContext2.setString( "path", "subsite" ); 79 siteContext2.saveNew(); 80 } 81 82 protected void setUp() throws Exception { 83 ConnectionSingleton.runBatchUpdate( new FileReader( "webapp/WEB-INF/db/transfer-reset.sql" ) ); 85 86 filterTestUrl = System.getProperty( "filtertest.url" ); 90 } 91 92 protected void tearDown() { 93 } 94 95 public void testInvalidDomain() throws Exception { 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 { 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 { 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 { 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 { 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 { 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 { 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 { 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 { 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 { 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 |