    001package 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
    022import static org.junit.Assert.assertEquals;
    023import static org.junit.Assert.assertTrue;
    024import static org.junit.Assert.assertFalse;
    025
    026import org.apache.fulcrum.parser.DefaultParameterParser;
    027import org.apache.fulcrum.parser.ParameterParser;
    028import org.apache.fulcrum.parser.ParserService;
    029import org.apache.turbine.services.TurbineServices;
    030import org.apache.turbine.test.BaseTestCase;
    031import org.apache.turbine.util.ServerData;
    032import org.apache.turbine.util.TurbineConfig;
    033import org.junit.AfterClass;
    034import org.junit.Before;
    035import org.junit.BeforeClass;
    036import org.junit.Test;
    037
    038/**
    039 * Testing of the TurbineURI class
    040 *
    041 * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
    042 * @author <a href="mailto:seade@backstagetech.com.au">Scott Eade</a>
    043 * @version $Id: TurbineURITest.java 1616993 2014-08-09 17:03:07Z tv $
    044 */
    045public class TurbineURITest extends BaseTestCase
    046{
    047    private TurbineURI turi;
    048
    049    private ParserService parserService;
    050
    051    private static TurbineConfig tc = null;
    052
    053
    054    @BeforeClass
    055    public static void init() {
    056        tc =  new TurbineConfig(
    057                            ".",
    058                            "/conf/test/CompleteTurbineResources.properties");
    059        tc.initialize();
    060    }
    061    /**
    062     * Performs any initialization that must happen before each test is run.
    063     */
    064
    065    @Before
    066    public void setup()
    067    {
    068        // Setup configuration
    069
    070        ServerData sd = new ServerData("www.testserver.com",
    071                URIConstants.HTTP_PORT, URIConstants.HTTP, "/servlet/turbine",
    072                "/context");
    073        turi = new TurbineURI(sd);
    074
    075        parserService = (ParserService)TurbineServices.getInstance().getService(ParserService.ROLE);
    076    }
    077
    078    /**
    079     * Clean up after each test is run.
    080     */
    081    @AfterClass
    082    public static void tearDown()
    083    {
    084        if (tc != null)
    085        {
    086            tc.dispose();
    087        }
    088
    089    }
    090
    091    @Test public void testAddRemove()
    092    {
    093
    094        assertFalse("TurbineURI should not have a pathInfo", turi.hasPathInfo());
    095        assertFalse("TurbineURI must not have a queryData", turi.hasQueryData());
    096        turi.addPathInfo("test", "x");
    097        assertTrue("TurbineURI must have a pathInfo", turi.hasPathInfo());
    098        assertFalse("TurbineURI must not have a queryData", turi.hasQueryData());
    099        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}