directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r982272 [2/2] - in /directory: apacheds/trunk/core-api/src/main/java/org/apache/directory/server/core/ apacheds/trunk/core-api/src/main/java/org/apache/directory/server/core/administrative/ apacheds/trunk/core/src/main/java/org/apache/direc...
Date Wed, 04 Aug 2010 14:53:23 GMT
Modified: directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/name/DNTest.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/name/DNTest.java?rev=982272&r1=982271&r2=982272&view=diff
==============================================================================
--- directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/name/DNTest.java (original)
+++ directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/name/DNTest.java Wed Aug  4 14:53:22 2010
@@ -6,16 +6,16 @@
  *  to you 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. 
- *  
+ *  under the License.
+ *
  */
 package org.apache.directory.shared.ldap.name;
 
@@ -359,8 +359,8 @@ public class DNTest
 
 
     /**
-     * Test for DIRSTUDIO-589, DIRSTUDIO-591, DIRSHARED-38 
-     * 
+     * Test for DIRSTUDIO-589, DIRSTUDIO-591, DIRSHARED-38
+     *
      * Check escaped sharp followed by a hex sequence
      * (without the ESC it would be a valid hexstring).
      */
@@ -912,7 +912,7 @@ public class DNTest
     public void testDnGetPrefixPos0() throws LdapException
     {
         DN dn = new DN( "a=b, c=d,e = f" );
-        DN newDn = ( ( DN ) dn.getPrefix( 0 ) );
+        DN newDn = ( dn.getPrefix( 0 ) );
         assertEquals( "", newDn.getName() );
     }
 
@@ -924,7 +924,7 @@ public class DNTest
     public void testDnGetPrefixPos1() throws LdapException
     {
         DN dn = new DN( "a=b, c=d,e = f" );
-        DN newDn = ( ( DN ) dn.getPrefix( 1 ) );
+        DN newDn = ( dn.getPrefix( 1 ) );
         assertEquals( "e = f", newDn.getName() );
     }
 
@@ -936,7 +936,7 @@ public class DNTest
     public void testDnGetPrefixPos2() throws LdapException
     {
         DN dn = new DN( "a=b, c=d,e = f" );
-        DN newDn = ( ( DN ) dn.getPrefix( 2 ) );
+        DN newDn = ( dn.getPrefix( 2 ) );
         assertEquals( " c=d,e = f", newDn.getName() );
     }
 
@@ -948,7 +948,7 @@ public class DNTest
     public void testDnGetPrefixPos3() throws LdapException
     {
         DN dn = new DN( "a=b, c=d,e = f" );
-        DN newDn = ( ( DN ) dn.getPrefix( 3 ) );
+        DN newDn = ( dn.getPrefix( 3 ) );
         assertEquals( "a=b, c=d,e = f", newDn.getName() );
     }
 
@@ -981,7 +981,7 @@ public class DNTest
     public void testDnGetPrefixEmptyDN()
     {
         DN dn = new DN();
-        DN newDn = ( ( DN ) dn.getPrefix( 0 ) );
+        DN newDn = ( dn.getPrefix( 0 ) );
         assertEquals( "", newDn.getName() );
     }
 
@@ -994,7 +994,7 @@ public class DNTest
     public void testDnGetSuffixPos0() throws LdapException
     {
         DN dn = new DN( "a=b, c=d,e = f" );
-        DN newDn = ( ( DN ) dn.getSuffix( 0 ) );
+        DN newDn = ( dn.getSuffix( 0 ) );
         assertEquals( "a=b, c=d,e = f", newDn.getName() );
     }
 
@@ -1006,7 +1006,7 @@ public class DNTest
     public void testDnGetSuffixPos1() throws LdapException
     {
         DN dn = new DN( "a=b, c=d,e = f" );
-        DN newDn = ( ( DN ) dn.getSuffix( 1 ) );
+        DN newDn = ( dn.getSuffix( 1 ) );
         assertEquals( "a=b, c=d", newDn.getName() );
     }
 
@@ -1018,7 +1018,7 @@ public class DNTest
     public void testDnGetSuffixPos2() throws LdapException
     {
         DN dn = new DN( "a=b, c=d,e = f" );
-        DN newDn = ( ( DN ) dn.getSuffix( 2 ) );
+        DN newDn = ( dn.getSuffix( 2 ) );
         assertEquals( "a=b", newDn.getName() );
     }
 
@@ -1030,7 +1030,7 @@ public class DNTest
     public void testDnGetSuffixPos3() throws LdapException
     {
         DN dn = new DN( "a=b, c=d,e = f" );
-        DN newDn = ( ( DN ) dn.getSuffix( 3 ) );
+        DN newDn = ( dn.getSuffix( 3 ) );
         assertEquals( "", newDn.getName() );
     }
 
@@ -1063,7 +1063,7 @@ public class DNTest
     public void testDnGetSuffixEmptyDN()
     {
         DN dn = new DN();
-        DN newDn = ( ( DN ) dn.getSuffix( 0 ) );
+        DN newDn = ( dn.getSuffix( 0 ) );
         assertEquals( "", newDn.getName() );
     }
 
@@ -1099,9 +1099,9 @@ public class DNTest
     {
         DN dn = new DN( "a=b, c=d" );
         DN clonedDn = dn.remove( 0 );
-        
+
         assertFalse( dn == clonedDn );
-        
+
         clonedDn = clonedDn.remove( 0 );
 
         assertEquals( true, clonedDn.isEmpty() );
@@ -1585,8 +1585,8 @@ public class DNTest
 
         DN name = DnParser.getNameParser().parse( dn );
 
-        assertEquals( dn, ( ( DN ) name ).getName() );
-        assertEquals( "cn=Emmanuel  L\u00E9charny", ((DN)name).getNormName() );
+        assertEquals( dn, ( name ).getName() );
+        assertEquals( "cn=Emmanuel  L\u00E9charny", (name).getNormName() );
     }
 
 
@@ -1702,7 +1702,7 @@ public class DNTest
 
             /**
              * Compute the instance's hash code
-             * @return the instance's hash code 
+             * @return the instance's hash code
              */
             public int hashCode()
             {
@@ -1968,7 +1968,7 @@ public class DNTest
         DN dn = new DN( "cn=Airline,cn=Website,cn=HomeDir,cn=John,ou=Marketing,ou=West" );
         String[] expected = new String[]{ "cn=Airline", "cn=Website", "cn=HomeDir", "cn=John", "ou=Marketing", "ou=West" };
         int count = 0;
-        
+
         for ( RDN rdn : dn )
         {
             assertEquals( expected[count], rdn.toString() );
@@ -2004,6 +2004,27 @@ public class DNTest
         assertEquals( "ou=East", name.getRdn( 0 ).getName() );
     }
 
+
+    /**
+     * Test the getRdns() method
+     */
+    @Test
+    public void testGetRdns() throws Exception
+    {
+        DN dn = new DN( "cn=HomeDir,cn=John,ou=Marketing,ou=East" );
+
+        String[] expected = new String[]{ "cn=HomeDir", "cn=John", "ou=Marketing", "ou=East" };
+
+        int i = 0;
+
+        for ( RDN rdn : dn )
+        {
+            assertEquals( expected[i], rdn.getName() );
+            i++;
+        }
+    }
+
+
     /**
      * Class to test for getSuffix
      *
@@ -2071,12 +2092,12 @@ public class DNTest
         assertTrue( !n0.isChildOf( n6 ) );
         assertTrue( !n0.isChildOf( n7 ) );
         assertTrue( !n0.isChildOf( n8 ) );
-        
+
         DN nn0 = new DN( "cn=zero" );
         DN nn10 = new DN( "cn=one,cn=zero" );
         DN nn210 = new DN( "cn=two,cn=one,cn=zero" );
         DN nn3210 = new DN( "cn=three,cn=two,cn=one,cn=zero" );
-        
+
         assertTrue( nn0.isChildOf( nn0 ) );
         assertTrue( nn10.isChildOf( nn0 ) );
         assertTrue( nn210.isChildOf( nn0 ) );
@@ -2090,14 +2111,14 @@ public class DNTest
         assertTrue( nn3210.isChildOf( nn210 ) );
 
         assertTrue( nn3210.isChildOf( nn3210 ) );
-        
-        assertTrue( "Starting DN fails with ADS DN", 
+
+        assertTrue( "Starting DN fails with ADS DN",
             new DN( "ou=foo,dc=apache,dc=org" ).isChildOf( new DN( "dc=apache,dc=org" ) ) );
-        
-        assertTrue( "Starting DN fails with Java LdapName", 
+
+        assertTrue( "Starting DN fails with Java LdapName",
             new DN( "ou=foo,dc=apache,dc=org" ).isChildOf( new DN( "dc=apache,dc=org" ) ) );
 
-        assertTrue( "Starting DN fails with Java LdapName", 
+        assertTrue( "Starting DN fails with Java LdapName",
             new DN( "dc=apache,dc=org" ).isChildOf( new DN( "dc=apache,dc=org" ) ) );
     }
 
@@ -2221,7 +2242,7 @@ public class DNTest
 
         name = name.addAll( name0 );
         assertTrue( name0.equals( name ) );
-        
+
         name = name.addAll( 2, name1 );
         assertTrue( name2.equals( name ) );
 
@@ -2486,7 +2507,7 @@ public class DNTest
 
         DN result = DN.normalize( name, oids );
 
-        assertTrue( ((DN)result).getNormName().equals( "ou=some people,dc=example,dc=com" ) );
+        assertTrue( (result).getNormName().equals( "ou=some people,dc=example,dc=com" ) );
     }
 
 
@@ -2542,9 +2563,9 @@ public class DNTest
         DN result = DN.normalize( name, oidOids );
 
         assertEquals(
-            ((DN)result).getNormName(),
+            (result).getNormName(),
             "0.9.2342.19200300.100.1.25=and some animals+2.5.4.11=some people,0.9.2342.19200300.100.1.25=example,0.9.2342.19200300.100.1.25=com" );
-        assertTrue( ( ( DN ) result )
+        assertTrue( ( result )
             .getName()
             .equals(
                 "2.5.4.11= Some   People   + 0.9.2342.19200300.100.1.25=  And   Some anImAls,0.9.2342.19200300.100.1.25 = eXample,dc= cOm" ) );
@@ -2754,7 +2775,7 @@ public class DNTest
         DN n21 =  new DN( "cn=two,cn=one" );
         DN n2 =  new DN( "cn=two" );
         DN n1 =  new DN( "cn=one" );
-        
+
         assertTrue( n3210.hasSuffix( n3 ) );
         assertTrue( n3210.hasSuffix( n32 ) );
         assertTrue( n3210.hasSuffix( n321 ) );
@@ -2816,12 +2837,12 @@ public class DNTest
     public void testDoubleQuoteInNameDIRSERVER_642() throws LdapException, InvalidNameException
     {
         DN name1 = new DN( "cn=\"Kylie Minogue\",dc=example,dc=com" );
-        
+
         String[] expected = new String[]{ "cn=\"Kylie Minogue\"", "dc=example", "dc=com" };
 
         List<RDN> j = name1.getRdns();
         int count = 0;
-        
+
 
         for ( RDN rdn:j )
         {
@@ -3190,40 +3211,40 @@ public class DNTest
         dn.normalize( oids );
 
         long t0 = System.currentTimeMillis();
-        
+
         for ( int i = 0; i < 1000; i++ )
         {
             ByteArrayOutputStream baos = new ByteArrayOutputStream();
             ObjectOutputStream out = new ObjectOutputStream( baos );
 
             DnSerializer.serialize( dn, out );
-            
+
             byte[] data = baos.toByteArray();
             ObjectInputStream in = new ObjectInputStream( new ByteArrayInputStream( data ) );
-            
+
             DN dn1 = DnSerializer.deserialize( in );
         }
-        
+
         long t1 = System.currentTimeMillis();
-        
+
         System.out.println( "delta :" + ( t1 - t0) );
 
         long t2 = System.currentTimeMillis();
-        
+
         for ( int i = 0; i < 1000000; i++ )
         {
             //ByteArrayOutputStream baos = new ByteArrayOutputStream();
             //ObjectOutputStream out = new ObjectOutputStream( baos );
 
             //DnSerializer.serializeString( dn, out );
-            
+
             //byte[] data = baos.toByteArray();
             //ObjectInputStream in = new ObjectInputStream( new ByteArrayInputStream( data ) );
-            
+
             //DN dn1 = DnSerializer.deserializeString( in, oids );
             dn.normalize( oids );
         }
-        
+
         long t3 = System.currentTimeMillis();
 
         System.out.println( "delta :" + ( t3 - t2) );
@@ -3249,14 +3270,14 @@ public class DNTest
         assertEquals( dn, DnSerializer.deserialize( in ) );
         assertEquals( dn, deserializeDN( serializeDN( dn ) ) );
     }
-    
+
     @Test
     public void testCompositeRDN() throws LdapException
     {
         assertTrue( DN.isValid( "a=b+c=d+e=f,g=h" ) );
 
         DN dn = new DN( "a=b+c=d+e=f,g=h" );
-        
+
         assertEquals( "a=b+c=d+e=f,g=h", dn.toString() );
     }
 
@@ -3266,7 +3287,7 @@ public class DNTest
         assertTrue( DN.isValid( "1.2.3.4.5=0+1.2.3.4.6=0+1.2.3.4.7=omnischmomni,2.5.4.3=subtree,0.9.2342.19200300.100.1.25=example,0.9.2342.19200300.100.1.25=com" ) );
 
         DN dn = new DN( "1.2.3.4.5=0+1.2.3.4.6=0+1.2.3.4.7=omnischmomni,2.5.4.3=subtree,0.9.2342.19200300.100.1.25=example,0.9.2342.19200300.100.1.25=com" );
-        
+
         assertEquals( "1.2.3.4.5=0+1.2.3.4.6=0+1.2.3.4.7=omnischmomni,2.5.4.3=subtree,0.9.2342.19200300.100.1.25=example,0.9.2342.19200300.100.1.25=com", dn.toString() );
     }
 
@@ -3291,7 +3312,7 @@ public class DNTest
         assertEquals( "ABC", atav3.getUpValue().getString() );
         assertTrue( Arrays.equals( StringTools.getBytesUtf8( "ABC" ),atav3.getNormValue().getBytes() ) );
 
-        // antlr parser: 
+        // antlr parser:
         DN dn4 = new DN( " cn = \\41\\42\\43 , ou=system " );
         assertEquals( " cn = \\41\\42\\43 ", dn4.getRdn().getName() );
         AVA atav4 = dn4.getRdn().getAtav();
@@ -3307,7 +3328,7 @@ public class DNTest
         assertEquals( "ABC", atav5.getUpValue().getString() );
         assertEquals( "ABC", atav5.getNormValue().getString() );
 
-        // fast parser: string value with trailing spaces 
+        // fast parser: string value with trailing spaces
         DN dn2 = new DN( " cn = Amos Tori , ou=system " );
         assertEquals( " cn = Amos Tori ", dn2.getRdn().getName() );
         AVA atav2 = dn2.getRdn().getAtav();
@@ -3336,14 +3357,14 @@ public class DNTest
         assertEquals( "ou=a,ou=system", dn2.getNormName() );
         assertEquals( "ou=A\\20", dn2.getRdn().getName() );
         assertEquals( "ou=a", dn2.getRdn().getNormName() );
-        
+
         DN dn3 = new DN( "ou=\\ ,ou=system" );
         dn3.normalize( oids );
         assertEquals( "ou=\\ ,ou=system", dn3.getName() );
         assertEquals( "ou=,ou=system", dn3.getNormName() );
         assertEquals( "ou=\\ ", dn3.getRdn().getName() );
         assertEquals( "ou=", dn3.getRdn().getNormName() );
-        
+
         DN dn4 = new DN( "ou=\\20,ou=system" );
         dn4.normalize( oids );
         assertEquals( "ou=\\20,ou=system", dn4.getName() );
@@ -3367,7 +3388,7 @@ public class DNTest
         assertEquals( "cn=loopback+ipHostNumber=127.0.0.1", dn1.getRdn().getName() );
         assertEquals( "cn=loopback+iphostnumber=127.0.0.1", dn1.getRdn().getNormName() );
         assertEquals( "127.0.0.1", dn1.getRdn().getAttributeTypeAndValue( "ipHostNumber" ).getUpValue().get() );
-        
+
         // numeric OID with suffix
         DN dn2 = new DN( "cn=loopback+ipHostNumber=X127.0.0.1,ou=Hosts,dc=mygfs,dc=com" );
         assertEquals( "cn=loopback+ipHostNumber=X127.0.0.1,ou=Hosts,dc=mygfs,dc=com", dn2.getName() );
@@ -3390,7 +3411,7 @@ public class DNTest
         assertEquals( "cn=loopback+iphostnumber=\\#127.0.0.1 Z", dn4.getRdn().getNormName() );
     }
 
-    
+
     @Test
     public void testNormalizeAscii() throws Exception
     {
@@ -3420,16 +3441,16 @@ public class DNTest
         assertEquals( "  ou  =  Example ", atav.getUpName() );
     }
 
-    
+
     @Test
     public void testNormalizeAsciiComposite() throws Exception
     {
         DN dn = new DN( "  ou  =  Example + ou = TEST ,  ou  =  COM " );
-        
+
         dn.normalize( oidOids );
         assertEquals( "2.5.4.11=example+2.5.4.11=test,2.5.4.11=com", dn.getNormName() );
         assertEquals( "  ou  =  Example + ou = TEST ,  ou  =  COM ", dn.getName() );
-        
+
         RDN rdn = dn.getRdn();
         assertEquals( "2.5.4.11", rdn.getNormType() );
         assertEquals( "example", rdn.getNormValue().getString() );
@@ -3437,21 +3458,21 @@ public class DNTest
         assertEquals( "ou", rdn.getUpType() );
         assertEquals( "Example", rdn.getUpValue().getString() );
         assertEquals( "  ou  =  Example + ou = TEST ", rdn.getName() );
-        
+
         // The first ATAV
         AVA atav = rdn.getAtav();
-        
+
         assertEquals( "2.5.4.11=example", atav.getNormName() );
         assertEquals( "2.5.4.11", atav.getNormType() );
         assertEquals( "example", atav.getNormValue().get() );
-        
+
         assertEquals( "ou", atav.getUpType() );
         assertEquals( "Example", atav.getUpValue().get() );
-        
+
         assertEquals( "  ou  =  Example ", atav.getUpName() );
-        
+
         assertEquals( 2, rdn.getNbAtavs() );
-        
+
         // The second ATAV
         for ( AVA ava : rdn )
         {
@@ -3460,27 +3481,27 @@ public class DNTest
                 // Skip the first one
                 continue;
             }
-            
+
             assertEquals( "2.5.4.11=test", ava.getNormName() );
             assertEquals( "2.5.4.11", ava.getNormType() );
             assertEquals( "test", ava.getNormValue().get() );
-            
+
             assertEquals( "ou", ava.getUpType() );
             assertEquals( "TEST", ava.getUpValue().get() );
             assertEquals( " ou = TEST ", ava.getUpName() );
         }
     }
 
-    
+
     @Test
     public void testNormalizeAsciiWithEscaped() throws Exception
     {
         DN dn = new DN( "  ou  =  Ex\\+mple ,  ou  =  COM " );
-        
+
         dn.normalize( oidOids );
         assertEquals( "2.5.4.11=ex\\+mple,2.5.4.11=com", dn.getNormName() );
         assertEquals( "  ou  =  Ex\\+mple ,  ou  =  COM ", dn.getName() );
-        
+
         RDN rdn = dn.getRdn();
         assertEquals( "2.5.4.11", rdn.getNormType() );
         assertEquals( "ex+mple", rdn.getNormValue().getString() );
@@ -3488,30 +3509,30 @@ public class DNTest
         assertEquals( "ou", rdn.getUpType() );
         assertEquals( "Ex+mple", rdn.getUpValue().getString() );
         assertEquals( "  ou  =  Ex\\+mple ", rdn.getName() );
-        
+
         AVA atav = rdn.getAtav();
-        
+
         assertEquals( "2.5.4.11=ex\\+mple", atav.getNormName() );
         assertEquals( "2.5.4.11", atav.getNormType() );
         assertEquals( "ex+mple", atav.getNormValue().get() );
-        
+
         assertEquals( "ou", atav.getUpType() );
         assertEquals( "Ex+mple", atav.getUpValue().get() );
-        
+
         assertEquals( "  ou  =  Ex\\+mple ", atav.getUpName() );
     }
 
-    
+
     @Test
     public void testNormalizeCompositeWithEscaped() throws Exception
     {
         DN dn = new DN( "  OU  =  Ex\\+mple + ou = T\\+ST\\  ,  ou  =  COM " );
-        
+
         // ------------------------------------------------------------------
         // Before normalization
         assertEquals( "  OU  =  Ex\\+mple + ou = T\\+ST\\  ,  ou  =  COM ", dn.getName() );
         assertEquals( "ou=Ex\\+mple+ou=T\\+ST\\ ,ou=COM", dn.getNormName() );
-        
+
         // Check the first RDN
         RDN rdn = dn.getRdn();
         assertEquals( "  OU  =  Ex\\+mple + ou = T\\+ST\\  ", rdn.getName() );
@@ -3519,24 +3540,24 @@ public class DNTest
 
         assertEquals( "OU", rdn.getUpType() );
         assertEquals( "ou", rdn.getNormType() );
-        
+
         assertEquals( "Ex+mple", rdn.getUpValue().getString() );
         assertEquals( "Ex+mple", rdn.getNormValue().getString() );
-       
+
         // The first ATAV
         AVA atav = rdn.getAtav();
-        
+
         assertEquals( "  OU  =  Ex\\+mple ", atav.getUpName() );
         assertEquals( "ou=Ex\\+mple", atav.getNormName() );
-        
+
         assertEquals( "ou", atav.getNormType() );
         assertEquals( "OU", atav.getUpType() );
-        
+
         assertEquals( "Ex+mple", atav.getUpValue().get() );
         assertEquals( "Ex+mple", atav.getNormValue().get() );
-        
+
         assertEquals( 2, rdn.getNbAtavs() );
-        
+
         // The second ATAV
         for ( AVA ava : rdn )
         {
@@ -3545,13 +3566,13 @@ public class DNTest
                 // Skip the first one
                 continue;
             }
-            
+
             assertEquals( " ou = T\\+ST\\  ", ava.getUpName() );
             assertEquals( "ou=T\\+ST\\ ", ava.getNormName() );
 
             assertEquals( "ou", ava.getUpType() );
             assertEquals( "ou", ava.getNormType() );
-            
+
             assertEquals( "T+ST ", ava.getUpValue().get() );
             assertEquals( "T+ST ", ava.getNormValue().get() );
         }
@@ -3559,10 +3580,10 @@ public class DNTest
         // ------------------------------------------------------------------
         // Now normalize the DN
         dn.normalize( oidOids );
-        
+
         assertEquals( "  OU  =  Ex\\+mple + ou = T\\+ST\\  ,  ou  =  COM ", dn.getName() );
         assertEquals( "2.5.4.11=ex\\+mple+2.5.4.11=t\\+st,2.5.4.11=com", dn.getNormName() );
-        
+
         // Check the first RDN
         rdn = dn.getRdn();
         assertEquals( "  OU  =  Ex\\+mple + ou = T\\+ST\\  ", rdn.getName() );
@@ -3570,24 +3591,24 @@ public class DNTest
 
         assertEquals( "OU", rdn.getUpType() );
         assertEquals( "2.5.4.11", rdn.getNormType() );
-        
+
         assertEquals( "Ex+mple", rdn.getUpValue().getString() );
         assertEquals( "ex+mple", rdn.getNormValue().getString() );
-        
+
         // The first ATAV
         atav = rdn.getAtav();
-        
+
         assertEquals( "  OU  =  Ex\\+mple ", atav.getUpName() );
         assertEquals( "2.5.4.11=ex\\+mple", atav.getNormName() );
-        
+
         assertEquals( "2.5.4.11", atav.getNormType() );
         assertEquals( "OU", atav.getUpType() );
-        
+
         assertEquals( "Ex+mple", atav.getUpValue().get() );
         assertEquals( "ex+mple", atav.getNormValue().get() );
-        
+
         assertEquals( 2, rdn.getNbAtavs() );
-        
+
         // The second ATAV
         for ( AVA ava : rdn )
         {
@@ -3596,19 +3617,19 @@ public class DNTest
                 // Skip the first one
                 continue;
             }
-            
+
             assertEquals( " ou = T\\+ST\\  ", ava.getUpName() );
             assertEquals( "2.5.4.11=t\\+st", ava.getNormName() );
 
             assertEquals( "ou", ava.getUpType() );
             assertEquals( "2.5.4.11", ava.getNormType() );
-            
+
             assertEquals( "T+ST ", ava.getUpValue().get() );
             assertEquals( "t+st", ava.getNormValue().get() );
         }
     }
-    
-    
+
+
     //-------------------------------------------------------------------------
     // test the iterator
     //-------------------------------------------------------------------------
@@ -3616,50 +3637,50 @@ public class DNTest
     public void testIteratorNullDN()
     {
         DN dn = DN.EMPTY_DN;
-        
+
         for ( RDN rdn : dn )
         {
             fail( "Should not be there" );
         }
-        
+
         assertTrue( true );
     }
-    
-    
+
+
     @Test
     public void testIteratorOneRDN() throws Exception
     {
         DN dn = new DN( "ou=example" );
         int count = 0;
-        
+
         for ( RDN rdn : dn )
         {
             count++;
             assertEquals( "ou=example", rdn.getName() );
         }
-        
+
         assertEquals( 1, count );
     }
-    
-    
+
+
     @Test
     public void testIteratorMultipleRDN() throws Exception
     {
         DN dn = new DN( "sn=joe+cn=doe,dc=apache,dc=org" );
         int count = 0;
-        
+
         String[] expected = new String[]{ "sn=joe+cn=doe", "dc=apache", "dc=org" };
-        
+
         for ( RDN rdn : dn )
         {
             assertEquals( expected[count], rdn.getName() );
             count++;
         }
-        
+
         assertEquals( 3, count );
     }
-    
-    
+
+
     @Test
     public void testIsParentOfTrue() throws Exception
     {
@@ -3668,14 +3689,14 @@ public class DNTest
         DN parent2 = new DN( "dc=apache, dc=org" );
         DN parent3 = new DN( "dc=org" );
         DN notParent = new DN( "ou=example,dc=apache, dc=com" );
-        
+
         assertTrue( parent1.isParentOf( dn ) );
         assertTrue( parent2.isParentOf( dn ) );
         assertTrue( parent3.isParentOf( dn ) );
         assertFalse( notParent.isParentOf( dn ) );
     }
-    
-    
+
+
     @Test
     public void testIsChildOfTrue() throws Exception
     {
@@ -3684,7 +3705,7 @@ public class DNTest
         DN parent2 = new DN( "dc=apache, dc=org" );
         DN parent3 = new DN( "dc=org" );
         DN notParent = new DN( "dc=apache, dc=com" );
-        
+
         assertTrue( dn.isChildOf( parent1 ) );
         assertTrue( dn.isChildOf( parent2 ) );
         assertTrue( dn.isChildOf( parent3 ) );

Modified: directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/util/tree/TestDnNode.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/util/tree/TestDnNode.java?rev=982272&r1=982271&r2=982272&view=diff
==============================================================================
--- directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/util/tree/TestDnNode.java (original)
+++ directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/util/tree/TestDnNode.java Wed Aug  4 14:53:22 2010
@@ -22,20 +22,23 @@ package org.apache.directory.shared.ldap
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
 
+import java.util.Map;
+
 import org.apache.directory.junit.tools.Concurrent;
 import org.apache.directory.junit.tools.ConcurrentJunitRunner;
 import org.apache.directory.shared.ldap.exception.LdapException;
+import org.apache.directory.shared.ldap.exception.LdapUnwillingToPerformException;
 import org.apache.directory.shared.ldap.name.DN;
-import org.junit.After;
-import org.junit.Before;
+import org.apache.directory.shared.ldap.name.RDN;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 
 
 /**
- * Test the Dn Nodes 
+ * Test the Dn Nodes
  *
  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
  */
@@ -43,106 +46,630 @@ import org.junit.runner.RunWith;
 @Concurrent()
 public class TestDnNode
 {
-    /** A structure to hold all the DNs */
-    DnBranchNode<DN> dnLookupTree;
-    DN dn1;
-    DN dn2;
-    DN dn3;
-    DN dn4;
-    DN dn5;
-    DN dn6;
-
-    /**
-     * Create the elements we will test
-     */
-    @Before
-    public void setUp()  throws Exception
-    {
-        dnLookupTree = new DnBranchNode<DN>();
-        
-        dn1 = new DN( "dc=directory,dc=apache,dc=org" );
-        dn2 = new DN( "dc=mina,dc=apache,dc=org" );
-        dn3 = new DN( "dc=test,dc=com" );
-        dn4 = new DN( "dc=acme,dc=com" );
-        dn5 = new DN( "dc=acme,c=us,dc=com" );
-        dn6 = new DN( "dc=empty" );
+    //---------------------------------------------------------------------------
+    // Test the Add( DN ) operation
+    //---------------------------------------------------------------------------
+    /**
+     * Test the addition of a null DN
+     */
+    @Test( expected=LdapUnwillingToPerformException.class)
+    public void testAddNullDNNoElem() throws LdapException
+    {
+        DnNode<DN> tree = new DnNode<DN>();
 
-        dnLookupTree.add( dn1, dn1 );
-        dnLookupTree.add( dn2, dn2 );
-        dnLookupTree.add( dn3, dn3 );
-        dnLookupTree.add( dn4, dn4 );
-        dnLookupTree.add( dn5, dn5 );
-        dnLookupTree.add( dn6, dn6 );
+        tree.add( null );
     }
-    
-    
+
+
     /**
-     * Clean the tree
-     *
+     * Test the addition of a DN with three RDN
      */
-    @After
-    public void tearDown()
+    @Test
+    public void testAdd3LevelDNNoElem() throws LdapException
     {
-        dnLookupTree = null;
+        DnNode<DN> tree = new DnNode<DN>( DN.EMPTY_DN, null );
+        DN dn = new DN( "dc=c,dc=b,dc=a" );
+
+        tree.add( dn );
+
+        assertNotNull( tree );
+
+        Map<RDN, DnNode<DN>> children = tree.getChildren();
+        assertNotNull( children );
+
+        assertEquals( 1, children.size() );
+        assertNull( tree.getElement() );
+
+        DnNode<DN> level1 = children.get( new RDN( "dc=a" ) );
+        DnNode<DN> level2 = level1.getChildren().get( new RDN( "dc=b" ) );
+        DnNode<DN> level3 = level2.getChildren().get( new RDN( "dc=c" ) );
+
+        assertNotNull( level3 );
+        assertFalse( level3.hasElement() );
     }
-    
-    
+
+
     /**
-     * Test the addition of a single DN
+     * Test the addition of two DNs not overlapping
      */
-    @Test public void testNewTree() throws LdapException
+    @Test
+    public void testAdd2DistinctDNsNoElem() throws LdapException
     {
-        /** A structure to hold all the DNs */
-        DnBranchNode<DN> dnLookupTree = new DnBranchNode<DN>();
-        
-        DN suffix = new DN( "dc=example, dc=com" );
-        
-        dnLookupTree.add( suffix, suffix );
-        
-        assertNotNull( dnLookupTree );
-        assertTrue( dnLookupTree instanceof DnBranchNode );
-        assertTrue( ((DnBranchNode<DN>)dnLookupTree).contains( "dc=com" ) );
-        
-        DnNode<DN> child = ((DnBranchNode<DN>)dnLookupTree).getChild( "dc=com" );
-        assertTrue( child instanceof DnBranchNode );
-        assertTrue( ((DnBranchNode<DN>)child).contains( "dc=example" ) );
+        DnNode<DN> tree = new DnNode<DN>();
+        DN dn1 = new DN( "dc=b,dc=a" );
+        DN dn2 = new DN( "dc=f,dc=e" );
+
+        tree.add( dn1 );
+        tree.add( dn2 );
 
-        child = ((DnBranchNode<DN>)child).getChild( "dc=example" );
-        assertEquals( suffix, ((DnLeafNode<DN>)child).getElement() );
+        assertNotNull( tree );
+
+        Map<RDN, DnNode<DN>> children = tree.getChildren();
+        assertNotNull( children );
+
+        assertEquals( 2, children.size() );
+        assertNull( tree.getElement() );
+
+        DnNode<DN> level1 = children.get( new RDN( "dc=a" ) );
+        DnNode<DN> level2 = level1.getChildren().get( new RDN( "dc=b" ) );
+
+        assertNotNull( level2 );
+        assertFalse( level2.hasElement() );
+
+        level1 = children.get( new RDN( "dc=e" ) );
+        level2 = level1.getChildren().get( new RDN( "dc=f" ) );
+
+        assertNotNull( level2 );
+        assertFalse( level2.hasElement() );
     }
 
 
     /**
-     * Test additions in a tree 
+     * Test the addition of two overlapping DNs
      */
     @Test
-    public void testComplexTreeCreation() throws LdapException
+    public void testAdd2OverlappingDNsNoElem() throws LdapException
     {
-        
-        assertTrue( dnLookupTree.hasParentElement( dn1 ) );
-        assertTrue( dnLookupTree.hasParentElement( dn2 ) );
-        assertTrue( dnLookupTree.hasParentElement( dn3 ) );
-        assertTrue( dnLookupTree.hasParentElement( dn4 ) );
-        assertTrue( dnLookupTree.hasParentElement( dn5 ) );
-        assertTrue( dnLookupTree.hasParentElement( dn6 ) );
-        assertTrue( dnLookupTree.hasParentElement( new DN( "dc=nothing,dc=empty" ) ) );
-        assertFalse( dnLookupTree.hasParentElement( new DN(  "dc=directory,dc=apache,dc=root" ) ) );
+        DnNode<DN> tree = new DnNode<DN>();
+        DN dn1 = new DN( "dc=b,dc=a" );
+        DN dn2 = new DN( "dc=f,dc=a" );
+
+        tree.add( dn1 );
+        tree.add( dn2 );
+
+        assertNotNull( tree );
+
+        Map<RDN, DnNode<DN>> children = tree.getChildren();
+        assertNotNull( children );
+
+        assertEquals( 1, children.size() );
+        assertNull( tree.getElement() );
+
+        DnNode<DN> level1 = children.get( new RDN( "dc=a" ) );
+        DnNode<DN> level2 = level1.getChildren().get( new RDN( "dc=b" ) );
+
+        Map<RDN, DnNode<DN>> childrenDn1 = level1.getChildren();
+        assertNotNull( childrenDn1 );
+
+        assertEquals( 2, childrenDn1.size() );
+        assertNull( level1.getElement() );
+
+        assertNotNull( level2 );
+        assertFalse( level2.hasElement() );
+
+        level1 = children.get( new RDN( "dc=a" ) );
+        level2 = level1.getChildren().get( new RDN( "dc=f" ) );
+
+        assertNotNull( level2 );
+        assertFalse( level2.hasElement() );
     }
-    
-    
+
+
     /**
-     * Test that we can add an entry twice without any problem
-     * TODO testAddEntryTwice.
-     *
+     * Test the addition of two equal DNs
+     */
+    @Test( expected=LdapUnwillingToPerformException.class)
+    public void testAdd2EqualDNsNoElem() throws LdapException
+    {
+        DnNode<DN> tree = new DnNode<DN>();
+        DN dn1 = new DN( "dc=b,dc=a" );
+        DN dn2 = new DN( "dc=b,dc=a" );
+
+        tree.add( dn1 );
+        tree.add( dn2 );
+    }
+
+
+    //---------------------------------------------------------------------------
+    // Test the Add( DN, N ) operation
+    //---------------------------------------------------------------------------
+    /**
+     * Test the addition of a null DN
+     */
+    @Test( expected=LdapUnwillingToPerformException.class)
+    public void testAddNullDN() throws LdapException
+    {
+        DnNode<DN> tree = new DnNode<DN>();
+
+        tree.add( (DN)null, null );
+    }
+
+
+    /**
+     * Test the addition of a DN with three RDN
      */
     @Test
-    public void testAddEntryTwice() throws LdapException
+    public void testAdd3LevelDN() throws LdapException
     {
-        assertEquals( 6, dnLookupTree.size() );
+        DnNode<DN> tree = new DnNode<DN>();
+        DN dn = new DN( "dc=c,dc=b,dc=a" );
 
-        dnLookupTree.add( dn1, dn1 );
-        
-        assertEquals( 6, dnLookupTree.size() );
+        tree.add( dn, dn );
+
+        assertNotNull( tree );
+
+        Map<RDN, DnNode<DN>> children = tree.getChildren();
+        assertNotNull( children );
+
+        assertEquals( 1, children.size() );
+        assertNull( tree.getElement() );
+
+        DnNode<DN> level1 = children.get( new RDN( "dc=a" ) );
+        DnNode<DN> level2 = level1.getChildren().get( new RDN( "dc=b" ) );
+        DnNode<DN> level3 = level2.getChildren().get( new RDN( "dc=c" ) );
+
+        assertNotNull( level3 );
+        assertEquals( dn, level3.getElement() );
+    }
+
+
+    /**
+     * Test the addition of two DNs not overlapping
+     */
+    @Test
+    public void testAdd2DistinctDNs() throws LdapException
+    {
+        DnNode<DN> tree = new DnNode<DN>();
+        DN dn1 = new DN( "dc=b,dc=a" );
+        DN dn2 = new DN( "dc=f,dc=e" );
+
+        tree.add( dn1, dn1 );
+        tree.add( dn2, dn2 );
+
+        assertNotNull( tree );
+
+        Map<RDN, DnNode<DN>> children = tree.getChildren();
+        assertNotNull( children );
+
+        assertEquals( 2, children.size() );
+        assertNull( tree.getElement() );
+
+        DnNode<DN> level1 = children.get( new RDN( "dc=a" ) );
+        DnNode<DN> level2 = level1.getChildren().get( new RDN( "dc=b" ) );
+
+        assertNotNull( level2 );
+        assertEquals( dn1, level2.getElement() );
+
+        level1 = children.get( new RDN( "dc=e" ) );
+        level2 = level1.getChildren().get( new RDN( "dc=f" ) );
+
+        assertNotNull( level2 );
+        assertEquals( dn2, level2.getElement() );
+    }
+
+
+    /**
+     * Test the addition of two overlapping DNs
+     */
+    @Test
+    public void testAdd2OverlappingDNs() throws LdapException
+    {
+        DnNode<DN> tree = new DnNode<DN>();
+        DN dn1 = new DN( "dc=b,dc=a" );
+        DN dn2 = new DN( "dc=f,dc=a" );
+
+        tree.add( dn1, dn1 );
+        tree.add( dn2, dn2 );
+
+        assertNotNull( tree );
+
+        Map<RDN, DnNode<DN>> children = tree.getChildren();
+        assertNotNull( children );
+
+        assertEquals( 1, children.size() );
+        assertNull( tree.getElement() );
+
+        DnNode<DN> level1 = children.get( new RDN( "dc=a" ) );
+        DnNode<DN> level2 = level1.getChildren().get( new RDN( "dc=b" ) );
+
+        Map<RDN, DnNode<DN>> childrenDn1 = level1.getChildren();
+        assertNotNull( childrenDn1 );
+
+        assertEquals( 2, childrenDn1.size() );
+        assertNull( level1.getElement() );
+
+        assertNotNull( level2 );
+        assertEquals( dn1, level2.getElement() );
+
+        level1 = children.get( new RDN( "dc=a" ) );
+        level2 = level1.getChildren().get( new RDN( "dc=f" ) );
+
+        assertNotNull( level2 );
+        assertEquals( dn2, level2.getElement() );
+    }
+
+
+    /**
+     * Test the addition of two equal DNs
+     */
+    @Test( expected=LdapUnwillingToPerformException.class)
+    public void testAdd2EqualDNs() throws LdapException
+    {
+        DnNode<DN> tree = new DnNode<DN>();
+        DN dn1 = new DN( "dc=b,dc=a" );
+        DN dn2 = new DN( "dc=b,dc=a" );
+
+        tree.add( dn1, dn1 );
+        tree.add( dn2, dn2 );
+    }
+
+
+    //---------------------------------------------------------------------------
+    // Test the hasChildren method
+    //---------------------------------------------------------------------------
+    @Test
+    public void testHasChildren() throws Exception
+    {
+        DnNode<DN> tree = new DnNode<DN>();
+        DN dn1 = new DN( "dc=b,dc=a" );
+        tree.add( dn1 );
+
+        assertTrue( tree.hasChildren() );
+        Map<RDN, DnNode<DN>> children = tree.getChildren();
+        assertNotNull( children );
+
+        DnNode<DN> child = children.get( new RDN( "dc=a" ) );
+        assertTrue( child.hasChildren() );
+
+        children = child.getChildren();
+        child = children.get( new RDN( "dc=b" ) );
+        assertFalse( child.hasChildren() );
+    }
+
+
+    //---------------------------------------------------------------------------
+    // Test the hasChildren(DN) method
+    //---------------------------------------------------------------------------
+    @Test
+    public void testHasChildrenDN() throws Exception
+    {
+        DnNode<DN> tree = new DnNode<DN>();
+        DN dn1 = new DN( "dc=b,dc=a" );
+        tree.add( dn1 );
+
+        assertTrue( tree.hasChildren( new DN( "dc=a" ) ) );
+        assertFalse( tree.hasChildren( dn1 ) );
+    }
+
+
+    //---------------------------------------------------------------------------
+    // Test the isLeaf() method
+    //---------------------------------------------------------------------------
+    @Test
+    public void testIsLeaf() throws Exception
+    {
+        DnNode<DN> tree = new DnNode<DN>();
+        DN dn = new DN( "dc=c,dc=b,dc=a" );
+        tree.add( dn );
+
+        assertFalse( tree.isLeaf() );
+
+        DnNode<DN> child = tree.getChild( new RDN( "dc=a" ) );
+        assertFalse( child.isLeaf() );
+
+        child = child.getChild( new RDN( "dc=b" ) );
+        assertFalse( child.isLeaf() );
+
+        child = child.getChild( new RDN( "dc=c" ) );
+        assertTrue( child.isLeaf() );
+    }
+
+
+    //---------------------------------------------------------------------------
+    // Test the isLeaf(DN) method
+    //---------------------------------------------------------------------------
+    @Test
+    public void testIsLeafDN() throws Exception
+    {
+        DnNode<DN> tree = new DnNode<DN>();
+        DN dn1 = new DN( "dc=c,dc=b,dc=a" );
+        tree.add( dn1, dn1 );
+
+        DN dn2 = new DN( "dc=e,dc=a" );
+        tree.add( dn2 );
+
+        assertFalse( tree.isLeaf( DN.EMPTY_DN ) );
+        assertFalse( tree.isLeaf( new DN( "dc=a" ) ) );
+        assertFalse( tree.isLeaf( new DN( "dc=b,dc=a" ) ) );
+        assertTrue( tree.isLeaf( dn1 ) );
+        assertTrue( tree.isLeaf( dn2 ) );
+    }
+
+
+    //---------------------------------------------------------------------------
+    // Test the getElement() method
+    //---------------------------------------------------------------------------
+    @Test
+    public void testGetElement() throws Exception
+    {
+        DnNode<DN> tree = new DnNode<DN>();
+        DN dn = new DN( "dc=c,dc=b,dc=a" );
+        tree.add( dn, dn );
+
+        assertNull( tree.getElement() );
+
+        DnNode<DN> child = tree.getChild( new RDN( "dc=a" ) );
+        assertNull( child.getElement() );
+
+        child = child.getChild( new RDN( "dc=b" ) );
+        assertNull( child.getElement() );
+
+        child = child.getChild( new RDN( "dc=c" ) );
+        assertEquals( dn, child.getElement() );
+    }
+
+
+    //---------------------------------------------------------------------------
+    // Test the hasElement() method
+    //---------------------------------------------------------------------------
+    @Test
+    public void testHasElement() throws Exception
+    {
+        DnNode<DN> tree = new DnNode<DN>();
+        DN dn = new DN( "dc=c,dc=b,dc=a" );
+        tree.add( dn, dn );
+
+        assertFalse( tree.hasElement() );
+
+        DnNode<DN> child = tree.getChild( new RDN( "dc=a" ) );
+        assertFalse( child.hasElement() );
+
+        child = child.getChild( new RDN( "dc=b" ) );
+        assertFalse( child.hasElement() );
+
+        child = child.getChild( new RDN( "dc=c" ) );
+        assertTrue( child.hasElement() );
+    }
+
+
+    //---------------------------------------------------------------------------
+    // Test the getElement(DN) method
+    //---------------------------------------------------------------------------
+    @Test
+    public void testGetElementDN() throws Exception
+    {
+        DnNode<DN> tree = new DnNode<DN>();
+        DN dn1 = new DN( "dc=c,dc=b,dc=a" );
+        tree.add( dn1, dn1 );
+
+        DN dn2 = new DN( "dc=e,dc=a" );
+        tree.add( dn2, dn2 );
+
+        assertNull( tree.getElement( DN.EMPTY_DN ) );
+        assertNull( tree.getElement( new DN( "dc=a" ) ) );
+        assertNull( tree.getElement( new DN( "dc=b,dc=a" ) ) );
+        assertEquals( dn1, tree.getElement( dn1 ) );
+        assertEquals( dn2, tree.getElement( dn2 ) );
+        assertEquals( dn2, tree.getElement( new DN( "dc=g,dc=f,dc=e,dc=a" ) ) );
+    }
+
+
+    //---------------------------------------------------------------------------
+    // Test the hasElement(DN) method
+    //---------------------------------------------------------------------------
+    @Test
+    public void testHasElementDN() throws Exception
+    {
+        DnNode<DN> tree = new DnNode<DN>();
+        DN dn1 = new DN( "dc=c,dc=b,dc=a" );
+        tree.add( dn1, dn1 );
+
+        DN dn2 = new DN( "dc=e,dc=a" );
+        tree.add( dn2 );
+
+        assertFalse( tree.hasElement( DN.EMPTY_DN ) );
+        assertFalse( tree.hasElement( new DN( "dc=a" ) ) );
+        assertFalse( tree.hasElement( new DN( "dc=b,dc=a" ) ) );
+        assertTrue( tree.hasElement( dn1 ) );
+        assertFalse( tree.hasElement( dn2 ) );
+    }
+
+
+    //---------------------------------------------------------------------------
+    // Test the size() method
+    //---------------------------------------------------------------------------
+    @Test
+    public void testSize() throws LdapException
+    {
+        DnNode<DN> tree = new DnNode<DN>();
+        assertEquals( 1, tree.size() );
+
+        tree.add( new DN( "dc=b,dc=a" ) );
+        assertEquals( 3, tree.size() );
+
+        tree.add( new DN( "dc=f,dc=a" ) );
+        assertEquals( 4, tree.size() );
+
+        tree.add( new DN( "dc=a,dc=f,dc=a" ) );
+        assertEquals( 5, tree.size() );
+
+        tree.add( new DN( "dc=b,dc=f,dc=a" ) );
+        assertEquals( 6, tree.size() );
+
+        tree.add( new DN( "dc=z,dc=t" ) );
+        assertEquals( 8, tree.size() );
+    }
+
+
+    //---------------------------------------------------------------------------
+    // Test the getParent() method
+    //---------------------------------------------------------------------------
+    @Test
+    public void testGetParent() throws Exception
+    {
+        DnNode<DN> tree = new DnNode<DN>();
+        DN dn = new DN( "dc=c,dc=b,dc=a" );
+        tree.add( dn, dn );
+
+        assertNull( tree.getParent() );
+
+        DnNode<DN> child = tree.getChild( new RDN( "dc=a" ) );
+        assertEquals( tree, child.getParent() );
+
+        DnNode<DN> child1 = child.getChild( new RDN( "dc=b" ) );
+        assertEquals( child, child1.getParent() );
+
+        child = child1.getChild( new RDN( "dc=c" ) );
+        assertEquals( child1, child.getParent() );
+    }
+
+
+    //---------------------------------------------------------------------------
+    // Test the getNode(DN) method
+    //---------------------------------------------------------------------------
+    @Test
+    public void testGetNodeDN() throws Exception
+    {
+        DnNode<DN> tree = new DnNode<DN>();
+        DN dn1 = new DN( "dc=c,dc=b,dc=a" );
+        tree.add( dn1, dn1 );
+
+        DN dn2 = new DN( "dc=e,dc=a" );
+        tree.add( dn2, dn2 );
+
+        assertNull( tree.getNode( DN.EMPTY_DN ) );
+
+        DnNode<DN> child = tree.getChild( new RDN( "dc=a" ) );
+        assertEquals( child, tree.getNode( new DN( "dc=a" ) ) );
+
+        child = child.getChild( new RDN( "dc=b" ) );
+        assertEquals( child, tree.getNode( new DN( "dc=b,dc=a" ) ) );
+
+        child = child.getChild( new RDN( "dc=c" ) );
+        assertEquals( child, tree.getNode( new DN( "dc=c,dc=b,dc=a" ) ) );
+
+        assertEquals( child, tree.getNode( new DN( "dc=f,dc=e,dc=c,dc=b,dc=a" ) ) );
+    }
+
+
+    //---------------------------------------------------------------------------
+    // Test the hasParent() method
+    //---------------------------------------------------------------------------
+    @Test
+    public void testHasParent() throws Exception
+    {
+        DnNode<DN> tree = new DnNode<DN>();
+        DN dn = new DN( "dc=c,dc=b,dc=a" );
+        tree.add( dn, dn );
+
+        assertFalse( tree.hasParent() );
+
+        DnNode<DN> child = tree.getChild( new RDN( "dc=a" ) );
+        assertTrue( child.hasParent() );
+
+        DnNode<DN> child1 = child.getChild( new RDN( "dc=b" ) );
+        assertTrue( child1.hasParent() );
+
+        child = child1.getChild( new RDN( "dc=c" ) );
+        assertTrue( child.hasParent() );
+    }
+
+
+    //---------------------------------------------------------------------------
+    // Test the hasParent(DN) method
+    //---------------------------------------------------------------------------
+    @Test
+    public void testHasParentDN() throws Exception
+    {
+        DnNode<DN> tree = new DnNode<DN>();
+        DN dn1 = new DN( "dc=c,dc=b,dc=a" );
+        tree.add( dn1, dn1 );
+
+        DN dn2 = new DN( "dc=e,dc=a" );
+        tree.add( dn2, dn2 );
+
+        assertFalse( tree.hasParent( DN.EMPTY_DN ) );
+
+        DnNode<DN> child = tree.getChild( new RDN( "dc=a" ) );
+        assertTrue( tree.hasParent( new DN( "dc=a" ) ) );
+
+        child = child.getChild( new RDN( "dc=b" ) );
+        assertTrue( tree.hasParent( new DN( "dc=b,dc=a" ) ) );
+
+        child = child.getChild( new RDN( "dc=c" ) );
+        assertTrue( tree.hasParent( new DN( "dc=c,dc=b,dc=a" ) ) );
+
+        assertTrue( tree.hasParent( new DN( "dc=f,dc=e,dc=c,dc=b,dc=a" ) ) );
+    }
+
+
+    //---------------------------------------------------------------------------
+    // Test the getChild(RDN) method
+    //---------------------------------------------------------------------------
+    @Test
+    public void testGetChildRdn() throws Exception
+    {
+        DnNode<DN> tree = new DnNode<DN>();
+        DN dn = new DN( "dc=c,dc=b,dc=a" );
+        tree.add( dn, dn );
+
+        RDN rdnA = new RDN( "dc=a" );
+        RDN rdnB = new RDN( "dc=b" );
+        RDN rdnC = new RDN( "dc=c" );
+
+        DnNode<DN> child = tree.getChild( rdnA );
+        assertNotNull( child );
+        assertEquals( rdnA, child.getRdn() );
+
+        child = child.getChild( rdnB );
+        assertNotNull( child );
+        assertEquals( rdnB, child.getRdn() );
+
+        child = child.getChild( rdnC );
+        assertNotNull( child );
+        assertEquals( rdnC, child.getRdn() );
+    }
+
+
+    //---------------------------------------------------------------------------
+    // Test the contains(RDN) method
+    //---------------------------------------------------------------------------
+    @Test
+    public void testContains() throws Exception
+    {
+        DnNode<DN> tree = new DnNode<DN>();
+        DN dn = new DN( "dc=c,dc=b,dc=a" );
+        tree.add( dn, dn );
+
+        RDN rdnA = new RDN( "dc=a" );
+        RDN rdnB = new RDN( "dc=b" );
+        RDN rdnC = new RDN( "dc=c" );
+
+        assertTrue( tree.contains( rdnA ) );
+        assertFalse( tree.contains( rdnB ) );
+        assertFalse( tree.contains( rdnC ) );
+
+        DnNode<DN> child = tree.getChild( rdnA );
+
+        assertFalse( child.contains( rdnA ) );
+        assertTrue( child.contains( rdnB ) );
+        assertFalse( child.contains( rdnC ) );
+
+        child = child.getChild( rdnB );
+
+        assertFalse( child.contains( rdnA ) );
+        assertFalse( child.contains( rdnB ) );
+        assertTrue( child.contains( rdnC ) );
     }
 
     /**
@@ -151,58 +678,75 @@ public class TestDnNode
     @Test
     public void testComplexTreeDeletion() throws LdapException
     {
+        DnNode<DN> dnLookupTree = new DnNode<DN>();
+        DN dn1 = new DN( "dc=directory,dc=apache,dc=org" );
+        DN dn2 = new DN( "dc=mina,dc=apache,dc=org" );
+        DN dn3 = new DN( "dc=test,dc=com" );
+        DN dn4 = new DN( "dc=acme,dc=com" );
+        DN dn5 = new DN( "dc=acme,c=us,dc=com" );
+        DN dn6 = new DN( "dc=empty" );
+
+        dnLookupTree.add( dn1, dn1 );
+        dnLookupTree.add( dn2, dn2 );
+        dnLookupTree.add( dn3, dn3 );
+        dnLookupTree.add( dn4, dn4 );
+        dnLookupTree.add( dn5, dn5 );
+        dnLookupTree.add( dn6, dn6 );
+
+        assertEquals( 11, dnLookupTree.size() );
+
         dnLookupTree.remove( dn3 );
-        assertEquals( 5, dnLookupTree.size() );
-        assertTrue( dnLookupTree.hasParentElement( dn1 ) );
-        assertTrue( dnLookupTree.hasParentElement( dn2 ) );
-        assertTrue( dnLookupTree.hasParentElement( dn4 ) );
-        assertTrue( dnLookupTree.hasParentElement( dn5 ) );
-        assertTrue( dnLookupTree.hasParentElement( dn6 ) );
-        assertTrue( dnLookupTree.hasParentElement( new DN( "dc=nothing,dc=empty" ) ) );
-        assertFalse( dnLookupTree.hasParentElement( new DN(  "dc=directory,dc=apache,dc=root" ) ) );
+        assertEquals( 10, dnLookupTree.size() );
+        assertTrue( dnLookupTree.hasParent( dn1 ) );
+        assertTrue( dnLookupTree.hasParent( dn2 ) );
+        assertTrue( dnLookupTree.hasParent( dn4 ) );
+        assertTrue( dnLookupTree.hasParent( dn5 ) );
+        assertTrue( dnLookupTree.hasParent( dn6 ) );
+        assertTrue( dnLookupTree.hasParent( new DN( "dc=nothing,dc=empty" ) ) );
+        assertFalse( dnLookupTree.hasParent( new DN(  "dc=directory,dc=apache,dc=root" ) ) );
 
         dnLookupTree.remove( dn6 );
-        assertEquals( 4, dnLookupTree.size() );
-        assertTrue( dnLookupTree.hasParentElement( dn1 ) );
-        assertTrue( dnLookupTree.hasParentElement( dn2 ) );
-        assertTrue( dnLookupTree.hasParentElement( dn4 ) );
-        assertTrue( dnLookupTree.hasParentElement( dn5 ) );
-        assertFalse( dnLookupTree.hasParentElement( new DN( "dc=nothing,dc=empty" ) ) );
-        assertFalse( dnLookupTree.hasParentElement( new DN(  "dc=directory,dc=apache,dc=root" ) ) );
+        assertEquals( 9, dnLookupTree.size() );
+        assertTrue( dnLookupTree.hasParent( dn1 ) );
+        assertTrue( dnLookupTree.hasParent( dn2 ) );
+        assertTrue( dnLookupTree.hasParent( dn4 ) );
+        assertTrue( dnLookupTree.hasParent( dn5 ) );
+        assertFalse( dnLookupTree.hasParent( new DN( "dc=nothing,dc=empty" ) ) );
+        assertFalse( dnLookupTree.hasParent( new DN(  "dc=directory,dc=apache,dc=root" ) ) );
 
         dnLookupTree.remove( dn1 );
-        assertEquals( 3, dnLookupTree.size() );
-        assertTrue( dnLookupTree.hasParentElement( dn2 ) );
-        assertTrue( dnLookupTree.hasParentElement( dn4 ) );
-        assertTrue( dnLookupTree.hasParentElement( dn5 ) );
-        assertFalse( dnLookupTree.hasParentElement( new DN( "dc=nothing,dc=empty" ) ) );
-        assertFalse( dnLookupTree.hasParentElement( new DN(  "dc=directory,dc=apache,dc=root" ) ) );
+        assertEquals( 8, dnLookupTree.size() );
+        assertTrue( dnLookupTree.hasParent( dn2 ) );
+        assertTrue( dnLookupTree.hasParent( dn4 ) );
+        assertTrue( dnLookupTree.hasParent( dn5 ) );
+        assertFalse( dnLookupTree.hasParent( new DN( "dc=nothing,dc=empty" ) ) );
+        assertFalse( dnLookupTree.hasParent( new DN(  "dc=directory,dc=apache,dc=root" ) ) );
 
         // Should not change anything
         dnLookupTree.remove( dn3 );
-        assertEquals( 3, dnLookupTree.size() );
-        assertTrue( dnLookupTree.hasParentElement( dn2 ) );
-        assertTrue( dnLookupTree.hasParentElement( dn4 ) );
-        assertTrue( dnLookupTree.hasParentElement( dn5 ) );
-        assertFalse( dnLookupTree.hasParentElement( new DN( "dc=nothing,dc=empty" ) ) );
-        assertFalse( dnLookupTree.hasParentElement( new DN(  "dc=directory,dc=apache,dc=root" ) ) );
+        assertEquals( 8, dnLookupTree.size() );
+        assertTrue( dnLookupTree.hasParent( dn2 ) );
+        assertTrue( dnLookupTree.hasParent( dn4 ) );
+        assertTrue( dnLookupTree.hasParent( dn5 ) );
+        assertFalse( dnLookupTree.hasParent( new DN( "dc=nothing,dc=empty" ) ) );
+        assertFalse( dnLookupTree.hasParent( new DN(  "dc=directory,dc=apache,dc=root" ) ) );
 
         dnLookupTree.remove( dn5 );
-        assertEquals( 2, dnLookupTree.size() );
-        assertTrue( dnLookupTree.hasParentElement( dn2 ) );
-        assertTrue( dnLookupTree.hasParentElement( dn4 ) );
-        assertFalse( dnLookupTree.hasParentElement( new DN( "dc=nothing,dc=empty" ) ) );
-        assertFalse( dnLookupTree.hasParentElement( new DN(  "dc=directory,dc=apache,dc=root" ) ) );
+        assertEquals( 6, dnLookupTree.size() );
+        assertTrue( dnLookupTree.hasParent( dn2 ) );
+        assertTrue( dnLookupTree.hasParent( dn4 ) );
+        assertFalse( dnLookupTree.hasParent( new DN( "dc=nothing,dc=empty" ) ) );
+        assertFalse( dnLookupTree.hasParent( new DN(  "dc=directory,dc=apache,dc=root" ) ) );
 
         dnLookupTree.remove( dn2 );
-        assertEquals( 1, dnLookupTree.size() );
-        assertTrue( dnLookupTree.hasParentElement( dn4 ) );
-        assertFalse( dnLookupTree.hasParentElement( new DN( "dc=nothing,dc=empty" ) ) );
-        assertFalse( dnLookupTree.hasParentElement( new DN(  "dc=directory,dc=apache,dc=root" ) ) );
+        assertEquals( 3, dnLookupTree.size() );
+        assertTrue( dnLookupTree.hasParent( dn4 ) );
+        assertFalse( dnLookupTree.hasParent( new DN( "dc=nothing,dc=empty" ) ) );
+        assertFalse( dnLookupTree.hasParent( new DN(  "dc=directory,dc=apache,dc=root" ) ) );
 
         dnLookupTree.remove( dn4 );
-        assertEquals( 0, dnLookupTree.size() );
-        assertFalse( dnLookupTree.hasParentElement( new DN( "dc=nothing,dc=empty" ) ) );
-        assertFalse( dnLookupTree.hasParentElement( new DN(  "dc=directory,dc=apache,dc=root" ) ) );
+        assertEquals( 1, dnLookupTree.size() );
+        assertFalse( dnLookupTree.hasParent( new DN( "dc=nothing,dc=empty" ) ) );
+        assertFalse( dnLookupTree.hasParent( new DN(  "dc=directory,dc=apache,dc=root" ) ) );
     }
 }



Mime
View raw message