/*
 * 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.DefaultComponentSelector;
import org.apache.avalon.framework.component.Component;
import org.apache.avalon.framework.component.ComponentException;
import junit.framework.TestCase;

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

    class FeatureComponent
        implements Component
    {
        Object  m_feature;
        public FeatureComponent( final Object feature )
        {
            m_feature = feature;
        }
        public Object getFeature()
        {
           return m_feature;
        }
		
    }
    
    class Hint
    {
        String  m_name;
        public Hint( final String name )
        {
            m_name = name;
        }
        public String getName()
        {
           return m_name;
        }
    	
    }
	
	private DefaultComponentSelector m_componentSelector;

    public DefaultComponentSelectorTestCase()
    {
        this("DefaultComponentSelector Test Case");
    }

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

    protected void setUp()
        throws Exception
    {
        m_componentSelector = new DefaultComponentSelector();
    }

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

    public void testlookup()
        throws Exception
    {	
		Hint hintA = new Hint("a");
		Hint hintB = new Hint("b");	
		String exceptionSting = "exception thrown";
		String exceptionValue = "";
		m_componentSelector.put(hintA,new FeatureComponent(hintA));
		FeatureComponent  nameComponent = (FeatureComponent)m_componentSelector.select(hintA);
		m_componentSelector.put(hintB,new FeatureComponent(hintB));
		assertEquals( hintA, nameComponent.getFeature() );
		try
		{
			nameComponent = (FeatureComponent)m_componentSelector.select(new Hint("no component"));
		}
		catch	(ComponentException ce)
		{
			exceptionValue = exceptionSting;
		}
		assertEquals( exceptionValue, exceptionSting );
		
    }
	
    public void testmakeReadOnly()
        throws Exception
    {	
	    Hint hintA = new Hint("a");
	    Hint hintB = new Hint("b");
	    String exceptionSting = "exception thrown";
	    String exceptionValue = "";
	    m_componentSelector.put(hintB,new FeatureComponent(hintA));
 	    FeatureComponent  nameComponent = (FeatureComponent)m_componentSelector.select(hintB);
	    assertEquals( hintA, nameComponent.getFeature() );
	
		m_componentSelector.makeReadOnly();
    try
    {
	    m_componentSelector.put(hintB,new FeatureComponent(hintB));
    }
    catch	(IllegalStateException se)
    {
	    exceptionValue = exceptionSting;
    }
	    assertEquals( exceptionValue, exceptionSting );
	
    }
	
	

}






