/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you 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.directory.shared.ldap.schema.registries;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import javax.naming.NamingException;

import org.apache.directory.shared.ldap.schema.AttributeType;
import org.junit.Before;
import org.junit.Test;


/**
 * Test the AttributeTypeRegistry
 *
 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
 * @version $Rev$, $Date$
 */
public class AttributeTypeRegistryTest
{
    AttributeTypeRegistry atRegistry;
    
    @Before
    public void setup()
    {
        atRegistry = new DefaultAttributeTypeRegistry( new OidRegistry() );
    }
    
    
    @Test
    public void testUnregister() throws NamingException
    {
        AttributeType at0 = new AttributeType( "1.1" );
        at0.addName( "t", "test", "Test", "T" );
        atRegistry.register( at0 );
        
        atRegistry.unregister( "1.1" );
        assertFalse( atRegistry.contains( "1.1" ) );
        assertFalse( atRegistry.contains( "t" ) );
        assertFalse( atRegistry.contains( "T" ) );
        assertFalse( atRegistry.contains( "tEsT" ) );

        try
        {
            atRegistry.getOidByName( "T" );
            fail();
        }
        catch ( NamingException ne )
        {
            assertTrue( true );
        }
    }
    
    
    @Test
    public void testRegister() throws NamingException
    {
        AttributeType at0 = new AttributeType( "1.1" );
        at0.addName( "t", "test", "Test", "T" );
        atRegistry.register( at0 );
        
        assertTrue( atRegistry.contains( "1.1" ) );
        assertTrue( atRegistry.contains( "t" ) );
        assertTrue( atRegistry.contains( "T" ) );
        assertTrue( atRegistry.contains( "tEsT" ) );
        assertEquals( "1.1", atRegistry.getOidByName( "T" ) );
        
        try
        {
            atRegistry.register( at0 );
            fail();
        }
        catch ( NamingException ne )
        {
            assertTrue( true );
        }
    }
    
    
    @Test
    public void testClone() throws Exception
    {
        AttributeType at0 = new AttributeType( "1.1" );
        at0.addName( "t", "test", "Test", "T" );
        
        atRegistry.register( at0 );
        
        AttributeType at1 = new AttributeType( "1.2" );
        at1.addName( "u", "unit", "Unit", "U" );

        atRegistry.register( at1 );
        
        // Clone the ATRegistry
        AttributeTypeRegistry clone = (AttributeTypeRegistry)atRegistry.clone();
        
        assertEquals( at0, clone.lookup( "1.1" ) );
        assertEquals( at1, clone.lookup( "1.2" ) );
        
        atRegistry.unregister( "1.1" );
        assertFalse( atRegistry.contains( "1.1" ) );
        assertTrue( clone.contains( "1.1" ) );
        
        AttributeType at = atRegistry.lookup( "1.2" );
        at.setOid( "2.2" );
        
        at = atRegistry.lookup( "1.2" );
        assertEquals( "2.2", at.getOid() );

        at = clone.lookup( "1.2" );
        assertEquals( "1.2", at.getOid() );
    }
}

@@ -1543,27 +1543,8 @@
 try(
- new OidRegistry() 
 );%0A 
@@ -2919,1035 +2919,6 @@
   %7D%0A
-    %0A    %0A    @Test%0A    public void testClone() throws Exception%0A    %7B%0A        AttributeType at0 = new AttributeType( %221.1%22 );%0A        at0.addName( %22t%22, %22test%22, %22Test%22, %22T%22 );%0A        %0A        atRegistry.register( at0 );%0A        %0A        AttributeType at1 = new AttributeType( %221.2%22 );%0A        at1.addName( %22u%22, %22unit%22, %22Unit%22, %22U%22 );%0A%0A        atRegistry.register( at1 );%0A        %0A        // Clone the ATRegistry%0A        AttributeTypeRegistry clone = (AttributeTypeRegistry)atRegistry.clone();%0A        %0A        assertEquals( at0, clone.lookup( %221.1%22 ) );%0A        assertEquals( at1, clone.lookup( %221.2%22 ) );%0A        %0A        atRegistry.unregister( %221.1%22 );%0A        assertFalse( atRegistry.contains( %221.1%22 ) );%0A        assertTrue( clone.contains( %221.1%22 ) );%0A        %0A        AttributeType at = atRegistry.lookup( %221.2%22 );%0A        at.setOid( %222.2%22 );%0A        %0A        at = atRegistry.lookup( %221.2%22 );%0A        assertEquals( %222.2%22, at.getOid() );%0A%0A        at = clone.lookup( %221.2%22 );%0A        assertEquals( %221.2%22, at.getOid() );%0A    %7D%0A
 %7D%0A

Remove the clone test