    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 org.apache.fulcrum.parser.DefaultParameterParser;
    23  import org.apache.fulcrum.parser.ParameterParser;
    24  import org.apache.fulcrum.parser.ParserService;
    25  import org.apache.turbine.services.TurbineServices;
    26  import org.apache.turbine.test.BaseTestCase;
    27  import org.apache.turbine.util.ServerData;
    28  import org.apache.turbine.util.TurbineConfig;
    29
    30  /**
    31   * Testing of the TurbineURI class
    32   *
    33   * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
    34   * @author <a href="mailto:seade@backstagetech.com.au">Scott Eade</a>
    35   * @version $Id: TurbineURITest.java 717931 2008-11-15 21:40:43Z tv $
    36   */
    37  public class TurbineURITest extends BaseTestCase
    38  {
    39      private TurbineURI turi;
    40
    41      private ParserService parserService;
    42
    43      private static TurbineConfig tc = null;
    44
    45      /**
    46       * Constructor for test.
    47       *
    48       * @param testName
    49       *            name of the test being executed
    50       */
    51      public TurbineURITest(String testName) throws Exception
    52      {
    53          super(testName);
    54
    55          // Setup configuration
    56          tc =
    57              new TurbineConfig(
    58                  ".",
    59                  "/conf/test/CompleteTurbineResources.properties");
    60          tc.initialize();
    61      }
    62
    63      /**
    64       * Performs any initialization that must happen before each test is run.
    65       */
    66      protected void setUp()
    67      {
    68          ServerData sd = new ServerData("www.testserver.com",
    69                  URIConstants.HTTP_PORT, URIConstants.HTTP, "/servlet/turbine",
    70                  "/context");
    71          turi = new TurbineURI(sd);
    72
    73          parserService = (ParserService)TurbineServices.getInstance().getService(ParserService.ROLE);
    74      }
    75
    76      /**
    77       * Clean up after each test is run.
    78       */
    79      protected void tearDown()
    80      {
    81          if (tc != null)
    82          {
    83              tc.dispose();
    84          }
    85
    86          turi = null;
    87      }
    88
    89      public void testAddRemove()
    90      {
    91          assertEquals("TurbineURI should not have a pathInfo", false, turi
    92                  .hasPathInfo());
    93          assertEquals("TurbineURI must not have a queryData", false, turi
    94                  .hasQueryData());
    95          turi.addPathInfo("test", "x");
    96          assertEquals("TurbineURI must have a pathInfo", true, turi
    97                  .hasPathInfo());
    98          assertEquals("TurbineURI must not have a queryData", false, turi
    99                  .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 }