adammurdoch    02/02/08 14:20:00

  Modified:    
proposal/myrmidon/src/java/org/apache/myrmidon/components/configurer
                        DefaultConfigurer.java DefaultObjectConfigurer.java
                        ObjectConfigurer.java Resources.properties
               proposal/myrmidon/src/testcases/org/apache/myrmidon/components
                        AbstractComponentTest.java
               
proposal/myrmidon/src/testcases/org/apache/myrmidon/components/configurer
                        DefaultConfigurerTest.java MyRole1.java
  Added:       
proposal/myrmidon/src/testcases/org/apache/myrmidon/components/configurer
                        ConfigTest10.java StringToMyRole1Converter.java
  Log:
  Changes to DefaultConfigurer:
  
  * Uses the DataType role when creating instances for interface properties,
    rather than using the interface itself as the role.
  
  * Added ObjectConfigurer.getTypedProperty().  This replaces the implicit
    behaviour in DefaultObjectConfigurer.getProperty() where the typed
    property was returned for an unknown property name.
  
  * Typed properties are set using attributes and references, with the
    property's interface role shorthand name.  Previously, the DefaultConfigurer
    would attempt to set the typed property for any unknown attribute or
    reference name.
  
  * Can have a set() method for a typed property, rather than an add() method.
    Same semantics as setX() and addX() methods.
  
  * Added a several more test cases.
  
  Revision  Changes    Path
  1.25      +79 -21    
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.24
  retrieving revision 1.25
  diff -u -r1.24 -r1.25
  --- DefaultConfigurer.java    7 Feb 2002 13:02:19 -0000       1.24
  +++ DefaultConfigurer.java    8 Feb 2002 22:19:59 -0000       1.25
  @@ -28,12 +28,14 @@
   import org.apache.myrmidon.interfaces.type.TypeException;
   import org.apache.myrmidon.interfaces.type.TypeFactory;
   import org.apache.myrmidon.interfaces.type.TypeManager;
  +import org.apache.myrmidon.interfaces.role.RoleManager;
  +import org.apache.myrmidon.framework.DataType;
   
   /**
    * Class used to configure tasks.
    *
    * @author <a href="mailto:[EMAIL PROTECTED]">Peter Donald</a>
  - * @version $Revision: 1.24 $ $Date: 2002/02/07 13:02:19 $
  + * @version $Revision: 1.25 $ $Date: 2002/02/08 22:19:59 $
    */
   public class DefaultConfigurer
       extends AbstractLogEnabled
  @@ -48,6 +50,9 @@
       //TypeManager to use to create types in typed adders
       private TypeManager m_typeManager;
   
  +    //RoleManager to use to map from type names -> role shorthand
  +    private RoleManager m_roleManager;
  +
       ///Cached object configurers.  This is a map from Class to the
       ///ObjectConfigurer for that class.
       private Map m_configurerCache = new HashMap();
  @@ -57,6 +62,7 @@
       {
           m_converter = (MasterConverter)componentManager.lookup( 
MasterConverter.ROLE );
           m_typeManager = (TypeManager)componentManager.lookup( 
TypeManager.ROLE );
  +        m_roleManager = (RoleManager)componentManager.lookup( 
RoleManager.ROLE );
       }
   
       /**
  @@ -122,7 +128,6 @@
                   }
                   catch( final ConfigurationException ce )
                   {
  -                    ce.fillInStackTrace();
                       throw ce;
                   }
                   catch( final CascadingException ce )
  @@ -151,7 +156,6 @@
                   }
                   catch( final ConfigurationException ce )
                   {
  -                    ce.fillInStackTrace();
                       throw ce;
                   }
                   catch( final CascadingException ce )
  @@ -180,13 +184,12 @@
                   }
                   catch( final ConfigurationException ce )
                   {
  -                    ce.fillInStackTrace();
                       throw ce;
                   }
                   catch( final CascadingException ce )
                   {
                       final String message =
  -                        REZ.getString( "bad-set-element.error", name );
  +                        REZ.getString( "bad-set-element.error", elemName, 
name );
                       throw new ConfigurationException( message, ce );
                   }
               }
  @@ -269,8 +272,8 @@
           final String name = element.getName();
   
           // Locate the configurer for the child element
  -        final PropertyConfigurer childConfigurer =
  -            state.getConfigurer().getProperty( name );
  +        final PropertyConfigurer childConfigurer
  +            = getConfigurerFromName( state.getConfigurer(), name, true );
   
           // Create & configure the child element
           final Object child =
  @@ -288,9 +291,6 @@
                                        final Context context )
           throws CascadingException
       {
  -        // Adjust the name
  -        final String elementName = element.getName();
  -        final String name = elementName.substring( 0, elementName.length() - 
4 );
   
           // Extract the id
           final String id = element.getAttribute( "id" );
  @@ -302,6 +302,7 @@
           }
   
           // Set the property
  +        final String name = element.getName();
           setReference( state, name, id, context );
       }
   
  @@ -309,13 +310,17 @@
        * Sets a property using a reference.
        */
       private void setReference( final ConfigurationState state,
  -                               final String name,
  +                               final String refName,
                                  final String unresolvedId,
                                  final Context context )
           throws CascadingException
       {
  -        // Locate the configurer for the child element
  -        final PropertyConfigurer childConfigurer = 
state.getConfigurer().getProperty( name );
  +        // Adjust the name
  +        final String name = refName.substring( 0, refName.length() - 4 );
  +
  +        // Locate the configurer for the property
  +        final PropertyConfigurer childConfigurer
  +            = getConfigurerFromName( state.getConfigurer(), name, false );
   
           // Resolve any props in the id
           Object id = PropertyUtil.resolveProperty( unresolvedId, context, 
false );
  @@ -356,14 +361,13 @@
           if( name.toLowerCase().endsWith( "-ref" ) )
           {
               // A reference
  -            final String refName = name.substring( 0, name.length() - 4 );
  -            setReference( state, refName, value, context );
  +            setReference( state, name, value, context );
           }
           else
           {
               // Set the value
  -            final PropertyConfigurer propConfigurer =
  -                state.getConfigurer().getProperty( name );
  +            PropertyConfigurer propConfigurer
  +                = getConfigurerFromName( state.getConfigurer(), name, false 
);
               setValue( propConfigurer, state, value, context );
           }
       }
  @@ -424,7 +428,7 @@
           }
           else if( null == child )
           {
  -            // Create an instance using the default constructor
  +            // Create an instance
               if( type.isInterface() )
               {
                   child = createdTypedObject( name, type );
  @@ -440,17 +444,61 @@
       }
   
       /**
  +     * Determines the property configurer to use for a particular element
  +     * or attribute.  If the supplied name matches a property of the
  +     * class being configured, that property configurer is returned.  If
  +     * the supplied name matches the role shorthand for the class' typed
  +     * property, then the typed property configurer is used.
  +     *
  +     * @param configurer The configurer for the class being configured.
  +     * @param name The attribute/element name.
  +     */
  +    private PropertyConfigurer getConfigurerFromName( final ObjectConfigurer 
configurer,
  +                                                      final String name,
  +                                                      boolean ignoreRoleName 
)
  +        throws NoSuchPropertyException
  +    {
  +        // Try a named property
  +        final NoSuchPropertyException exc;
  +        try
  +        {
  +            return configurer.getProperty( name );
  +        }
  +        catch( NoSuchPropertyException e )
  +        {
  +            // Keep for later
  +            exc = e;
  +        }
  +
  +        // Try a typed property
  +        final PropertyConfigurer propertyConfigurer = 
configurer.getTypedProperty();
  +        if( ! ignoreRoleName )
  +        {
  +            final String roleShorthand = m_roleManager.getNameForRole( 
propertyConfigurer.getType().getName() );
  +            if( ! name.equalsIgnoreCase(roleShorthand) )
  +            {
  +                // Rethrow the original exception
  +                throw exc;
  +            }
  +        }
  +
  +        return propertyConfigurer;
  +    }
  +
  +    /**
        * Utility method to create an instance of the
  -     * specified type that satisfied supplied interface.
  +     * specified type that satisfies supplied interface.
        */
       private Object createdTypedObject( final String name,
                                          final Class type )
           throws ConfigurationException
       {
  -        final TypeFactory factory = getTypeFactory( type );
  +        // Attempt to create the object
  +        final Object obj;
           try
           {
  -            return factory.create( name );
  +            final TypeFactory factory = getTypeFactory( DataType.class );
  +            obj = factory.create( name );
           }
           catch( final Exception e )
           {
  @@ -460,6 +508,16 @@
                                  type.getName() );
               throw new ConfigurationException( message, e );
           }
  +
  +        // Check the types
  +        if( ! type.isInstance( obj ) )
  +        {
  +            final String message =
  +                REZ.getString( "mismatched-typed-object.error", name, 
type.getName() );
  +            throw new ConfigurationException( message );
  +        }
  +
  +        return obj;
       }
   
       /**
  
  
  
  1.10      +52 -37    
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.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- DefaultObjectConfigurer.java      1 Feb 2002 06:46:49 -0000       1.9
  +++ DefaultObjectConfigurer.java      8 Feb 2002 22:19:59 -0000       1.10
  @@ -26,7 +26,7 @@
    * of a class.
    *
    * @author <a href="mailto:[EMAIL PROTECTED]">Adam Murdoch</a>
  - * @version $Revision: 1.9 $ $Date: 2002/02/01 06:46:49 $
  + * @version $Revision: 1.10 $ $Date: 2002/02/08 22:19:59 $
    */
   class DefaultObjectConfigurer
       implements ObjectConfigurer
  @@ -47,6 +47,11 @@
       private final List m_allProps = new ArrayList();
   
       /**
  +     * The typed property configurer.
  +     */
  +    private PropertyConfigurer m_typedPropConfigurer;
  +
  +    /**
        * Content configurer.
        */
       private PropertyConfigurer m_contentConfigurer;
  @@ -67,15 +72,15 @@
       public void enableAll()
           throws ConfigurationException
       {
  -        // TODO - get rid of creators, and either setter or adders
  -        enableAdders();
  +        // TODO - get rid of creators
  +        enableProperties();
           enableContent();
       }
   
       /**
        * Enables all creators + adders.
        */
  -    public void enableAdders()
  +    private void enableProperties()
           throws ConfigurationException
       {
           final Map creators = findCreators();
  @@ -119,6 +124,16 @@
                   type = addMethod.getParameterTypes()[ 0 ];
               }
   
  +            final boolean isTypedProp = (propName.length() == 0);
  +            if( isTypedProp && !type.isInterface() )
  +            {
  +                final String message =
  +                    REZ.getString( "typed-adder-non-interface.error",
  +                                   m_class.getName(),
  +                                   type.getName() );
  +                throw new ConfigurationException( message );
  +            }
  +
               // Determine the max count for the property
               int maxCount = Integer.MAX_VALUE;
               if( addMethod != null && addMethod.getName().startsWith( "set" ) 
)
  @@ -132,8 +147,15 @@
                                                  createMethod,
                                                  addMethod,
                                                  maxCount );
  -            m_props.put( propName, configurer );
               m_allProps.add( configurer );
  +            if( isTypedProp )
  +            {
  +                m_typedPropConfigurer = configurer;
  +            }
  +            else
  +            {
  +                m_props.put( propName, configurer );
  +            }
           }
       }
   
  @@ -160,19 +182,7 @@
                   continue;
               }
   
  -            final boolean isTypedAdder = methodName.equals( "add" );
  -
  -            final Class paramType = method.getParameterTypes()[ 0 ];
  -            if( isTypedAdder && !paramType.isInterface() )
  -            {
  -                final String message =
  -                    REZ.getString( "typed-adder-non-interface.error",
  -                                   m_class.getName(),
  -                                   paramType.getName() );
  -                throw new ConfigurationException( message );
  -            }
  -
  -            // TODO - un-hard-code this
  +            // Skip the text content method
               if( methodName.equals( "addContent" ) )
               {
                   continue;
  @@ -180,8 +190,7 @@
   
               // Extract property name
               final String propName = extractName( 3, methodName );
  -
  -            final Class type = paramType;
  +            final Class type = method.getParameterTypes()[0];
   
               // Add to the adders map
               if( adders.containsKey( propName ) )
  @@ -190,15 +199,7 @@
                   final Class currentType = candidate.getParameterTypes()[ 0 ];
   
                   // Ditch the string version, if any
  -                if( isTypedAdder )
  -                {
  -                    // Both are string, or both are not string
  -                    final String message =
  -                        REZ.getString( 
"multiple-typed-adder-methods-for-element.error",
  -                                       m_class.getName() );
  -                    throw new ConfigurationException( message );
  -                }
  -                else if( currentType != String.class && type == String.class 
)
  +                if( currentType != String.class && type == String.class )
                   {
                       // New type is string, and current type is not.  Ignore
                       // the new method
  @@ -217,6 +218,7 @@
                   // Else, current type is string, and new type is not, so
                   // continue below, and overwrite the current method
               }
  +
               adders.put( propName, method );
           }
           return adders;
  @@ -253,7 +255,7 @@
                   final String message =
                       REZ.getString( 
"multiple-creator-methods-for-element.error",
                                      m_class.getName(),
  -                                   elemName );
  +                                   methodName );
                   throw new ConfigurationException( message );
               }
               creators.put( elemName, method );
  @@ -264,9 +266,13 @@
       /**
        * Enables content.
        */
  -    public void enableContent()
  +    private void enableContent()
           throws ConfigurationException
       {
  +        // TODO - should be using 'setContent', rather than 'addContent',
  +        // to better match the call-at-most-once semantics of the other
  +        // setter methods
  +
           // Locate any 'addContent' methods, which return void, and take
           // a single parameter.
           final Method[] methods = m_class.getMethods();
  @@ -355,16 +361,25 @@
               return configurer;
           }
   
  -        //Maybe there is a typed adder??
  -        configurer = (PropertyConfigurer)m_props.get( "" );
  -        if( null != configurer )
  +        // Unknown property
  +        final String message = REZ.getString( "unknown-property.error", 
m_class.getName(), name );
  +        throw new NoSuchPropertyException( message );
  +    }
  +
  +    /**
  +     * Returns a configurer for the typed property of this class.
  +     */
  +    public PropertyConfigurer getTypedProperty()
  +        throws NoSuchPropertyException
  +    {
  +        if( null != m_typedPropConfigurer )
           {
  -            return configurer;
  +            return m_typedPropConfigurer;
           }
           else
           {
  -            // Unknown property
  -            final String message = REZ.getString( "unknown-property.error", 
m_class.getName(), name );
  +            // No typed property
  +            final String message = REZ.getString( "no-typed-property.error", 
m_class.getName() );
               throw new NoSuchPropertyException( message );
           }
       }
  
  
  
  1.5       +11 -1     
jakarta-ant/proposal/myrmidon/src/java/org/apache/myrmidon/components/configurer/ObjectConfigurer.java
  
  Index: ObjectConfigurer.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-ant/proposal/myrmidon/src/java/org/apache/myrmidon/components/configurer/ObjectConfigurer.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- ObjectConfigurer.java     27 Jan 2002 23:37:27 -0000      1.4
  +++ ObjectConfigurer.java     8 Feb 2002 22:19:59 -0000       1.5
  @@ -13,7 +13,7 @@
    * Configures objects of a particular class.
    *
    * @author <a href="mailto:[EMAIL PROTECTED]">Adam Murdoch</a>
  - * @version $Revision: 1.4 $ $Date: 2002/01/27 23:37:27 $
  + * @version $Revision: 1.5 $ $Date: 2002/02/08 22:19:59 $
    */
   interface ObjectConfigurer
   {
  @@ -57,5 +57,15 @@
        * @throws NoSuchPropertyException If the class does not handle content.
        */
       PropertyConfigurer getContentConfigurer()
  +        throws NoSuchPropertyException;
  +
  +    /**
  +     * Returns a configurer for the typed property of this class.
  +     *
  +     * @return A configurer for the typed property.
  +     * @throws NoSuchPropertyException If the class does not have a typed
  +     *         property.
  +     */
  +    PropertyConfigurer getTypedProperty()
           throws NoSuchPropertyException;
   }
  
  
  
  1.10      +4 -4      
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.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- Resources.properties      1 Feb 2002 06:46:49 -0000       1.9
  +++ Resources.properties      8 Feb 2002 22:19:59 -0000       1.10
  @@ -3,9 +3,8 @@
   get-ref.error=Could not locate reference "{0}".
   mismatch-ref-types.error=Mismatched type for reference "{0}".  Was expecting 
an object of type {1}, instead found an object of type {2}.
   incompatible-element-types.error=Incompatible creator and adder/setter 
methods found in class {0} for property "{1}".
  -multiple-adder-methods-for-element.error=Multiple adder/setter methods found 
in class {0} for property "{1}".
  -multiple-typed-adder-methods-for-element.error=Multiple typed add() methods 
found in class {0}.
  -multiple-creator-methods-for-element.error=Multiple creator methods found in 
class {0} for property "{1}".
  +multiple-adder-methods-for-element.error=Multiple add{1}() or set{1}() 
methods found in class {0}.
  +multiple-creator-methods-for-element.error=Multiple {1}() methods found in 
class {0}.
   multiple-content-setter-methods.error=Multiple content setter methods found 
in class {0}.
   pending-property-value.error=An object created using the creator method has 
not been set using the adder/setter method.
   unknown-property.error=Class {0} does not have a "{1}" property.
  @@ -22,4 +21,5 @@
   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-factory-for-role.error=Unable to locate type factory for role "{0}"
  -create-typed-object.error=Could not create an object of type "{0}" of class 
{1}.
  \ No newline at end of file
  +create-typed-object.error=Could not create an object of type "{0}" of class 
{1}.
  +typed-property-not-supported.error=Class {0} does not have a typed property.
  \ No newline at end of file
  
  
  
  1.2       +18 -0     
jakarta-ant/proposal/myrmidon/src/testcases/org/apache/myrmidon/components/AbstractComponentTest.java
  
  Index: AbstractComponentTest.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-ant/proposal/myrmidon/src/testcases/org/apache/myrmidon/components/AbstractComponentTest.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- AbstractComponentTest.java        3 Feb 2002 02:35:37 -0000       1.1
  +++ AbstractComponentTest.java        8 Feb 2002 22:20:00 -0000       1.2
  @@ -38,6 +38,9 @@
   import org.apache.myrmidon.interfaces.extensions.ExtensionManager;
   import org.apache.myrmidon.interfaces.role.RoleManager;
   import org.apache.myrmidon.interfaces.type.TypeManager;
  +import org.apache.myrmidon.interfaces.type.TypeException;
  +import org.apache.myrmidon.interfaces.type.DefaultTypeFactory;
  +import org.apache.myrmidon.converter.Converter;
   
   /**
    * A base class for tests for the default components.
  @@ -143,6 +146,21 @@
                   composable.compose( m_componentManager );
               }
           }
  +    }
  +
  +    /**
  +     * Utility method to register a Converter.
  +     */
  +    protected void registerConverter( final Class converterClass,
  +                                      final Class sourceClass,
  +                                      final Class destClass )
  +        throws ComponentException, TypeException
  +    {
  +        ConverterRegistry converterRegistry = 
(ConverterRegistry)getComponentManager().lookup( ConverterRegistry.ROLE );
  +        converterRegistry.registerConverter( converterClass.getName(), 
sourceClass.getName(), destClass.getName() );
  +        DefaultTypeFactory factory = new DefaultTypeFactory( 
getClass().getClassLoader() );
  +        factory.addNameClassMapping( converterClass.getName(), 
converterClass.getName() );
  +        getTypeManager().registerType( Converter.class, 
converterClass.getName(), factory );
       }
   
       /**
  
  
  
  1.11      +130 -12   
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.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- DefaultConfigurerTest.java        3 Feb 2002 02:35:37 -0000       1.10
  +++ DefaultConfigurerTest.java        8 Feb 2002 22:20:00 -0000       1.11
  @@ -8,7 +8,7 @@
   package org.apache.myrmidon.components.configurer;
   
   import java.io.File;
  -import junit.framework.AssertionFailedError;
  +import org.apache.antlib.core.StringToIntegerConverter;
   import org.apache.avalon.excalibur.i18n.ResourceManager;
   import org.apache.avalon.excalibur.i18n.Resources;
   import org.apache.avalon.framework.configuration.ConfigurationException;
  @@ -16,7 +16,9 @@
   import org.apache.myrmidon.api.TaskContext;
   import org.apache.myrmidon.components.AbstractComponentTest;
   import org.apache.myrmidon.components.workspace.DefaultTaskContext;
  +import org.apache.myrmidon.framework.DataType;
   import org.apache.myrmidon.interfaces.configurer.Configurer;
  +import org.apache.myrmidon.interfaces.role.RoleManager;
   import org.apache.myrmidon.interfaces.type.DefaultTypeFactory;
   
   /**
  @@ -81,6 +83,35 @@
       }
   
       /**
  +     * Tests attribute conversion.
  +     */
  +    public void testAttributeConvert()
  +        throws Exception
  +    {
  +        // Setup test data
  +        final DefaultConfiguration config = new DefaultConfiguration( 
"test", "test" );
  +        config.setAttribute( "int-prop", "90" );
  +        config.setAttribute( "integer-prop", "-401" );
  +
  +        // Register the converter
  +        final Class converterClass = StringToIntegerConverter.class;
  +        final Class sourceClass = String.class;
  +        final Class destClass = Integer.class;
  +        registerConverter( converterClass, sourceClass, destClass );
  +
  +        final ConfigTest10 test = new ConfigTest10();
  +
  +        // Configure the object
  +        m_configurer.configure( test, config, m_context );
  +
  +        // Check result
  +        final ConfigTest10 expected = new ConfigTest10();
  +        expected.setIntProp( 90 );
  +        expected.setIntegerProp( new Integer(-401) );
  +        assertEquals( expected, test );
  +    }
  +
  +    /**
        * Tests setting an unknown attribute.
        */
       public void testSetUnknownAttribute()
  @@ -96,7 +127,7 @@
           try
           {
               m_configurer.configure( test, config, m_context );
  -            throw new AssertionFailedError();
  +            fail();
           }
           catch( final ConfigurationException ce )
           {
  @@ -155,7 +186,7 @@
           try
           {
               m_configurer.configure( test, config, m_context );
  -            throw new AssertionFailedError();
  +            fail();
           }
           catch( final ConfigurationException ce )
           {
  @@ -202,7 +233,7 @@
           try
           {
               m_configurer.configure( test, config, m_context );
  -            throw new AssertionFailedError();
  +            fail();
           }
           catch( final ConfigurationException ce )
           {
  @@ -281,6 +312,34 @@
       }
   
       /**
  +     * Tests that extra content is not allowed in a reference element.
  +     */
  +    public void testReferenceElementExtra()
  +        throws Exception
  +    {
  +        // Setup test data
  +        final DefaultConfiguration config = new DefaultConfiguration( 
"test", "test" );
  +        final DefaultConfiguration elem = new DefaultConfiguration( 
"some-prop-ref", "test" );
  +        elem.setAttribute( "id", "prop-a" );
  +        elem.setAttribute( "extra-attr", "some value" );
  +        config.addChild( elem );
  +
  +        final ConfigTest1 test = new ConfigTest1();
  +
  +        try
  +        {
  +            // Configure the object
  +            m_configurer.configure( test, config, m_context );
  +            fail();
  +        }
  +        catch( ConfigurationException e )
  +        {
  +            final String message = REZ.getString( 
"extra-config-for-ref.error" );
  +            assertSameMessage( message, e );
  +        }
  +    }
  +
  +    /**
        * Tests whether an object with a non-iterface typed adder causes an
        * exception.
        */
  @@ -326,8 +385,9 @@
           }
           catch( final ConfigurationException ce )
           {
  -            final String message = REZ.getString( 
"multiple-typed-adder-methods-for-element.error",
  -                                                  
ConfigTest5.class.getName() );
  +            final String message = REZ.getString( 
"multiple-adder-methods-for-element.error",
  +                                                  
ConfigTest5.class.getName(),
  +                                                  "");
               assertSameMessage( message, ce );
           }
       }
  @@ -349,8 +409,8 @@
           final DefaultTypeFactory factory = new DefaultTypeFactory( loader );
           factory.addNameClassMapping( "my-type1", MyType1.class.getName() );
           factory.addNameClassMapping( "my-type2", MyType2.class.getName() );
  -        getTypeManager().registerType( MyRole1.class, "my-type1", factory );
  -        getTypeManager().registerType( MyRole1.class, "my-type2", factory );
  +        getTypeManager().registerType( DataType.class, "my-type1", factory );
  +        getTypeManager().registerType( DataType.class, "my-type2", factory );
   
           final ConfigTest6 test = new ConfigTest6();
   
  @@ -364,6 +424,32 @@
       }
   
       /**
  +     * Tests to see if typed adder can be used via an attribute.
  +     */
  +    public void testTypedAdderAttribute()
  +        throws Exception
  +    {
  +        // Setup test data
  +        final DefaultConfiguration config = new DefaultConfiguration( 
"test", "test" );
  +        config.setAttribute( "my-role1", "some value" );
  +
  +        // Set up the converter and role
  +        RoleManager roleMgr = (RoleManager)getComponentManager().lookup( 
RoleManager.ROLE );
  +        roleMgr.addNameRoleMapping( "my-role1", MyRole1.ROLE );
  +        registerConverter( StringToMyRole1Converter.class, String.class, 
MyRole1.class );
  +
  +        final ConfigTest6 test = new ConfigTest6();
  +
  +        // Configure the object
  +        m_configurer.configure( test, config, m_context );
  +
  +        // Check result
  +        final ConfigTest6 expected = new ConfigTest6();
  +        expected.add( new MyType1() );
  +        assertEquals( expected, test );
  +    }
  +
  +    /**
        * Tests to see if typed adder works, with Configuration type.
        */
       public void testTypedConfigAdder()
  @@ -388,7 +474,7 @@
       }
   
       /**
  -     * Tests to see if typed adder works, with Configuration objects.
  +     * Tests to see if adder works, with Configuration objects.
        */
       public void testConfigAdder()
           throws Exception
  @@ -470,7 +556,7 @@
           try
           {
               m_configurer.configure( test, config, m_context );
  -            throw new AssertionFailedError();
  +            fail();
           }
           catch( ConfigurationException e )
           {
  @@ -498,7 +584,7 @@
           try
           {
               m_configurer.configure( test, config, m_context );
  -            throw new AssertionFailedError();
  +            fail();
           }
           catch( ConfigurationException e )
           {
  @@ -511,6 +597,38 @@
       }
   
       /**
  +     * Tests using a reference with a typed adder.  Tests using an attribute
  +     * and a nested element.
  +     */
  +    public void testTypedAdderReference()
  +        throws Exception
  +    {
  +        // Setup test data
  +        final DefaultConfiguration config = new DefaultConfiguration( 
"test", "test" );
  +        config.setAttribute( "my-role1-ref", "id" );
  +        final DefaultConfiguration child = new DefaultConfiguration( 
"my-role1-ref", "test" );
  +        child.setAttribute( "id", "id2" );
  +        config.addChild( child );
  +
  +        // Add role mapping, and add to reference to context
  +        final RoleManager roleMgr = 
(RoleManager)getComponentManager().lookup( RoleManager.ROLE );
  +        roleMgr.addNameRoleMapping( "my-role1", MyRole1.class.getName() );
  +        m_context.setProperty( "id", new MyType1() );
  +        m_context.setProperty( "id2", new MyType2() );
  +
  +        final ConfigTest6 test = new ConfigTest6();
  +
  +        // Configure the object
  +        m_configurer.configure( test, config, m_context );
  +
  +        // Compare against expected value
  +        final ConfigTest6 expected = new ConfigTest6();
  +        expected.add( new MyType1() );
  +        expected.add( new MyType2() );
  +        assertEquals( expected, test );
  +    }
  +
  +    /**
        * Tests reporting of nested errors.
        */
       public void testNestedErrors() throws Exception
  @@ -527,7 +645,7 @@
           {
               // Configure the object
               m_configurer.configure( test, config, m_context );
  -            throw new AssertionFailedError();
  +            fail();
           }
           catch( ConfigurationException e )
           {
  
  
  
  1.2       +5 -1      
jakarta-ant/proposal/myrmidon/src/testcases/org/apache/myrmidon/components/configurer/MyRole1.java
  
  Index: MyRole1.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-ant/proposal/myrmidon/src/testcases/org/apache/myrmidon/components/configurer/MyRole1.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- MyRole1.java      28 Jan 2002 00:08:31 -0000      1.1
  +++ MyRole1.java      8 Feb 2002 22:20:00 -0000       1.2
  @@ -7,12 +7,16 @@
    */
   package org.apache.myrmidon.components.configurer;
   
  +import org.apache.myrmidon.framework.DataType;
  +
   /**
    * A basic interface to test configurer.
    *
    * @author <a href="mailto:[EMAIL PROTECTED]">Peter Donald</a>
  - * @version $Revision: 1.1 $ $Date: 2002/01/28 00:08:31 $
  + * @version $Revision: 1.2 $ $Date: 2002/02/08 22:20:00 $
    */
   public interface MyRole1
  +    extends DataType
   {
  +    String ROLE = MyRole1.class.getName();
   }
  
  
  
  1.1                  
jakarta-ant/proposal/myrmidon/src/testcases/org/apache/myrmidon/components/configurer/ConfigTest10.java
  
  Index: ConfigTest10.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 org.apache.myrmidon.components.AbstractComponentTest;
  
  /**
   * A class for testing conversion.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]">Adam Murdoch</a>
   */
  public class ConfigTest10
  {
      private int m_intProp;
      private Integer m_integerProp;
  
      public void setIntProp( int intProp )
      {
          m_intProp = intProp;
      }
  
      public void setIntegerProp( Integer integerProp )
      {
          m_integerProp = integerProp;
      }
  
      public boolean equals( Object obj )
      {
          ConfigTest10 test = (ConfigTest10)obj;
          if( m_intProp != test.m_intProp )
          {
              return false;
          }
          if ( !AbstractComponentTest.equals( m_integerProp, test.m_integerProp 
) )
          {
              return false;
          }
  
          return true;
      }
  }
  
  
  
  1.1                  
jakarta-ant/proposal/myrmidon/src/testcases/org/apache/myrmidon/components/configurer/StringToMyRole1Converter.java
  
  Index: StringToMyRole1Converter.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 org.apache.myrmidon.converter.AbstractConverter;
  import org.apache.myrmidon.converter.ConverterException;
  import org.apache.avalon.framework.context.Context;
  
  /**
   * Converts from a string to a [EMAIL PROTECTED] MyRole1} implementation.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]">Adam Murdoch</a>
   */
  public class StringToMyRole1Converter
      extends AbstractConverter
  {
      public StringToMyRole1Converter()
      {
          super( String.class, MyRole1.class );
      }
  
      protected Object convert( Object original, Context context )
          throws ConverterException
      {
          return new MyType1();
      }
  }
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to