/*
 * The Apache Software License, Version 1.1
 *
 * Copyright (c) 2002 The Apache Software Foundation.  All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution, if
 *    any, must include the following acknowlegement:
 *       "This product includes software developed by the
 *        Apache Software Foundation (http://www.apache.org/)."
 *    Alternately, this acknowlegement may appear in the software itself,
 *    if and wherever such third-party acknowlegements normally appear.
 *
 * 4. The names "The Jakarta Project", "Ant", and "Apache Software
 *    Foundation" must not be used to endorse or promote products derived
 *    from this software without prior written permission. For written
 *    permission, please contact apache@apache.org.
 *
 * 5. Products derived from this software may not be called "Apache"
 *    nor may "Apache" appear in their names without prior written
 *    permission of the Apache Group.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 */
package org.apache.turbine;

import java.util.Enumeration;
import java.util.Locale;
import java.io.IOException;
import java.io.BufferedReader;
import java.security.Principal;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpSession;
import javax.servlet.ServletInputStream;
import javax.servlet.RequestDispatcher;

import junit.framework.TestCase;

import org.apache.turbine.services.rundata.DefaultTurbineRunData;

/**
 * A basic testcase for DynamicURI
 *
 * @author <a href="mailto:sbailliez@apache.org">Stephane Bailliez</a>
 */
public class DynamicURITest extends TestCase
{
    private DynamicURI duri;

    public DynamicURITest(String s)
    {
        super(s);
    }

    protected void setUp() throws Exception
    {
        duri = new DynamicURI();
        RunData data = new DefaultTurbineRunData();
        data.setServerName("jakarta.apache.org");
        data.setScriptName("/webapp");
        data.setServerPort(80);
        data.setServerScheme("http");
        data.setRequest( new NullHttpServletRequest() );
        ParameterParser parser = new ParameterParser();
        data.setParameterParser( parser );
        duri.init( data );
        duri.setEncodeUrl(false);
    }

    public void testRemovePathInfo()
    {
        duri.addPathInfo("key1", "value1");
        duri.addPathInfo("key2", "value2");
        duri.addPathInfo("key1", "value3");
        // remove all key1
        duri.removePathInfo("key1");
        assertEquals(1, duri.pathInfo.size());
        // key2 does not exists...
        duri.removePathInfo("key2");
        assertEquals(0, duri.pathInfo.size());
    }

    public void testRemoveQueryData()
    {
        duri.addQueryData("key1", "value1");
        duri.addQueryData("key2", "value2");
        duri.addQueryData("key1", "value3");
        // remove all key1
        duri.removeQueryData("key1");
        assertEquals(1, duri.queryData.size());
        // key3 does not exists...
        duri.removeQueryData("key2");
        assertEquals(0, duri.queryData.size());
    }

    // this one might be useful for profiling...
    public void testPerformanceToString()
    {
        final int its = 100;
        for (int i = 0; i < its; i++)
        {
            duri.addPathInfo("key1", "value1");
            duri.addPathInfo("key2", "value2");
            duri.addPathInfo("key3", "value3");
            duri.addPathInfo("key4", "value4");
            duri.addPathInfo("key5", "value5");
            duri.addQueryData("key6", "value6");
            duri.toString();
        }
    }

    // this one might be useful for profiling...
    public void testPerformanceGetA()
    {
        final int its = 100;
        for (int i = 0; i < its; i++)
        {
            duri.addPathInfo("key1", "value1");
            duri.addPathInfo("key2", "value2");
            duri.addPathInfo("key3", "value3");
            duri.addPathInfo("key4", "value4");
            duri.addPathInfo("key5", "value5");
            duri.addQueryData("key6", "value6");
            duri.getA("this is a name to href");
        }
    }

    public void testURLEncoding(){
        duri.addPathInfo("a key", "a value");
        duri.addPathInfo("a", "b");
        assertEquals("http://jakarta.apache.org/webapp/a+key/a+value/a/b", duri.toString());
    }

}

class NullHttpServletRequest implements HttpServletRequest {
    public Object getAttribute(String s)
    {
        return null;
    }

    public Enumeration getAttributeNames()
    {
        return null;
    }

    public String getAuthType()
    {
        return null;
    }

    public String getCharacterEncoding()
    {
        return null;
    }

    public int getContentLength()
    {
        return 0;
    }

    public String getContentType()
    {
        return null;
    }

    public String getContextPath()
    {
        return null;
    }

    public Cookie[] getCookies()
    {
        return new Cookie[0];
    }

    public long getDateHeader(String s)
    {
        return 0;
    }

    public String getHeader(String s)
    {
        return null;
    }

    public Enumeration getHeaderNames()
    {
        return null;
    }

    public Enumeration getHeaders(String s)
    {
        return null;
    }

    public ServletInputStream getInputStream() throws IOException
    {
        return null;
    }

    public int getIntHeader(String s)
    {
        return 0;
    }

    public Locale getLocale()
    {
        return null;
    }

    public Enumeration getLocales()
    {
        return null;
    }

    public String getMethod()
    {
        return null;
    }

    public String getParameter(String s)
    {
        return null;
    }

    public Enumeration getParameterNames()
    {
        return null;
    }

    public String[] getParameterValues(String s)
    {
        return new String[0];
    }

    public String getPathInfo()
    {
        return null;
    }

    public String getPathTranslated()
    {
        return null;
    }

    public String getProtocol()
    {
        return null;
    }

    public String getQueryString()
    {
        return null;
    }

    public BufferedReader getReader() throws IOException
    {
        return null;
    }

    public String getRealPath(String s)
    {
        return null;
    }

    public String getRemoteAddr()
    {
        return null;
    }

    public String getRemoteHost()
    {
        return null;
    }

    public String getRemoteUser()
    {
        return null;
    }

    public RequestDispatcher getRequestDispatcher(String s)
    {
        return null;
    }

    public String getRequestedSessionId()
    {
        return null;
    }

    public String getRequestURI()
    {
        return null;
    }

    public String getScheme()
    {
        return null;
    }

    public String getServerName()
    {
        return null;
    }

    public int getServerPort()
    {
        return 0;
    }

    public String getServletPath()
    {
        return null;
    }

    public HttpSession getSession()
    {
        return null;
    }

    public HttpSession getSession(boolean b)
    {
        return null;
    }

    public Principal getUserPrincipal()
    {
        return null;
    }

    public boolean isRequestedSessionIdFromCookie()
    {
        return false;
    }

    public boolean isRequestedSessionIdFromURL()
    {
        return false;
    }

    public boolean isRequestedSessionIdFromUrl()
    {
        return false;
    }

    public boolean isRequestedSessionIdValid()
    {
        return false;
    }

    public boolean isSecure()
    {
        return false;
    }

    public boolean isUserInRole(String s)
    {
        return false;
    }

    public void removeAttribute(String s)
    {
    }

    public void setAttribute(String s, Object o)
    {
    }
}
