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 junit.framework.TestSuite;
023:
024: import org.apache.commons.configuration.BaseConfiguration;
025: import org.apache.commons.configuration.Configuration;
026: import org.apache.commons.fileupload.FileItem;
027: import org.apache.commons.fileupload.disk.DiskFileItemFactory;
028: import org.apache.turbine.services.ServiceManager;
029: import org.apache.turbine.services.TurbineServices;
030: import org.apache.turbine.test.BaseTestCase;
031: import org.apache.turbine.util.ServerData;
032: import org.apache.turbine.util.parser.DefaultParameterParser;
033: import org.apache.turbine.util.parser.ParameterParser;
034: import org.apache.turbine.util.parser.ParserUtils;
035:
036: /**
037: * Testing of the TurbineURI class
038: *
039: * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
040: * @author <a href="mailto:seade@backstagetech.com.au">Scott Eade</a>
041: * @version $Id: TurbineURITest.java 534527 2007-05-02 16:10:59Z tv $
042: */
043: public class TurbineURITest extends BaseTestCase {
044: private TurbineURI turi;
045:
046: /**
047: * Constructor for test.
048: *
049: * @param testName name of the test being executed
050: */
051: public TurbineURITest(String testName) throws Exception {
052: super (testName);
053:
054: // Setup configuration
055: ServiceManager serviceManager = TurbineServices.getInstance();
056: serviceManager.setApplicationRoot(".");
057: Configuration cfg = new BaseConfiguration();
058: cfg.setProperty(ParserUtils.URL_CASE_FOLDING_KEY,
059: ParserUtils.URL_CASE_FOLDING_LOWER_VALUE);
060: serviceManager.setConfiguration(cfg);
061:
062: }
063:
064: /**
065: * Performs any initialization that must happen before each test is run.
066: */
067: protected void setUp() {
068: ServerData sd = new ServerData("www.testserver.com",
069: URIConstants.HTTP_PORT, URIConstants.HTTP,
070: "/servlet/turbine", "/context");
071: turi = new TurbineURI(sd);
072: }
073:
074: /**
075: * Clean up after each test is run.
076: */
077: protected void tearDown() {
078: turi = null;
079: }
080:
081: /**
082: * Factory method for creating a TestSuite for this class.
083: *
084: * @return the test suite
085: */
086: public static TestSuite suite() {
087: TestSuite suite = new TestSuite(TurbineURITest.class);
088: return suite;
089: }
090:
091: public void testAddRemove() {
092: assertEquals("TurbineURI should not have a pathInfo", false,
093: turi.hasPathInfo());
094: assertEquals("TurbineURI must not have a queryData", false,
095: turi.hasQueryData());
096: turi.addPathInfo("test", "x");
097: assertEquals("TurbineURI must have a pathInfo", true, turi
098: .hasPathInfo());
099: assertEquals("TurbineURI must not have a queryData", false,
100: turi.hasQueryData());
101: turi.removePathInfo("test");
102: assertEquals("TurbineURI must not have a pathInfo", false, turi
103: .hasPathInfo());
104: assertEquals("TurbineURI must not have a queryData", false,
105: turi.hasQueryData());
106:
107: assertEquals("TurbineURI should not have a queryData", false,
108: turi.hasQueryData());
109: assertEquals("TurbineURI must not have a pathInfo", false, turi
110: .hasPathInfo());
111: turi.addQueryData("test", "x");
112: assertEquals("TurbineURI must have a queryData", true, turi
113: .hasQueryData());
114: assertEquals("TurbineURI must not have a pathInfo", false, turi
115: .hasPathInfo());
116: turi.removeQueryData("test");
117: assertEquals("TurbineURI must not have a queryData", false,
118: turi.hasQueryData());
119: assertEquals("TurbineURI must not have a pathInfo", false, turi
120: .hasPathInfo());
121: }
122:
123: public void testEmptyAndNullQueryData() {
124: // Check empty String
125: assertEquals("/context/servlet/turbine", turi.getRelativeLink());
126: turi.addQueryData("test", "");
127: assertEquals("/context/servlet/turbine?test=", turi
128: .getRelativeLink());
129: turi.removeQueryData("test");
130:
131: // Check null
132: assertEquals("/context/servlet/turbine", turi.getRelativeLink());
133: turi.addQueryData("test", null);
134: assertEquals("/context/servlet/turbine?test=null", turi
135: .getRelativeLink());
136: turi.removeQueryData("test");
137: assertEquals("/context/servlet/turbine", turi.getRelativeLink());
138: }
139:
140: public void testEmptyAndNullPathInfo() {
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
146: .getRelativeLink());
147: turi.removePathInfo("test");
148:
149: // Check null
150: assertEquals("/context/servlet/turbine", turi.getRelativeLink());
151: turi.addPathInfo("test", null);
152: assertEquals("/context/servlet/turbine/test/null", turi
153: .getRelativeLink());
154: turi.removePathInfo("test");
155: assertEquals("/context/servlet/turbine", turi.getRelativeLink());
156: }
157:
158: public void testAddEmptyParameterParser() {
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() {
165: ParameterParser pp = new DefaultParameterParser();
166: pp.add("test", "");
167: turi.add(1, pp); // 1 = query data
168: assertEquals("/context/servlet/turbine?test=", turi
169: .getRelativeLink());
170: turi.removeQueryData("test");
171: assertEquals("/context/servlet/turbine", turi.getRelativeLink());
172:
173: pp = new DefaultParameterParser();
174: pp.add("test", (String) null);
175: turi.add(1, pp); // 1 = query data
176: // Should make the following work so as to be consistent with directly added values.
177: //assertEquals("/context/servlet/turbine?test=null", turi.getRelativeLink());
178: turi.removeQueryData("test");
179: assertEquals("/context/servlet/turbine", turi.getRelativeLink());
180:
181: // TRB-8
182: pp = new DefaultParameterParser();
183: DiskFileItemFactory factory = new DiskFileItemFactory(10240,
184: null);
185: FileItem test = factory.createItem("upload-field",
186: "application/octet-stream", false, null);
187: pp.add("upload-field", test);
188: turi.add(1, pp); // 1 = query data
189: assertEquals("/context/servlet/turbine?upload-field=", turi
190: .getRelativeLink());
191: turi.removeQueryData("upload-field");
192: assertEquals("/context/servlet/turbine", turi.getRelativeLink());
193: }
194:
195: }
|