ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dona...@apache.org
Subject cvs commit: jakarta-ant/proposal/myrmidon/src/testcases/org/apache/myrmidon/components/configurer MyType2.java MyType1.java ConfigTest8.java ConfigTest7.java ConfigTest6.java DefaultConfigurerTest.java
Date Mon, 28 Jan 2002 00:49:13 GMT
donaldp     02/01/27 16:49:13

  Modified:    proposal/myrmidon/src/java/org/apache/myrmidon/components/configurer
                        Resources.properties DefaultObjectConfigurer.java
                        DefaultConfigurer.java
               proposal/myrmidon/src/testcases/org/apache/myrmidon/components/configurer
                        DefaultConfigurerTest.java
  Added:       proposal/myrmidon/src/testcases/org/apache/myrmidon/components/configurer
                        MyType2.java MyType1.java ConfigTest8.java
                        ConfigTest7.java ConfigTest6.java
  Log:
  Add in the ability for types to automagicall instantiated based on registration into TypeManager
as previously discussed.
  
  Also added unit tests to verify everything works as expected
  
  Revision  Changes    Path
  1.7       +2 -1      jakarta-ant/proposal/myrmidon/src/java/org/apache/myrmidon/components/configurer/Resources.properties
  
  Index: Resources.properties
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/proposal/myrmidon/src/java/org/apache/myrmidon/components/configurer/Resources.properties,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- Resources.properties	27 Jan 2002 23:41:46 -0000	1.6
  +++ Resources.properties	28 Jan 2002 00:49:13 -0000	1.7
  @@ -20,4 +20,5 @@
   bad-set-element.error=Could not handle element <{1}>, nested in element <{0}>.
   no-content.error=Text content is not allowed for element <{0}>.
   bad-set-content.error=Could not set text content for element <{0}>.
  -typed-adder-non-interface.error=The typed adder for class "{0}" must have a single parameter
that is an interface rather than {1} which defines a class.
  \ No newline at end of file
  +typed-adder-non-interface.error=The typed adder for class "{0}" must have a single parameter
that is an interface rather than {1} which defines a class.
  +no-factory-for-role.error=Unable to locate type factory for role "{0}"
  \ No newline at end of file
  
  
  
  1.8       +9 -2      jakarta-ant/proposal/myrmidon/src/java/org/apache/myrmidon/components/configurer/DefaultObjectConfigurer.java
  
  Index: DefaultObjectConfigurer.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/proposal/myrmidon/src/java/org/apache/myrmidon/components/configurer/DefaultObjectConfigurer.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- DefaultObjectConfigurer.java	28 Jan 2002 00:09:13 -0000	1.7
  +++ DefaultObjectConfigurer.java	28 Jan 2002 00:49:13 -0000	1.8
  @@ -26,7 +26,7 @@
    * of a class.
    *
    * @author <a href="mailto:adammurdoch_ml@yahoo.com">Adam Murdoch</a>
  - * @version $Revision: 1.7 $ $Date: 2002/01/28 00:09:13 $
  + * @version $Revision: 1.8 $ $Date: 2002/01/28 00:49:13 $
    */
   class DefaultObjectConfigurer
       implements ObjectConfigurer
  @@ -351,7 +351,14 @@
       public PropertyConfigurer getProperty( final String name )
           throws NoSuchPropertyException
       {
  -        final PropertyConfigurer configurer = (PropertyConfigurer)m_props.get( name );
  +        PropertyConfigurer configurer = (PropertyConfigurer)m_props.get( name );
  +        if( null != configurer )
  +        {
  +            return configurer;
  +        }
  +
  +        //Maybe there is a typed adder??
  +        configurer = (PropertyConfigurer)m_props.get( "" );
           if( null != configurer )
           {
               return configurer;
  
  
  
  1.21      +102 -74   jakarta-ant/proposal/myrmidon/src/java/org/apache/myrmidon/components/configurer/DefaultConfigurer.java
  
  Index: DefaultConfigurer.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/proposal/myrmidon/src/java/org/apache/myrmidon/components/configurer/DefaultConfigurer.java,v
  retrieving revision 1.20
  retrieving revision 1.21
  diff -u -r1.20 -r1.21
  --- DefaultConfigurer.java	27 Jan 2002 23:41:18 -0000	1.20
  +++ DefaultConfigurer.java	28 Jan 2002 00:49:13 -0000	1.21
  @@ -26,6 +26,10 @@
   import org.apache.avalon.framework.logger.LogEnabled;
   import org.apache.myrmidon.interfaces.configurer.Configurer;
   import org.apache.myrmidon.interfaces.converter.MasterConverter;
  +import org.apache.myrmidon.interfaces.type.TypeFactory;
  +import org.apache.myrmidon.interfaces.type.TypeManager;
  +import org.apache.myrmidon.interfaces.type.TypeException;
  +import org.apache.myrmidon.api.TaskException;
   
   /**
    * Class used to configure tasks.
  @@ -42,6 +46,9 @@
       ///Converter to use for converting between values
       private MasterConverter m_converter;
   
  +    //TypeManager to use to create types in typed adders
  +    private TypeManager m_typeManager;
  +
       ///Cached object configurers.  This is a map from Class to the
       ///ObjectConfigurer for that class.
       private Map m_configurerCache = new HashMap();
  @@ -50,6 +57,7 @@
           throws ComponentException
       {
           m_converter = (MasterConverter)componentManager.lookup( MasterConverter.ROLE );
  +        m_typeManager = (TypeManager)componentManager.lookup( TypeManager.ROLE );
       }
   
       /**
  @@ -262,7 +270,8 @@
           final String name = element.getName();
   
           // Locate the configurer for the child element
  -        final PropertyConfigurer childConfigurer = state.getConfigurer().getProperty( name
);
  +        final PropertyConfigurer childConfigurer =
  +            state.getConfigurer().getProperty( name );
   
           // Create & configure the child element
           final Object child =
  @@ -272,79 +281,6 @@
           childConfigurer.addValue( state, child );
       }
   
  -    private Object setupChild( final ConfigurationState state,
  -                               final Configuration element,
  -                               final Context context,
  -                               final PropertyConfigurer childConfigurer )
  -        throws ConfigurationException
  -    {
  -        final String name = element.getName();
  -        final Class type = childConfigurer.getType();
  -        Object child = childConfigurer.createValue( state );
  -
  -        if( null == child && Configuration.class == type )
  -        {
  -            //special case where you have add(Configuration)
  -            return element;
  -        }
  -        else if( null == child )
  -        {
  -            // Create an instance using the default constructor
  -            if( type.isInterface() )
  -            {
  -                child = createdTypedObject( name, type );
  -                configureObject( child, element, context );
  -            }
  -            else
  -            {
  -                child = createObject( type );
  -                configureObject( child, element, context );
  -            }
  -        }
  -        configureObject( child, element, context );
  -        return child;
  -    }
  -
  -    /**
  -     * Utility method to create an instance of the
  -     * specified type that satisfied supplied interface.
  -     */
  -    private Object createdTypedObject( final String name,
  -                                       final Class type )
  -        throws ConfigurationException
  -    {
  -        try
  -        {
  -            return type.newInstance();
  -        }
  -        catch( final Exception e )
  -        {
  -            final String message =
  -                REZ.getString( "create-object.error",
  -                               type.getName() );
  -            throw new ConfigurationException( message, e );
  -        }
  -    }
  -
  -    /**
  -     * Utility method to instantiate an instance of the specified class.
  -     */
  -    private Object createObject( final Class type )
  -        throws ConfigurationException
  -    {
  -        try
  -        {
  -            return type.newInstance();
  -        }
  -        catch( final Exception e )
  -        {
  -            final String message =
  -                REZ.getString( "create-object.error",
  -                               type.getName() );
  -            throw new ConfigurationException( message, e );
  -        }
  -    }
  -
       /**
        * Configures a property from a reference.
        */
  @@ -467,5 +403,97 @@
               m_configurerCache.put( clazz, configurer );
           }
           return configurer;
  +    }
  +
  +    private Object setupChild( final ConfigurationState state,
  +                               final Configuration element,
  +                               final Context context,
  +                               final PropertyConfigurer childConfigurer )
  +        throws ConfigurationException
  +    {
  +        final String name = element.getName();
  +        final Class type = childConfigurer.getType();
  +        Object child = childConfigurer.createValue( state );
  +
  +        if( null == child && Configuration.class == type )
  +        {
  +            //special case where you have add...(Configuration)
  +            return element;
  +        }
  +        else if( null == child )
  +        {
  +            // Create an instance using the default constructor
  +            if( type.isInterface() )
  +            {
  +                child = createdTypedObject( name, type );
  +                configureObject( child, element, context );
  +            }
  +            else
  +            {
  +                child = createObject( type );
  +                configureObject( child, element, context );
  +            }
  +        }
  +        configureObject( child, element, context );
  +        return child;
  +    }
  +
  +    /**
  +     * Utility method to create an instance of the
  +     * specified type that satisfied supplied interface.
  +     */
  +    private Object createdTypedObject( final String name,
  +                                       final Class type )
  +        throws ConfigurationException
  +    {
  +        final TypeFactory factory = getTypeFactory( type.getName() );
  +        try
  +        {
  +            return factory.create( name );
  +        }
  +        catch( final Exception e )
  +        {
  +            final String message =
  +                REZ.getString( "create-typed-object.error",
  +                               name,
  +                               type.getName() );
  +            throw new ConfigurationException( message, e );
  +        }
  +    }
  +
  +    /**
  +     * Utility method to instantiate an instance of the specified class.
  +     */
  +    private Object createObject( final Class type )
  +        throws ConfigurationException
  +    {
  +        try
  +        {
  +            return type.newInstance();
  +        }
  +        catch( final Exception e )
  +        {
  +            final String message =
  +                REZ.getString( "create-object.error",
  +                               type.getName() );
  +            throw new ConfigurationException( message, e );
  +        }
  +    }
  +
  +    /**
  +     * Locates a type factory.
  +     */
  +    protected final TypeFactory getTypeFactory( final String role )
  +        throws ConfigurationException
  +    {
  +        try
  +        {
  +            return m_typeManager.getFactory( role );
  +        }
  +        catch( final TypeException te )
  +        {
  +            final String message = REZ.getString( "no-factory-for-role.error", role );
  +            throw new ConfigurationException( message, te );
  +        }
       }
   }
  
  
  
  1.5       +83 -0     jakarta-ant/proposal/myrmidon/src/testcases/org/apache/myrmidon/components/configurer/DefaultConfigurerTest.java
  
  Index: DefaultConfigurerTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/proposal/myrmidon/src/testcases/org/apache/myrmidon/components/configurer/DefaultConfigurerTest.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- DefaultConfigurerTest.java	28 Jan 2002 00:08:31 -0000	1.4
  +++ DefaultConfigurerTest.java	28 Jan 2002 00:49:13 -0000	1.5
  @@ -36,6 +36,7 @@
   import org.apache.myrmidon.interfaces.configurer.Configurer;
   import org.apache.myrmidon.interfaces.converter.ConverterRegistry;
   import org.apache.myrmidon.interfaces.converter.MasterConverter;
  +import org.apache.myrmidon.interfaces.type.DefaultTypeFactory;
   import org.apache.myrmidon.interfaces.type.TypeManager;
   
   /**
  @@ -51,6 +52,7 @@
   
       private DefaultComponentManager m_componentManager;
       private Configurer m_configurer;
  +    private TypeManager m_typeManager;
       private Logger m_logger;
       private DefaultTaskContext m_context;
   
  @@ -134,6 +136,8 @@
           // Find the configurer
           m_configurer = (Configurer)m_componentManager.lookup( Configurer.ROLE );
   
  +        // Find the typeManager
  +        m_typeManager = (TypeManager)m_componentManager.lookup( TypeManager.ROLE );
       }
   
       /**
  @@ -410,6 +414,85 @@
                                                     MyRole2.class.getName() );
               assertSameMessage( message, ce );
           }
  +    }
  +
  +    /**
  +     * Tests to see if typed adder works.
  +     */
  +    public void testTypedAdder()
  +        throws Exception
  +    {
  +        // Setup test data
  +        final DefaultConfiguration config = new DefaultConfiguration( "test", "test" );
  +        final DefaultConfiguration child1 = new DefaultConfiguration( "my-type1", "test"
);
  +        final DefaultConfiguration child2 = new DefaultConfiguration( "my-type2", "test"
);
  +        config.addChild( child1 );
  +        config.addChild( child2 );
  +
  +        final ClassLoader loader = getClass().getClassLoader();
  +        final DefaultTypeFactory factory = new DefaultTypeFactory( loader );
  +        factory.addNameClassMapping( "my-type1", MyType1.class.getName() );
  +        factory.addNameClassMapping( "my-type2", MyType2.class.getName() );
  +        m_typeManager.registerType( MyRole1.class.getName(), "my-type1", factory );
  +        m_typeManager.registerType( MyRole1.class.getName(), "my-type2", factory );
  +
  +        final ConfigTest6 test = new ConfigTest6();
  +
  +        // Configure the object
  +        m_configurer.configure( test, config, m_context );
  +
  +        final ConfigTest6 expected = new ConfigTest6();
  +        expected.add( new MyType1() );
  +        expected.add( new MyType2() );
  +        assertEquals( expected, test );
  +    }
  +
  +    /**
  +     * Tests to see if typed adder works.
  +     */
  +    public void testTypedConfigAdder()
  +        throws Exception
  +    {
  +        // Setup test data
  +        final DefaultConfiguration config = new DefaultConfiguration( "test", "test" );
  +        final DefaultConfiguration child1 = new DefaultConfiguration( "my-type1", "test"
);
  +        final DefaultConfiguration child2 = new DefaultConfiguration( "my-type2", "test"
);
  +        config.addChild( child1 );
  +        config.addChild( child2 );
  +
  +        final ConfigTest7 test = new ConfigTest7();
  +
  +        // Configure the object
  +        m_configurer.configure( test, config, m_context );
  +
  +        final ConfigTest7 expected = new ConfigTest7();
  +        expected.add( child1 );
  +        expected.add( child2 );
  +        assertEquals( expected, test );
  +    }
  +
  +    /**
  +     * Tests to see if typed adder works.
  +     */
  +    public void testConfigAdder()
  +        throws Exception
  +    {
  +        // Setup test data
  +        final DefaultConfiguration config = new DefaultConfiguration( "test", "test" );
  +        final DefaultConfiguration child1 = new DefaultConfiguration( "config", "test"
);
  +        final DefaultConfiguration child2 = new DefaultConfiguration( "config", "test"
);
  +        config.addChild( child1 );
  +        config.addChild( child2 );
  +
  +        final ConfigTest8 test = new ConfigTest8();
  +
  +        // Configure the object
  +        m_configurer.configure( test, config, m_context );
  +
  +        final ConfigTest8 expected = new ConfigTest8();
  +        expected.addConfig( child1 );
  +        expected.addConfig( child2 );
  +        assertEquals( expected, test );
       }
   
       /**
  
  
  
  1.1                  jakarta-ant/proposal/myrmidon/src/testcases/org/apache/myrmidon/components/configurer/MyType2.java
  
  Index: MyType2.java
  ===================================================================
  /*
   * 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.txt file.
   */
  package org.apache.myrmidon.components.configurer;
  
  /**
   * A basic implementation of MyRole1 to test configurer.
   *
   * @author <a href="mailto:peter@apache.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2002/01/28 00:49:13 $
   */
  public class MyType2
      implements MyRole1
  {
      public boolean equals( final Object object )
      {
          return object.getClass() == getClass();
      }
  }
  
  
  
  1.1                  jakarta-ant/proposal/myrmidon/src/testcases/org/apache/myrmidon/components/configurer/MyType1.java
  
  Index: MyType1.java
  ===================================================================
  /*
   * 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.txt file.
   */
  package org.apache.myrmidon.components.configurer;
  
  /**
   * A basic implementation of MyRole1 to test configurer.
   *
   * @author <a href="mailto:peter@apache.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2002/01/28 00:49:13 $
   */
  public class MyType1
      implements MyRole1
  {
      public boolean equals( final Object object )
      {
          return object.getClass() == getClass();
      }
  }
  
  
  
  1.1                  jakarta-ant/proposal/myrmidon/src/testcases/org/apache/myrmidon/components/configurer/ConfigTest8.java
  
  Index: ConfigTest8.java
  ===================================================================
  /*
   * 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.txt file.
   */
  package org.apache.myrmidon.components.configurer;
  
  import java.util.ArrayList;
  import junit.framework.AssertionFailedError;
  import org.apache.avalon.framework.configuration.Configuration;
  
  /**
   * Simple class to test adder for Configurations.
   *
   * @author <a href="mailto:peter@apache.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2002/01/28 00:49:13 $
   */
  public class ConfigTest8
  {
      private ArrayList m_configurations = new ArrayList();
  
      public void addConfig( final Configuration configuration )
      {
          m_configurations.add( configuration );
      }
  
      public boolean equals( final Object object )
      {
          final ConfigTest8 other = (ConfigTest8)object;
          return m_configurations.equals( other.m_configurations );
      }
  }
  
  
  
  1.1                  jakarta-ant/proposal/myrmidon/src/testcases/org/apache/myrmidon/components/configurer/ConfigTest7.java
  
  Index: ConfigTest7.java
  ===================================================================
  /*
   * 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.txt file.
   */
  package org.apache.myrmidon.components.configurer;
  
  import java.util.ArrayList;
  import junit.framework.AssertionFailedError;
  import org.apache.avalon.framework.configuration.Configuration;
  
  /**
   * Simple class to test adder for Configurations.
   *
   * @author <a href="mailto:peter@apache.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2002/01/28 00:49:13 $
   */
  public class ConfigTest7
  {
      private ArrayList m_configurations = new ArrayList();
  
      public void add( final Configuration configuration )
      {
          m_configurations.add( configuration );
      }
  
      public boolean equals( final Object object )
      {
          final ConfigTest7 other = (ConfigTest7)object;
          return m_configurations.equals( other.m_configurations );
      }
  }
  
  
  
  1.1                  jakarta-ant/proposal/myrmidon/src/testcases/org/apache/myrmidon/components/configurer/ConfigTest6.java
  
  Index: ConfigTest6.java
  ===================================================================
  /*
   * 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.txt file.
   */
  package org.apache.myrmidon.components.configurer;
  
  import java.util.ArrayList;
  import junit.framework.AssertionFailedError;
  import org.apache.avalon.framework.configuration.Configuration;
  
  /**
   * Simple class to test typed adder.
   *
   * @author <a href="mailto:peter@apache.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2002/01/28 00:49:13 $
   */
  public class ConfigTest6
  {
      private ArrayList m_roles = new ArrayList();
  
      public void add( final MyRole1 role1 )
      {
          m_roles.add( role1 );
      }
  
      public boolean equals( final Object object )
      {
          final ConfigTest6 other = (ConfigTest6)object;
          return m_roles.equals( other.m_roles );
      }
  }
  
  
  

--
To unsubscribe, e-mail:   <mailto:ant-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:ant-dev-help@jakarta.apache.org>


Mime
View raw message