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 2005/12/25 20:25:42 UTC
svn commit: r359004 -
/directory/shared/ldap/branches/DN-refactoring/common/src/test/org/apache/ldap/common/name/RdnTest.java
Author: elecharny
Date: Sun Dec 25 11:25:37 2005
New Revision: 359004
URL: http://svn.apache.org/viewcvs?rev=359004&view=rev
Log:
- Added many tests for the new version of Rdn
- Renamed from RDNTest to RdnTest
Added:
directory/shared/ldap/branches/DN-refactoring/common/src/test/org/apache/ldap/common/name/RdnTest.java
- copied, changed from r357849, directory/shared/ldap/branches/DN-refactoring/common/src/test/org/apache/ldap/common/name/LdapRDNTest.java
Copied: directory/shared/ldap/branches/DN-refactoring/common/src/test/org/apache/ldap/common/name/RdnTest.java (from r357849, directory/shared/ldap/branches/DN-refactoring/common/src/test/org/apache/ldap/common/name/LdapRDNTest.java)
URL: http://svn.apache.org/viewcvs/directory/shared/ldap/branches/DN-refactoring/common/src/test/org/apache/ldap/common/name/RdnTest.java?p2=directory/shared/ldap/branches/DN-refactoring/common/src/test/org/apache/ldap/common/name/RdnTest.java&p1=directory/shared/ldap/branches/DN-refactoring/common/src/test/org/apache/ldap/common/name/LdapRDNTest.java&r1=357849&r2=359004&rev=359004&view=diff
==============================================================================
--- directory/shared/ldap/branches/DN-refactoring/common/src/test/org/apache/ldap/common/name/LdapRDNTest.java (original)
+++ directory/shared/ldap/branches/DN-refactoring/common/src/test/org/apache/ldap/common/name/RdnTest.java Sun Dec 25 11:25:37 2005
@@ -17,19 +17,23 @@
package org.apache.ldap.common.name;
import javax.naming.InvalidNameException;
+import javax.naming.NamingException;
+import javax.naming.directory.Attribute;
+import javax.naming.directory.Attributes;
import junit.framework.Assert;
import junit.framework.TestCase;
-import org.apache.ldap.common.name.LdapRDN;
-import org.apache.ldap.common.name.RDNParser;
+import org.apache.asn1.codec.util.StringUtils;
+import org.apache.ldap.common.name.Rdn;
+import org.apache.ldap.common.name.RdnParser;
/**
- * Test the class LdapRDN
+ * Test the class Rdn
*
* @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
*/
-public class LdapRDNTest extends TestCase
+public class RdnTest extends TestCase
{
//~ Methods ------------------------------------------------------------------------------------
@@ -43,73 +47,73 @@
/**
* Test a null RDN
*/
- public void testLdapRDNNull() throws InvalidNameException
+ public void testRdnNull() throws InvalidNameException
{
- Assert.assertEquals( "", new LdapRDN().toString() );
+ Assert.assertEquals( "", new Rdn().toString() );
}
/**
* test an empty RDN
*/
- public void testLdapRDNEmpty() throws InvalidNameException
+ public void testRdnEmpty() throws InvalidNameException
{
- Assert.assertEquals( "", new LdapRDN( "" ).toString() );
+ Assert.assertEquals( "", new Rdn( "" ).toString() );
}
/**
* test a simple RDN : a = b
*/
- public void testLdapRDNSimple() throws InvalidNameException
+ public void testRdnSimple() throws InvalidNameException
{
- Assert.assertEquals( "a=b", new LdapRDN( "a = b" ).toString() );
+ Assert.assertEquals( "a=b", new Rdn( "a = b" ).toString() );
}
/**
* test a composite RDN : a = b, d = e
*/
- public void testLdapRDNComposite() throws InvalidNameException
+ public void testRdnComposite() throws InvalidNameException
{
- Assert.assertEquals( "a=b+c=d", new LdapRDN( "a = b + c = d" ).toString() );
+ Assert.assertEquals( "a=b+c=d", new Rdn( "a = b + c = d" ).toString() );
}
/**
* test a composite RDN with or without spaces: a=b, a =b, a= b, a = b, a = b
*/
- public void testLdapRDNCompositeWithSpace() throws InvalidNameException
+ public void testRdnCompositeWithSpace() throws InvalidNameException
{
- Assert.assertEquals( "a=b", new LdapRDN( "a=b" ).toString() );
- Assert.assertEquals( "a=b", new LdapRDN( " a=b" ).toString() );
- Assert.assertEquals( "a=b", new LdapRDN( "a =b" ).toString() );
- Assert.assertEquals( "a=b", new LdapRDN( "a= b" ).toString() );
- Assert.assertEquals( "a=b", new LdapRDN( "a=b " ).toString() );
- Assert.assertEquals( "a=b", new LdapRDN( " a =b" ).toString() );
- Assert.assertEquals( "a=b", new LdapRDN( " a= b" ).toString() );
- Assert.assertEquals( "a=b", new LdapRDN( " a=b " ).toString() );
- Assert.assertEquals( "a=b", new LdapRDN( "a = b" ).toString() );
- Assert.assertEquals( "a=b", new LdapRDN( "a =b " ).toString() );
- Assert.assertEquals( "a=b", new LdapRDN( "a= b " ).toString() );
- Assert.assertEquals( "a=b", new LdapRDN( " a = b" ).toString() );
- Assert.assertEquals( "a=b", new LdapRDN( " a =b " ).toString() );
- Assert.assertEquals( "a=b", new LdapRDN( " a= b " ).toString() );
- Assert.assertEquals( "a=b", new LdapRDN( "a = b " ).toString() );
- Assert.assertEquals( "a=b", new LdapRDN( " a = b " ).toString() );
+ Assert.assertEquals( "a=b", new Rdn( "a=b" ).toString() );
+ Assert.assertEquals( "a=b", new Rdn( " a=b" ).toString() );
+ Assert.assertEquals( "a=b", new Rdn( "a =b" ).toString() );
+ Assert.assertEquals( "a=b", new Rdn( "a= b" ).toString() );
+ Assert.assertEquals( "a=b", new Rdn( "a=b " ).toString() );
+ Assert.assertEquals( "a=b", new Rdn( " a =b" ).toString() );
+ Assert.assertEquals( "a=b", new Rdn( " a= b" ).toString() );
+ Assert.assertEquals( "a=b", new Rdn( " a=b " ).toString() );
+ Assert.assertEquals( "a=b", new Rdn( "a = b" ).toString() );
+ Assert.assertEquals( "a=b", new Rdn( "a =b " ).toString() );
+ Assert.assertEquals( "a=b", new Rdn( "a= b " ).toString() );
+ Assert.assertEquals( "a=b", new Rdn( " a = b" ).toString() );
+ Assert.assertEquals( "a=b", new Rdn( " a =b " ).toString() );
+ Assert.assertEquals( "a=b", new Rdn( " a= b " ).toString() );
+ Assert.assertEquals( "a=b", new Rdn( "a = b " ).toString() );
+ Assert.assertEquals( "a=b", new Rdn( " a = b " ).toString() );
}
/**
* test a simple RDN with differents separators : a = b + c = d
*/
- public void testLdapRDNSimpleMultivaluedAttribute() throws InvalidNameException
+ public void testRdnSimpleMultivaluedAttribute() throws InvalidNameException
{
- String result = new LdapRDN( "a = b + c = d" ).toString();
+ String result = new Rdn( "a = b + c = d" ).toString();
Assert.assertEquals( "a=b+c=d", result );
}
/**
* test a composite RDN with differents separators : a=b+c=d, e=f + g=h + i=j
*/
- public void testLdapRDNCompositeMultivaluedAttribute() throws InvalidNameException
+ public void testRdnCompositeMultivaluedAttribute() throws InvalidNameException
{
- LdapRDN rdn = new LdapRDN( "a =b+c=d + e=f + g =h + i =j " );
+ Rdn rdn = new Rdn( "a =b+c=d + e=f + g =h + i =j " );
// NameComponent are not ordered
Assert.assertEquals( "b", rdn.getValue("a") );
@@ -122,64 +126,64 @@
/**
* test a simple RDN with an oid prefix (uppercase) : OID.12.34.56 = azerty
*/
- public void testLdapRDNOidUpper() throws InvalidNameException
+ public void testRdnOidUpper() throws InvalidNameException
{
Assert.assertEquals( "oid.12.34.56=azerty",
- new LdapRDN( "OID.12.34.56 = azerty" ).toString() );
+ new Rdn( "OID.12.34.56 = azerty" ).toString() );
}
/**
* test a simple RDN with an oid prefix (lowercase) : oid.12.34.56 = azerty
*/
- public void testLdapRDNOidLower() throws InvalidNameException
+ public void testRdnOidLower() throws InvalidNameException
{
Assert.assertEquals( "oid.12.34.56=azerty",
- new LdapRDN( "oid.12.34.56 = azerty" ).toString() );
+ new Rdn( "oid.12.34.56 = azerty" ).toString() );
}
/**
* test a simple RDN with an oid attribut wiithout oid prefix : 12.34.56 = azerty
*/
- public void testLdapRDNOidWithoutPrefix() throws InvalidNameException
+ public void testRdnOidWithoutPrefix() throws InvalidNameException
{
Assert.assertEquals( "12.34.56=azerty",
- new LdapRDN( "12.34.56 = azerty" ).toString() );
+ new Rdn( "12.34.56 = azerty" ).toString() );
}
/**
* test a composite RDN with an oid attribut wiithout oid prefix : 12.34.56 = azerty; 7.8 = test
*/
- public void testLdapRDNCompositeOidWithoutPrefix() throws InvalidNameException
+ public void testRdnCompositeOidWithoutPrefix() throws InvalidNameException
{
- String result = new LdapRDN( "12.34.56 = azerty + 7.8 = test" ).toString();
+ String result = new Rdn( "12.34.56 = azerty + 7.8 = test" ).toString();
Assert.assertEquals( "12.34.56=azerty+7.8=test", result );
}
/**
* test a simple RDN with pair char attribute value : a = \,\=\+\<\>\#\;\\\"\A0\00"
*/
- public void testLdapRDNPairCharAttributeValue() throws InvalidNameException
+ public void testRdnPairCharAttributeValue() throws InvalidNameException
{
Assert.assertEquals( "a=\\,\\=\\+\\<\\>\\#\\;\\\\\\\"\\A0\\00",
- new LdapRDN( "a = \\,\\=\\+\\<\\>\\#\\;\\\\\\\"\\A0\\00" ).toString() );
+ new Rdn( "a = \\,\\=\\+\\<\\>\\#\\;\\\\\\\"\\A0\\00" ).toString() );
}
/**
* test a simple RDN with hexString attribute value : a = #0010A0AAFF
*/
- public void testLdapRDNHexStringAttributeValue() throws InvalidNameException
+ public void testRdnHexStringAttributeValue() throws InvalidNameException
{
Assert.assertEquals( "a=#0010A0AAFF",
- new LdapRDN( "a = #0010A0AAFF" ).toString() );
+ new Rdn( "a = #0010A0AAFF" ).toString() );
}
/**
* test a simple RDN with quoted attribute value : a = "quoted \"value"
*/
- public void testLdapRDNQuotedAttributeValue() throws InvalidNameException
+ public void testRdnQuotedAttributeValue() throws InvalidNameException
{
Assert.assertEquals( "a=quoted \\\"value",
- new LdapRDN( "a = quoted \\\"value" ).toString() );
+ new Rdn( "a = quoted \\\"value" ).toString() );
}
/**
@@ -187,13 +191,11 @@
*/
public void testRDNCloningOneNameComponent() throws InvalidNameException
{
- LdapRDN rdn = new LdapRDN( "a", "b" );
+ Rdn rdn = new Rdn( "a", "b" );
- LdapRDN rdnClone = (LdapRDN)rdn.clone();
+ Rdn rdnClone = (Rdn)rdn.clone();
- rdn.removeAttributeTypeAndValue( "a" );
-
- RDNParser.parse( "c=d", rdn );
+ RdnParser.parse( "c=d", rdn );
Assert.assertEquals( "b" , rdnClone.getValue( "a" ) );
}
@@ -203,14 +205,12 @@
*/
public void testRDNCloningTwoNameComponent() throws InvalidNameException
{
- LdapRDN rdn = new LdapRDN( "a", "b" );
- rdn.addAttributeTypeAndValue( "aa", "bb" );
+ Rdn rdn = new Rdn( "a = b + aa = bb" );
- LdapRDN rdnClone = (LdapRDN)rdn.clone();
+ Rdn rdnClone = (Rdn)rdn.clone();
- rdn.removeAttributeTypeAndValue( "aa" );
rdn.clear();
- RDNParser.parse( "c=d", rdn );
+ RdnParser.parse( "c=d", rdn );
Assert.assertEquals( "b" , rdnClone.getValue( "a" ) );
Assert.assertEquals( "bb" , rdnClone.getValue( "aa" ) );
@@ -218,23 +218,13 @@
}
/**
- * Test the clone method for a RDN.
- */
- public void testRDNRemoveNameComponent() throws InvalidNameException
- {
- LdapRDN rdn = new LdapRDN( " a = b + c = d + a = f + g = h " );
- rdn.removeAttributeTypeAndValue( "a" );
- Assert.assertEquals( "c=d+g=h", rdn.toString() );
- }
-
- /**
* Test the compareTo method for a RDN.
*/
public void testRDNCompareToNull() throws InvalidNameException
{
- LdapRDN rdn1 = new LdapRDN( " a = b + c = d + a = f + g = h " );
- LdapRDN rdn2 = null;
- Assert.assertEquals( LdapRDN.NOT_EQUALS, rdn1.compareTo( rdn2 ) );
+ Rdn rdn1 = new Rdn( " a = b + c = d + a = f + g = h " );
+ Rdn rdn2 = null;
+ Assert.assertEquals( 1, rdn1.compareTo( rdn2 ) );
}
/**
@@ -242,8 +232,8 @@
*/
public void testRDNCompareToNCS2NC() throws InvalidNameException
{
- LdapRDN rdn1 = new LdapRDN( " a = b + c = d + a = f + g = h " );
- LdapRDN rdn2 = new LdapRDN( " a = b " );
+ Rdn rdn1 = new Rdn( " a = b + c = d + a = f + g = h " );
+ Rdn rdn2 = new Rdn( " a = b " );
Assert.assertTrue( rdn1.compareTo( rdn2 ) > 0);
}
@@ -252,8 +242,8 @@
*/
public void testRDNCompareToNC2NCS() throws InvalidNameException
{
- LdapRDN rdn1 = new LdapRDN( " a = b " );
- LdapRDN rdn2 = new LdapRDN( " a = b + c = d + a = f + g = h " );
+ Rdn rdn1 = new Rdn( " a = b " );
+ Rdn rdn2 = new Rdn( " a = b + c = d + a = f + g = h " );
Assert.assertTrue( rdn1.compareTo( rdn2 ) < 0 );
}
@@ -263,10 +253,10 @@
*/
public void testRDNCompareToNCS2NCSOrdered() throws InvalidNameException
{
- LdapRDN rdn1 = new LdapRDN( " a = b + c = d + a = f + g = h " );
- LdapRDN rdn2 = new LdapRDN( " a = b + c = d + a = f + g = h " );
+ Rdn rdn1 = new Rdn( " a = b + c = d + a = f + g = h " );
+ Rdn rdn2 = new Rdn( " a = b + c = d + a = f + g = h " );
- Assert.assertEquals( LdapRDN.EQUALS, rdn1.compareTo( rdn2 ) );
+ Assert.assertEquals( 0, rdn1.compareTo( rdn2 ) );
}
/**
@@ -274,10 +264,10 @@
*/
public void testRDNCompareToNCS2NCSUnordered() throws InvalidNameException
{
- LdapRDN rdn1 = new LdapRDN( " a = b + a = f + g = h + c = d " );
- LdapRDN rdn2 = new LdapRDN( " a = b + c = d + a = f + g = h " );
+ Rdn rdn1 = new Rdn( " a = b + a = f + g = h + c = d " );
+ Rdn rdn2 = new Rdn( " a = b + c = d + a = f + g = h " );
- Assert.assertEquals( LdapRDN.EQUALS, rdn1.compareTo( rdn2 ) );
+ Assert.assertEquals( 0, rdn1.compareTo( rdn2 ) );
}
/**
@@ -285,10 +275,10 @@
*/
public void testRDNCompareToNCS2NCSNotEquals() throws InvalidNameException
{
- LdapRDN rdn1 = new LdapRDN( " a = f + g = h + c = d " );
- LdapRDN rdn2 = new LdapRDN( " c = d + a = h + g = h " );
+ Rdn rdn1 = new Rdn( " a = f + g = h + c = d " );
+ Rdn rdn2 = new Rdn( " c = d + a = h + g = h " );
- Assert.assertEquals( LdapRDN.NOT_EQUALS, rdn1.compareTo( rdn2 ) );
+ Assert.assertEquals( -1, rdn1.compareTo( rdn2 ) );
}
/**
@@ -296,10 +286,10 @@
*/
public void testRDNCompareToNC2NC() throws InvalidNameException
{
- LdapRDN rdn1 = new LdapRDN( " a = b " );
- LdapRDN rdn2 = new LdapRDN( " a = b " );
+ Rdn rdn1 = new Rdn( " a = b " );
+ Rdn rdn2 = new Rdn( " a = b " );
- Assert.assertEquals( LdapRDN.EQUALS, rdn1.compareTo( rdn2 ) );
+ Assert.assertEquals( 0, rdn1.compareTo( rdn2 ) );
}
/**
@@ -307,10 +297,11 @@
*/
public void testRDNCompareToNC2NCUperCase() throws InvalidNameException
{
- LdapRDN rdn1 = new LdapRDN( " a = b " );
- LdapRDN rdn2 = new LdapRDN( " A = b " );
+ Rdn rdn1 = new Rdn( " a = b " );
+ Rdn rdn2 = new Rdn( " A = b " );
- Assert.assertEquals( LdapRDN.EQUALS, rdn1.compareTo( rdn2 ) );
+ Assert.assertEquals( 0, rdn1.compareTo( rdn2 ) );
+ Assert.assertEquals( true, rdn1.equals( rdn2 ) );
}
/**
@@ -318,10 +309,143 @@
*/
public void testRDNCompareToNC2NCNotEquals() throws InvalidNameException
{
- LdapRDN rdn1 = new LdapRDN( " a = b " );
- LdapRDN rdn2 = new LdapRDN( " A = d " );
+ Rdn rdn1 = new Rdn( " a = b " );
+ Rdn rdn2 = new Rdn( " A = d " );
Assert.assertTrue( rdn1.compareTo( rdn2 ) < 0 );
}
-}
+
+ public void testToAttributes() throws InvalidNameException, NamingException
+ {
+ Rdn rdn = new Rdn( " a = b + a = f + g = h + c = d " );
+
+ Attributes attributes = rdn.toAttributes();
+
+ Assert.assertNotNull( attributes.get( "a" ) );
+ Assert.assertNotNull( attributes.get( "g" ) );
+ Assert.assertNotNull( attributes.get( "c" ) );
+
+ Attribute attribute = attributes.get( "a" );
+
+ Assert.assertNotNull( attribute.get( 0 ) );
+ Assert.assertEquals( "b", attribute.get( 0 ) );
+
+ Assert.assertNotNull( attribute.get( 1 ) );
+ Assert.assertEquals( "f", attribute.get( 1 ) );
+
+ attribute = attributes.get( "g" );
+ Assert.assertNotNull( attribute.get( 0 ) );
+ Assert.assertEquals( "h", attribute.get( 0 ) );
+
+ attribute = attributes.get( "c" );
+ Assert.assertNotNull( attribute.get( 0 ) );
+ Assert.assertEquals( "d", attribute.get( 0 ) );
+ }
+
+ public void testGetValue() throws InvalidNameException
+ {
+ Rdn rdn = new Rdn( " a = b + a = f + g = h + c = d " );
+
+ Assert.assertEquals( "b", rdn.getValue() );
+ }
+
+ public void testGetType() throws InvalidNameException
+ {
+ Rdn rdn = new Rdn( " a = b + a = f + g = h + c = d " );
+
+ Assert.assertEquals( "a", rdn.getType() );
+ }
+
+ public void testGetSize() throws InvalidNameException
+ {
+ Rdn rdn = new Rdn( " a = b + a = f + g = h + c = d " );
+
+ Assert.assertEquals( 4, rdn.size() );
+ }
+
+ public void testGetSize0() throws InvalidNameException
+ {
+ Rdn rdn = new Rdn();
+
+ Assert.assertEquals( 0, rdn.size() );
+ }
+
+ public void testEquals() throws InvalidNameException
+ {
+ Rdn rdn = new Rdn( "a=b + c=d + a=f" );
+
+ Assert.assertFalse( rdn.equals( null ) );
+ Assert.assertFalse( rdn.equals( new String( "test" ) ) );
+ Assert.assertFalse( rdn.equals( new Rdn( "a=c + c=d + a=f" ) ) );
+ Assert.assertFalse( rdn.equals( new Rdn( "a=b" ) ) );
+ Assert.assertTrue( rdn.equals( new Rdn( "a=b + c=d + a=f" ) ) );
+ Assert.assertTrue( rdn.equals( new Rdn( "a=b + C=d + A=f" ) ) );
+ Assert.assertTrue( rdn.equals( new Rdn( "c=d + a=f + a=b" ) ) );
+ }
+ public void testUnescapeValueHexa() throws InvalidNameException
+ {
+ byte[] res = (byte[])Rdn.unescapeValue( "#fF" );
+
+ Assert.assertEquals( "0xFF ", StringUtils.dumpBytes( res ) );
+
+ res = (byte[])Rdn.unescapeValue( "#0123456789aBCDEF" );
+ Assert.assertEquals( "0x01 0x23 0x45 0x67 0x89 0xAB 0xCD 0xEF ", StringUtils.dumpBytes( res ) );
+ }
+
+ public void testUnescapeValueHexaWrong() throws InvalidNameException
+ {
+ try
+ {
+ Rdn.unescapeValue( "#fF1" );
+ Assert.fail(); // Should not happen
+ }
+ catch ( IllegalArgumentException iae )
+ {
+ Assert.assertTrue( true );
+ }
+ }
+
+ public void testUnescapeValueString() throws InvalidNameException
+ {
+ String res = (String)Rdn.unescapeValue( "azerty" );
+
+ Assert.assertEquals( "azerty", res );
+ }
+
+ public void testUnescapeValueStringSpecial() throws InvalidNameException
+ {
+ String res = (String)Rdn.unescapeValue( "\\\\\\#\\,\\+\\;\\<\\>\\=\\\"\\ " );
+
+ Assert.assertEquals( "\\#,+;<>=\" ", res );
+ }
+
+ public void testEscapeValueString() throws InvalidNameException
+ {
+ String res = Rdn.escapeValue( StringUtils.getBytesUtf8( "azerty" ) );
+
+ Assert.assertEquals( "azerty", res );
+ }
+
+ public void testEscapeValueStringSpecial() throws InvalidNameException
+ {
+ String res = Rdn.escapeValue( StringUtils.getBytesUtf8( "\\#,+;<>=\" " ) );
+
+ Assert.assertEquals( "\\\\\\#\\,\\+\\;\\<\\>\\=\\\"\\ ", res );
+ }
+
+ public void testEscapeValueNumeric() throws InvalidNameException
+ {
+ String res = Rdn.escapeValue( new byte[]{ '-', 0x00, '-', 0x1F, '-', 0x7F, '-' } );
+
+ Assert.assertEquals( "-\\00-\\1F-\\7F-", res );
+ }
+
+ public void testEscapeValueMix() throws InvalidNameException
+ {
+ String res = Rdn.escapeValue( new byte[]{ '\\', 0x00, '-', '+', '#', 0x7F, '-' } );
+
+ Assert.assertEquals( "\\\\\\00-\\+\\#\\7F-", res );
+ }
+
+}