View Javadoc
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 static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertTrue;
24  import static org.junit.Assert.assertFalse;
25  
26  import org.apache.fulcrum.parser.DefaultParameterParser;
27  import org.apache.fulcrum.parser.ParameterParser;
28  import org.apache.fulcrum.parser.ParserService;
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.TurbineConfig;
33  import org.junit.AfterClass;
34  import org.junit.Before;
35  import org.junit.BeforeClass;
36  import org.junit.Test;
37  
38  /**
39   * Testing of the TurbineURI class
40   *
41   * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
42   * @author <a href="mailto:seade@backstagetech.com.au">Scott Eade</a>
43   * @version $Id: TurbineURITest.java 1616993 2014-08-09 17:03:07Z tv $
44   */
45  public class TurbineURITest extends BaseTestCase
46  {
47      private TurbineURI turi;
48  
49      private ParserService parserService;
50  
51      private static TurbineConfig tc = null;
52  
53  
54      @BeforeClass
55      public static void init() {
56          tc =  new TurbineConfig(
57                              ".",
58                              "/conf/test/CompleteTurbineResources.properties");
59          tc.initialize();
60      }
61      /**
62       * Performs any initialization that must happen before each test is run.
63       */
64  
65      @Before
66      public void setup()
67      {
68          // Setup configuration
69  
70          ServerData sd = new ServerData("www.testserver.com",
71                  URIConstants.HTTP_PORT, URIConstants.HTTP, "/servlet/turbine",
72                  "/context");
73          turi = new TurbineURI(sd);
74  
75          parserService = (ParserService)TurbineServices.getInstance().getService(ParserService.ROLE);
76      }
77  
78      /**
79       * Clean up after each test is run.
80       */
81      @AfterClass
82      public static void tearDown()
83      {
84          if (tc != null)
85          {
86              tc.dispose();
87          }
88  
89      }
90  
91      @Test public void testAddRemove()
92      {
93  
94          assertFalse("TurbineURI should not have a pathInfo", turi.hasPathInfo());
95          assertFalse("TurbineURI must not have a queryData", turi.hasQueryData());
96          turi.addPathInfo("test", "x");
97          assertTrue("TurbineURI must have a pathInfo", turi.hasPathInfo());
98          assertFalse("TurbineURI must not have a queryData", turi.hasQueryData());
99          turi.removePathInfo("test");
100         assertFalse("TurbineURI must not have a pathInfo", turi.hasPathInfo());
101         assertFalse("TurbineURI must not have a queryData", turi.hasQueryData());
102 
103         assertFalse("TurbineURI should not have a queryData", turi.hasQueryData());
104         assertFalse("TurbineURI must not have a pathInfo", turi.hasPathInfo());
105         turi.addQueryData("test", "x");
106         assertTrue("TurbineURI must have a queryData", turi.hasQueryData());
107         assertFalse("TurbineURI must not have a pathInfo", turi.hasPathInfo());
108         turi.removeQueryData("test");
109         assertFalse("TurbineURI must not have a queryData", turi.hasQueryData());
110         assertFalse("TurbineURI must not have a pathInfo", turi.hasPathInfo());
111     }
112 
113     @Test public void testEmptyAndNullQueryData()
114     {
115         // Check empty String
116         assertEquals("/context/servlet/turbine", turi.getRelativeLink());
117         turi.addQueryData("test", "");
118         assertEquals("/context/servlet/turbine?test=", turi.getRelativeLink());
119         turi.removeQueryData("test");
120 
121         // Check null
122         assertEquals("/context/servlet/turbine", turi.getRelativeLink());
123         turi.addQueryData("test", null);
124         assertEquals("/context/servlet/turbine?test=null", turi
125                 .getRelativeLink());
126         turi.removeQueryData("test");
127         assertEquals("/context/servlet/turbine", turi.getRelativeLink());
128     }
129 
130     @Test public void testEmptyAndNullPathInfo()
131     {
132         // Check empty String
133         assertEquals("/context/servlet/turbine", turi.getRelativeLink());
134         turi.addPathInfo("test", "");
135         // Kind of susspect result - might result in "//" in the URL.
136         assertEquals("/context/servlet/turbine/test/", turi.getRelativeLink());
137         turi.removePathInfo("test");
138 
139         // Check null
140         assertEquals("/context/servlet/turbine", turi.getRelativeLink());
141         turi.addPathInfo("test", null);
142         assertEquals("/context/servlet/turbine/test/null", turi
143                 .getRelativeLink());
144         turi.removePathInfo("test");
145         assertEquals("/context/servlet/turbine", turi.getRelativeLink());
146     }
147 
148     @Test public void testAddEmptyParameterParser()
149     {
150         ParameterParser pp = new DefaultParameterParser();
151         turi.add(1, pp); // 1 = query data
152         assertEquals("/context/servlet/turbine", turi.getRelativeLink());
153     }
154 
155     @Test public void testAddParameterParser() throws InstantiationException
156     {
157         ParameterParser pp = parserService.getParser(DefaultParameterParser.class);
158         pp.add("test", "");
159         turi.add(1, pp); // 1 = query data
160         assertEquals("/context/servlet/turbine?test=", turi.getRelativeLink());
161         turi.removeQueryData("test");
162         assertEquals("/context/servlet/turbine", turi.getRelativeLink());
163 
164         parserService.putParser(pp);
165         pp = parserService.getParser(DefaultParameterParser.class);
166         pp.add("test", (String) null);
167         turi.add(1, pp); // 1 = query data
168         // Should make the following work so as to be consistent with directly
169         // added values.
170         // assertEquals("/context/servlet/turbine?test=null",
171         // turi.getRelativeLink());
172         turi.removeQueryData("test");
173         assertEquals("/context/servlet/turbine", turi.getRelativeLink());
174 
175         // TRB-8
176         //
177         // This is commented out for now as it results in a ClassCastException.
178         // The 2_3 branch parser changes need to be merged into the fulcrum
179         // code.
180         //
181         // pp = new DefaultParameterParser();
182         // DiskFileItemFactory factory = new DiskFileItemFactory(10240, null);
183         // FileItem test = factory.createItem("upload-field",
184         // "application/octet-stream", false, null);
185         // pp.append("upload-field", test);
186         // // The following causes a ClassCastException with or without the
187         // TRB-8 fix.
188         // turi.add(1, pp); // 1 = query data
189         // assertEquals("/context/servlet/turbine?upload-field=",
190         // turi.getRelativeLink());
191         // turi.removeQueryData("upload-field");
192         // assertEquals("/context/servlet/turbine", turi.getRelativeLink());
193     }
194 
195 }