You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by el...@apache.org on 2012/02/27 16:04:42 UTC
svn commit: r1294174 - in /directory/shared/trunk:
integ/src/test/java/org/apache/directory/shared/ldap/model/name/
ldap/model/src/main/java/org/apache/directory/shared/ldap/model/name/
ldap/model/src/test/java/org/apache/directory/shared/ldap/model/name/
Author: elecharny
Date: Mon Feb 27 15:04:42 2012
New Revision: 1294174
URL: http://svn.apache.org/viewvc?rev=1294174&view=rev
Log:
o The Ava class now implements the Comparable interface
o Added tests for the compareTo method
o Fixed some potential NPE with Schema aware operations
Modified:
directory/shared/trunk/integ/src/test/java/org/apache/directory/shared/ldap/model/name/AvaTest.java
directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/name/Ava.java
directory/shared/trunk/ldap/model/src/test/java/org/apache/directory/shared/ldap/model/name/AvaTest.java
Modified: directory/shared/trunk/integ/src/test/java/org/apache/directory/shared/ldap/model/name/AvaTest.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/integ/src/test/java/org/apache/directory/shared/ldap/model/name/AvaTest.java?rev=1294174&r1=1294173&r2=1294174&view=diff
==============================================================================
--- directory/shared/trunk/integ/src/test/java/org/apache/directory/shared/ldap/model/name/AvaTest.java (original)
+++ directory/shared/trunk/integ/src/test/java/org/apache/directory/shared/ldap/model/name/AvaTest.java Mon Feb 27 15:04:42 2012
@@ -24,7 +24,10 @@ import static org.junit.Assert.assertEqu
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
+import java.util.Arrays;
+
import org.apache.directory.shared.ldap.model.exception.LdapException;
+import org.apache.directory.shared.ldap.model.exception.LdapInvalidDnException;
import org.apache.directory.shared.ldap.model.schema.SchemaManager;
import org.apache.directory.shared.ldap.schemamanager.impl.DefaultSchemaManager;
import org.junit.BeforeClass;
@@ -179,7 +182,7 @@ public class AvaTest
* Compares two equals atavs
*/
@Test
- public void testCompareToEquals() throws LdapException
+ public void testEqualsSameAva() throws LdapException
{
Ava atav1 = new Ava( schemaManager, "cn", "b" );
Ava atav2 = new Ava( schemaManager, "cn", "b" );
@@ -192,13 +195,26 @@ public class AvaTest
* Compares two equals atavs but with a type in different case
*/
@Test
- public void testCompareToEqualsCase() throws LdapException
+ public void testEqualsUpperCaseAT() throws LdapException
{
Ava atav1 = new Ava( schemaManager, "cn", "b" );
Ava atav2 = new Ava( schemaManager, "CN", "b" );
assertTrue( atav1.equals( atav2 ) );
}
+
+
+ /**
+ * Compares two equals atavs but with a type in different case
+ */
+ @Test
+ public void testEqualsSameValues() throws LdapException
+ {
+ Ava atav1 = new Ava( schemaManager, "cn", " B a" );
+ Ava atav2 = new Ava( schemaManager, "CN", "b a" );
+
+ assertTrue( atav1.equals( atav2 ) );
+ }
/**
@@ -309,4 +325,81 @@ public class AvaTest
assertEquals( null, errors );
}
+
+
+ @Test
+ public void testCompareToSameAva() throws LdapInvalidDnException
+ {
+ Ava atav1 = new Ava( schemaManager, "cn", "b" );
+ Ava atav2 = new Ava( schemaManager, "cn", "b" );
+ Ava atav3 = new Ava( schemaManager, "commonName", "b" );
+ Ava atav4 = new Ava( schemaManager, "2.5.4.3", " B " );
+
+ // 1 with others
+ assertEquals( 0, atav1.compareTo( atav1 ) );
+ assertEquals( 0, atav1.compareTo( atav2 ) );
+ assertEquals( 0, atav1.compareTo( atav3 ) );
+ assertEquals( 0, atav1.compareTo( atav4 ) );
+
+ // 2 with others
+ assertEquals( 0, atav2.compareTo( atav1 ) );
+ assertEquals( 0, atav2.compareTo( atav2 ) );
+ assertEquals( 0, atav2.compareTo( atav3 ) );
+ assertEquals( 0, atav2.compareTo( atav4 ) );
+
+ // 3 with others
+ assertEquals( 0, atav3.compareTo( atav1 ) );
+ assertEquals( 0, atav3.compareTo( atav2 ) );
+ assertEquals( 0, atav3.compareTo( atav3 ) );
+ assertEquals( 0, atav3.compareTo( atav4 ) );
+
+ // 4 with others
+ assertEquals( 0, atav4.compareTo( atav1 ) );
+ assertEquals( 0, atav4.compareTo( atav2 ) );
+ assertEquals( 0, atav4.compareTo( atav3 ) );
+ assertEquals( 0, atav4.compareTo( atav4 ) );
+ }
+
+
+ @Test
+ public void testCompareAvaOrder() throws LdapInvalidDnException
+ {
+ Ava atav1 = new Ava( schemaManager, "cn", " B " );
+ Ava atav2 = new Ava( schemaManager, "sn", " c" );
+
+ // atav1 should be before atav2
+ assertEquals( -1, atav1.compareTo( atav2 ) );
+ assertEquals( 1, atav2.compareTo( atav1 ) );
+
+ Ava atav3 = new Ava( schemaManager, "2.5.4.3", "A " );
+
+ // Atav1 shoud be after atav3
+ assertEquals( 1, atav1.compareTo( atav3 ) );
+ assertEquals( -1, atav3.compareTo( atav1 ) );
+ }
+
+
+ @Test
+ public void testSortAva() throws LdapInvalidDnException
+ {
+ Ava atav1 = new Ava( schemaManager, "cn", " B " );
+ Ava atav2 = new Ava( schemaManager, "sn", " c" );
+ Ava atav3 = new Ava( schemaManager, "2.5.4.3", "A " );
+ Ava atav4 = new Ava( schemaManager, "2.5.4.11", " C " );
+ Ava atav5 = new Ava( schemaManager, "ou", "B " );
+ Ava atav6 = new Ava( schemaManager, "ou", "D " );
+ Ava atav7 = new Ava( schemaManager, "CN", " " );
+
+ Ava[] avas = new Ava[] { atav1, atav2, atav3, atav4, atav5, atav6, atav7 };
+
+ Arrays.sort( avas );
+
+ assertEquals( atav5, avas[0] );
+ assertEquals( atav4, avas[1] );
+ assertEquals( atav6, avas[2] );
+ assertEquals( atav7, avas[3] );
+ assertEquals( atav3, avas[4] );
+ assertEquals( atav1, avas[5] );
+ assertEquals( atav2, avas[6] );
+ }
}
Modified: directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/name/Ava.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/name/Ava.java?rev=1294174&r1=1294173&r2=1294174&view=diff
==============================================================================
--- directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/name/Ava.java (original)
+++ directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/name/Ava.java Mon Feb 27 15:04:42 2012
@@ -24,6 +24,7 @@ import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
+import java.util.Arrays;
import org.apache.directory.shared.i18n.I18n;
import org.apache.directory.shared.ldap.model.entry.BinaryValue;
@@ -34,6 +35,7 @@ import org.apache.directory.shared.ldap.
import org.apache.directory.shared.ldap.model.exception.LdapInvalidDnException;
import org.apache.directory.shared.ldap.model.message.ResultCodeEnum;
import org.apache.directory.shared.ldap.model.schema.AttributeType;
+import org.apache.directory.shared.ldap.model.schema.LdapComparator;
import org.apache.directory.shared.ldap.model.schema.MatchingRule;
import org.apache.directory.shared.ldap.model.schema.SchemaManager;
import org.apache.directory.shared.util.Strings;
@@ -54,7 +56,7 @@ import org.slf4j.LoggerFactory;
*
* @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
*/
-public class Ava implements Externalizable, Cloneable
+public class Ava implements Externalizable, Cloneable, Comparable<Ava>
{
/**
* Declares the Serial Version Uid.
@@ -153,6 +155,8 @@ public class Ava implements Externalizab
{
if ( schemaManager != null )
{
+ this.schemaManager = schemaManager;
+
try
{
attributeType = schemaManager.lookupAttributeTypeRegistry( upType );
@@ -215,6 +219,8 @@ public class Ava implements Externalizab
{
if ( schemaManager != null )
{
+ this.schemaManager = schemaManager;
+
try
{
attributeType = schemaManager.lookupAttributeTypeRegistry( upType );
@@ -904,8 +910,18 @@ public class Ava implements Externalizab
return equalityMatchingRule.getLdapComparator().compare( normValue.getValue(),
instance.normValue.getValue() ) == 0;
}
-
- return false;
+ else
+ {
+ // No Equality MR, use a direct comparison
+ if ( normValue instanceof BinaryValue )
+ {
+ return Arrays.equals( normValue.getBytes(), instance.normValue.getBytes() );
+ }
+ else
+ {
+ return normValue.getString().equals( instance.normValue.getString() );
+ }
+ }
}
else
{
@@ -1134,6 +1150,152 @@ public class Ava implements Externalizab
{
return attributeType;
}
+
+
+ private int compareValues( Ava that )
+ {
+ int comp = 0;
+
+ if ( normValue.getNormValue() instanceof String )
+ {
+ comp = ((String)normValue.getNormValue()).compareTo( ((String)that.normValue.getNormValue()) );
+
+ return comp;
+ }
+ else
+ {
+ byte[] bytes1 = (byte[])normValue.getNormValue();
+ byte[] bytes2 = (byte[])that.normValue.getNormValue();
+
+ for ( int pos = 0; pos < bytes1.length; pos++ )
+ {
+ int v1 = ( bytes1[pos] & 0x00FF );
+ int v2 = ( bytes2[pos] & 0x00FF );
+
+ if ( v1 > v2 )
+ {
+ return 1;
+ }
+ else if ( v2 > v1 )
+ {
+ return -1;
+ }
+ }
+
+ return 0;
+ }
+
+ }
+
+
+ /**
+ * @see Comparable#compareTo(Object)
+ */
+ public int compareTo( Ava that )
+ {
+ if ( that == null )
+ {
+ return 1;
+ }
+
+ int comp = 0;
+
+ if ( schemaManager == null )
+ {
+ // Compare the ATs
+ comp = normType.compareTo( that.normType );
+
+ if ( comp != 0 )
+ {
+ return comp;
+ }
+
+ // and compare the values
+ if ( normValue == null )
+ {
+ if ( that.normValue == null )
+ {
+ return 0;
+ }
+ else
+ {
+ return -1;
+ }
+ }
+ else
+ {
+ if ( that.normValue == null )
+ {
+ return 1;
+ }
+ else
+ {
+ if ( normValue instanceof StringValue )
+ {
+ comp = ((StringValue)normValue).compareTo( (StringValue)that.normValue );
+
+ return comp;
+ }
+ else
+ {
+ comp = ((BinaryValue)normValue).compareTo( (BinaryValue)that.normValue );
+
+ return comp;
+ }
+ }
+ }
+ }
+ else
+ {
+ if ( that.schemaManager == null )
+ {
+ // Problem : we will apply the current Ava SchemaManager to the given Ava
+ try
+ {
+ that.apply( schemaManager );
+ }
+ catch ( LdapInvalidDnException lide )
+ {
+ return 1;
+ }
+ }
+
+ // First compare the AT OID
+ comp = attributeType.getOid().compareTo( that.attributeType.getOid() );
+
+ if ( comp != 0 )
+ {
+ return comp;
+ }
+
+ // Now, compare the two values using the ordering matchingRule comparator, if any
+ MatchingRule orderingMR = attributeType.getOrdering();
+
+ if ( orderingMR != null )
+ {
+ LdapComparator<Object> comparator = (LdapComparator<Object>)orderingMR.getLdapComparator();
+
+ if ( comparator != null )
+ {
+ comp = comparator.compare( normValue.getNormValue(), that.normValue.getNormValue() );
+
+ return comp;
+ }
+ else
+ {
+ comp = compareValues( that );
+
+ return comp;
+ }
+ }
+ else
+ {
+ comp = compareValues( that );
+
+ return comp;
+ }
+ }
+ }
/**
Modified: directory/shared/trunk/ldap/model/src/test/java/org/apache/directory/shared/ldap/model/name/AvaTest.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/model/src/test/java/org/apache/directory/shared/ldap/model/name/AvaTest.java?rev=1294174&r1=1294173&r2=1294174&view=diff
==============================================================================
--- directory/shared/trunk/ldap/model/src/test/java/org/apache/directory/shared/ldap/model/name/AvaTest.java (original)
+++ directory/shared/trunk/ldap/model/src/test/java/org/apache/directory/shared/ldap/model/name/AvaTest.java Mon Feb 27 15:04:42 2012
@@ -159,7 +159,7 @@ public class AvaTest
* Compares two equals atavs
*/
@Test
- public void testCompareToEquals() throws LdapException
+ public void testEqualsAttributeEquals() throws LdapException
{
Ava atav1 = new Ava( schemaManager, "a", "b" );
Ava atav2 = new Ava( schemaManager, "a", "b" );
@@ -172,7 +172,7 @@ public class AvaTest
* Compares two equals atavs but with a type in different case
*/
@Test
- public void testCompareToEqualsCase() throws LdapException
+ public void testEqualsAttributeIdSameCase() throws LdapException
{
Ava atav1 = new Ava( schemaManager, "a", "b" );
Ava atav2 = new Ava( schemaManager, "A", "b" );
@@ -186,7 +186,7 @@ public class AvaTest
* superior
*/
@Test
- public void testCompareAtav1TypeSuperior() throws LdapException
+ public void testEqualsAtav1TypeSuperior() throws LdapException
{
Ava atav1 = new Ava( schemaManager, "b", "b" );
@@ -201,7 +201,7 @@ public class AvaTest
* superior
*/
@Test
- public void testCompareAtav2TypeSuperior() throws LdapException
+ public void testEqualsAtav2TypeSuperior() throws LdapException
{
Ava atav1 = new Ava( schemaManager, "a", "b" );
Ava atav2 = new Ava( schemaManager, "b", "b" );
@@ -215,7 +215,7 @@ public class AvaTest
* superior
*/
@Test
- public void testCompareAtav1ValueSuperior() throws LdapException
+ public void testEqualsAtav1ValueSuperior() throws LdapException
{
Ava atav1 = new Ava( schemaManager, "a", "b" );
Ava atav2 = new Ava( schemaManager, "a", "a" );
@@ -229,7 +229,7 @@ public class AvaTest
* superior
*/
@Test
- public void testCompareAtav2ValueSuperior() throws LdapException
+ public void testEqualsAtav2ValueSuperior() throws LdapException
{
Ava atav1 = new Ava( schemaManager, "a", "a" );
Ava atav2 = new Ava( schemaManager, "a", "b" );