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 com.sun.jndi.ldap.LdapName in JDK
 * 1.4.
 */
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 com.sun.jndi.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("cn=four,cn=three,cn=two,cn=one", jName.toString());
        assertEquals("cn=four,cn=three,cn=two,cn=one", aName.toString());
        assertEquals(jName.toString(), aName.toString());
    }

    public void testGetPrefix() throws NamingException
    {
        Name jName = new com.sun.jndi.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());

        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());
        
        jName = new com.sun.jndi.ldap.LdapName("");
        aName = new org.apache.ldap.common.name.LdapName("");
        
        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 com.sun.jndi.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());

        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
    {
        Name jName = new com.sun.jndi.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());
        assertEquals("cn=five,cn=four,cn=three,cn=two,cn=one", aName.toString());
    }

    public void testAddIntString() throws NamingException
    {
        Name jName = new com.sun.jndi.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());
        assertEquals("cn=four,cn=three,cn=two,cn=one,cn=zero", aName.toString());

        assertSame(jName, jName.add(2, "cn=one.5"));
        assertSame(aName, aName.add(2, "cn=one.5"));
        assertEquals(jName.toString(), aName.toString());
        assertEquals("cn=four,cn=three,cn=two,cn=one.5,cn=one,cn=zero", aName.toString());

        assertSame(jName, jName.add(jName.size(), "cn=five"));
        assertSame(aName, aName.add(aName.size(), "cn=five"));
        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
    {
        Name jName = new com.sun.jndi.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 com.sun.jndi.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());
        assertEquals("cn=seven,cn=six,cn=four,cn=three,cn=two,cn=one", aName.toString());
    }

    public void testAddAllIntName() throws NamingException
    {
        Name jName = new com.sun.jndi.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 com.sun.jndi.ldap.LdapName("cn=zero.5,cn=zero")));
        assertSame(aName, aName.addAll(0, new org.apache.ldap.common.name.LdapName(
                "cn=zero.5,cn=zero")));
        assertEquals(jName.toString(), aName.toString());
        assertEquals("cn=four,cn=three,cn=two,cn=one,cn=zero.5,cn=zero", aName.toString());

        assertSame(jName, jName.addAll(2, new com.sun.jndi.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")));
        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());

        assertSame(jName, jName.addAll(jName.size(), new com.sun.jndi.ldap.LdapName(
                "cn=six,cn=five")));
        assertSame(aName, aName.addAll(aName.size(), new org.apache.ldap.common.name.LdapName(
                "cn=six,cn=five")));
        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
    {
        Name jName = new com.sun.jndi.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");

        assertEquals(jName.startsWith(new com.sun.jndi.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 com.sun.jndi.ldap.LdapName("cn=three,cn=two,cn=one")),
                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
    {
        Name jName = new com.sun.jndi.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");

        assertEquals(jName.endsWith(new com.sun.jndi.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 com.sun.jndi.ldap.LdapName("cn=three,cn=two,cn=one")),
                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
    {
        Name jName = new com.sun.jndi.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("cn=one", aName.remove(0).toString());
        assertEquals("cn=four,cn=three,cn=two", aName.toString());

        assertEquals(jName.remove(jName.size() - 1).toString(), aName.remove(aName.size() - 1)
                .toString());
        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
    {
        
        Name jName = new com.sun.jndi.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(a.hasMoreElements());
            assertEquals(j.nextElement(), a.nextElement());
        }

        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());
    }
}
