/* * Copyright 2004 The Apache Software Foundation * * Licensed 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. * */ 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. */ public class NameTest extends TestCase { public static void main(String[] args) { junit.textui.TestRunner.run(NameTest.class); } public void testName() throws NamingException { 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"); assertEquals(jName.toString(), "cn=four,cn=three,cn=two,cn=one"); assertEquals(aName.toString(), "cn=four,cn=three,cn=two,cn=one"); assertEquals(jName.toString(), aName.toString()); } public void testGetPrefix() throws NamingException { 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"); assertEquals(jName.getPrefix(0).toString(), aName.getPrefix(0).toString()); assertEquals(jName.getPrefix(1).toString(), aName.getPrefix(1).toString()); assertEquals(jName.getPrefix(2).toString(), aName.getPrefix(2).toString()); assertEquals(jName.getPrefix(3).toString(), aName.getPrefix(3).toString()); assertEquals(jName.getPrefix(4).toString(), aName.getPrefix(4).toString()); } public void testGetSuffix() throws NamingException { 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"); assertEquals(jName.getSuffix(0).toString(), aName.getSuffix(0).toString()); assertEquals(jName.getSuffix(1).toString(), aName.getSuffix(1).toString()); assertEquals(jName.getSuffix(2).toString(), aName.getSuffix(2).toString()); assertEquals(jName.getSuffix(3).toString(), aName.getSuffix(3).toString()); assertEquals(jName.getSuffix(4).toString(), aName.getSuffix(4).toString()); } public void testAddString() throws NamingException { 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"); assertSame(jName, jName.add("cn=five")); assertSame(aName, aName.add("cn=five")); assertEquals(jName.toString(), aName.toString()); } public void testAddIntString() throws NamingException { 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"); assertSame(jName, jName.add(0,"cn=zero")); assertSame(aName, aName.add(0,"cn=zero")); assertEquals(jName.toString(), aName.toString()); assertSame(jName, jName.add(2,"cn=one.5")); assertSame(aName, aName.add(2,"cn=one.5")); assertEquals(jName.toString(), aName.toString()); assertSame(jName, jName.add(jName.size(),"cn=five")); assertSame(aName, aName.add(aName.size(),"cn=five")); assertEquals(jName.toString(), aName.toString()); } public void testAddAllName() throws NamingException { 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"); 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"))); assertEquals(jName.toString(), aName.toString()); } public void testAddAllIntName() throws NamingException { 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"); assertSame(jName, jName.addAll(0, new javax.naming.ldap.LdapName("cn=zero,cn=zero.5"))); assertSame(aName, aName.addAll(0, new org.apache.ldap.common.name.LdapName("cn=zero,cn=zero.5"))); assertEquals(jName.toString(), aName.toString()); assertSame(jName, jName.addAll(2, new javax.naming.ldap.LdapName("cn=zero,cn=zero.5"))); assertSame(aName, aName.addAll(2, new org.apache.ldap.common.name.LdapName("cn=zero,cn=zero.5"))); assertEquals(jName.toString(), aName.toString()); assertSame(jName, jName.addAll(jName.size(), new javax.naming.ldap.LdapName("cn=zero,cn=zero.5"))); assertSame(aName, aName.addAll(aName.size(), new org.apache.ldap.common.name.LdapName("cn=zero,cn=zero.5"))); assertEquals(jName.toString(), aName.toString()); } public void testStartsWithName() throws NamingException { 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"); assertEquals(jName.startsWith(new javax.naming.ldap.LdapName("cn=seven,cn=six,cn=five")), aName.startsWith(new org.apache.ldap.common.name.LdapName("cn=seven,cn=six,cn=five"))); assertEquals(jName.startsWith(new javax.naming.ldap.LdapName("cn=three,cn=two,cn=one")), aName.startsWith(new org.apache.ldap.common.name.LdapName("cn=three,cn=two,cn=one"))); } public void testEndsWithName() throws NamingException { 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"); assertEquals(jName.endsWith(new javax.naming.ldap.LdapName("cn=seven,cn=six,cn=five")), aName.endsWith(new org.apache.ldap.common.name.LdapName("cn=seven,cn=six,cn=five"))); assertEquals(jName.endsWith(new javax.naming.ldap.LdapName("cn=three,cn=two,cn=one")), aName.endsWith(new org.apache.ldap.common.name.LdapName("cn=three,cn=two,cn=one"))); } public void testRemove() throws NamingException { 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"); assertEquals(jName.remove(0).toString(), aName.remove(0).toString()); assertEquals(jName.toString(), aName.toString()); assertEquals(jName.remove(jName.size() - 1).toString(), aName.remove(aName.size() - 1).toString()); assertEquals(jName.toString(), aName.toString()); } public void testGetAll() throws NamingException { 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"); Enumeration j = jName.getAll(); Enumeration a = aName.getAll(); while (j.hasMoreElements()) { assertTrue(j.hasMoreElements()); assertEquals(j.nextElement(), a.nextElement()); } } }