You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@beehive.apache.org by ri...@apache.org on 2004/12/12 22:20:22 UTC

svn commit: r111667 - in incubator/beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/core: . urls

Author: rich
Date: Sun Dec 12 13:20:21 2004
New Revision: 111667

URL: http://svn.apache.org/viewcvs?view=rev&rev=111667
Log:
Missed this added test in my last checkin.


Added:
   incubator/beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/core/
   incubator/beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/core/urls/
   incubator/beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/core/urls/MutableURITest.java   (contents, props changed)

Added: incubator/beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/core/urls/MutableURITest.java
Url: http://svn.apache.org/viewcvs/incubator/beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/core/urls/MutableURITest.java?view=auto&rev=111667
==============================================================================
--- (empty file)
+++ incubator/beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/core/urls/MutableURITest.java	Sun Dec 12 13:20:21 2004
@@ -0,0 +1,506 @@
+/*
+ * Copyright 2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * $Header:$
+ */
+package org.apache.beehive.netui.test.core.urls;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import org.apache.beehive.netui.core.urls.MutableURI;
+
+/**
+ * MutableURI JUnit TestCase.
+ */
+public class MutableURITest extends TestCase
+{
+    private static final String DEFAULT_ENCODING = "UTF-8";
+
+    //
+    // Strings for tests. Elements are...
+    // encoding, scheme, user info, host, port, path, query, fragment, result
+    //
+    private String[][] _tests =
+    {
+        // test the ftp scheme + host + path
+        { "ftp", null, "ftp.is.co.za", null,
+          "/rfc/rfc1808.txt", null, null,
+          "ftp://ftp.is.co.za/rfc/rfc1808.txt" },
+
+        // include user info
+        { "telnet", "user:pword", "bogus-machine.com", null,
+          "", null, null,
+          "telnet://user:pword@bogus-machine.com" },
+
+        // test for IPv6 addresses
+        { "http", null, "[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]", "80",
+          "/index.html", null, null,
+          "http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/index.html" },
+
+        // test for relative path + query
+        { null, null, null, null,
+          "../test/start.jsp", "skip=true", null,
+          "../test/start.jsp?skip=true" },
+
+        // test for path + query
+        { null, null, null, null,
+          "/portal/MockPortal.jsp",
+          "smokeTestAaltAction=goNested&smokeTestA_submit=true", null,
+          "/portal/MockPortal.jsp?smokeTestAaltAction=goNested&smokeTestA_submit=true" },
+
+        // test for scheme + host + path
+        { "https", null, "localhost", null,
+          "/tomcat-docs/jasper/docs/api/index.html", null, null,
+          "https://localhost/tomcat-docs/jasper/docs/api/index.html" },
+
+        // test for fragment only
+        { null, null, null, null,
+          "", null, "myFragment", "#myFragment" },
+
+        // test for scheme + host + path + fragment
+        { "http", null, "localhost", "8080", "/",
+          null, "myFragment", "http://localhost:8080/#myFragment" },
+
+        // test for empty path with fragment
+        { "http", null, "localhost", "8080", "",
+          null, "myFragment", "http://localhost:8080#myFragment" },
+
+        // test for query parameter names without values and multiple
+        // instances of the same parameter name
+        { "http", null, "localhost", "8080",
+          "/test-servlet/TestServlet", "param1&param1=&param2", null,
+          "http://localhost:8080/test-servlet/TestServlet?param1&param1=&param2" },
+
+        // test URI with a session ID
+        { "http", null, "localhost", "8080",
+          "/Web/d/newAction1.do;jsessionid=F0C07D10C0E8CD22618ED1178F0F62C8",
+          null, null,
+          "http://localhost:8080/Web/d/newAction1.do;jsessionid=F0C07D10C0E8CD22618ED1178F0F62C8" },
+
+        // test for query with escaped characters
+        { "http", null, "localhost", "8080",
+          "/test-servlet/TestServlet",
+          "textId=%C4%F3%BD%D0%BA%D1%A4%DF&spacesAmpQuotes=%22text%20space1%20%26%20space2%22&tags=%3CsomeTag%3E&percent=100%25tilda=%7Etilda", null,
+          "http://localhost:8080/test-servlet/TestServlet?textId=%C4%F3%BD%D0%BA%D1%A4%DF&spacesAmpQuotes=%22text%20space1%20%26%20space2%22&tags=%3CsomeTag%3E&percent=100%25tilda=%7Etilda" }
+    };
+
+    public MutableURITest( String name )
+    {
+        super( name );
+    }
+
+    public static void main( String[] args )
+    {
+        junit.textui.TestRunner.run( suite() );
+    }
+
+    public static Test suite()
+    {
+        return new TestSuite( MutableURITest.class );
+    }
+
+    protected void setUp()
+    {
+    }
+
+    protected void tearDown()
+    {
+    }
+
+    public void testConstructors()
+    {
+        for (int i = 0; i < _tests.length; i++ )
+        {
+            String scheme = _tests[i][0];
+            String userInfo = _tests[i][1];
+            String host = _tests[i][2];
+            int port = MutableURI.UNDEFINED_PORT;
+            String integer = _tests[i][3];
+            if ( integer != null && integer.trim().length() > 0 )
+            {
+                port = Integer.parseInt( integer );
+            }
+            String path = _tests[i][4];
+            String query = _tests[i][5];
+            String fragment = _tests[i][6];
+            String uriString = _tests[i][7];
+
+            try
+            {
+                MutableURI uri = new MutableURI( scheme, userInfo, host, port,
+                                                 path, query, fragment );
+                assertEquals( uriString, uri.toString() );
+                MutableURI other = new MutableURI( uriString );
+                assertEquals( uri, other );
+                other = new MutableURI( new URI( uriString ) );
+                assertEquals( uri, other );
+            }
+            catch ( URISyntaxException e )
+            {
+                fail( "Test failed for URI, \"" + uriString + "\", with a URISyntaxException: " + e.getMessage() );
+            }
+        }
+    }
+
+    public void testSetters()
+    {
+        for (int i = 0; i < _tests.length; i++ )
+        {
+            String scheme = _tests[i][0];
+            String userInfo = _tests[i][1];
+            String host = _tests[i][2];
+            int port = MutableURI.UNDEFINED_PORT;
+            String integer = _tests[i][3];
+            if ( integer != null && integer.trim().length() > 0 )
+            {
+                port = Integer.parseInt( integer );
+            }
+            String path = _tests[i][4];
+            String query = _tests[i][5];
+            String fragment = _tests[i][6];
+            String uriString = _tests[i][7];
+
+            try
+            {
+                MutableURI uri = new MutableURI();
+                uri.setScheme( scheme );
+                uri.setHost( host );
+                uri.setUserInfo( userInfo );
+                uri.setPort( port );
+                uri.setPath( path );
+                uri.setQuery( query );
+                uri.setFragment( fragment );
+                assertEquals( uriString, uri.toString() );
+
+                MutableURI other = new MutableURI( uriString );
+                assertEquals( uri, other );
+            }
+            catch ( URISyntaxException e )
+            {
+                fail( "Test failed for URI, \"" + uriString + "\", with a URISyntaxException: " + e.getMessage() );
+            }
+        }
+    }
+
+    public void testGetters()
+    {
+        for (int i = 0; i < _tests.length; i++ )
+        {
+            String scheme = _tests[i][0];
+            String userInfo = _tests[i][1];
+            String host = _tests[i][2];
+            int port = MutableURI.UNDEFINED_PORT;
+            String integer = _tests[i][3];
+            if ( integer != null && integer.trim().length() > 0 )
+            {
+                port = Integer.parseInt( integer );
+            }
+            String path = _tests[i][4];
+            String query = _tests[i][5];
+            String fragment = _tests[i][6];
+            String uriString = _tests[i][7];
+
+            try
+            {
+                MutableURI uri = new MutableURI( uriString );
+                assertEquals( uriString, uri.toString() );
+                assertEquals( uri.getScheme(), scheme );
+                assertEquals( uri.getHost(), host );
+                assertEquals( uri.getUserInfo(), userInfo );
+                assertEquals( uri.getPort(), port );
+                assertEquals( uri.getPath(), path );
+                assertEquals( uri.getQuery( "&" ), query );
+                assertEquals( uri.getFragment(), fragment );
+            }
+            catch ( URISyntaxException e )
+            {
+                fail( "Test failed for URI, \"" + uriString + "\", with a URISyntaxException: " + e.getMessage() );
+            }
+        }
+    }
+
+    public void testIsAbsolute()
+    {
+        try
+        {
+            MutableURI uri = new MutableURI( "http://localhost/test?param1=true" );
+            assertTrue( uri.isAbsolute() );
+            uri = new MutableURI( "/test?param1=true" );
+            assertFalse( uri.isAbsolute() );
+        }
+        catch ( URISyntaxException e )
+        {
+            fail( "Test failed with a URISyntaxException: " + e.getMessage() );
+        }
+    }
+
+    public void testQueryParameters()
+    {
+        MutableURI origURI = new MutableURI();
+        origURI.setEncoding( DEFAULT_ENCODING );
+        MutableURI uri = new MutableURI();
+        uri.setEncoding( DEFAULT_ENCODING );
+        String path = "/path";
+        String name = "paramName";
+        String value = "paramValue";
+        String query = name + "=" + value;
+        origURI.setPath( path );
+        uri.setPath( path );
+        origURI.setQuery( query );
+        uri.setQuery( query );
+        assertEquals( uri.getParameter( name ), value );
+
+        // now use addParameter() to set the query
+        uri.setQuery( null );
+        uri.addParameter( name, value, true );
+        assertEquals( uri, origURI );
+        assertEquals( uri.getParameter( name ), value );
+        assertEquals( uri.getParameters( name ).size(), 1 );
+        assertEquals( uri.getParameters().size(), 1 );
+
+        // remove the parameter
+        uri.removeParameter( name );
+        assertFalse( uri.equals( origURI ) );
+        assertEquals( uri.getParameters( name ).size(), 0 );
+        assertEquals( uri.getParameters().size(), 0 );
+
+        // add a parameter with a null value (just a name)
+        uri.addParameter( name, null, true );
+        List< String > values = uri.getParameters( name );
+        assertEquals( values.size(), 1 );
+        assertEquals( values.get( 0 ), null );
+
+        // ask for a parameter that does not exist
+        values = uri.getParameters( "bogus name" );
+        assertEquals( values.size(), 0 );
+
+        // add a parameter values with the same name
+        String[] initValues = { "", null, "value1", "value2" };
+        uri.setQuery( null );
+        int i = 0;
+
+        for ( i = 0; i < initValues.length; i++ )
+        {
+           uri.addParameter( name, initValues[i], true );
+        }
+
+        assertEquals( uri.getParameter( name ), initValues[0] );
+        values = uri.getParameters( name );
+        assertEquals( values.size(), initValues.length );
+        assertEquals( uri.getParameters().size(), 1 );
+        i = 0;
+
+        for ( String v : values )
+        {
+            assertEquals( v, initValues[i++] );
+        }
+
+        // and add a bunch of paramters
+        String[][] initParams =
+        {
+            { "param1", "value1" }, { "param2", "value2" },
+            { "param3", "value3" }, { "param4", "value4" },
+            { "param5", "value5" }, { "param6", "value6" },
+            { "param7", "value7" }, { "param8", "value8" },
+            { "param9", "value9" }, { "param10", "value10" },
+            { "param11", "value11" }, { "param12", "value12" }
+        };
+
+        uri.setQuery( null );
+        HashMap map = new HashMap();
+
+        for ( i = 0; i < initParams.length; i++ )
+        {
+            map.put( initParams[i][0], initParams[i][1] );
+        }
+
+        uri.addParameters( map, true );
+        assertEquals( uri.getParameters( initParams[0][0] ).size(), 1 );
+        assertEquals( uri.getParameters().size(), initParams.length );
+        Map< String, List< String > > params = uri.getParameters();
+        i = 0;
+
+        for ( String n : params.keySet() )
+        {
+            for ( String v : params.get( n ) )
+            {
+                assertTrue( map.containsKey( n ) );
+                assertEquals( v, map.get( n ) );
+            }
+            i++;
+        }
+    }
+
+    public void testEncoding()
+    {
+        String path = "/path";
+
+        MutableURI utf8EncodedUri = new MutableURI();
+        utf8EncodedUri.setEncoding( DEFAULT_ENCODING );
+        utf8EncodedUri.setPath( path );
+
+        MutableURI eucJPEncodedUri = new MutableURI();
+        eucJPEncodedUri.setEncoding( "EUC_JP" );
+        eucJPEncodedUri.setPath( path );
+
+        // test encoding of URI reserved characters, etc.
+        String reserved = "semi;slash/quest?colon:at@and&equ=plus+doller$comm,";
+        String mark = "hyph-un_per.ex!tilda~ast*ap'lp(rp)";
+        utf8EncodedUri.addParameter( reserved, mark, false );
+        eucJPEncodedUri.addParameter( reserved, mark, false );
+        assertEquals( utf8EncodedUri.toString(), eucJPEncodedUri.toString() );
+        assertEquals( utf8EncodedUri.getQuery( "&" ),
+                      "semi%3Bslash%2Fquest%3Fcolon%3Aat%40and%26equ%3Dplus%2Bdoller%24comm%2C"
+                      + "=" + "hyph-un_per.ex%21tilda%7East*ap%27lp%28rp%29" );
+
+        utf8EncodedUri.setQuery( null );
+        eucJPEncodedUri.setQuery( null );
+        String unwise = "lcb{rcb}bar|bs\\ctr^lb[rb]lq`";
+        String delims = "lt<gt>lb#perc%\"quotes\"";
+        String excluded = "space " + delims;
+        utf8EncodedUri.addParameter( unwise, excluded, false );
+        eucJPEncodedUri.addParameter( unwise, excluded, false );
+        assertEquals( utf8EncodedUri.toString(), eucJPEncodedUri.toString() );
+        assertEquals( utf8EncodedUri.getQuery( "&" ),
+                      "lcb%7Brcb%7Dbar%7Cbs%5Cctr%5Elb%5Brb%5Dlq%60"
+                      + "=" + "space+lt%3Cgt%3Elb%23perc%25%22quotes%22" );
+
+        // test encoding of mu;tibyte characters in a URI.
+        utf8EncodedUri.setQuery( null );
+        eucJPEncodedUri.setQuery( null );
+        String name = "name";
+        String japaneseUnicode = "\u63d0\u51fa\u6e08\u307f";
+        utf8EncodedUri.addParameter( name, japaneseUnicode, false );
+        eucJPEncodedUri.addParameter( name, japaneseUnicode, false );
+        assertFalse( utf8EncodedUri.toString().equals( eucJPEncodedUri.toString() ) );
+        assertEquals( utf8EncodedUri.getQuery( "&" ),
+                      name + "=" + "%E6%8F%90%E5%87%BA%E6%B8%88%E3%81%BF" );
+        assertEquals( eucJPEncodedUri.getQuery( "&" ),
+                      name + "=" + "%C4%F3%BD%D0%BA%D1%A4%DF" );
+    }
+
+    public void testToXMLString()
+    {
+        MutableURI uri = new MutableURI();
+        uri.setScheme( "https" );
+        uri.setHost( "localhost" );
+        uri.setPort( 443 );
+        uri.setPath( "/test" );
+        uri.setQuery( "param1&param2&param3=&param3=true&param4=true" );
+        String xmlString = "https://localhost:443/test?param1&amp;param2&amp;param3=&amp;param3=true&amp;param4=true";
+        assertEquals( uri.toXMLString(), xmlString );
+    }
+
+    public void testEquals()
+    {
+        //
+        // Note that the current implementation MutableURI, with query
+        // parameters stored in a LinkedHashMap of Lists, has the
+        // potential to change order of params in the query... such
+        // as this example. So the resulting string has params in a
+        // different order. However equality should still hold true.
+        //
+        String uriString = "https://localhost:443/test?param1&param1=&param1=true&param2&param3=true";
+
+        try
+        {
+            MutableURI uriA = new MutableURI();
+            uriA.setScheme( "https" );
+            uriA.setHost( "localhost" );
+            uriA.setPort( 443 );
+            uriA.setPath( "/test" );
+            uriA.setQuery( "param1&param2&param1=&param3=true&param1=true" );
+
+            MutableURI uriB = new MutableURI( uriString );
+            assertEquals( uriA.toString(), uriString );
+            assertEquals( uriA.toString(), uriB.toString() );
+
+            MutableURI uriC = new MutableURI( new URI( uriString ) );
+
+            // Test all properties of equality...
+            // 1. hashCodes are equal
+            assertEquals( uriA.hashCode(), uriA.hashCode() );
+
+            // 2. reflexive
+            assertTrue( uriA.equals( uriA ) );
+
+            // 3. symmetric
+            assertTrue( uriA.equals( uriB ) );
+            assertTrue( uriB.equals( uriA ) );
+
+            // 4. transitive
+            assertTrue( uriA.equals( uriB ) );
+            assertTrue( uriB.equals( uriC ) );
+            assertTrue( uriC.equals( uriA ) );
+
+            // 5. consistent
+            uriC.setPath( "/differentPath" );
+            assertFalse( uriC.equals( uriA ) );
+
+            // 6. x.equals(null) should return false.
+            assertFalse( uriA.equals( null ) );
+
+            // and subclasses return false as not to break the symmetric property
+            class AnotherMutableURI extends MutableURI
+            {
+                boolean reference = true;
+
+                AnotherMutableURI( URI uri ) { super( uri ); }
+
+                void setReference( boolean ref ) { reference = ref; }
+
+                boolean isReference() { return reference; }
+            }
+
+            AnotherMutableURI another = new AnotherMutableURI( new URI( uriString ) );
+            assertFalse( uriA.equals( another ) );
+
+        }
+        catch ( URISyntaxException e )
+        {
+            fail( "Test failed for URI, \"" + uriString + "\", with a URISyntaxException: " + e.getMessage() );
+        }
+    }
+
+    // basic diagnostic utility when writing tests
+    private void dumpURI( MutableURI uri )
+    {
+        if ( uri == null )
+        {
+            System.out.println( "uri == null" );
+        }
+        else
+        {
+            System.out.println( "string:     " + uri.toString() );
+            System.out.println( "XML string: " + uri.toXMLString() );
+            System.out.println( "scheme:     " + uri.getScheme() );
+            System.out.println( "user info:  " + uri.getUserInfo() );
+            System.out.println( "host:       " + uri.getHost() );
+            System.out.println( "port:       " + uri.getPort() );
+            System.out.println( "path:       " + uri.getPath() );
+            System.out.println( "query:      " + uri.getQuery( "&" ) );
+            System.out.println( "fragment:   " + uri.getFragment() );
+            System.out.println( "encoding:   " + uri.getEncoding() );
+        }
+    }
+}