You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by se...@apache.org on 2008/05/17 20:31:45 UTC

svn commit: r657415 - in /directory/shared/trunk/ldap/src: main/java/org/apache/directory/shared/ldap/codec/util/LdapURL.java test/java/org/apache/directory/shared/ldap/codec/util/LdapUrlTest.java

Author: seelmann
Date: Sat May 17 11:31:44 2008
New Revision: 657415

URL: http://svn.apache.org/viewvc?rev=657415&view=rev
Log:
Fix for DIRSHARED-7:
o Added setters
o Added constants for scheme
o Use scheme field in toString()
o Added tests


Modified:
    directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/util/LdapURL.java
    directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/codec/util/LdapUrlTest.java

Modified: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/util/LdapURL.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/util/LdapURL.java?rev=657415&r1=657414&r2=657415&view=diff
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/util/LdapURL.java (original)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/util/LdapURL.java Sat May 17 11:31:44 2008
@@ -65,6 +65,13 @@
  */
 public class LdapURL
 {
+    
+    /** The constant for "ldaps://" scheme. */
+    private static final String LDAPS_SCHEME = "ldaps://";
+
+    /** The constant for "ldap://" scheme. */
+    private static final String LDAP_SCHEME = "ldap://";
+
     // ~ Static fields/initializers
     // -----------------------------------------------------------------
 
@@ -115,6 +122,7 @@
      */
     public LdapURL()
     {
+        scheme = LDAP_SCHEME;
         host = null;
         port = -1;
         dn = null;
@@ -132,6 +140,7 @@
      */
     public void parse( char[] chars ) throws LdapURLEncodingException
     {
+        scheme = LDAP_SCHEME;
         host = null;
         port = -1;
         dn = null;
@@ -155,8 +164,8 @@
         int pos = 0;
 
         // The scheme
-        if ( ( ( pos = StringTools.areEquals( chars, 0, "ldap://" ) ) == StringTools.NOT_EQUAL )
-            && ( ( pos = StringTools.areEquals( chars, 0, "ldaps://" ) ) == StringTools.NOT_EQUAL ) )
+        if ( ( ( pos = StringTools.areEquals( chars, 0, LDAP_SCHEME ) ) == StringTools.NOT_EQUAL )
+            && ( ( pos = StringTools.areEquals( chars, 0, LDAPS_SCHEME ) ) == StringTools.NOT_EQUAL ) )
         {
             throw new LdapURLEncodingException( "A LdapUrl must start with \"ldap://\" or \"ldaps://\"" );
         }
@@ -1194,8 +1203,9 @@
      */
     public String toString()
     {
+        StringBuffer sb = new StringBuffer();
 
-        StringBuffer sb = new StringBuffer( "ldap://" );
+        sb.append( scheme );
 
         sb.append( ( host == null ) ? "" : host );
 
@@ -1389,6 +1399,9 @@
 
 
     /**
+     * Returns the scope, one of {@link SearchControls.OBJECT_SCOPE}, 
+     * {@link SearchControls.ONELEVEL_SCOPE} or {@link SearchControls.SUBTREE_SCOPE}.
+     * 
      * @return Returns the scope.
      */
     public int getScope()
@@ -1473,4 +1486,149 @@
         final LdapURL other = ( LdapURL ) obj;
         return this.toString().equals( other.toString() );
     }
+
+    
+    /**
+     * Sets the scheme. Must be "ldap://" or "ldaps://", otherwise "ldap://" is assumed as default.
+     * 
+     * @param scheme the new scheme
+     */
+    public void setScheme( String scheme )
+    {
+        if ( scheme != null && LDAP_SCHEME.equals( scheme ) || LDAPS_SCHEME.equals( scheme ) )
+        {
+            this.scheme = scheme;
+        }
+        else
+        {
+            this.scheme = LDAP_SCHEME;
+        }
+
+    }
+
+
+    /**
+     * Sets the host.
+     * 
+     * @param host the new host
+     */
+    public void setHost( String host )
+    {
+        this.host = host;
+    }
+
+
+    /**
+     * Sets the port. Must be between 1 and 65535, otherwise -1 is assumed as default.
+     * 
+     * @param port the new port
+     */
+    public void setPort( int port )
+    {
+        if ( port < 1 || port > 65535 )
+        {
+            this.port = -1;
+        }
+        else
+        {
+            this.port = port;
+        }
+    }
+
+
+    /**
+     * Sets the dn.
+     * 
+     * @param dn the new dn
+     */
+    public void setDn( LdapDN dn )
+    {
+        this.dn = dn;
+    }
+
+
+    /**
+     * Sets the attributes, null removes all existing attributes.
+     * 
+     * @param attributes the new attributes
+     */
+    public void setAttributes( List<String> attributes )
+    {
+        if ( attributes == null )
+        {
+            this.attributes.clear();
+        }
+        else
+        {
+            this.attributes = attributes;
+        }
+    }
+
+
+    /**
+     * Sets the scope. Must be one of {@link SearchControls.OBJECT_SCOPE}, 
+     * {@link SearchControls.ONELEVEL_SCOPE} or {@link SearchControls.SUBTREE_SCOPE},
+     * otherwise {@link SearchControls.OBJECT_SCOPE} is assumed as default.
+     * 
+     * @param scope the new scope
+     */
+    public void setScope( int scope )
+    {
+        if ( scope == SearchControls.ONELEVEL_SCOPE || scope == SearchControls.SUBTREE_SCOPE )
+        {
+            this.scope = scope;
+        }
+        else
+        {
+            this.scope = SearchControls.OBJECT_SCOPE;
+        }
+    }
+
+
+    /**
+     * Sets the filter.
+     * 
+     * @param filter the new filter
+     */
+    public void setFilter( String filter )
+    {
+        this.filter = filter;
+    }
+
+
+    /**
+     * Sets the extensions, null removes all existing extensions.
+     * 
+     * @param extensions the extensions
+     */
+    public void setExtensions( Map<String, String> extensions )
+    {
+        if ( extensions == null )
+        {
+            this.extensions.clear();
+        }
+        else
+        {
+            this.extensions = extensions;
+        }
+    }
+
+
+    /**
+     * Sets the critical extensions, null removes all existing critical extensions.
+     * 
+     * @param criticalExtensions the critical extensions
+     */
+    public void setCriticalExtensions( Map<String, String> criticalExtensions )
+    {
+        if ( criticalExtensions == null )
+        {
+            this.criticalExtensions.clear();
+        }
+        else
+        {
+            this.criticalExtensions = criticalExtensions;
+        }
+    }
+
 }

Modified: directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/codec/util/LdapUrlTest.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/codec/util/LdapUrlTest.java?rev=657415&r1=657414&r2=657415&view=diff
==============================================================================
--- directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/codec/util/LdapUrlTest.java (original)
+++ directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/codec/util/LdapUrlTest.java Sat May 17 11:31:44 2008
@@ -20,11 +20,18 @@
 package org.apache.directory.shared.ldap.codec.util;
 
 
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.naming.InvalidNameException;
+import javax.naming.directory.SearchControls;
+
 import junit.framework.Assert;
 import junit.framework.TestCase;
 
 import org.apache.directory.shared.ldap.codec.util.LdapURL;
 import org.apache.directory.shared.ldap.codec.util.LdapURLEncodingException;
+import org.apache.directory.shared.ldap.name.LdapDN;
 
 
 /**
@@ -600,4 +607,184 @@
         Assert.assertEquals( "ldap:///??sub??!bindname=cn=Manager%2co=Foo", new LdapURL(
             "ldap:///??sub??!bindname=cn=Manager%2co=Foo" ).toString() );
     }
+
+
+    /**
+     * test an empty ldaps:// LdapURL
+     */
+    public void testLdapDNEmptyLdaps() throws LdapURLEncodingException
+    {
+        Assert.assertEquals( "ldaps:///", new LdapURL( "ldaps:///" ).toString() );
+    }
+
+
+    /**
+     * test an simple ldaps:// LdapURL
+     */
+    public void testLdapDNSimpleLdaps() throws LdapURLEncodingException
+    {
+        Assert.assertEquals( "ldaps://directory.apache.org:80/", new LdapURL( "ldaps://directory.apache.org:80/" )
+            .toString() );
+    }
+
+
+    /**
+     * test the setScheme() method
+     */
+    public void testLdapDNSetScheme() throws LdapURLEncodingException
+    {
+        LdapURL url = new LdapURL();
+        Assert.assertEquals( "ldap://", url.getScheme() );
+
+        url.setScheme( "invalid" );
+        Assert.assertEquals( "ldap://", url.getScheme() );
+
+        url.setScheme( "ldap://" );
+        Assert.assertEquals( "ldap://", url.getScheme() );
+
+        url.setScheme( "ldaps://" );
+        Assert.assertEquals( "ldaps://", url.getScheme() );
+
+        url.setScheme( null );
+        Assert.assertEquals( "ldap://", url.getScheme() );
+    }
+
+
+    /**
+     * test the setHost() method
+     */
+    public void testLdapDNSetHost() throws LdapURLEncodingException
+    {
+        LdapURL url = new LdapURL();
+        Assert.assertNull( url.getHost() );
+
+        url.setHost( "ldap.apache.org" );
+        Assert.assertEquals( "ldap.apache.org", url.getHost() );
+        Assert.assertEquals( "ldap://ldap.apache.org/", url.toString() );
+
+        url.setHost( null );
+        Assert.assertNull( url.getHost() );
+        Assert.assertEquals( "ldap:///", url.toString() );
+    }
+
+
+    /**
+     * test the setPort() method
+     */
+    public void testLdapDNSetPort() throws LdapURLEncodingException
+    {
+        LdapURL url = new LdapURL();
+        Assert.assertEquals( -1, url.getPort() );
+
+        url.setPort( 389 );
+        Assert.assertEquals( 389, url.getPort() );
+        Assert.assertEquals( "ldap://:389/", url.toString() );
+
+        url.setPort( 0 );
+        Assert.assertEquals( -1, url.getPort() );
+        Assert.assertEquals( "ldap:///", url.toString() );
+
+        url.setPort( 65536 );
+        Assert.assertEquals( -1, url.getPort() );
+        Assert.assertEquals( "ldap:///", url.toString() );
+    }
+
+
+    /**
+     * test the setDn() method
+     */
+    public void testLdapDNSetDn() throws LdapURLEncodingException, InvalidNameException
+    {
+        LdapURL url = new LdapURL();
+        Assert.assertNull( url.getDn() );
+
+        LdapDN dn = new LdapDN( "dc=example,dc=com" );
+        url.setDn( dn );
+        Assert.assertEquals( dn, url.getDn() );
+        Assert.assertEquals( "ldap:///dc=example,dc=com", url.toString() );
+
+        url.setDn( null );
+        Assert.assertNull( url.getDn() );
+        Assert.assertEquals( "ldap:///", url.toString() );
+    }
+
+
+    /**
+     * test the setAttributes() method
+     */
+    public void testLdapDNSetAttributes() throws LdapURLEncodingException, InvalidNameException
+    {
+        LdapURL url = new LdapURL();
+        Assert.assertNotNull( url.getAttributes() );
+        Assert.assertTrue( url.getAttributes().isEmpty() );
+
+        List<String> attributes = new ArrayList<String>();
+        url.setDn( new LdapDN( "dc=example,dc=com" ) );
+
+        url.setAttributes( null );
+        Assert.assertNotNull( url.getAttributes() );
+        Assert.assertTrue( url.getAttributes().isEmpty() );
+        Assert.assertEquals( "ldap:///dc=example,dc=com", url.toString() );
+
+        attributes.add( "cn" );
+        url.setAttributes( attributes );
+        Assert.assertNotNull( url.getAttributes() );
+        Assert.assertEquals( 1, url.getAttributes().size() );
+        Assert.assertEquals( "ldap:///dc=example,dc=com?cn", url.toString() );
+
+        attributes.add( "userPassword;binary" );
+        url.setAttributes( attributes );
+        Assert.assertNotNull( url.getAttributes() );
+        Assert.assertEquals( 2, url.getAttributes().size() );
+        Assert.assertEquals( "ldap:///dc=example,dc=com?cn,userPassword;binary", url.toString() );
+    }
+
+
+    /**
+     * test the setScope() method
+     */
+    public void testLdapDNSetScope() throws LdapURLEncodingException, InvalidNameException
+    {
+        LdapURL url = new LdapURL();
+        Assert.assertEquals( SearchControls.OBJECT_SCOPE, url.getScope() );
+
+        url.setDn( new LdapDN( "dc=example,dc=com" ) );
+
+        url.setScope( SearchControls.ONELEVEL_SCOPE );
+        Assert.assertEquals( SearchControls.ONELEVEL_SCOPE, url.getScope() );
+        Assert.assertEquals( "ldap:///dc=example,dc=com??one", url.toString() );
+
+        url.setScope( SearchControls.SUBTREE_SCOPE );
+        Assert.assertEquals( SearchControls.SUBTREE_SCOPE, url.getScope() );
+        Assert.assertEquals( "ldap:///dc=example,dc=com??sub", url.toString() );
+
+        url.setScope( -1 );
+        Assert.assertEquals( SearchControls.OBJECT_SCOPE, url.getScope() );
+        Assert.assertEquals( "ldap:///dc=example,dc=com", url.toString() );
+    }
+
+
+    /**
+     * test the setFilter() method
+     */
+    public void testLdapDNSetFilter() throws LdapURLEncodingException, InvalidNameException
+    {
+        LdapURL url = new LdapURL();
+        Assert.assertNull( url.getFilter() );
+
+        url.setDn( new LdapDN( "dc=example,dc=com" ) );
+
+        url.setFilter( "(objectClass=person)" );
+        Assert.assertEquals( "(objectClass=person)", url.getFilter() );
+        Assert.assertEquals( "ldap:///dc=example,dc=com???(objectClass=person)", url.toString() );
+
+        url.setFilter( "(cn=Babs Jensen)" );
+        Assert.assertEquals( "(cn=Babs Jensen)", url.getFilter() );
+        Assert.assertEquals( "ldap:///dc=example,dc=com???(cn=Babs%20Jensen)", url.toString() );
+
+        url.setFilter( null );
+        Assert.assertNull( url.getFilter() );
+        Assert.assertEquals( "ldap:///dc=example,dc=com", url.toString() );
+    }
+
 }