package org.apache.ldap.common.name; import java.util.Enumeration; import javax.naming.Name; import javax.naming.NamingException; import junit.framework.TestCase; /** * Test the compliance of LdapName against javax.naming.ldap.LdapName in JDK 1.5. * All the 1.5 results are asserted as strings in this version to avoid compiling * against 1.5. */ public class NameTest extends TestCase { public static void main(String[] args) { junit.textui.TestRunner.run(NameTest.class); } public void testName() throws NamingException { // JAVA 1.5 Name jName = new javax.naming.ldap.LdapName("cn=four,cn=three,cn=two,cn=one"); Name aName = new org.apache.ldap.common.name.LdapName("cn=four,cn=three,cn=two,cn=one"); // JAVA 1.5 assertEquals("cn=four,cn=three,cn=two,cn=one", jName.toString()); assertEquals("cn=four,cn=three,cn=two,cn=one", aName.toString()); // JAVA 1.5 assertEquals(jName.toString(), aName.toString()); } public void testGetPrefix() throws NamingException { // JAVA 1.5 Name jName = new javax.naming.ldap.LdapName("cn=four,cn=three,cn=two,cn=one"); Name aName = new org.apache.ldap.common.name.LdapName("cn=four,cn=three,cn=two,cn=one"); // JAVA 1.5 assertEquals(jName.getPrefix(0).toString(), aName.getPrefix(0).toString()); // JAVA 1.5 assertEquals(jName.getPrefix(1).toString(), aName.getPrefix(1).toString()); // JAVA 1.5 assertEquals(jName.getPrefix(2).toString(), aName.getPrefix(2).toString()); // JAVA 1.5 assertEquals(jName.getPrefix(3).toString(), aName.getPrefix(3).toString()); // JAVA 1.5 assertEquals(jName.getPrefix(4).toString(), aName.getPrefix(4).toString()); assertEquals("", aName.getPrefix(0).toString()); assertEquals("cn=one", aName.getPrefix(1).toString()); assertEquals("cn=two,cn=one", aName.getPrefix(2).toString()); assertEquals("cn=three,cn=two,cn=one", aName.getPrefix(3).toString()); assertEquals("cn=four,cn=three,cn=two,cn=one", aName.getPrefix(4).toString()); } public void testGetSuffix() throws NamingException { // JAVA 1.5 Name jName = new javax.naming.ldap.LdapName("cn=four,cn=three,cn=two,cn=one"); Name aName = new org.apache.ldap.common.name.LdapName("cn=four,cn=three,cn=two,cn=one"); // JAVA 1.5 assertEquals(jName.getSuffix(0).toString(), aName.getSuffix(0).toString()); // JAVA 1.5 assertEquals(jName.getSuffix(1).toString(), aName.getSuffix(1).toString()); // JAVA 1.5 assertEquals(jName.getSuffix(2).toString(), aName.getSuffix(2).toString()); // JAVA 1.5 assertEquals(jName.getSuffix(3).toString(), aName.getSuffix(3).toString()); // JAVA 1.5 assertEquals(jName.getSuffix(4).toString(), aName.getSuffix(4).toString()); assertEquals("cn=four,cn=three,cn=two,cn=one", aName.getSuffix(0).toString()); assertEquals("cn=four,cn=three,cn=two", aName.getSuffix(1).toString()); assertEquals("cn=four,cn=three", aName.getSuffix(2).toString()); assertEquals("cn=four", aName.getSuffix(3).toString()); assertEquals("", aName.getSuffix(4).toString()); } public void testAddString() throws NamingException { // JAVA 1.5 Name jName = new javax.naming.ldap.LdapName("cn=four,cn=three,cn=two,cn=one"); Name aName = new org.apache.ldap.common.name.LdapName("cn=four,cn=three,cn=two,cn=one"); // JAVA 1.5 assertSame(jName, jName.add("cn=five")); assertSame(aName, aName.add("cn=five")); // JAVA 1.5 assertEquals(jName.toString(), aName.toString()); assertEquals("cn=five,cn=four,cn=three,cn=two,cn=one", aName.toString()); } public void testAddIntString() throws NamingException { // JAVA 1.5 Name jName = new javax.naming.ldap.LdapName("cn=four,cn=three,cn=two,cn=one"); Name aName = new org.apache.ldap.common.name.LdapName("cn=four,cn=three,cn=two,cn=one"); // JAVA 1.5 assertSame(jName, jName.add(0,"cn=zero")); assertSame(aName, aName.add(0,"cn=zero")); // JAVA 1.5 assertEquals(jName.toString(), aName.toString()); assertEquals("cn=four,cn=three,cn=two,cn=one,cn=zero", aName.toString()); // JAVA 1.5 assertSame(jName, jName.add(2,"cn=one.5")); assertSame(aName, aName.add(2,"cn=one.5")); // JAVA 1.5 assertEquals(jName.toString(), aName.toString()); assertEquals("cn=four,cn=three,cn=two,cn=one.5,cn=one,cn=zero", aName.toString()); // JAVA 1.5 assertSame(jName, jName.add(jName.size(),"cn=five")); assertSame(aName, aName.add(aName.size(),"cn=five")); // JAVA 1.5 assertEquals(jName.toString(), aName.toString()); assertEquals("cn=five,cn=four,cn=three,cn=two,cn=one.5,cn=one,cn=zero", aName.toString()); } public void testAddAllName() throws NamingException { // JAVA 1.5 Name jName = new javax.naming.ldap.LdapName("cn=four,cn=three,cn=two,cn=one"); Name aName = new org.apache.ldap.common.name.LdapName("cn=four,cn=three,cn=two,cn=one"); // JAVA 1.5 assertSame(jName, jName.addAll(new javax.naming.ldap.LdapName("cn=seven,cn=six"))); assertSame(aName, aName.addAll(new org.apache.ldap.common.name.LdapName("cn=seven,cn=six"))); // JAVA 1.5 assertEquals(jName.toString(), aName.toString()); assertEquals("cn=seven,cn=six,cn=four,cn=three,cn=two,cn=one", aName.toString()); } public void testAddAllIntName() throws NamingException { // JAVA 1.5 Name jName = new javax.naming.ldap.LdapName("cn=four,cn=three,cn=two,cn=one"); Name aName = new org.apache.ldap.common.name.LdapName("cn=four,cn=three,cn=two,cn=one"); // JAVA 1.5 assertSame(jName, jName.addAll(0, new javax.naming.ldap.LdapName("cn=zero.5,cn=zero"))); assertSame(aName, aName.addAll(0, new org.apache.ldap.common.name.LdapName("cn=zero.5,cn=zero"))); // JAVA 1.5 assertEquals(jName.toString(), aName.toString()); assertEquals("cn=four,cn=three,cn=two,cn=one,cn=zero.5,cn=zero", aName.toString()); // JAVA 1.5 assertSame(jName, jName.addAll(2, new javax.naming.ldap.LdapName("cn=zero.7,cn=zero.6"))); assertSame(aName, aName.addAll(2, new org.apache.ldap.common.name.LdapName("cn=zero.7,cn=zero.6"))); // JAVA 1.5 assertEquals(jName.toString(), aName.toString()); assertEquals("cn=four,cn=three,cn=two,cn=one,cn=zero.7,cn=zero.6,cn=zero.5,cn=zero", aName.toString()); // JAVA 1.5 assertSame(jName, jName.addAll(jName.size(), new javax.naming.ldap.LdapName("cn=six,cn=five"))); assertSame(aName, aName.addAll(aName.size(), new org.apache.ldap.common.name.LdapName("cn=six,cn=five"))); // JAVA 1.5 assertEquals(jName.toString(), aName.toString()); assertEquals("cn=six,cn=five,cn=four,cn=three,cn=two,cn=one,cn=zero.7,cn=zero.6,cn=zero.5,cn=zero", aName.toString()); } public void testStartsWithName() throws NamingException { // JAVA 1.5 Name jName = new javax.naming.ldap.LdapName("cn=seven,cn=six,cn=five,cn=four,cn=three,cn=two,cn=one"); Name aName = new org.apache.ldap.common.name.LdapName("cn=seven,cn=six,cn=five,cn=four,cn=three,cn=two,cn=one"); // JAVA 1.5 assertEquals(jName.startsWith(new javax.naming.ldap.LdapName("cn=seven,cn=six,cn=five")), // JAVA 1.5 aName.startsWith(new org.apache.ldap.common.name.LdapName("cn=seven,cn=six,cn=five"))); // JAVA 1.5 assertEquals(jName.startsWith(new javax.naming.ldap.LdapName("cn=three,cn=two,cn=one")), // JAVA 1.5 aName.startsWith(new org.apache.ldap.common.name.LdapName("cn=three,cn=two,cn=one"))); assertFalse(aName.startsWith(new org.apache.ldap.common.name.LdapName("cn=seven,cn=six,cn=five"))); assertTrue(aName.startsWith(new org.apache.ldap.common.name.LdapName("cn=three,cn=two,cn=one"))); } public void testEndsWithName() throws NamingException { // JAVA 1.5 Name jName = new javax.naming.ldap.LdapName("cn=seven,cn=six,cn=five,cn=four,cn=three,cn=two,cn=one"); Name aName = new org.apache.ldap.common.name.LdapName("cn=seven,cn=six,cn=five,cn=four,cn=three,cn=two,cn=one"); // JAVA 1.5 assertEquals(jName.endsWith(new javax.naming.ldap.LdapName("cn=seven,cn=six,cn=five")), // JAVA 1.5 aName.endsWith(new org.apache.ldap.common.name.LdapName("cn=seven,cn=six,cn=five"))); // JAVA 1.5 assertEquals(jName.endsWith(new javax.naming.ldap.LdapName("cn=three,cn=two,cn=one")), // JAVA 1.5 aName.endsWith(new org.apache.ldap.common.name.LdapName("cn=three,cn=two,cn=one"))); assertTrue(aName.endsWith(new org.apache.ldap.common.name.LdapName("cn=seven,cn=six,cn=five"))); assertFalse(aName.endsWith(new org.apache.ldap.common.name.LdapName("cn=three,cn=two,cn=one"))); } public void testRemove() throws NamingException { // JAVA 1.5 Name jName = new javax.naming.ldap.LdapName("cn=four,cn=three,cn=two,cn=one"); Name aName = new org.apache.ldap.common.name.LdapName("cn=four,cn=three,cn=two,cn=one"); // JAVA 1.5 assertEquals(jName.remove(0).toString(), aName.remove(0).toString()); // JAVA 1.5 assertEquals(jName.toString(), aName.toString()); assertEquals("cn=one", aName.remove(0).toString()); assertEquals("cn=four,cn=three,cn=two", aName.toString()); // JAVA 1.5 assertEquals(jName.remove(jName.size() - 1).toString(), aName.remove(aName.size() - 1).toString()); // JAVA 1.5 assertEquals(jName.toString(), aName.toString()); assertEquals("cn=four", aName.remove(aName.size() - 1).toString()); assertEquals("cn=three,cn=two", aName.toString()); } public void testGetAll() throws NamingException { // JAVA 1.5 Name jName = new javax.naming.ldap.LdapName("cn=four,cn=three,cn=two,cn=one"); Name aName = new org.apache.ldap.common.name.LdapName("cn=four,cn=three,cn=two,cn=one"); // JAVA 1.5 Enumeration j = jName.getAll(); // JAVA 1.5 Enumeration a = aName.getAll(); // JAVA 1.5 while (j.hasMoreElements()) // JAVA 1.5 { // JAVA 1.5 assertTrue(a.hasMoreElements()); // JAVA 1.5 assertEquals(j.nextElement(), a.nextElement()); // JAVA 1.5 } Enumeration a = aName.getAll(); assertTrue(a.hasMoreElements()); assertEquals("cn=one", a.nextElement()); assertTrue(a.hasMoreElements()); assertEquals("cn=two", a.nextElement()); assertTrue(a.hasMoreElements()); assertEquals("cn=three", a.nextElement()); assertTrue(a.hasMoreElements()); assertEquals("cn=four", a.nextElement()); assertFalse(a.hasMoreElements()); } }