1   package org.apache.turbine.util.uri;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import junit.framework.TestSuite;
23  
24  import org.apache.commons.configuration.BaseConfiguration;
25  import org.apache.commons.configuration.Configuration;
26  import org.apache.commons.fileupload.FileItem;
27  import org.apache.commons.fileupload.disk.DiskFileItemFactory;
28  import org.apache.turbine.services.ServiceManager;
29  import org.apache.turbine.services.TurbineServices;
30  import org.apache.turbine.test.BaseTestCase;
31  import org.apache.turbine.util.ServerData;
32  import org.apache.turbine.util.parser.DefaultParameterParser;
33  import org.apache.turbine.util.parser.ParameterParser;
34  import org.apache.turbine.util.parser.ParserUtils;
35  
36  /***
37   * Testing of the TurbineURI class
38   *
39   * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
40   * @author <a href="mailto:seade@backstagetech.com.au">Scott Eade</a>
41   * @version $Id: TurbineURITest.java 534527 2007-05-02 16:10:59Z tv $
42   */
43  public class TurbineURITest extends BaseTestCase
44  {
45      private TurbineURI turi;
46  
47      /***
48       * Constructor for test.
49       *
50       * @param testName name of the test being executed
51       */
52      public TurbineURITest(String testName)
53              throws Exception
54      {
55          super(testName);
56  
57          // Setup configuration
58          ServiceManager serviceManager = TurbineServices.getInstance();
59          serviceManager.setApplicationRoot(".");
60          Configuration cfg = new BaseConfiguration();
61          cfg.setProperty(ParserUtils.URL_CASE_FOLDING_KEY,
62                  ParserUtils.URL_CASE_FOLDING_LOWER_VALUE );
63          serviceManager.setConfiguration(cfg);
64  
65      }
66  
67      /***
68       * Performs any initialization that must happen before each test is run.
69       */
70      protected void setUp()
71      {
72          ServerData sd = new ServerData("www.testserver.com",
73                  URIConstants.HTTP_PORT, URIConstants.HTTP,
74                  "/servlet/turbine", "/context");
75          turi = new TurbineURI(sd);
76      }
77  
78      /***
79       * Clean up after each test is run.
80       */
81      protected void tearDown()
82      {
83          turi = null;
84      }
85  
86      /***
87       * Factory method for creating a TestSuite for this class.
88       *
89       * @return the test suite
90       */
91      public static TestSuite suite()
92      {
93          TestSuite suite = new TestSuite(TurbineURITest.class);
94          return suite;
95      }
96  
97      public void testAddRemove()
98      {
99          assertEquals("TurbineURI should not have a pathInfo", false, turi.hasPathInfo());
100         assertEquals("TurbineURI must not have a queryData", false, turi.hasQueryData());
101         turi.addPathInfo("test","x");
102         assertEquals("TurbineURI must have a pathInfo", true, turi.hasPathInfo());
103         assertEquals("TurbineURI must not have a queryData", false, turi.hasQueryData());
104         turi.removePathInfo("test");
105         assertEquals("TurbineURI must not have a pathInfo", false, turi.hasPathInfo());
106         assertEquals("TurbineURI must not have a queryData", false, turi.hasQueryData());
107 
108         assertEquals("TurbineURI should not have a queryData", false, turi.hasQueryData());
109         assertEquals("TurbineURI must not have a pathInfo", false, turi.hasPathInfo());
110         turi.addQueryData("test","x");
111         assertEquals("TurbineURI must have a queryData", true, turi.hasQueryData());
112         assertEquals("TurbineURI must not have a pathInfo", false, turi.hasPathInfo());
113         turi.removeQueryData("test");
114         assertEquals("TurbineURI must not have a queryData", false, turi.hasQueryData());
115         assertEquals("TurbineURI must not have a pathInfo", false, turi.hasPathInfo());
116     }
117 
118     public void testEmptyAndNullQueryData()
119     {
120         // Check empty String
121         assertEquals("/context/servlet/turbine", turi.getRelativeLink());
122         turi.addQueryData("test", "");
123         assertEquals("/context/servlet/turbine?test=", turi.getRelativeLink());
124         turi.removeQueryData("test");
125 
126         // Check null
127         assertEquals("/context/servlet/turbine", turi.getRelativeLink());
128         turi.addQueryData("test", null);
129         assertEquals("/context/servlet/turbine?test=null", turi.getRelativeLink());
130         turi.removeQueryData("test");
131         assertEquals("/context/servlet/turbine", turi.getRelativeLink());
132     }
133 
134     public void testEmptyAndNullPathInfo()
135     {
136         // Check empty String
137         assertEquals("/context/servlet/turbine", turi.getRelativeLink());
138         turi.addPathInfo("test", "");
139         // Kind of susspect result - might result in "//" in the URL.
140         assertEquals("/context/servlet/turbine/test/", turi.getRelativeLink());
141         turi.removePathInfo("test");
142 
143         // Check null
144         assertEquals("/context/servlet/turbine", turi.getRelativeLink());
145         turi.addPathInfo("test", null);
146         assertEquals("/context/servlet/turbine/test/null", turi.getRelativeLink());
147         turi.removePathInfo("test");
148         assertEquals("/context/servlet/turbine", turi.getRelativeLink());
149     }
150 
151     public void testAddEmptyParameterParser()
152     {
153         ParameterParser pp = new DefaultParameterParser();
154         turi.add(1, pp); // 1 = query data
155         assertEquals("/context/servlet/turbine", turi.getRelativeLink());
156     }
157 
158     public void testAddParameterParser()
159     {
160         ParameterParser pp = new DefaultParameterParser();
161         pp.add("test", "");
162         turi.add(1, pp); // 1 = query data
163         assertEquals("/context/servlet/turbine?test=", turi.getRelativeLink());
164         turi.removeQueryData("test");
165         assertEquals("/context/servlet/turbine", turi.getRelativeLink());
166 
167         pp = new DefaultParameterParser();
168         pp.add("test", (String) null);
169         turi.add(1, pp); // 1 = query data
170         // Should make the following work so as to be consistent with directly added values.
171         //assertEquals("/context/servlet/turbine?test=null", turi.getRelativeLink());
172         turi.removeQueryData("test");
173         assertEquals("/context/servlet/turbine", turi.getRelativeLink());
174 
175         // TRB-8
176         pp = new DefaultParameterParser();
177         DiskFileItemFactory factory = new DiskFileItemFactory(10240, null);
178         FileItem test = factory.createItem("upload-field", "application/octet-stream", false, null);
179         pp.add("upload-field", test);
180         turi.add(1, pp); // 1 = query data
181         assertEquals("/context/servlet/turbine?upload-field=", turi.getRelativeLink());
182         turi.removeQueryData("upload-field");
183         assertEquals("/context/servlet/turbine", turi.getRelativeLink());
184     }
185 
186 }