/*
 * Copyright (C) The Apache Software Foundation. All rights reserved.
 *
 * This software is published under the terms of the Apache Software License
 * version 1.1, a copy of which has been included with this distribution in
 * the LICENSE file.
 */
package org.apache.avalon.framework.component.test;

import org.apache.avalon.framework.component.DefaultComponentManager;
import org.apache.avalon.framework.component.Component;
import org.apache.avalon.framework.component.ComponentException;
import junit.framework.TestCase;

/**
 * Test the basic public methods of DefaultComponentManager.
 *
 * @author <a href="mailto:rantene@hotmail.com">Ran Tene</a>
 */
 
public final class DefaultComponentManagerTestCase
    extends TestCase
{

    class NameComponent
        implements Component
    {
        String  m_name;
        public NameComponent( final String name )
        {
            m_name = name;
        }
        public String getName()
        {
           return m_name;
        }
		
    }
    
	
	private DefaultComponentManager m_componentManager;

    public DefaultComponentManagerTestCase()
    {
        this("DefaultComponentManager Test Case");
    }

    public DefaultComponentManagerTestCase( final String name )
    {
        super( name );
    }

    protected void setUp()
        throws Exception
    {
        m_componentManager = new DefaultComponentManager();
    }

    protected  void tearDown()
        throws Exception
    {
        m_componentManager = null;
    }

    public void testlookup()
        throws Exception
    {	
		String roleA = "a";
		String roleB = "b";	
		String exceptionSting = "exception thrown";
		String exceptionValue = "";
		m_componentManager.put(roleA,new NameComponent(roleA));
		NameComponent  nameComponent = (NameComponent)m_componentManager.lookup(roleA);
		m_componentManager.put(roleB,new NameComponent(roleB));
		assertEquals( roleA, nameComponent.getName() );
		try
		{
			nameComponent = (NameComponent)m_componentManager.lookup("no.role");
		}
		catch	(ComponentException ce)
		{
			exceptionValue = exceptionSting;
		}
		assertEquals( exceptionValue, exceptionSting );
		DefaultComponentManager childComponentManager = new DefaultComponentManager(m_componentManager);
		nameComponent = (NameComponent)childComponentManager.lookup(roleB);
		assertEquals( roleB, nameComponent.getName() );
		
    }
	
    public void testmakeReadOnly()
        throws Exception
    {	
	    String roleA = "a";
	    String roleB = "b";	
	    String exceptionSting = "exception thrown";
	    String exceptionValue = "";
	
	    m_componentManager.put(roleA,new NameComponent(roleA));
	    NameComponent  nameComponent = (NameComponent)m_componentManager.lookup(roleA);
	    assertEquals( roleA, nameComponent.getName() );
	
		m_componentManager.makeReadOnly();
    try
    {
	    m_componentManager.put(roleB,new NameComponent(roleB));
    }
    catch	(IllegalStateException se)
    {
		exceptionValue = exceptionSting;
    }
		assertEquals( exceptionValue, exceptionSting );
	
    }
	
	

}







