You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@beehive.apache.org by cr...@apache.org on 2005/12/06 19:35:40 UTC

svn commit: r354491 - in /beehive/trunk/netui: src/core/org/apache/beehive/netui/core/urls/ test/src/junitTests/org/apache/beehive/netui/test/core/urls/ test/webapps/drt/testRecorder/tests/ test/webapps/drt/web/tags/anchor/

Author: crogers
Date: Tue Dec  6 10:35:21 2005
New Revision: 354491

URL: http://svn.apache.org/viewcvs?rev=354491&view=rev
Log:
Fix for http://issues.apache.org/jira/browse/BEEHIVE-1011 - netui anchor tag does not properly render a 'mailto' href.

The changes include modifications to MutableURI to support opaque URLs, corresponding updates to FreezableMutableURI, additions to the junit tests, and an update of the Anchor test.

tests: drt, bvt in netui (WinXP)
BB: self


Modified:
    beehive/trunk/netui/src/core/org/apache/beehive/netui/core/urls/FreezableMutableURI.java
    beehive/trunk/netui/src/core/org/apache/beehive/netui/core/urls/MutableURI.java
    beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/core/urls/FreezableMutableURITest.java
    beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/core/urls/MutableURITest.java
    beehive/trunk/netui/test/webapps/drt/testRecorder/tests/Anchor.xml
    beehive/trunk/netui/test/webapps/drt/web/tags/anchor/Begin.jsp

Modified: beehive/trunk/netui/src/core/org/apache/beehive/netui/core/urls/FreezableMutableURI.java
URL: http://svn.apache.org/viewcvs/beehive/trunk/netui/src/core/org/apache/beehive/netui/core/urls/FreezableMutableURI.java?rev=354491&r1=354490&r2=354491&view=diff
==============================================================================
--- beehive/trunk/netui/src/core/org/apache/beehive/netui/core/urls/FreezableMutableURI.java (original)
+++ beehive/trunk/netui/src/core/org/apache/beehive/netui/core/urls/FreezableMutableURI.java Tue Dec  6 10:35:21 2005
@@ -291,6 +291,24 @@
         super.setFragment( fragment );
     }
 
+    /**
+     * Sets the URI to be opaque using the given scheme and
+     * schemeSpecificPart.
+     * <p> From {@link URI}: &quot;A URI is opaque if, and only
+     * if, it is absolute and its scheme-specific part does not begin with
+     * a slash character ('/'). An opaque URI has a scheme, a
+     * scheme-specific part, and possibly a fragment; all other components
+     * are undefined.&quot; </p>
+     *
+     * @param scheme the scheme component of this URI
+     * @param schemeSpecificPart the scheme-specific part of this URI
+     */
+    public void setOpaque( String scheme, String schemeSpecificPart )
+    {
+        testFrozen();
+        super.setOpaque( scheme, schemeSpecificPart );
+    }
+
     public boolean equals( Object o )
     {
         if ( this == o )

Modified: beehive/trunk/netui/src/core/org/apache/beehive/netui/core/urls/MutableURI.java
URL: http://svn.apache.org/viewcvs/beehive/trunk/netui/src/core/org/apache/beehive/netui/core/urls/MutableURI.java?rev=354491&r1=354490&r2=354491&view=diff
==============================================================================
--- beehive/trunk/netui/src/core/org/apache/beehive/netui/core/urls/MutableURI.java (original)
+++ beehive/trunk/netui/src/core/org/apache/beehive/netui/core/urls/MutableURI.java Tue Dec  6 10:35:21 2005
@@ -54,10 +54,6 @@
  *
  * <p> There is a static convenience method in this class so callers can
  * easily encode unencoded components before setting it in this object. </p>
- *
- * TODO... We need to implement some conditions for opaque URIs like mailto, etc.
- * to determine what to do about values of path when (path == null) => opaque and
- * URI.getPath() would return "" for non-opaue URIs.
  */
 public class MutableURI
 {
@@ -74,6 +70,15 @@
     private String _scheme;
 
     /**
+     * Flag indicating whether this is for an opaque URI or not.
+     * An "opaque URI is an absolute URI whose scheme-specific part does not
+     * begin with a slash character". This class will allow a caller to set
+     * the URI to opaque and use a specific scheme-specific part.
+     */
+    private boolean _opaque = false;
+    private String _schemeSpecificPart;
+
+    /**
      * User information "may consist of a user name and, optionally,
      * scheme-specific information about how to gain authorization to
      * access the server.
@@ -260,13 +265,38 @@
      */
     public void setURI( URI uri )
     {
-        setScheme( uri.getScheme() );
-        setUserInfo( uri.getRawUserInfo() );
-        setHost( uri.getHost() );
-        setPort( uri.getPort() );
-        setPath( uri.getRawPath() );
-        setQuery( uri.getRawQuery() );
-        setFragment( uri.getRawFragment() );
+
+        if ( uri == null )
+        {
+            setScheme( null );
+            setUserInfo( null );
+            setHost( null );
+            setPort( UNDEFINED_PORT );
+            setPath( null );
+            setQuery( null );
+            setFragment( null );
+        }
+        else if ( uri.isOpaque() )
+        {
+            setUserInfo( null );
+            setHost( null );
+            setPort( UNDEFINED_PORT );
+            setPath( null );
+            setQuery( null );
+            setOpaque( uri.getScheme(), uri.getSchemeSpecificPart() );
+            setFragment( uri.getRawFragment() );
+        }
+        else
+        {
+            setSchemeSpecificPart( null );
+            setScheme( uri.getScheme() );
+            setUserInfo( uri.getRawUserInfo() );
+            setHost( uri.getHost() );
+            setPort( uri.getPort() );
+            setPath( uri.getRawPath() );
+            setQuery( uri.getRawQuery() );
+            setFragment( uri.getRawFragment() );
+        }
     }
 
     /**
@@ -366,6 +396,9 @@
             {
                 _host = host;
             }
+
+            _opaque = false;
+            setSchemeSpecificPart( null );
         }
 
         if ( _host == null )
@@ -433,6 +466,8 @@
         else
         {
             _path = path;
+            _opaque = false;
+            setSchemeSpecificPart( null );
         }
     }
 
@@ -552,7 +587,10 @@
      */
     public void addParameter( String name, String value, boolean encoded )
     {
-        if ( name == null ) throw new IllegalArgumentException( "A parameter name may not be null." );
+        if ( name == null )
+        {
+            throw new IllegalArgumentException( "A parameter name may not be null." );
+        }
 
         if ( !encoded )
         {
@@ -560,7 +598,12 @@
             value = encode( value );
         }
 
-        if ( _params == null ) _params = new LinkedHashMap/*< String, List< String > >*/();
+        if ( _params == null )
+        {
+            _params = new LinkedHashMap/*< String, List< String > >*/();
+            _opaque = false;
+            setSchemeSpecificPart( null );
+        }
         List/*< String >*/ values = ( List ) _params.get( name );
         
         if ( values == null )
@@ -600,6 +643,8 @@
         if ( _params == null )
         {
             _params = new LinkedHashMap/*< String, List< String > >*/();
+            _opaque = false;
+            setSchemeSpecificPart( null );
         }
 
         Iterator keys = newParams.keySet().iterator();
@@ -774,6 +819,71 @@
     }
 
     /**
+     * Tells whether or not this URI is opaque.
+     *
+     * @return <tt>true</tt> if this URI was explicitly set as opaque
+     */
+    public boolean isOpaque()
+    {
+        return _opaque;
+    }
+
+    /**
+     * Sets the URI to be opaque using the given scheme and
+     * schemeSpecificPart.
+     * <p> From {@link URI}: &quot;A URI is opaque if, and only
+     * if, it is absolute and its scheme-specific part does not begin with
+     * a slash character ('/'). An opaque URI has a scheme, a
+     * scheme-specific part, and possibly a fragment; all other components
+     * are undefined.&quot; </p>
+     *
+     * @param scheme the scheme component of this URI
+     * @param schemeSpecificPart the scheme-specific part of this URI
+     */
+    public void setOpaque( String scheme, String schemeSpecificPart )
+    {
+        if ( scheme == null || scheme.length() == 0
+                || schemeSpecificPart == null
+                || schemeSpecificPart.length() == 0
+                || schemeSpecificPart.indexOf( '/' ) > 0 )
+        {
+            throw new IllegalArgumentException( "Not a proper opaque URI." );
+        }
+
+        _opaque = true;
+        setScheme( scheme );
+        setSchemeSpecificPart( schemeSpecificPart );
+        setUserInfo( null );
+        setHost( null );
+        setPort( UNDEFINED_PORT );
+        setPath( null );
+        setQuery( null );
+    }
+
+    /**
+     * Returns the scheme-specific part of this URI if it is opaque.
+     * Otherwise, this method returns null.
+     * @return the scheme-specific part of this URI if this URI was
+     *         explicitly set as opaque. Otherwise, return null.
+     */
+    public String getSchemeSpecificPart()
+    {
+        if ( isOpaque() )
+            return _schemeSpecificPart;
+
+        return null;
+    }
+
+    /**
+     * Set the scheme-specific part of this (opaque) URI
+     * @param schemeSpecificPart the scheme-specific part of this URI
+     */
+    protected void setSchemeSpecificPart( String schemeSpecificPart )
+    {
+        _schemeSpecificPart = schemeSpecificPart;
+    }
+
+    /**
      * Returns a string form of this URI. The {@link URIContext}
      * encapsulates the data needed to write out the string form.
      *
@@ -791,6 +901,11 @@
         if ( getScheme() != null )
         {
             buf.append( getScheme() ).append( ':' );
+
+            if ( isOpaque() )
+            {
+                buf.append( getSchemeSpecificPart() );
+            }
         }
 
         // Append the user info, host and, port
@@ -839,9 +954,7 @@
             buf.append( '#' ).append( getFragment() );
         }
 
-        String url = buf.toString();
-
-        return url;
+        return buf.toString();
     }
 
     /**
@@ -932,6 +1045,17 @@
         }
 
         MutableURI testURI = ( MutableURI ) object;
+
+        if ( isOpaque() )
+        {
+            if ( ( _scheme == testURI.getScheme() || ( _scheme != null && _scheme.equalsIgnoreCase( testURI.getScheme() ) ) ) &&
+                    ( _schemeSpecificPart == testURI.getSchemeSpecificPart()
+                            || ( _schemeSpecificPart != null && _schemeSpecificPart.equals( testURI.getSchemeSpecificPart() ) ) ) &&
+                    ( _fragment == testURI.getFragment() || ( _fragment != null && _fragment.equals( testURI.getFragment() ) ) ) )
+            {
+                return true;
+            }
+        }
 
         if ( ( _scheme == testURI.getScheme() || ( _scheme != null && _scheme.equalsIgnoreCase( testURI.getScheme() ) ) ) &&
                 ( _userInfo == testURI.getUserInfo() || ( _userInfo != null && _userInfo.equals( testURI.getUserInfo() ) ) ) &&

Modified: beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/core/urls/FreezableMutableURITest.java
URL: http://svn.apache.org/viewcvs/beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/core/urls/FreezableMutableURITest.java?rev=354491&r1=354490&r2=354491&view=diff
==============================================================================
--- beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/core/urls/FreezableMutableURITest.java (original)
+++ beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/core/urls/FreezableMutableURITest.java Tue Dec  6 10:35:21 2005
@@ -94,6 +94,7 @@
     public void testConstructors()
     {
         URIContext uriContext = MutableURI.getDefaultContext();
+        String uriString = null;
 
         for ( int i = 0; i < _tests.length; i++ )
         {
@@ -109,7 +110,7 @@
             String path = _tests[i][4];
             String query = _tests[i][5];
             String fragment = _tests[i][6];
-            String uriString = _tests[i][7];
+            uriString = _tests[i][7];
 
             try
             {
@@ -126,6 +127,20 @@
                 fail( "Test failed for URI, \"" + uriString + "\", with a URISyntaxException: " + e.getMessage() );
             }
         }
+
+        // opaque URI
+        try
+        {
+            uriString = "news:comp.lang.java";
+            FreezableMutableURI opaqueURI = new FreezableMutableURI( uriString, true );
+            assertEquals( uriString, opaqueURI.getURIString( uriContext ) );
+            assertTrue( opaqueURI.isAbsolute() );
+            assertTrue( opaqueURI.isOpaque() );
+        }
+        catch ( URISyntaxException e )
+        {
+            fail( "Test failed for URI, \"" + uriString + "\", with a URISyntaxException: " + e.getMessage() );
+        }
     }
 
     public void testSetters()
@@ -300,11 +315,23 @@
             threw = true;
         }
         assertTrue( threw );
+
+        threw = false;
+        try
+        {
+            uri.setOpaque( "news", "comp.lang.java" );
+        }
+        catch ( IllegalStateException e )
+        {
+            threw = true;
+        }
+        assertTrue( threw );
     }
 
     public void testGettersWhenFrozen()
     {
         URIContext uriContext = MutableURI.getDefaultContext();
+        String uriString = null;
 
         for ( int i = 0; i < _tests.length; i++ )
         {
@@ -320,7 +347,7 @@
             String path = _tests[i][4];
             String query = _tests[i][5];
             String fragment = _tests[i][6];
-            String uriString = _tests[i][7];
+            uriString = _tests[i][7];
 
             try
             {
@@ -341,6 +368,24 @@
             {
                 fail( "Test failed for URI, \"" + uriString + "\", with a URISyntaxException: " + e.getMessage() );
             }
+        }
+
+        // opaque URI
+        try
+        {
+            String scheme = "news";
+            String schemeSpecificPart = "comp.lang.java";
+            uriString = scheme + ":" + schemeSpecificPart;
+            FreezableMutableURI opaqueURI = new FreezableMutableURI( uriString, true );
+            opaqueURI.setFrozen( true );
+            assertTrue( opaqueURI.isFrozen() );
+            assertEquals( uriString, opaqueURI.getURIString( uriContext ) );
+            assertEquals( opaqueURI.getScheme(), scheme );
+            assertEquals( opaqueURI.getSchemeSpecificPart(), schemeSpecificPart );
+        }
+        catch ( URISyntaxException e )
+        {
+            fail( "Test failed for URI, \"" + uriString + "\", with a URISyntaxException: " + e.getMessage() );
         }
     }
 

Modified: beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/core/urls/MutableURITest.java
URL: http://svn.apache.org/viewcvs/beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/core/urls/MutableURITest.java?rev=354491&r1=354490&r2=354491&view=diff
==============================================================================
--- beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/core/urls/MutableURITest.java (original)
+++ beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/core/urls/MutableURITest.java Tue Dec  6 10:35:21 2005
@@ -126,6 +126,7 @@
     public void testConstructors()
     {
         URIContext uriContext = MutableURI.getDefaultContext();
+        String uriString = null;
 
         for (int i = 0; i < _tests.length; i++ )
         {
@@ -141,7 +142,7 @@
             String path = _tests[i][4];
             String query = _tests[i][5];
             String fragment = _tests[i][6];
-            String uriString = _tests[i][7];
+            uriString = _tests[i][7];
 
             try
             {
@@ -158,6 +159,20 @@
                 fail( "Test failed for URI, \"" + uriString + "\", with a URISyntaxException: " + e.getMessage() );
             }
         }
+
+        // opaque URI
+        try
+        {
+            uriString = "news:comp.lang.java";
+            MutableURI opaqueURI = new MutableURI( uriString, true );
+            assertEquals( uriString, opaqueURI.getURIString( uriContext ) );
+            assertTrue( opaqueURI.isAbsolute() );
+            assertTrue( opaqueURI.isOpaque() );
+        }
+        catch ( URISyntaxException e )
+        {
+            fail( "Test failed for URI, \"" + uriString + "\", with a URISyntaxException: " + e.getMessage() );
+        }
     }
 
     public void testSetters()
@@ -323,7 +338,7 @@
         assertEquals( uri.getParameters().size(), 1 );
         i = 0;
 
-        for ( java.util.Iterator ii = values.iterator(); ii.hasNext(); )  
+        for ( java.util.Iterator ii = values.iterator(); ii.hasNext(); )
         {
             String v = ( String ) ii.next();
             assertEquals( v, initValues[i++] );
@@ -353,10 +368,10 @@
         assertEquals( uri.getParameters().size(), initParams.length );
         Map/*< String, List< String > >*/ params = uri.getParameters();
 
-        for ( java.util.Iterator j = params.keySet().iterator(); j.hasNext(); )  
+        for ( java.util.Iterator j = params.keySet().iterator(); j.hasNext(); )
         {
             String n = ( String ) j.next();
-            for ( java.util.Iterator k = ( ( List ) params.get( n ) ).iterator(); k.hasNext(); )  
+            for ( java.util.Iterator k = ( ( List ) params.get( n ) ).iterator(); k.hasNext(); )
             {
                 String v = ( String ) k.next();
                 assertTrue( map.containsKey( n ) );
@@ -433,7 +448,7 @@
             assertEquals( "http://localhost:80/unwise/lcb%7Brcb%7Dbar%7Cbs%5Cctr%5Elb%5Brb%5Dlq%60",
                           uri.getURIString( uriContext ) );
 
-            // excluded. note that '#' is interpreted as fragment 
+            // excluded. note that '#' is interpreted as fragment
             // and should not be escaped in this test
             uri = new MutableURI( "http://localhost:80/excluded/" + excluded, false );
             assertEquals( "http://localhost:80/excluded/space%20lt%3Cgt%3Elb#perc%25%22quotes%22",
@@ -445,7 +460,7 @@
         }
     }
 
-    public void testgetURIStringForXML()
+    public void testGetURIStringForXML()
     {
         MutableURI uri = new MutableURI();
         uri.setScheme( "https" );
@@ -532,6 +547,54 @@
         catch ( URISyntaxException e )
         {
             fail( "Test failed for URI, \"" + uriString + "\", with a URISyntaxException: " + e.getMessage() );
+        }
+    }
+
+    public void testOpaque()
+    {
+        try
+        {
+            String scheme = "news";
+            String schemeSpecificPart = "comp.lang.java";
+            String uriString = scheme + ":" + schemeSpecificPart;
+            URIContext uriContext = MutableURI.getDefaultContext();
+
+            // constructor, setters, getters
+            MutableURI uriA = new MutableURI( "mailto:abc@tuv.wxyz.com", true );
+            assertTrue( uriA.isAbsolute() );
+            assertTrue( uriA.isOpaque() );
+            uriA.setURI( uriString, true );
+            assertTrue( uriA.isAbsolute() );
+            assertTrue( uriA.isOpaque() );
+            assertEquals( uriString, uriA.getURIString( uriContext ) );
+            assertEquals( uriA.getScheme(), scheme );
+            assertEquals( uriA.getSchemeSpecificPart(), schemeSpecificPart );
+
+            MutableURI uriB = new MutableURI( "http://localhost/test", true );
+            assertFalse( uriB.isOpaque() );
+            uriB.setURI( "/test?param1=true", true );
+            assertFalse( uriB.isOpaque() );
+
+            // check equality
+            uriB.setOpaque( scheme, schemeSpecificPart );
+            assertTrue( uriB.isOpaque() );
+            assertTrue( uriA.equals( uriA ) );
+            assertTrue( uriA.equals( uriB ) );
+            assertTrue( uriB.equals( uriA ) );
+
+            // change to non-opaque URI
+            uriB.setPath( "/test" );
+            assertFalse( uriB.isOpaque() );
+            uriB.setOpaque( scheme, schemeSpecificPart );
+            assertTrue( uriB.isOpaque() );
+            uriB.setHost( "localhost" );
+            assertFalse( uriB.isOpaque() );
+            uriB.setURI( "/test?param1=true", true );
+            assertEquals( uriB.getScheme(), null );
+        }
+        catch ( URISyntaxException e )
+        {
+            fail( "Test failed with a URISyntaxException: " + e.getMessage() );
         }
     }
 

Modified: beehive/trunk/netui/test/webapps/drt/testRecorder/tests/Anchor.xml
URL: http://svn.apache.org/viewcvs/beehive/trunk/netui/test/webapps/drt/testRecorder/tests/Anchor.xml?rev=354491&r1=354490&r2=354491&view=diff
==============================================================================
--- beehive/trunk/netui/test/webapps/drt/testRecorder/tests/Anchor.xml (original)
+++ beehive/trunk/netui/test/webapps/drt/testRecorder/tests/Anchor.xml Tue Dec  6 10:35:21 2005
@@ -105,6 +105,10 @@
 <a id="Label" name="Label"></a>
 <a href="#Top">Go To Top</a>
 </p>
+<p>
+And test an opaque URI in an href...
+<a href="mailto:qrs@tuv.wxyz.com">mailto anchor</a>
+</p>
 </body>
 </html>]]></ses:responseBody>
          </ses:response>
@@ -292,6 +296,10 @@
 <a id="Label" name="Label"></a>
 <a href="#Top">Go To Top</a>
 </p>
+<p>
+And test an opaque URI in an href...
+<a href="mailto:qrs@tuv.wxyz.com">mailto anchor</a>
+</p>
 </body>
 </html>]]></ses:responseBody>
          </ses:response>
@@ -479,6 +487,10 @@
 <a id="Label" name="Label"></a>
 <a href="#Top">Go To Top</a>
 </p>
+<p>
+And test an opaque URI in an href...
+<a href="mailto:qrs@tuv.wxyz.com">mailto anchor</a>
+</p>
 </body>
 </html>]]></ses:responseBody>
          </ses:response>
@@ -666,6 +678,10 @@
 <a id="Label" name="Label"></a>
 <a href="#Top">Go To Top</a>
 </p>
+<p>
+And test an opaque URI in an href...
+<a href="mailto:qrs@tuv.wxyz.com">mailto anchor</a>
+</p>
 </body>
 </html>]]></ses:responseBody>
          </ses:response>
@@ -852,6 +868,10 @@
 This is a named location 'Label'.  The link will take you back to the top of the page<br>
 <a id="Label" name="Label"></a>
 <a href="#Top">Go To Top</a>
+</p>
+<p>
+And test an opaque URI in an href...
+<a href="mailto:qrs@tuv.wxyz.com">mailto anchor</a>
 </p>
 </body>
 </html>]]></ses:responseBody>

Modified: beehive/trunk/netui/test/webapps/drt/web/tags/anchor/Begin.jsp
URL: http://svn.apache.org/viewcvs/beehive/trunk/netui/test/webapps/drt/web/tags/anchor/Begin.jsp?rev=354491&r1=354490&r2=354491&view=diff
==============================================================================
--- beehive/trunk/netui/test/webapps/drt/web/tags/anchor/Begin.jsp (original)
+++ beehive/trunk/netui/test/webapps/drt/web/tags/anchor/Begin.jsp Tue Dec  6 10:35:21 2005
@@ -41,5 +41,9 @@
 <netui:anchor tagId="Label"/>
 <netui:anchor linkName="#Top">Go To Top</netui:anchor>
 </p>
+<p>
+And test an opaque URI in an href...
+<netui:anchor href="mailto:qrs@tuv.wxyz.com">mailto anchor</netui:anchor>
+</p>
 </body>
 </html>