From a989b254f04d33e5bc2370b62121bd56830ac2f1 Mon Sep 17 00:00:00 2001 From: adammurdoch Date: Tue, 19 Mar 2002 11:19:24 +0000 Subject: [PATCH] Configurer changes: * Handle the case where a class has both a setFoo() and addFoo() method. * Ignore addContent( String ) if there is a non-String addContent() method. * Ignore add( String ) if there is a non-String add() method. * Added test cases for these. git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@271898 13f79535-47bb-0310-9956-ffa450edef68 --- .../configurer/DefaultObjectConfigurer.java | 282 +++++++++--------- .../configurer/Resources.properties | 8 +- .../test/DefaultConfigurerTestCase.java | 62 +++- .../configurer/test/data/ConfigTestEmpty.java | 4 +- .../data/ConfigTestIgnoreStringMethods.java | 48 ++- .../test/data/ConfigTestSetAndAdd.java | 50 ++++ .../test/data/ConfigTestSetAttribute.java | 12 +- .../test/data/ConfigTestSetElement.java | 12 +- .../test/data/ConfigTestTypedAdder.java | 1 - .../test/data/ConfigTestUnknownReference.java | 3 - .../test/DefaultConfigurerTestCase.java | 62 +++- .../configurer/test/data/ConfigTestEmpty.java | 4 +- .../data/ConfigTestIgnoreStringMethods.java | 48 ++- .../test/data/ConfigTestSetAndAdd.java | 50 ++++ .../test/data/ConfigTestSetAttribute.java | 12 +- .../test/data/ConfigTestSetElement.java | 12 +- .../test/data/ConfigTestTypedAdder.java | 1 - .../test/data/ConfigTestUnknownReference.java | 3 - 18 files changed, 435 insertions(+), 239 deletions(-) create mode 100644 proposal/myrmidon/src/test/org/apache/myrmidon/components/configurer/test/data/ConfigTestSetAndAdd.java create mode 100644 proposal/myrmidon/src/testcases/org/apache/myrmidon/components/configurer/test/data/ConfigTestSetAndAdd.java diff --git a/proposal/myrmidon/src/java/org/apache/myrmidon/components/configurer/DefaultObjectConfigurer.java b/proposal/myrmidon/src/java/org/apache/myrmidon/components/configurer/DefaultObjectConfigurer.java index df4ce15c1..515c25d0b 100644 --- a/proposal/myrmidon/src/java/org/apache/myrmidon/components/configurer/DefaultObjectConfigurer.java +++ b/proposal/myrmidon/src/java/org/apache/myrmidon/components/configurer/DefaultObjectConfigurer.java @@ -10,11 +10,11 @@ package org.apache.myrmidon.components.configurer; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.util.ArrayList; -import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Collection; import org.apache.avalon.excalibur.i18n.ResourceManager; import org.apache.avalon.excalibur.i18n.Resources; import org.apache.avalon.framework.configuration.ConfigurationException; @@ -35,7 +35,7 @@ class DefaultObjectConfigurer private final Class m_class; /** - * All property configurers. (For XML elements) + * Adder property configurers. (For XML elements) */ private final HashMap m_adders = new HashMap(); @@ -54,6 +54,11 @@ class DefaultObjectConfigurer */ private PropertyConfigurer m_contentConfigurer; + /** + * Total number of properties. + */ + private int m_propCount; + /** * Creates an object configurer for a particular class. The newly * created configurer will not handle any attributes, elements, or content. @@ -70,97 +75,143 @@ class DefaultObjectConfigurer private void enableAll() throws ConfigurationException { - enableProperties(); + enableSetters(); + enableAdders(); + enableTypedAdder(); enableContent(); } /** - * Enables all adders. + * Enables all setters. */ - private void enableProperties() + private void enableSetters() throws ConfigurationException { - final Map configurers = findPropertyConfigurers(); + // Locate all the setter methods + final Collection methods = findMethods( "set", false ); - // Add the elements + // Create a configurer for each setter + final Iterator iterator = methods.iterator(); + while( iterator.hasNext() ) + { + final Method method = (Method)iterator.next(); + final Class type = method.getParameterTypes()[ 0 ]; + final String propName = extractName( 3, method.getName() ); - final Iterator iterator = configurers.keySet().iterator(); + final DefaultPropertyConfigurer setter = + new DefaultPropertyConfigurer( getPropertyCount(), + type, + method, + 1 ); + m_setters.put( propName, setter ); + } + } + + /** + * Enables all adders. + */ + private void enableAdders() + throws ConfigurationException + { + // Locate all the adder methods + final Collection methods = findMethods( "add", false ); + + final Iterator iterator = methods.iterator(); while( iterator.hasNext() ) { - final String name = (String)iterator.next(); - final Method method = (Method)configurers.get( name ); - final boolean isSetter = method.getName().startsWith( "set" ); + final Method method = (Method)iterator.next(); + final String methodName = method.getName(); - // Determine and check the return type - final Class type = method.getParameterTypes()[ 0 ]; - final boolean isTypedProp = ( name.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 ); - } - else if( isTypedProp && isSetter ) - { - final String message = - REZ.getString( "typed-setter-not-allowed.error", - m_class.getName(), - type.getName() ); - throw new ConfigurationException( message ); - } - else if( isTypedProp && null != m_typedPropertyConfigurer ) + // Skip the text content method + if( methodName.equals( "addContent" ) ) { - final String message = - REZ.getString( "typed-adder-duplicates.error", - m_class.getName(), - type.getName() ); - throw new ConfigurationException( message ); + continue; } - // Determine the max count for the property - if( isSetter ) - { - final DefaultPropertyConfigurer setter = - new DefaultPropertyConfigurer( getPropertyCount(), - type, - method, - 1 ); - m_setters.put( name, setter ); - } - else - { - final DefaultPropertyConfigurer configurer = - new DefaultPropertyConfigurer( getPropertyCount(), - type, - method, - Integer.MAX_VALUE ); - if( isTypedProp ) - { - m_typedPropertyConfigurer = configurer; - } - else - { - m_adders.put( name, configurer ); - } - } + final Class type = method.getParameterTypes()[ 0 ]; + final String propName = extractName( 3, methodName ); + + final DefaultPropertyConfigurer configurer = + new DefaultPropertyConfigurer( getPropertyCount(), + type, + method, + Integer.MAX_VALUE ); + m_adders.put( propName, configurer ); } } /** - * Locate all 'add' and 'set' methods which return void, and take a - * single parameter. + * Enables the typed adder. */ - private Map findPropertyConfigurers() + private void enableTypedAdder() throws ConfigurationException { - final Map adders = new HashMap(); - final List methodSet = new ArrayList(); - findMethodsWithPrefix( "add", methodSet ); - findMethodsWithPrefix( "set", methodSet ); + final Collection methods = findMethods( "add", true ); + if( methods.size() == 0 ) + { + return; + } + + final Method method = (Method)methods.iterator().next(); + final Class type = method.getParameterTypes()[ 0 ]; + + // TODO - this isn't necessary + if( !type.isInterface() ) + { + final String message = + REZ.getString( "typed-adder-non-interface.error", + m_class.getName(), + type.getName() ); + throw new ConfigurationException( message ); + } + + m_typedPropertyConfigurer + = new DefaultPropertyConfigurer( getPropertyCount(), + type, + method, + Integer.MAX_VALUE ); + } - final Iterator iterator = methodSet.iterator(); + /** + * Enables text content. + */ + private void enableContent() + throws ConfigurationException + { + // Locate the 'addContent' methods, which return void, and take + // a single parameter. + final Collection methods = findMethods( "addContent", true ); + if( methods.size() == 0 ) + { + return; + } + + final Method method = (Method)methods.iterator().next(); + final Class type = method.getParameterTypes()[ 0 ]; + m_contentConfigurer = new DefaultPropertyConfigurer( getPropertyCount(), + type, + method, + 1 ); + } + + /** + * Locate all methods whose name starts with a particular + * prefix, and which are non-static, return void, and take a single + * parameter. If there are more than one matching methods of a given + * name, the method that takes a String parameter (if any) is ignored. + * If after that there are more than one matching methods of a given + * name, an exception is thrown. + * + * @return Map from property name -> Method object for that property. + */ + private Collection findMethods( final String prefix, + final boolean exactMatch ) + throws ConfigurationException + { + final Map methods = new HashMap(); + final List allMethods = findMethodsWithPrefix( prefix, exactMatch ); + + final Iterator iterator = allMethods.iterator(); while( iterator.hasNext() ) { final Method method = (Method)iterator.next(); @@ -171,26 +222,13 @@ class DefaultObjectConfigurer continue; } - // Skip the text content method - if( methodName.equals( "addContent" ) ) - { - continue; - } - // Extract property name - final String propName = extractName( 3, methodName ); final Class type = method.getParameterTypes()[ 0 ]; // Add to the adders map - if( adders.containsKey( propName ) ) + if( methods.containsKey( methodName ) ) { - final Method candidate = (Method)adders.get( propName ); - final String operation = methodName.substring( 0, 3 ); - if( !candidate.getName().startsWith( operation ) ) - { - continue; - } - + final Method candidate = (Method)methods.get( methodName ); final Class currentType = candidate.getParameterTypes()[ 0 ]; // Ditch the string version, if any @@ -202,70 +240,27 @@ class DefaultObjectConfigurer } else if( currentType != String.class || type == String.class ) { - // Both are string, or both are not string + // Both are string (which would be odd), or both are not string final String message = - REZ.getString( "multiple-adder-methods-for-element.error", + REZ.getString( "multiple-methods-for-element.error", m_class.getName(), - propName ); + methodName ); throw new ConfigurationException( message ); } // Else, current type is string, and new type is not, so - // continue below, and overwrite the current method + // continue below, and replace the current method } - adders.put( propName, method ); + methods.put( methodName, method ); } - return adders; - } - /** - * Enables content. - */ - 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(); - for( int i = 0; i < methods.length; i++ ) - { - final Method method = methods[ i ]; - final String methodName = method.getName(); - if( Modifier.isStatic( method.getModifiers() ) || - !methodName.equals( "addContent" ) || - method.getReturnType() != Void.TYPE || - method.getParameterTypes().length != 1 ) - { - continue; - } - - // Check for multiple content setters - if( null != m_contentConfigurer ) - { - final String message = - REZ.getString( "multiple-content-setter-methods.error", m_class.getName() ); - throw new ConfigurationException( message ); - } - - final Class type = method.getParameterTypes()[ 0 ]; - m_contentConfigurer = - new DefaultPropertyConfigurer( getPropertyCount(), - type, - method, - 1 ); - } + return methods.values(); } private int getPropertyCount() { - final int typedSize = ( null != m_typedPropertyConfigurer ) ? 1 : 0; - final int contentSize = ( null != m_contentConfigurer ) ? 1 : 0; - return m_adders.size() + m_setters.size() + contentSize + typedSize; + return m_propCount++; } /** @@ -369,22 +364,31 @@ class DefaultObjectConfigurer * Locates all non-static methods whose name starts with a particular * prefix. */ - private void findMethodsWithPrefix( final String prefix, final Collection matches ) + private List findMethodsWithPrefix( final String prefix, + final boolean exactMatch ) { + final ArrayList matches = new ArrayList(); final int prefixLen = prefix.length(); final Method[] methods = m_class.getMethods(); for( int i = 0; i < methods.length; i++ ) { final Method method = methods[ i ]; final String methodName = method.getName(); - if( Modifier.isStatic( method.getModifiers() ) || - methodName.length() < prefixLen || - !methodName.startsWith( prefix ) ) + if( Modifier.isStatic( method.getModifiers() ) ) + { + continue; + } + if( methodName.length() < prefixLen || !methodName.startsWith( prefix ) ) + { + continue; + } + if( exactMatch && methodName.length() != prefixLen ) { continue; } matches.add( method ); } + return matches; } } diff --git a/proposal/myrmidon/src/java/org/apache/myrmidon/components/configurer/Resources.properties b/proposal/myrmidon/src/java/org/apache/myrmidon/components/configurer/Resources.properties index b8382d6b0..182cacdda 100644 --- a/proposal/myrmidon/src/java/org/apache/myrmidon/components/configurer/Resources.properties +++ b/proposal/myrmidon/src/java/org/apache/myrmidon/components/configurer/Resources.properties @@ -2,9 +2,7 @@ create-object.error=Could not create an object of class {0}. extra-config-for-ref.error=A reference element can only include an "id" attribute. mismatch-ref-types.error=Could not convert reference "{0}" to the type expected for property "{1}". incompatible-element-types.error=Incompatible creator and adder/setter 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-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. +multiple-methods-for-element.error=Multiple non-String {1}() methods found in class {0}. too-many-values.error=Too many values for this property. no-complex-type.error=Can not get complex type for non-primitive type {0}. no-such-attribute.error=Element <{0}> does not support attribute "{1}". @@ -16,6 +14,4 @@ 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. create-typed-object.error=Could not create an object of type "{0}" of class {1}. unknown-reference.error=Could not find referenced object "{0}". -bad-configure-element.error=Could not configure element <{0}>. -typed-setter-not-allowed.error=Not allowed to have "typed" setters as found in class {0}. -typed-adder-duplicates.error=Multiple typed adders found in class {0}. \ No newline at end of file +bad-configure-element.error=Could not configure element <{0}>. \ No newline at end of file diff --git a/proposal/myrmidon/src/test/org/apache/myrmidon/components/configurer/test/DefaultConfigurerTestCase.java b/proposal/myrmidon/src/test/org/apache/myrmidon/components/configurer/test/DefaultConfigurerTestCase.java index d814cda09..4a265b79b 100644 --- a/proposal/myrmidon/src/test/org/apache/myrmidon/components/configurer/test/DefaultConfigurerTestCase.java +++ b/proposal/myrmidon/src/test/org/apache/myrmidon/components/configurer/test/DefaultConfigurerTestCase.java @@ -38,6 +38,7 @@ import org.apache.myrmidon.components.configurer.test.data.ConfigTestTypedAdderR import org.apache.myrmidon.components.configurer.test.data.ConfigTestTypedAdderRole; import org.apache.myrmidon.components.configurer.test.data.ConfigTestTypedConfigAdder; import org.apache.myrmidon.components.configurer.test.data.ConfigTestUnknownReference; +import org.apache.myrmidon.components.configurer.test.data.ConfigTestSetAndAdd; import org.apache.myrmidon.components.workspace.DefaultTaskContext; import org.apache.myrmidon.framework.DataType; import org.apache.myrmidon.interfaces.configurer.Configurer; @@ -80,7 +81,7 @@ public class DefaultConfigurerTestCase } /** - * Tests setting an attribute, via adder and setter methods. + * Tests setting an attribute, via a setter method. */ public void testSetAttribute() throws Exception @@ -89,8 +90,6 @@ public class DefaultConfigurerTestCase final DefaultConfiguration config = new DefaultConfiguration( "test", "test" ); final String value1 = "some value"; config.setAttribute( "some-prop", value1 ); - final String value2 = "some other value"; - config.setAttribute( "prop", value2 ); final ConfigTestSetAttribute test = new ConfigTestSetAttribute(); @@ -100,7 +99,6 @@ public class DefaultConfigurerTestCase // Check result final ConfigTestSetAttribute expected = new ConfigTestSetAttribute(); expected.setSomeProp( value1 ); - expected.setProp( value2 ); assertEquals( expected, test ); } @@ -167,7 +165,7 @@ public class DefaultConfigurerTestCase final String value1 = "some value"; child1.setAttribute( "some-prop", value1 ); config.addChild( child1 ); - final DefaultConfiguration child2 = new DefaultConfiguration( "another-prop", "test" ); + final DefaultConfiguration child2 = new DefaultConfiguration( "prop", "test" ); final String value2 = "another value"; child2.setAttribute( "some-prop", value2 ); config.addChild( child2 ); @@ -184,7 +182,7 @@ public class DefaultConfigurerTestCase expected.addProp( elem ); elem = new ConfigTestSetElement(); elem.setSomeProp( value2 ); - expected.addAnotherProp( elem ); + expected.addProp( elem ); assertEquals( expected, test ); } @@ -465,9 +463,9 @@ public class DefaultConfigurerTestCase final String[] messages = new String[] { REZ.getString( "bad-configure-element.error", "test" ), - REZ.getString( "multiple-adder-methods-for-element.error", + REZ.getString( "multiple-methods-for-element.error", ConfigTestMultipleTypedAdder.class.getName(), - "" ) + "add" ) }; assertSameMessage( messages, ce ); } @@ -486,8 +484,9 @@ public class DefaultConfigurerTestCase config.addChild( child1 ); config.addChild( child2 ); - registerType( DataType.ROLE, "my-type1", MyType1.class ); - registerType( DataType.ROLE, "my-type2", MyType2.class ); + registerRole( new RoleInfo( MyRole1.ROLE, "my-role1", MyRole1.class ) ); + registerType( MyRole1.ROLE, "my-type1", MyType1.class ); + registerType( MyRole1.ROLE, "my-type2", MyType2.class ); final ConfigTestTypedAdder test = new ConfigTestTypedAdder(); @@ -606,7 +605,7 @@ public class DefaultConfigurerTestCase /** * Tests to check that Configurable is handled properly. */ - public void testConfigable() + public void testConfigurable() throws Exception { // Setup test data @@ -773,11 +772,17 @@ public class DefaultConfigurerTestCase { // Setup test data final DefaultConfiguration config = new DefaultConfiguration( "test", "test" ); - DefaultConfiguration elem = new DefaultConfiguration( "prop1", "test" ); + config.setAttribute( "prop1", "some-value" ); + config.setValue( "99" ); + DefaultConfiguration elem = new DefaultConfiguration( "prop2", "test" ); config.addChild( elem ); - elem = new DefaultConfiguration( "prop2", "test" ); + elem = new DefaultConfiguration( "my-type1", "test" ); config.addChild( elem ); + registerConverter( ObjectToMyRole1Converter.class, String.class, MyRole1.class ); + registerConverter( StringToIntegerConverter.class, String.class, Integer.class ); + registerType( DataType.ROLE, "my-type1", MyType1.class ); + final ConfigTestIgnoreStringMethods test = new ConfigTestIgnoreStringMethods(); // Configure the object @@ -785,8 +790,37 @@ public class DefaultConfigurerTestCase // Test expected value final ConfigTestIgnoreStringMethods expected = new ConfigTestIgnoreStringMethods(); - expected.addProp1( new ConfigTestIgnoreStringMethods() ); + expected.setProp1( new MyRole1Adaptor( "some-value" ) ); expected.addProp2( new ConfigTestIgnoreStringMethods() ); + expected.add( new MyType1() ); + expected.addContent( 99 ); + assertEquals( expected, test ); + } + + /** + * Tests that a class with a setter and adder with the same property name + * is handled correctly. + */ + public void testSetAndAdd() throws Exception + { + // Setup test data + final DefaultConfiguration config = new DefaultConfiguration( "test", "test" ); + config.setAttribute( "prop", "some value" ); + DefaultConfiguration elem = new DefaultConfiguration( "prop", "test" ); + elem.setAttribute( "prop", "another value" ); + config.addChild( elem ); + + final ConfigTestSetAndAdd test = new ConfigTestSetAndAdd(); + + // Configure the object + configure( test, config ); + + // Test expected value + final ConfigTestSetAndAdd expected = new ConfigTestSetAndAdd(); + expected.setProp( "some value" ); + final ConfigTestSetAndAdd nested = new ConfigTestSetAndAdd(); + nested.setProp( "another value" ); + expected.addProp( nested ); assertEquals( expected, test ); } diff --git a/proposal/myrmidon/src/test/org/apache/myrmidon/components/configurer/test/data/ConfigTestEmpty.java b/proposal/myrmidon/src/test/org/apache/myrmidon/components/configurer/test/data/ConfigTestEmpty.java index b39612392..7438e1b6b 100644 --- a/proposal/myrmidon/src/test/org/apache/myrmidon/components/configurer/test/data/ConfigTestEmpty.java +++ b/proposal/myrmidon/src/test/org/apache/myrmidon/components/configurer/test/data/ConfigTestEmpty.java @@ -7,12 +7,10 @@ */ package org.apache.myrmidon.components.configurer.test.data; -import java.util.ArrayList; -import java.util.List; import org.apache.myrmidon.framework.DataType; /** - * A simple test class with string properties. + * An empty class. * * @author Adam Murdoch */ diff --git a/proposal/myrmidon/src/test/org/apache/myrmidon/components/configurer/test/data/ConfigTestIgnoreStringMethods.java b/proposal/myrmidon/src/test/org/apache/myrmidon/components/configurer/test/data/ConfigTestIgnoreStringMethods.java index 369649b7c..9ecd3d688 100644 --- a/proposal/myrmidon/src/test/org/apache/myrmidon/components/configurer/test/data/ConfigTestIgnoreStringMethods.java +++ b/proposal/myrmidon/src/test/org/apache/myrmidon/components/configurer/test/data/ConfigTestIgnoreStringMethods.java @@ -10,6 +10,7 @@ package org.apache.myrmidon.components.configurer.test.data; import java.util.ArrayList; import junit.framework.AssertionFailedError; import org.apache.myrmidon.components.configurer.test.DefaultConfigurerTestCase; +import org.apache.myrmidon.components.configurer.test.MyRole1; /** * A test class with multiple setters/adders/creators for a property. @@ -18,8 +19,10 @@ import org.apache.myrmidon.components.configurer.test.DefaultConfigurerTestCase; */ public class ConfigTestIgnoreStringMethods { - private ConfigTestIgnoreStringMethods m_prop1; + private MyRole1 m_prop1; private ArrayList m_prop2 = new ArrayList(); + private int m_content; + private ArrayList m_typed = new ArrayList(); public boolean equals( Object obj ) { @@ -32,19 +35,27 @@ public class ConfigTestIgnoreStringMethods { return false; } + if( m_content != test.m_content ) + { + return false; + } + if( !m_typed.equals( test.m_typed ) ) + { + return false; + } return true; } // - // Multiple setters + // Multiple Setters // - public void addProp1( final String value ) + public void setProp1( final String value ) { throw new AssertionFailedError(); } - public void addProp1( final ConfigTestIgnoreStringMethods value ) + public void setProp1( final MyRole1 value ) { m_prop1 = value; } @@ -62,4 +73,33 @@ public class ConfigTestIgnoreStringMethods { m_prop2.add( value ); } + + // + // Multiple typed adders + // + + public void add( final String value ) + { + throw new AssertionFailedError(); + } + + public void add( final MyRole1 value ) + { + m_typed.add( value ); + } + + // + // Multiple content setters + // + + public void addContent( final int value ) + { + m_content = value; + } + + public void addContent( final String value ) + { + throw new AssertionFailedError(); + } + } diff --git a/proposal/myrmidon/src/test/org/apache/myrmidon/components/configurer/test/data/ConfigTestSetAndAdd.java b/proposal/myrmidon/src/test/org/apache/myrmidon/components/configurer/test/data/ConfigTestSetAndAdd.java new file mode 100644 index 000000000..68ebb1d7a --- /dev/null +++ b/proposal/myrmidon/src/test/org/apache/myrmidon/components/configurer/test/data/ConfigTestSetAndAdd.java @@ -0,0 +1,50 @@ +/* + * 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.test.data; + +import java.util.ArrayList; +import org.apache.myrmidon.AbstractMyrmidonTest; + +/** + * A test class with a setter and adder with the same property name. + * + * @author Adam Murdoch + * @version $Revision$ $Date$ + */ +public class ConfigTestSetAndAdd +{ + private String m_prop; + private ArrayList m_nested = new ArrayList(); + + public void setProp( final String prop ) + { + m_prop = prop; + } + + public void addProp( final ConfigTestSetAndAdd elem ) + { + m_nested.add( elem ); + } + + public boolean equals( final Object obj ) + { + ConfigTestSetAndAdd test = (ConfigTestSetAndAdd)obj; + if( ! AbstractMyrmidonTest.equals( m_prop, test.m_prop) ) + { + return false; + } + else if( ! m_nested.equals( test.m_nested ) ) + { + return false; + } + else + { + return true; + } + } +} diff --git a/proposal/myrmidon/src/test/org/apache/myrmidon/components/configurer/test/data/ConfigTestSetAttribute.java b/proposal/myrmidon/src/test/org/apache/myrmidon/components/configurer/test/data/ConfigTestSetAttribute.java index 0a76b998a..4a933ef33 100644 --- a/proposal/myrmidon/src/test/org/apache/myrmidon/components/configurer/test/data/ConfigTestSetAttribute.java +++ b/proposal/myrmidon/src/test/org/apache/myrmidon/components/configurer/test/data/ConfigTestSetAttribute.java @@ -12,7 +12,7 @@ import java.util.ArrayList; import org.apache.myrmidon.components.configurer.test.DefaultConfigurerTestCase; /** - * Simple class to test typed adder. + * Simple class to test setter. * * @author Peter Donald * @version $Revision$ $Date$ @@ -20,7 +20,6 @@ import org.apache.myrmidon.components.configurer.test.DefaultConfigurerTestCase; public class ConfigTestSetAttribute { private String m_someProp; - private List m_propList = new ArrayList(); public boolean equals( final Object obj ) { @@ -29,10 +28,6 @@ public class ConfigTestSetAttribute { return false; } - else if( !m_propList.equals( test.m_propList ) ) - { - return false; - } else { return true; @@ -43,9 +38,4 @@ public class ConfigTestSetAttribute { m_someProp = value; } - - public void setProp( final String value ) - { - m_propList.add( value ); - } } diff --git a/proposal/myrmidon/src/test/org/apache/myrmidon/components/configurer/test/data/ConfigTestSetElement.java b/proposal/myrmidon/src/test/org/apache/myrmidon/components/configurer/test/data/ConfigTestSetElement.java index 704248b46..d360a72ea 100644 --- a/proposal/myrmidon/src/test/org/apache/myrmidon/components/configurer/test/data/ConfigTestSetElement.java +++ b/proposal/myrmidon/src/test/org/apache/myrmidon/components/configurer/test/data/ConfigTestSetElement.java @@ -18,18 +18,13 @@ import org.apache.myrmidon.components.configurer.test.DefaultConfigurerTestCase; */ public class ConfigTestSetElement { - private ConfigTestSetElement m_prop; private List m_propList = new ArrayList(); private String m_someProp; public boolean equals( Object obj ) { ConfigTestSetElement test = (ConfigTestSetElement)obj; - if( !DefaultConfigurerTestCase.equals( m_prop, test.m_prop ) ) - { - return false; - } - else if( !m_propList.equals( test.m_propList ) ) + if( !m_propList.equals( test.m_propList ) ) { return false; } @@ -46,11 +41,6 @@ public class ConfigTestSetElement } public void addProp( final ConfigTestSetElement test ) - { - m_prop = test; - } - - public void addAnotherProp( final ConfigTestSetElement test ) { m_propList.add( test ); } diff --git a/proposal/myrmidon/src/test/org/apache/myrmidon/components/configurer/test/data/ConfigTestTypedAdder.java b/proposal/myrmidon/src/test/org/apache/myrmidon/components/configurer/test/data/ConfigTestTypedAdder.java index 4beafcdd6..fdea6422b 100644 --- a/proposal/myrmidon/src/test/org/apache/myrmidon/components/configurer/test/data/ConfigTestTypedAdder.java +++ b/proposal/myrmidon/src/test/org/apache/myrmidon/components/configurer/test/data/ConfigTestTypedAdder.java @@ -8,7 +8,6 @@ package org.apache.myrmidon.components.configurer.test.data; import java.util.ArrayList; -import org.apache.avalon.framework.configuration.Configuration; import org.apache.myrmidon.components.configurer.test.MyRole1; /** diff --git a/proposal/myrmidon/src/test/org/apache/myrmidon/components/configurer/test/data/ConfigTestUnknownReference.java b/proposal/myrmidon/src/test/org/apache/myrmidon/components/configurer/test/data/ConfigTestUnknownReference.java index b27b04a8c..d304f1c4c 100644 --- a/proposal/myrmidon/src/test/org/apache/myrmidon/components/configurer/test/data/ConfigTestUnknownReference.java +++ b/proposal/myrmidon/src/test/org/apache/myrmidon/components/configurer/test/data/ConfigTestUnknownReference.java @@ -7,10 +7,7 @@ */ package org.apache.myrmidon.components.configurer.test.data; -import java.util.ArrayList; -import java.util.List; import org.apache.myrmidon.framework.DataType; -import org.apache.myrmidon.components.configurer.test.DefaultConfigurerTestCase; /** * A simple test class with string properties. diff --git a/proposal/myrmidon/src/testcases/org/apache/myrmidon/components/configurer/test/DefaultConfigurerTestCase.java b/proposal/myrmidon/src/testcases/org/apache/myrmidon/components/configurer/test/DefaultConfigurerTestCase.java index d814cda09..4a265b79b 100644 --- a/proposal/myrmidon/src/testcases/org/apache/myrmidon/components/configurer/test/DefaultConfigurerTestCase.java +++ b/proposal/myrmidon/src/testcases/org/apache/myrmidon/components/configurer/test/DefaultConfigurerTestCase.java @@ -38,6 +38,7 @@ import org.apache.myrmidon.components.configurer.test.data.ConfigTestTypedAdderR import org.apache.myrmidon.components.configurer.test.data.ConfigTestTypedAdderRole; import org.apache.myrmidon.components.configurer.test.data.ConfigTestTypedConfigAdder; import org.apache.myrmidon.components.configurer.test.data.ConfigTestUnknownReference; +import org.apache.myrmidon.components.configurer.test.data.ConfigTestSetAndAdd; import org.apache.myrmidon.components.workspace.DefaultTaskContext; import org.apache.myrmidon.framework.DataType; import org.apache.myrmidon.interfaces.configurer.Configurer; @@ -80,7 +81,7 @@ public class DefaultConfigurerTestCase } /** - * Tests setting an attribute, via adder and setter methods. + * Tests setting an attribute, via a setter method. */ public void testSetAttribute() throws Exception @@ -89,8 +90,6 @@ public class DefaultConfigurerTestCase final DefaultConfiguration config = new DefaultConfiguration( "test", "test" ); final String value1 = "some value"; config.setAttribute( "some-prop", value1 ); - final String value2 = "some other value"; - config.setAttribute( "prop", value2 ); final ConfigTestSetAttribute test = new ConfigTestSetAttribute(); @@ -100,7 +99,6 @@ public class DefaultConfigurerTestCase // Check result final ConfigTestSetAttribute expected = new ConfigTestSetAttribute(); expected.setSomeProp( value1 ); - expected.setProp( value2 ); assertEquals( expected, test ); } @@ -167,7 +165,7 @@ public class DefaultConfigurerTestCase final String value1 = "some value"; child1.setAttribute( "some-prop", value1 ); config.addChild( child1 ); - final DefaultConfiguration child2 = new DefaultConfiguration( "another-prop", "test" ); + final DefaultConfiguration child2 = new DefaultConfiguration( "prop", "test" ); final String value2 = "another value"; child2.setAttribute( "some-prop", value2 ); config.addChild( child2 ); @@ -184,7 +182,7 @@ public class DefaultConfigurerTestCase expected.addProp( elem ); elem = new ConfigTestSetElement(); elem.setSomeProp( value2 ); - expected.addAnotherProp( elem ); + expected.addProp( elem ); assertEquals( expected, test ); } @@ -465,9 +463,9 @@ public class DefaultConfigurerTestCase final String[] messages = new String[] { REZ.getString( "bad-configure-element.error", "test" ), - REZ.getString( "multiple-adder-methods-for-element.error", + REZ.getString( "multiple-methods-for-element.error", ConfigTestMultipleTypedAdder.class.getName(), - "" ) + "add" ) }; assertSameMessage( messages, ce ); } @@ -486,8 +484,9 @@ public class DefaultConfigurerTestCase config.addChild( child1 ); config.addChild( child2 ); - registerType( DataType.ROLE, "my-type1", MyType1.class ); - registerType( DataType.ROLE, "my-type2", MyType2.class ); + registerRole( new RoleInfo( MyRole1.ROLE, "my-role1", MyRole1.class ) ); + registerType( MyRole1.ROLE, "my-type1", MyType1.class ); + registerType( MyRole1.ROLE, "my-type2", MyType2.class ); final ConfigTestTypedAdder test = new ConfigTestTypedAdder(); @@ -606,7 +605,7 @@ public class DefaultConfigurerTestCase /** * Tests to check that Configurable is handled properly. */ - public void testConfigable() + public void testConfigurable() throws Exception { // Setup test data @@ -773,11 +772,17 @@ public class DefaultConfigurerTestCase { // Setup test data final DefaultConfiguration config = new DefaultConfiguration( "test", "test" ); - DefaultConfiguration elem = new DefaultConfiguration( "prop1", "test" ); + config.setAttribute( "prop1", "some-value" ); + config.setValue( "99" ); + DefaultConfiguration elem = new DefaultConfiguration( "prop2", "test" ); config.addChild( elem ); - elem = new DefaultConfiguration( "prop2", "test" ); + elem = new DefaultConfiguration( "my-type1", "test" ); config.addChild( elem ); + registerConverter( ObjectToMyRole1Converter.class, String.class, MyRole1.class ); + registerConverter( StringToIntegerConverter.class, String.class, Integer.class ); + registerType( DataType.ROLE, "my-type1", MyType1.class ); + final ConfigTestIgnoreStringMethods test = new ConfigTestIgnoreStringMethods(); // Configure the object @@ -785,8 +790,37 @@ public class DefaultConfigurerTestCase // Test expected value final ConfigTestIgnoreStringMethods expected = new ConfigTestIgnoreStringMethods(); - expected.addProp1( new ConfigTestIgnoreStringMethods() ); + expected.setProp1( new MyRole1Adaptor( "some-value" ) ); expected.addProp2( new ConfigTestIgnoreStringMethods() ); + expected.add( new MyType1() ); + expected.addContent( 99 ); + assertEquals( expected, test ); + } + + /** + * Tests that a class with a setter and adder with the same property name + * is handled correctly. + */ + public void testSetAndAdd() throws Exception + { + // Setup test data + final DefaultConfiguration config = new DefaultConfiguration( "test", "test" ); + config.setAttribute( "prop", "some value" ); + DefaultConfiguration elem = new DefaultConfiguration( "prop", "test" ); + elem.setAttribute( "prop", "another value" ); + config.addChild( elem ); + + final ConfigTestSetAndAdd test = new ConfigTestSetAndAdd(); + + // Configure the object + configure( test, config ); + + // Test expected value + final ConfigTestSetAndAdd expected = new ConfigTestSetAndAdd(); + expected.setProp( "some value" ); + final ConfigTestSetAndAdd nested = new ConfigTestSetAndAdd(); + nested.setProp( "another value" ); + expected.addProp( nested ); assertEquals( expected, test ); } diff --git a/proposal/myrmidon/src/testcases/org/apache/myrmidon/components/configurer/test/data/ConfigTestEmpty.java b/proposal/myrmidon/src/testcases/org/apache/myrmidon/components/configurer/test/data/ConfigTestEmpty.java index b39612392..7438e1b6b 100644 --- a/proposal/myrmidon/src/testcases/org/apache/myrmidon/components/configurer/test/data/ConfigTestEmpty.java +++ b/proposal/myrmidon/src/testcases/org/apache/myrmidon/components/configurer/test/data/ConfigTestEmpty.java @@ -7,12 +7,10 @@ */ package org.apache.myrmidon.components.configurer.test.data; -import java.util.ArrayList; -import java.util.List; import org.apache.myrmidon.framework.DataType; /** - * A simple test class with string properties. + * An empty class. * * @author Adam Murdoch */ diff --git a/proposal/myrmidon/src/testcases/org/apache/myrmidon/components/configurer/test/data/ConfigTestIgnoreStringMethods.java b/proposal/myrmidon/src/testcases/org/apache/myrmidon/components/configurer/test/data/ConfigTestIgnoreStringMethods.java index 369649b7c..9ecd3d688 100644 --- a/proposal/myrmidon/src/testcases/org/apache/myrmidon/components/configurer/test/data/ConfigTestIgnoreStringMethods.java +++ b/proposal/myrmidon/src/testcases/org/apache/myrmidon/components/configurer/test/data/ConfigTestIgnoreStringMethods.java @@ -10,6 +10,7 @@ package org.apache.myrmidon.components.configurer.test.data; import java.util.ArrayList; import junit.framework.AssertionFailedError; import org.apache.myrmidon.components.configurer.test.DefaultConfigurerTestCase; +import org.apache.myrmidon.components.configurer.test.MyRole1; /** * A test class with multiple setters/adders/creators for a property. @@ -18,8 +19,10 @@ import org.apache.myrmidon.components.configurer.test.DefaultConfigurerTestCase; */ public class ConfigTestIgnoreStringMethods { - private ConfigTestIgnoreStringMethods m_prop1; + private MyRole1 m_prop1; private ArrayList m_prop2 = new ArrayList(); + private int m_content; + private ArrayList m_typed = new ArrayList(); public boolean equals( Object obj ) { @@ -32,19 +35,27 @@ public class ConfigTestIgnoreStringMethods { return false; } + if( m_content != test.m_content ) + { + return false; + } + if( !m_typed.equals( test.m_typed ) ) + { + return false; + } return true; } // - // Multiple setters + // Multiple Setters // - public void addProp1( final String value ) + public void setProp1( final String value ) { throw new AssertionFailedError(); } - public void addProp1( final ConfigTestIgnoreStringMethods value ) + public void setProp1( final MyRole1 value ) { m_prop1 = value; } @@ -62,4 +73,33 @@ public class ConfigTestIgnoreStringMethods { m_prop2.add( value ); } + + // + // Multiple typed adders + // + + public void add( final String value ) + { + throw new AssertionFailedError(); + } + + public void add( final MyRole1 value ) + { + m_typed.add( value ); + } + + // + // Multiple content setters + // + + public void addContent( final int value ) + { + m_content = value; + } + + public void addContent( final String value ) + { + throw new AssertionFailedError(); + } + } diff --git a/proposal/myrmidon/src/testcases/org/apache/myrmidon/components/configurer/test/data/ConfigTestSetAndAdd.java b/proposal/myrmidon/src/testcases/org/apache/myrmidon/components/configurer/test/data/ConfigTestSetAndAdd.java new file mode 100644 index 000000000..68ebb1d7a --- /dev/null +++ b/proposal/myrmidon/src/testcases/org/apache/myrmidon/components/configurer/test/data/ConfigTestSetAndAdd.java @@ -0,0 +1,50 @@ +/* + * 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.test.data; + +import java.util.ArrayList; +import org.apache.myrmidon.AbstractMyrmidonTest; + +/** + * A test class with a setter and adder with the same property name. + * + * @author Adam Murdoch + * @version $Revision$ $Date$ + */ +public class ConfigTestSetAndAdd +{ + private String m_prop; + private ArrayList m_nested = new ArrayList(); + + public void setProp( final String prop ) + { + m_prop = prop; + } + + public void addProp( final ConfigTestSetAndAdd elem ) + { + m_nested.add( elem ); + } + + public boolean equals( final Object obj ) + { + ConfigTestSetAndAdd test = (ConfigTestSetAndAdd)obj; + if( ! AbstractMyrmidonTest.equals( m_prop, test.m_prop) ) + { + return false; + } + else if( ! m_nested.equals( test.m_nested ) ) + { + return false; + } + else + { + return true; + } + } +} diff --git a/proposal/myrmidon/src/testcases/org/apache/myrmidon/components/configurer/test/data/ConfigTestSetAttribute.java b/proposal/myrmidon/src/testcases/org/apache/myrmidon/components/configurer/test/data/ConfigTestSetAttribute.java index 0a76b998a..4a933ef33 100644 --- a/proposal/myrmidon/src/testcases/org/apache/myrmidon/components/configurer/test/data/ConfigTestSetAttribute.java +++ b/proposal/myrmidon/src/testcases/org/apache/myrmidon/components/configurer/test/data/ConfigTestSetAttribute.java @@ -12,7 +12,7 @@ import java.util.ArrayList; import org.apache.myrmidon.components.configurer.test.DefaultConfigurerTestCase; /** - * Simple class to test typed adder. + * Simple class to test setter. * * @author Peter Donald * @version $Revision$ $Date$ @@ -20,7 +20,6 @@ import org.apache.myrmidon.components.configurer.test.DefaultConfigurerTestCase; public class ConfigTestSetAttribute { private String m_someProp; - private List m_propList = new ArrayList(); public boolean equals( final Object obj ) { @@ -29,10 +28,6 @@ public class ConfigTestSetAttribute { return false; } - else if( !m_propList.equals( test.m_propList ) ) - { - return false; - } else { return true; @@ -43,9 +38,4 @@ public class ConfigTestSetAttribute { m_someProp = value; } - - public void setProp( final String value ) - { - m_propList.add( value ); - } } diff --git a/proposal/myrmidon/src/testcases/org/apache/myrmidon/components/configurer/test/data/ConfigTestSetElement.java b/proposal/myrmidon/src/testcases/org/apache/myrmidon/components/configurer/test/data/ConfigTestSetElement.java index 704248b46..d360a72ea 100644 --- a/proposal/myrmidon/src/testcases/org/apache/myrmidon/components/configurer/test/data/ConfigTestSetElement.java +++ b/proposal/myrmidon/src/testcases/org/apache/myrmidon/components/configurer/test/data/ConfigTestSetElement.java @@ -18,18 +18,13 @@ import org.apache.myrmidon.components.configurer.test.DefaultConfigurerTestCase; */ public class ConfigTestSetElement { - private ConfigTestSetElement m_prop; private List m_propList = new ArrayList(); private String m_someProp; public boolean equals( Object obj ) { ConfigTestSetElement test = (ConfigTestSetElement)obj; - if( !DefaultConfigurerTestCase.equals( m_prop, test.m_prop ) ) - { - return false; - } - else if( !m_propList.equals( test.m_propList ) ) + if( !m_propList.equals( test.m_propList ) ) { return false; } @@ -46,11 +41,6 @@ public class ConfigTestSetElement } public void addProp( final ConfigTestSetElement test ) - { - m_prop = test; - } - - public void addAnotherProp( final ConfigTestSetElement test ) { m_propList.add( test ); } diff --git a/proposal/myrmidon/src/testcases/org/apache/myrmidon/components/configurer/test/data/ConfigTestTypedAdder.java b/proposal/myrmidon/src/testcases/org/apache/myrmidon/components/configurer/test/data/ConfigTestTypedAdder.java index 4beafcdd6..fdea6422b 100644 --- a/proposal/myrmidon/src/testcases/org/apache/myrmidon/components/configurer/test/data/ConfigTestTypedAdder.java +++ b/proposal/myrmidon/src/testcases/org/apache/myrmidon/components/configurer/test/data/ConfigTestTypedAdder.java @@ -8,7 +8,6 @@ package org.apache.myrmidon.components.configurer.test.data; import java.util.ArrayList; -import org.apache.avalon.framework.configuration.Configuration; import org.apache.myrmidon.components.configurer.test.MyRole1; /** diff --git a/proposal/myrmidon/src/testcases/org/apache/myrmidon/components/configurer/test/data/ConfigTestUnknownReference.java b/proposal/myrmidon/src/testcases/org/apache/myrmidon/components/configurer/test/data/ConfigTestUnknownReference.java index b27b04a8c..d304f1c4c 100644 --- a/proposal/myrmidon/src/testcases/org/apache/myrmidon/components/configurer/test/data/ConfigTestUnknownReference.java +++ b/proposal/myrmidon/src/testcases/org/apache/myrmidon/components/configurer/test/data/ConfigTestUnknownReference.java @@ -7,10 +7,7 @@ */ package org.apache.myrmidon.components.configurer.test.data; -import java.util.ArrayList; -import java.util.List; import org.apache.myrmidon.framework.DataType; -import org.apache.myrmidon.components.configurer.test.DefaultConfigurerTestCase; /** * A simple test class with string properties.