package org.apache.avalon.framework.configuration.test;


import java.util.List;
import junit.framework.TestCase;
import org.apache.avalon.framework.configuration.DefaultConfiguration;
import org.apache.avalon.framework.configuration.Configuration;
/**
 * Test the basic public methods of DefaultConfiguration ,
 * @author <a href="mailto:rantene@hotmail.com">Ran Tene</a>
 */

public final class DefaultConfigurationTestCase extends TestCase
{
 
    private     DefaultConfiguration mConfiguration; 
	
    public DefaultConfigurationTestCase()
    {
        this("DefaultConfiguration Test Case");
    }


    public DefaultConfigurationTestCase( String name )
     {
         super( name );
     }

    public void setUp()
    {
		mConfiguration = new DefaultConfiguration( "a", "b" );
    }

	public void tearDowm()
	{
		mConfiguration =null;
	}
	
	
	 public void testGetValue()
	         throws Exception
	 {
		 String orgValue = "Original String";
		 mConfiguration.setValue(orgValue);
	 	 assertEquals( orgValue, mConfiguration.getValue() );
	 }

	 public void testGetValueAsInteger()
	         throws Exception
	 {
		 int orgValue = 55;
		 String strValue = Integer.toHexString(orgValue);
		 mConfiguration.setValue("0x"+ strValue);
	 	 assertEquals( orgValue, mConfiguration.getValueAsInteger() );
	 }
	 
	 public void testGetAttribute()
	         throws Exception
	 {
		  String key = "key";
		  String value = "original value";
		  String defaultStr = "default";
		  mConfiguration.setAttribute(key,value);
		  assertEquals( value, mConfiguration.getAttribute(key,defaultStr) );
	  	  assertEquals(defaultStr , mConfiguration.getAttribute("newKey",defaultStr) );
	 }
	 
	 public void testMakeReadOnly()
	 {
		  String key = "key";
		  String value = "original value";
		  String exception ="exception not thrown";;
		  String exceptionStr ="Configuration is read only";
		  mConfiguration.makeReadOnly();
	  try 
	  {
		  mConfiguration.setAttribute(key,value);
	  } catch (IllegalStateException e)
	  {
		  exception = exceptionStr;
	  }
		  assertEquals( exception, exceptionStr );
	 }
	 
	public void testAddRemoveChild()
	{
		String childName ="child";
		Configuration child = new DefaultConfiguration( childName, "child location" );;
		mConfiguration.addChild(child);
		assertEquals( child, mConfiguration.getChild(childName) );
		mConfiguration.removeChild(child);
		assertEquals( null, mConfiguration.getChild(childName,false) );
	
	}
}





