directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From seelm...@apache.org
Subject svn commit: r666691 - in /directory/shared/trunk/ldap/src: main/java/org/apache/directory/shared/ldap/name/Rdn.java test/java/org/apache/directory/shared/ldap/name/RdnTest.java
Date Wed, 11 Jun 2008 14:56:21 GMT
Author: seelmann
Date: Wed Jun 11 07:56:21 2008
New Revision: 666691

URL: http://svn.apache.org/viewvc?rev=666691&view=rev
Log:
Fix for DIRSHARED-3: Rdn.compareTo() should be invertable

Modified:
    directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/name/Rdn.java
    directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/name/RdnTest.java

Modified: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/name/Rdn.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/name/Rdn.java?rev=666691&r1=666690&r2=666691&view=diff
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/name/Rdn.java
(original)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/name/Rdn.java
Wed Jun 11 07:56:21 2008
@@ -752,43 +752,28 @@
                    // We have more than one value. We will
                    // go through all of them.
 
-                   for ( AttributeTypeAndValue current:atavs )
+                   // the types are already normalized and sorted in the atavs TreeSet
+                   // so we could compare the 1st with the 1st, then the 2nd with the 2nd,
etc.
+                   Iterator<AttributeTypeAndValue> localIterator = atavs.iterator();
+                   Iterator<AttributeTypeAndValue> paramIterator = rdn.atavs.iterator();
+                   
+                   while(localIterator.hasNext() || paramIterator.hasNext())
                    {
-                       String type = current.getNormType();
-
-                       if ( rdn.atavTypes.containsKey( type ) )
+                       if(!localIterator.hasNext())
                        {
-                           List<AttributeTypeAndValue> atavLocalList = ( List<AttributeTypeAndValue>
) atavTypes.get( type );
-                           List<AttributeTypeAndValue> atavParamList = ( List<AttributeTypeAndValue>
) rdn.atavTypes.get( type );
-
-                           // We have to verify that each value of the
-                           // first list are present in
-                           // the second list
-                           for ( AttributeTypeAndValue atavLocal:atavLocalList )
-                           {
-                               boolean found = false;
-
-                               for ( AttributeTypeAndValue atavParam:atavParamList )
-                               {
-                                   if ( atavLocal.compareTo( atavParam ) == EQUAL )
-                                   {
-                                       found = true;
-                                       break;
-                                   }
-                               }
-
-                               if ( !found )
-                               {
-                                   // The ATAV does not exist in the second RDN
-                                   return SUPERIOR;
-                               }
-                           }
+                           return SUPERIOR;
                        }
-                       else
+                       if(!paramIterator.hasNext())
                        {
-                           // We can't find an atav in the rdn : the current
-                           // one is superior
-                           return SUPERIOR;
+                           return INFERIOR;
+                       }
+                       
+                       AttributeTypeAndValue localAtav = localIterator.next();
+                       AttributeTypeAndValue paramAtav = paramIterator.next();
+                       int result = localAtav.compareTo( paramAtav );
+                       if ( result != EQUAL )
+                       {
+                           return result;
                        }
                    }
 

Modified: directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/name/RdnTest.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/name/RdnTest.java?rev=666691&r1=666690&r2=666691&view=diff
==============================================================================
--- directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/name/RdnTest.java
(original)
+++ directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/name/RdnTest.java
Wed Jun 11 07:56:21 2008
@@ -316,7 +316,7 @@
     {
         Rdn rdn1 = new Rdn( " a = b + c = d + a = f + g = h " );
         Rdn rdn2 = null;
-        assertEquals( 1, rdn1.compareTo( rdn2 ) );
+        assertTrue( rdn1.compareTo( rdn2 ) > 0 );
     }
 
 
@@ -390,8 +390,9 @@
         Rdn rdn1 = new Rdn( " a = f + g = h + c = d " );
         Rdn rdn2 = new Rdn( " c = d + a = h + g = h " );
 
-        assertEquals( 1, rdn1.compareTo( rdn2 ) );
-        assertEquals( 1, rdn2.compareTo( rdn1 ) );
+        assertTrue( rdn1.compareTo( rdn2 ) < 0 );
+        assertTrue( rdn2.compareTo( rdn1 ) > 0 );
+        assertEquals( 0, rdn1.compareTo( rdn2 ) + rdn2.compareTo( rdn1 ) );
     }
 
 
@@ -402,34 +403,40 @@
      * @throws InvalidNameException
      */
     @Test
-    public void test_DIRSHARED_2() throws InvalidNameException
+    public void testCompareSecondAtav() throws InvalidNameException
     {
         // the second ATAV differs
         Rdn rdn1 = new Rdn( " a = b + c = d " );
         Rdn rdn2 = new Rdn( " a = b + c = y " );
-        assertTrue( rdn1.compareTo( rdn2 ) != 0 );
+        assertTrue( rdn1.compareTo( rdn2 ) < 0 );
+        assertTrue( rdn2.compareTo( rdn1 ) > 0 );
+        assertEquals( 0, rdn1.compareTo( rdn2 ) + rdn2.compareTo( rdn1 ) );
 
         // the third ATAV differs
         Rdn rdn3 = new Rdn( " a = b + c = d + e = f " );
         Rdn rdn4 = new Rdn( " a = b + c = d + e = y " );
-        assertTrue( rdn3.compareTo( rdn4 ) != 0 );
+        assertTrue( rdn3.compareTo( rdn4 ) < 0 );
+        assertTrue( rdn4.compareTo( rdn3 ) > 0 );
+        assertEquals( 0, rdn3.compareTo( rdn4 ) + rdn4.compareTo( rdn3 ) );
 
         // the second ATAV differs in value only
         Rdn rdn5 = new Rdn( " a = b + a = c " );
         Rdn rdn6 = new Rdn( " a = b + a = y " );
-        assertTrue( rdn5.compareTo( rdn6 ) != 0 );
+        assertTrue( rdn5.compareTo( rdn6 ) < 0 );
+        assertTrue( rdn6.compareTo( rdn5 ) > 0 );
+        assertEquals( 0, rdn5.compareTo( rdn6 ) + rdn6.compareTo( rdn5 ) );
     }
 
 
     /**
-     * Test for DIRSHARED-3.
+     * Test for DIRSHARED-2.
      * The compare operation should return a correct value (1 or -1)
      * depending on the ATAVs, not on their position.
      * 
      * @throws InvalidNameException
      */
     @Test
-    public void test_DIRSHARED_3() throws InvalidNameException
+    public void testCompareIndependentFromOrder() throws InvalidNameException
     {
         Rdn rdn1 = new Rdn( " a = b + c = d " );
         Rdn rdn2 = new Rdn( " c = d + a = b " );
@@ -437,14 +444,84 @@
 
         rdn1 = new Rdn( " a = b + c = e " );
         rdn2 = new Rdn( " c = d + a = b " );
-        assertEquals( 1, rdn1.compareTo( rdn2 ) );
-        assertEquals( 1, rdn2.compareTo( rdn1 ) );
+        assertTrue( rdn1.compareTo( rdn2 ) > 0 );
+        assertTrue( rdn2.compareTo( rdn1 ) < 0 );
+        assertEquals( 0, rdn1.compareTo( rdn2 ) + rdn2.compareTo( rdn1 ) );
 
         rdn1 = new Rdn( " a = b + c = d " );
         rdn2 = new Rdn( " e = f + g = h " );
-        assertEquals( 1, rdn1.compareTo( rdn2 ) );
-        assertEquals( 1, rdn2.compareTo( rdn1 ) );
+        assertTrue( rdn1.compareTo( rdn2 ) < 0 );
+        assertTrue( rdn2.compareTo( rdn1 ) > 0 );
+        assertEquals( 0, rdn1.compareTo( rdn2 ) + rdn2.compareTo( rdn1 ) );
+    }
+
+
+    /**
+     * Test for DIRSHARED-3.
+     * Tests that compareTo() is invertable for single-valued RDNs.
+     * 
+     * @throws InvalidNameException
+     */
+    @Test
+    public void testCompareInvertableNC2NC() throws InvalidNameException
+    {
+        Rdn rdn1 = new Rdn( " a = b " );
+        Rdn rdn2 = new Rdn( " a = c " );
+        assertTrue( rdn1.compareTo( rdn2 ) < 0 );
+        assertTrue( rdn2.compareTo( rdn1 ) > 0 );
+        assertEquals( 0, rdn1.compareTo( rdn2 ) + rdn2.compareTo( rdn1 ) );
+
+    }
+
+
+    /**
+     * Test for DIRSHARED-3.
+     * Tests that compareTo() is invertable for multi-valued RDNs with different values.
+     * 
+     * @throws InvalidNameException
+     */
+    @Test
+    public void testCompareInvertableNCS2NCSDifferentValues() throws InvalidNameException
+    {
+        Rdn rdn1 = new Rdn( " a = b + a = c " );
+        Rdn rdn2 = new Rdn( " a = b + a = y " );
+        assertTrue( rdn1.compareTo( rdn2 ) < 0 );
+        assertTrue( rdn2.compareTo( rdn1 ) > 0 );
+        assertEquals( 0, rdn1.compareTo( rdn2 ) + rdn2.compareTo( rdn1 ) );
+    }
+
+
+    /**
+     * Test for DIRSHARED-3.
+     * Tests that compareTo() is invertable for multi-valued RDNs with different types.
+     * 
+     * @throws InvalidNameException
+     */
+    @Test
+    public void testCompareInvertableNCS2NCSDifferentTypes() throws InvalidNameException
+    {
+        Rdn rdn1 = new Rdn( " a = b + c = d  " );
+        Rdn rdn2 = new Rdn( " e = f + g = h " );
+        assertTrue( rdn1.compareTo( rdn2 ) < 0 );
+        assertTrue( rdn2.compareTo( rdn1 ) > 0 );
+        assertEquals( 0, rdn1.compareTo( rdn2 ) + rdn2.compareTo( rdn1 ) );
+    }
 
+
+    /**
+     * Test for DIRSHARED-3.
+     * Tests that compareTo() is invertable for multi-valued RDNs with different order.
+     * 
+     * @throws InvalidNameException
+     */
+    @Test
+    public void testCompareInvertableNCS2NCSUnordered() throws InvalidNameException
+    {
+        Rdn rdn1 = new Rdn( " c = d + a = b " );
+        Rdn rdn2 = new Rdn( " a = b + e = f " );
+        assertTrue( rdn1.compareTo( rdn2 ) < 0 );
+        assertTrue( rdn2.compareTo( rdn1 ) > 0 );
+        assertEquals( 0, rdn1.compareTo( rdn2 ) + rdn2.compareTo( rdn1 ) );
     }
 
 



Mime
View raw message