KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > web > rewrite > RewriteTestCase


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2006, JBoss Inc., and individual contributors as indicated
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22
23
24 package org.jboss.web.rewrite;
25
26 import junit.framework.Test;
27 import junit.framework.TestCase;
28 import junit.framework.TestSuite;
29
30 public class RewriteTestCase extends TestCase {
31
32     protected class TestResolver extends Resolver {
33
34         public String JavaDoc resolve(String JavaDoc key) {
35             return "server_variable_value[" + key + "]";
36         }
37
38         public String JavaDoc resolveHttp(String JavaDoc key) {
39             return "http_header_value[" + key + "]";
40         }
41
42         public String JavaDoc resolveSsl(String JavaDoc key) {
43             return "ssl_property_value[" + key + "]";
44         }
45         
46         public boolean resolveResource(int type, String JavaDoc name) {
47             return true;
48         }
49
50     }
51     
52     /**
53      * Construct a new instance of this test case.
54      *
55      * @param name Name of the test case
56      */

57     public RewriteTestCase(String JavaDoc name) {
58         super(name);
59     }
60
61     /**
62      * Set up instance variables required by this test case.
63      */

64     public void setUp() {
65     }
66
67     /**
68      * Return the tests included in this test suite.
69      */

70     public static Test suite() {
71         return (new TestSuite(RewriteTestCase.class));
72     }
73
74     /**
75      * Tear down instance variables required by this test case.
76      */

77     public void tearDown() {
78     }
79     
80     public void testCanonicalUrl() {
81         Resolver resolver = new TestResolver();
82         
83         RewriteRule rule1 = new RewriteRule();
84         rule1.setPatternString("^/~([^/]+)/?(.*)");
85         rule1.setSubstitutionString("/u/$1/$2");
86         
87         RewriteRule rule2 = new RewriteRule();
88         rule2.setPatternString("^/([uge])/([^/]+)$");
89         rule2.setSubstitutionString("/$1/$2/");
90
91         RewriteRule[] rules = new RewriteRule[2];
92         rules[0] = rule1;
93         rules[1] = rule2;
94         for (int i = 0; i < rules.length; i++) {
95             rules[i].parse(null);
96         }
97         
98         String JavaDoc result = rewriteUrl("/~user/foo/bar", resolver, rules).toString();
99         assertEquals("/u/user/foo/bar", result);
100         result = rewriteUrl("/u/user", resolver, rules).toString();
101         assertEquals("/u/user/", result);
102     }
103     
104     public void testCanonicalHostname() {
105         Resolver resolver = new TestResolver();
106         
107         RewriteRule rule = new RewriteRule();
108         rule.setPatternString("^/(.*)");
109         rule.setSubstitutionString("http://fully.qualified.domain.name:%{SERVER_PORT}/$1");
110         RewriteRule[] rules = new RewriteRule[1];
111         rules[0] = rule;
112         
113         RewriteCond cond1 = new RewriteCond();
114         cond1.setTestString("%{HTTP_HOST}");
115         cond1.setCondPattern("!^fully\\.qualified\\.domain\\.name");
116         cond1.setNocase(true);
117         
118         RewriteCond cond2 = new RewriteCond();
119         cond2.setTestString("%{HTTP_HOST}");
120         cond2.setCondPattern("!^$");
121         
122         RewriteCond cond3 = new RewriteCond();
123         cond3.setTestString("%{SERVER_PORT}");
124         cond3.setCondPattern("!^80$");
125         
126         rule.addCondition(cond1);
127         rule.addCondition(cond2);
128         rule.addCondition(cond3);
129         
130         for (int i = 0; i < rules.length; i++) {
131             rules[i].parse(null);
132         }
133         
134         String JavaDoc result = rewriteUrl("/foo/bar", resolver, rules).toString();
135         assertEquals("http://fully.qualified.domain.name:server_variable_value[SERVER_PORT]/foo/bar", result);
136     }
137     
138     public void testMovedDocumentRoot() {
139         Resolver resolver = new TestResolver();
140
141         RewriteRule rule = new RewriteRule();
142         rule.setPatternString("^/$");
143         rule.setSubstitutionString("/about/");
144         
145         RewriteRule[] rules = new RewriteRule[1];
146         rules[0] = rule;
147         for (int i = 0; i < rules.length; i++) {
148             rules[i].parse(null);
149         }
150         
151         String JavaDoc result = rewriteUrl("/", resolver, rules).toString();
152         assertEquals("/about/", result);
153     }
154     
155     public void testParsing() {
156         Object JavaDoc result = null;
157         String JavaDoc test = null;
158         RewriteRule resultRule = null;
159         RewriteCond resultCond = null;
160         
161         test = "RewriteRule ^/~([^/]+)/?(.*) /u/$1/$2 [R]";
162         result = RewriteValve.parse(test);
163         if (result instanceof RewriteRule) {
164             resultRule = (RewriteRule) result;
165         }
166         assertNotNull(resultRule);
167         assertTrue(resultRule.isRedirect());
168         assertEquals(resultRule.getPatternString(), "^/~([^/]+)/?(.*)");
169         assertEquals(resultRule.getSubstitutionString(), "/u/$1/$2");
170         resultRule = null;
171         
172         test = "RewriteRule ^/(.*) http://fully.qualified.domain.name:%{SERVER_PORT}/$1 [L,R]";
173         result = RewriteValve.parse(test);
174         if (result instanceof RewriteRule) {
175             resultRule = (RewriteRule) result;
176         }
177         assertNotNull(resultRule);
178         assertTrue(resultRule.isRedirect());
179         assertTrue(resultRule.isLast());
180         assertEquals(resultRule.getPatternString(), "^/(.*)");
181         assertEquals(resultRule.getSubstitutionString(), "http://fully.qualified.domain.name:%{SERVER_PORT}/$1");
182         resultRule = null;
183         
184         test = "RewriteCond %{HTTP_HOST} !^fully\\.qualified\\.domain\\.name [NC]";
185         result = RewriteValve.parse(test);
186         if (result instanceof RewriteCond) {
187             resultCond = (RewriteCond) result;
188         }
189         assertNotNull(resultCond);
190         assertTrue(resultCond.isNocase());
191         assertEquals(resultCond.getTestString(), "%{HTTP_HOST}");
192         assertEquals(resultCond.getCondPattern(), "!^fully\\.qualified\\.domain\\.name");
193         resultCond = null;
194         
195     }
196     
197     
198     public static CharSequence JavaDoc rewriteUrl(CharSequence JavaDoc url, Resolver resolver, RewriteRule[] rules) {
199         if (rules == null)
200             return url;
201         for (int i = 0; i < rules.length; i++) {
202             CharSequence JavaDoc newurl = rules[i].evaluate(url, resolver);
203             if (newurl != null) {
204                 // Check some extra flags, including redirect, proxying, etc
205
// FIXME
206
url = newurl;
207             }
208         }
209         return url;
210     }
211
212 }
213
Popular Tags