001 package org.apache.turbine.util.uri;
002
003 /*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements. See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership. The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License. You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied. See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022 import org.apache.fulcrum.parser.DefaultParameterParser;
023 import org.apache.fulcrum.parser.ParameterParser;
024 import org.apache.fulcrum.parser.ParserService;
025 import org.apache.turbine.services.TurbineServices;
026 import org.apache.turbine.test.BaseTestCase;
027 import org.apache.turbine.util.ServerData;
028 import org.apache.turbine.util.TurbineConfig;
029
030 /**
031 * Testing of the TurbineURI class
032 *
033 * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
034 * @author <a href="mailto:seade@backstagetech.com.au">Scott Eade</a>
035 * @version $Id: TurbineURITest.java 717931 2008-11-15 21:40:43Z tv $
036 */
037 public class TurbineURITest extends BaseTestCase
038 {
039 private TurbineURI turi;
040
041 private ParserService parserService;
042
043 private static TurbineConfig tc = null;
044
045 /**
046 * Constructor for test.
047 *
048 * @param testName
049 * name of the test being executed
050 */
051 public TurbineURITest(String testName) throws Exception
052 {
053 super(testName);
054
055 // Setup configuration
056 tc =
057 new TurbineConfig(
058 ".",
059 "/conf/test/CompleteTurbineResources.properties");
060 tc.initialize();
061 }
062
063 /**
064 * Performs any initialization that must happen before each test is run.
065 */
066 protected void setUp()
067 {
068 ServerData sd = new ServerData("www.testserver.com",
069 URIConstants.HTTP_PORT, URIConstants.HTTP, "/servlet/turbine",
070 "/context");
071 turi = new TurbineURI(sd);
072
073 parserService = (ParserService)TurbineServices.getInstance().getService(ParserService.ROLE);
074 }
075
076 /**
077 * Clean up after each test is run.
078 */
079 protected void tearDown()
080 {
081 if (tc != null)
082 {
083 tc.dispose();
084 }
085
086 turi = null;
087 }
088
089 public void testAddRemove()
090 {
091 assertEquals("TurbineURI should not have a pathInfo", false, turi
092 .hasPathInfo());
093 assertEquals("TurbineURI must not have a queryData", false, turi
094 .hasQueryData());
095 turi.addPathInfo("test", "x");
096 assertEquals("TurbineURI must have a pathInfo", true, turi
097 .hasPathInfo());
098 assertEquals("TurbineURI must not have a queryData", false, turi
099 .hasQueryData());
100 turi.removePathInfo("test");
101 assertEquals("TurbineURI must not have a pathInfo", false, turi
102 .hasPathInfo());
103 assertEquals("TurbineURI must not have a queryData", false, turi
104 .hasQueryData());
105
106 assertEquals("TurbineURI should not have a queryData", false, turi
107 .hasQueryData());
108 assertEquals("TurbineURI must not have a pathInfo", false, turi
109 .hasPathInfo());
110 turi.addQueryData("test", "x");
111 assertEquals("TurbineURI must have a queryData", true, turi
112 .hasQueryData());
113 assertEquals("TurbineURI must not have a pathInfo", false, turi
114 .hasPathInfo());
115 turi.removeQueryData("test");
116 assertEquals("TurbineURI must not have a queryData", false, turi
117 .hasQueryData());
118 assertEquals("TurbineURI must not have a pathInfo", false, turi
119 .hasPathInfo());
120 }
121
122 public void testEmptyAndNullQueryData()
123 {
124 // Check empty String
125 assertEquals("/context/servlet/turbine", turi.getRelativeLink());
126 turi.addQueryData("test", "");
127 assertEquals("/context/servlet/turbine?test=", turi.getRelativeLink());
128 turi.removeQueryData("test");
129
130 // Check null
131 assertEquals("/context/servlet/turbine", turi.getRelativeLink());
132 turi.addQueryData("test", null);
133 assertEquals("/context/servlet/turbine?test=null", turi
134 .getRelativeLink());
135 turi.removeQueryData("test");
136 assertEquals("/context/servlet/turbine", turi.getRelativeLink());
137 }
138
139 public void testEmptyAndNullPathInfo()
140 {
141 // Check empty String
142 assertEquals("/context/servlet/turbine", turi.getRelativeLink());
143 turi.addPathInfo("test", "");
144 // Kind of susspect result - might result in "//" in the URL.
145 assertEquals("/context/servlet/turbine/test/", turi.getRelativeLink());
146 turi.removePathInfo("test");
147
148 // Check null
149 assertEquals("/context/servlet/turbine", turi.getRelativeLink());
150 turi.addPathInfo("test", null);
151 assertEquals("/context/servlet/turbine/test/null", turi
152 .getRelativeLink());
153 turi.removePathInfo("test");
154 assertEquals("/context/servlet/turbine", turi.getRelativeLink());
155 }
156
157 public void testAddEmptyParameterParser()
158 {
159 ParameterParser pp = new DefaultParameterParser();
160 turi.add(1, pp); // 1 = query data
161 assertEquals("/context/servlet/turbine", turi.getRelativeLink());
162 }
163
164 public void testAddParameterParser() throws InstantiationException
165 {
166 ParameterParser pp = (ParameterParser) parserService.getParser(DefaultParameterParser.class);
167 pp.add("test", "");
168 turi.add(1, pp); // 1 = query data
169 assertEquals("/context/servlet/turbine?test=", turi.getRelativeLink());
170 turi.removeQueryData("test");
171 assertEquals("/context/servlet/turbine", turi.getRelativeLink());
172
173 parserService.putParser(pp);
174 pp = (ParameterParser) parserService.getParser(DefaultParameterParser.class);
175 pp.add("test", (String) null);
176 turi.add(1, pp); // 1 = query data
177 // Should make the following work so as to be consistent with directly
178 // added values.
179 // assertEquals("/context/servlet/turbine?test=null",
180 // turi.getRelativeLink());
181 turi.removeQueryData("test");
182 assertEquals("/context/servlet/turbine", turi.getRelativeLink());
183
184 // TRB-8
185 //
186 // This is commented out for now as it results in a ClassCastException.
187 // The 2_3 branch parser changes need to be merged into the fulcrum
188 // code.
189 //
190 // pp = new DefaultParameterParser();
191 // DiskFileItemFactory factory = new DiskFileItemFactory(10240, null);
192 // FileItem test = factory.createItem("upload-field",
193 // "application/octet-stream", false, null);
194 // pp.append("upload-field", test);
195 // // The following causes a ClassCastException with or without the
196 // TRB-8 fix.
197 // turi.add(1, pp); // 1 = query data
198 // assertEquals("/context/servlet/turbine?upload-field=",
199 // turi.getRelativeLink());
200 // turi.removeQueryData("upload-field");
201 // assertEquals("/context/servlet/turbine", turi.getRelativeLink());
202 }
203
204 }