Browse Source

* Changed DefaultPropertyResolver to use the converter to convert from Object -> String.

* Added general purpose Object -> String converter that simply uses Object.toString().

* Reorganised AbstractComponentTestCase, to give sub-classes the opportunity to replace
  the default implementation of a component, with a particular implementation.


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@271942 13f79535-47bb-0310-9956-ffa450edef68
master
adammurdoch 23 years ago
parent
commit
56074611a9
10 changed files with 242 additions and 38 deletions
  1. +38
    -0
      proposal/myrmidon/src/java/org/apache/antlib/core/ObjectToStringConverter.java
  2. +40
    -6
      proposal/myrmidon/src/java/org/apache/myrmidon/components/property/DefaultPropertyResolver.java
  3. +19
    -9
      proposal/myrmidon/src/test/org/apache/myrmidon/components/AbstractComponentTest.java
  4. +19
    -1
      proposal/myrmidon/src/test/org/apache/myrmidon/components/configurer/test/DefaultConfigurerTestCase.java
  5. +18
    -0
      proposal/myrmidon/src/test/org/apache/myrmidon/components/deployer/test/DefaultDeployerTestCase.java
  6. +26
    -6
      proposal/myrmidon/src/test/org/apache/myrmidon/components/property/test/AbstractPropertyResolverTestCase.java
  7. +19
    -9
      proposal/myrmidon/src/testcases/org/apache/myrmidon/components/AbstractComponentTest.java
  8. +19
    -1
      proposal/myrmidon/src/testcases/org/apache/myrmidon/components/configurer/test/DefaultConfigurerTestCase.java
  9. +18
    -0
      proposal/myrmidon/src/testcases/org/apache/myrmidon/components/deployer/test/DefaultDeployerTestCase.java
  10. +26
    -6
      proposal/myrmidon/src/testcases/org/apache/myrmidon/components/property/test/AbstractPropertyResolverTestCase.java

+ 38
- 0
proposal/myrmidon/src/java/org/apache/antlib/core/ObjectToStringConverter.java View File

@@ -0,0 +1,38 @@
/*
* 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.antlib.core;

import org.apache.aut.converter.AbstractConverter;
import org.apache.aut.converter.ConverterException;

/**
* A general-purpose converter that converts an Object to a String using
* its toString() method.
*
* @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
* @version $Revision$ $Date$
*
* @ant:converter source="java.lang.Object" destination="java.lang.String"
*/
public class ObjectToStringConverter
extends AbstractConverter
{
public ObjectToStringConverter()
{
super( Object.class, String.class );
}

/**
* Converts an object.
*/
protected Object convert( final Object original, final Object context )
throws ConverterException
{
return original.toString();
}
}

+ 40
- 6
proposal/myrmidon/src/java/org/apache/myrmidon/components/property/DefaultPropertyResolver.java View File

@@ -7,10 +7,15 @@
*/
package org.apache.myrmidon.components.property;

import org.apache.aut.converter.Converter;
import org.apache.aut.converter.ConverterException;
import org.apache.avalon.excalibur.i18n.ResourceManager;
import org.apache.avalon.excalibur.i18n.Resources;
import org.apache.myrmidon.api.TaskException;
import org.apache.avalon.framework.service.ServiceException;
import org.apache.avalon.framework.service.ServiceManager;
import org.apache.avalon.framework.service.Serviceable;
import org.apache.myrmidon.api.TaskContext;
import org.apache.myrmidon.api.TaskException;
import org.apache.myrmidon.interfaces.property.PropertyResolver;

/**
@@ -21,11 +26,18 @@ import org.apache.myrmidon.interfaces.property.PropertyResolver;
* @version $Revision$ $Date$
*/
public class DefaultPropertyResolver
implements PropertyResolver
implements PropertyResolver, Serviceable
{
private final static Resources REZ =
ResourceManager.getPackageResources( DefaultPropertyResolver.class );

private Converter m_converter;

public void service( final ServiceManager serviceManager ) throws ServiceException
{
m_converter = (Converter)serviceManager.lookup( Converter.ROLE );
}

/**
* Resolve a string property. This evaluates all property
* substitutions based on specified context.
@@ -66,9 +78,9 @@ public class DefaultPropertyResolver

while( true )
{
final Object propertyValue =
getPropertyValue( content.substring( start + 2, end ),
context );
final String propertyValue =
getPropertyStringValue( content.substring( start + 2, end ),
context );

sb.append( content.substring( lastPlace, start ) );
sb.append( propertyValue );
@@ -127,7 +139,7 @@ public class DefaultPropertyResolver
{
final String propertyName = content.substring( start + 2, end );
final Object key = recursiveResolveProperty( propertyName, context );
final Object value = getPropertyValue( key.toString(), context );
final String value = getPropertyStringValue( key.toString(), context );

sb.append( content.substring( lastPlace, start ) );
sb.append( value );
@@ -228,6 +240,28 @@ public class DefaultPropertyResolver
throw new TaskException( message );
}

/**
* Returns a property's value, converted to a String.
*/
private String getPropertyStringValue( final String propertyName,
final TaskContext context )
throws TaskException
{
final Object value = getPropertyValue( propertyName, context );
if( value instanceof String )
{
return (String)value;
}
try
{
return (String)m_converter.convert( String.class, value, context );
}
catch( final ConverterException e )
{
throw new TaskException( e.getMessage(), e );
}
}

/**
* Retrieve a value from the specified context using the specified key.
*


+ 19
- 9
proposal/myrmidon/src/test/org/apache/myrmidon/components/AbstractComponentTest.java View File

@@ -73,27 +73,27 @@ public abstract class AbstractComponentTest
m_serviceManager = new DefaultServiceManager();
List components = new ArrayList();

Object component = new DefaultMasterConverter();
Object component = createComponent( Converter.ROLE, DefaultMasterConverter.class );
m_serviceManager.put( Converter.ROLE, component );
components.add( component );

component = new DefaultConverterRegistry();
component = createComponent( ConverterRegistry.ROLE, DefaultConverterRegistry.class );
m_serviceManager.put( ConverterRegistry.ROLE, component );
components.add( component );

component = new DefaultTypeManager();
component = createComponent( TypeManager.ROLE, DefaultTypeManager.class );
m_serviceManager.put( TypeManager.ROLE, component );
components.add( component );

component = new DefaultConfigurer();
component = createComponent( Configurer.ROLE, DefaultConfigurer.class );
m_serviceManager.put( Configurer.ROLE, component );
components.add( component );

component = new DefaultDeployer();
component = createComponent( Deployer.ROLE, DefaultDeployer.class );
m_serviceManager.put( Deployer.ROLE, component );
components.add( component );

component = new DefaultExecutor();
component = createComponent( Executor.ROLE, DefaultExecutor.class );
m_serviceManager.put( Executor.ROLE, component );
components.add( component );

@@ -102,15 +102,15 @@ public abstract class AbstractComponentTest
m_serviceManager.put( ClassLoaderManager.ROLE, classLoaderMgr );
components.add( classLoaderMgr );

component = new DefaultExtensionManager();
component = createComponent( ExtensionManager.ROLE, DefaultExtensionManager.class );
m_serviceManager.put( ExtensionManager.ROLE, component );
components.add( component );

component = new DefaultRoleManager();
component = createComponent( RoleManager.ROLE, DefaultRoleManager.class );
m_serviceManager.put( RoleManager.ROLE, component );
components.add( component );

component = new DefaultPropertyResolver();
component = createComponent( PropertyResolver.ROLE, DefaultPropertyResolver.class );
m_serviceManager.put( PropertyResolver.ROLE, component );
components.add( component );

@@ -147,6 +147,16 @@ public abstract class AbstractComponentTest
return m_serviceManager;
}

/**
* Creates an instance of a component. Sub-classes can override this
* method to add a particular implementation to the set of test components.
*/
protected Object createComponent( final String role, final Class defaultImpl )
throws Exception
{
return defaultImpl.newInstance();
}

/**
* Returns the type manager.
*/


+ 19
- 1
proposal/myrmidon/src/test/org/apache/myrmidon/components/configurer/test/DefaultConfigurerTestCase.java View File

@@ -15,6 +15,7 @@ import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.avalon.framework.configuration.DefaultConfiguration;
import org.apache.myrmidon.api.TaskContext;
import org.apache.myrmidon.components.AbstractComponentTest;
import org.apache.myrmidon.components.configurer.DefaultConfigurer;
import org.apache.myrmidon.components.configurer.test.data.ConfigTestAttributeConvert;
import org.apache.myrmidon.components.configurer.test.data.ConfigTestConfigAdder;
import org.apache.myrmidon.components.configurer.test.data.ConfigTestContent;
@@ -30,6 +31,7 @@ import org.apache.myrmidon.components.configurer.test.data.ConfigTestPropResolut
import org.apache.myrmidon.components.configurer.test.data.ConfigTestReferenceAttribute;
import org.apache.myrmidon.components.configurer.test.data.ConfigTestReferenceConversion;
import org.apache.myrmidon.components.configurer.test.data.ConfigTestReferenceElement;
import org.apache.myrmidon.components.configurer.test.data.ConfigTestSetAndAdd;
import org.apache.myrmidon.components.configurer.test.data.ConfigTestSetAttribute;
import org.apache.myrmidon.components.configurer.test.data.ConfigTestSetElement;
import org.apache.myrmidon.components.configurer.test.data.ConfigTestTypedAdder;
@@ -38,7 +40,6 @@ 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,6 +81,23 @@ public class DefaultConfigurerTestCase
m_context.setProperty( TaskContext.BASE_DIRECTORY, baseDir );
}

/**
* Creates an instance of a component. Sub-classes can override this
* method to add a particular implementation to the set of test components.
*/
protected Object createComponent( final String role, final Class defaultImpl )
throws Exception
{
if( role.equals( Configurer.ROLE) )
{
return new DefaultConfigurer();
}
else
{
return super.createComponent( role, defaultImpl );
}
}

/**
* Tests setting an attribute, via a setter method.
*/


+ 18
- 0
proposal/myrmidon/src/test/org/apache/myrmidon/components/deployer/test/DefaultDeployerTestCase.java View File

@@ -11,6 +11,7 @@ import java.io.File;
import org.apache.aut.converter.Converter;
import org.apache.aut.converter.ConverterException;
import org.apache.myrmidon.components.AbstractComponentTest;
import org.apache.myrmidon.components.deployer.DefaultDeployer;
import org.apache.myrmidon.framework.DataType;
import org.apache.myrmidon.interfaces.deployer.ConverterDefinition;
import org.apache.myrmidon.interfaces.deployer.Deployer;
@@ -48,6 +49,23 @@ public class DefaultDeployerTestCase
m_converter = (Converter)getServiceManager().lookup( Converter.ROLE );
}

/**
* Creates an instance of a component. Sub-classes can override this
* method to add a particular implementation to the set of test components.
*/
protected Object createComponent( final String role, final Class defaultImpl )
throws Exception
{
if( role.equals( Deployer.ROLE) )
{
return new DefaultDeployer();
}
else
{
return super.createComponent( role, defaultImpl );
}
}

/**
* Tests deployment of a single type from a ClassLoader.
*/


+ 26
- 6
proposal/myrmidon/src/test/org/apache/myrmidon/components/property/test/AbstractPropertyResolverTestCase.java View File

@@ -9,10 +9,11 @@ package org.apache.myrmidon.components.property.test;

import java.io.File;
import java.util.Date;
import org.apache.antlib.core.ObjectToStringConverter;
import org.apache.avalon.excalibur.i18n.Resources;
import org.apache.myrmidon.AbstractMyrmidonTest;
import org.apache.myrmidon.api.TaskException;
import org.apache.myrmidon.api.TaskContext;
import org.apache.myrmidon.api.TaskException;
import org.apache.myrmidon.components.AbstractComponentTest;
import org.apache.myrmidon.components.workspace.DefaultTaskContext;
import org.apache.myrmidon.interfaces.property.PropertyResolver;

@@ -23,25 +24,44 @@ import org.apache.myrmidon.interfaces.property.PropertyResolver;
* @version $Revision$ $Date$
*/
public abstract class AbstractPropertyResolverTestCase
extends AbstractMyrmidonTest
extends AbstractComponentTest
{
protected final static Resources REZ = getResourcesForTested( AbstractPropertyResolverTestCase.class );

protected PropertyResolver m_resolver;
protected DefaultTaskContext m_context;

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

protected void setUp() throws Exception
{
m_resolver = createResolver();
m_resolver = (PropertyResolver)getServiceManager().lookup( PropertyResolver.ROLE );

m_context = new DefaultTaskContext( null, null, getLogger() );
m_context.setProperty( "intProp", new Integer( 333 ) );
m_context.setProperty( "stringProp", "String property" );

registerConverter( ObjectToStringConverter.class, Object.class, String.class );
}

/**
* Creates an instance of a component. Sub-classes can override this
* method to add a particular implementation to the set of test components.
*/
protected Object createComponent( String role, Class defaultImpl )
throws Exception
{
if( role.equals( PropertyResolver.ROLE) )
{
return createResolver();
}
else
{
return super.createComponent( role, defaultImpl );
}
}

/**
@@ -65,7 +85,7 @@ public abstract class AbstractPropertyResolverTestCase
/**
* Simple tests with property on it's own, and accompanied by text.
*/
private void testPropertyValue( Object propObject )
private void testPropertyValue( final Object propObject )
throws Exception
{
m_context.setProperty( "typedProp", propObject );


+ 19
- 9
proposal/myrmidon/src/testcases/org/apache/myrmidon/components/AbstractComponentTest.java View File

@@ -73,27 +73,27 @@ public abstract class AbstractComponentTest
m_serviceManager = new DefaultServiceManager();
List components = new ArrayList();

Object component = new DefaultMasterConverter();
Object component = createComponent( Converter.ROLE, DefaultMasterConverter.class );
m_serviceManager.put( Converter.ROLE, component );
components.add( component );

component = new DefaultConverterRegistry();
component = createComponent( ConverterRegistry.ROLE, DefaultConverterRegistry.class );
m_serviceManager.put( ConverterRegistry.ROLE, component );
components.add( component );

component = new DefaultTypeManager();
component = createComponent( TypeManager.ROLE, DefaultTypeManager.class );
m_serviceManager.put( TypeManager.ROLE, component );
components.add( component );

component = new DefaultConfigurer();
component = createComponent( Configurer.ROLE, DefaultConfigurer.class );
m_serviceManager.put( Configurer.ROLE, component );
components.add( component );

component = new DefaultDeployer();
component = createComponent( Deployer.ROLE, DefaultDeployer.class );
m_serviceManager.put( Deployer.ROLE, component );
components.add( component );

component = new DefaultExecutor();
component = createComponent( Executor.ROLE, DefaultExecutor.class );
m_serviceManager.put( Executor.ROLE, component );
components.add( component );

@@ -102,15 +102,15 @@ public abstract class AbstractComponentTest
m_serviceManager.put( ClassLoaderManager.ROLE, classLoaderMgr );
components.add( classLoaderMgr );

component = new DefaultExtensionManager();
component = createComponent( ExtensionManager.ROLE, DefaultExtensionManager.class );
m_serviceManager.put( ExtensionManager.ROLE, component );
components.add( component );

component = new DefaultRoleManager();
component = createComponent( RoleManager.ROLE, DefaultRoleManager.class );
m_serviceManager.put( RoleManager.ROLE, component );
components.add( component );

component = new DefaultPropertyResolver();
component = createComponent( PropertyResolver.ROLE, DefaultPropertyResolver.class );
m_serviceManager.put( PropertyResolver.ROLE, component );
components.add( component );

@@ -147,6 +147,16 @@ public abstract class AbstractComponentTest
return m_serviceManager;
}

/**
* Creates an instance of a component. Sub-classes can override this
* method to add a particular implementation to the set of test components.
*/
protected Object createComponent( final String role, final Class defaultImpl )
throws Exception
{
return defaultImpl.newInstance();
}

/**
* Returns the type manager.
*/


+ 19
- 1
proposal/myrmidon/src/testcases/org/apache/myrmidon/components/configurer/test/DefaultConfigurerTestCase.java View File

@@ -15,6 +15,7 @@ import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.avalon.framework.configuration.DefaultConfiguration;
import org.apache.myrmidon.api.TaskContext;
import org.apache.myrmidon.components.AbstractComponentTest;
import org.apache.myrmidon.components.configurer.DefaultConfigurer;
import org.apache.myrmidon.components.configurer.test.data.ConfigTestAttributeConvert;
import org.apache.myrmidon.components.configurer.test.data.ConfigTestConfigAdder;
import org.apache.myrmidon.components.configurer.test.data.ConfigTestContent;
@@ -30,6 +31,7 @@ import org.apache.myrmidon.components.configurer.test.data.ConfigTestPropResolut
import org.apache.myrmidon.components.configurer.test.data.ConfigTestReferenceAttribute;
import org.apache.myrmidon.components.configurer.test.data.ConfigTestReferenceConversion;
import org.apache.myrmidon.components.configurer.test.data.ConfigTestReferenceElement;
import org.apache.myrmidon.components.configurer.test.data.ConfigTestSetAndAdd;
import org.apache.myrmidon.components.configurer.test.data.ConfigTestSetAttribute;
import org.apache.myrmidon.components.configurer.test.data.ConfigTestSetElement;
import org.apache.myrmidon.components.configurer.test.data.ConfigTestTypedAdder;
@@ -38,7 +40,6 @@ 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,6 +81,23 @@ public class DefaultConfigurerTestCase
m_context.setProperty( TaskContext.BASE_DIRECTORY, baseDir );
}

/**
* Creates an instance of a component. Sub-classes can override this
* method to add a particular implementation to the set of test components.
*/
protected Object createComponent( final String role, final Class defaultImpl )
throws Exception
{
if( role.equals( Configurer.ROLE) )
{
return new DefaultConfigurer();
}
else
{
return super.createComponent( role, defaultImpl );
}
}

/**
* Tests setting an attribute, via a setter method.
*/


+ 18
- 0
proposal/myrmidon/src/testcases/org/apache/myrmidon/components/deployer/test/DefaultDeployerTestCase.java View File

@@ -11,6 +11,7 @@ import java.io.File;
import org.apache.aut.converter.Converter;
import org.apache.aut.converter.ConverterException;
import org.apache.myrmidon.components.AbstractComponentTest;
import org.apache.myrmidon.components.deployer.DefaultDeployer;
import org.apache.myrmidon.framework.DataType;
import org.apache.myrmidon.interfaces.deployer.ConverterDefinition;
import org.apache.myrmidon.interfaces.deployer.Deployer;
@@ -48,6 +49,23 @@ public class DefaultDeployerTestCase
m_converter = (Converter)getServiceManager().lookup( Converter.ROLE );
}

/**
* Creates an instance of a component. Sub-classes can override this
* method to add a particular implementation to the set of test components.
*/
protected Object createComponent( final String role, final Class defaultImpl )
throws Exception
{
if( role.equals( Deployer.ROLE) )
{
return new DefaultDeployer();
}
else
{
return super.createComponent( role, defaultImpl );
}
}

/**
* Tests deployment of a single type from a ClassLoader.
*/


+ 26
- 6
proposal/myrmidon/src/testcases/org/apache/myrmidon/components/property/test/AbstractPropertyResolverTestCase.java View File

@@ -9,10 +9,11 @@ package org.apache.myrmidon.components.property.test;

import java.io.File;
import java.util.Date;
import org.apache.antlib.core.ObjectToStringConverter;
import org.apache.avalon.excalibur.i18n.Resources;
import org.apache.myrmidon.AbstractMyrmidonTest;
import org.apache.myrmidon.api.TaskException;
import org.apache.myrmidon.api.TaskContext;
import org.apache.myrmidon.api.TaskException;
import org.apache.myrmidon.components.AbstractComponentTest;
import org.apache.myrmidon.components.workspace.DefaultTaskContext;
import org.apache.myrmidon.interfaces.property.PropertyResolver;

@@ -23,25 +24,44 @@ import org.apache.myrmidon.interfaces.property.PropertyResolver;
* @version $Revision$ $Date$
*/
public abstract class AbstractPropertyResolverTestCase
extends AbstractMyrmidonTest
extends AbstractComponentTest
{
protected final static Resources REZ = getResourcesForTested( AbstractPropertyResolverTestCase.class );

protected PropertyResolver m_resolver;
protected DefaultTaskContext m_context;

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

protected void setUp() throws Exception
{
m_resolver = createResolver();
m_resolver = (PropertyResolver)getServiceManager().lookup( PropertyResolver.ROLE );

m_context = new DefaultTaskContext( null, null, getLogger() );
m_context.setProperty( "intProp", new Integer( 333 ) );
m_context.setProperty( "stringProp", "String property" );

registerConverter( ObjectToStringConverter.class, Object.class, String.class );
}

/**
* Creates an instance of a component. Sub-classes can override this
* method to add a particular implementation to the set of test components.
*/
protected Object createComponent( String role, Class defaultImpl )
throws Exception
{
if( role.equals( PropertyResolver.ROLE) )
{
return createResolver();
}
else
{
return super.createComponent( role, defaultImpl );
}
}

/**
@@ -65,7 +85,7 @@ public abstract class AbstractPropertyResolverTestCase
/**
* Simple tests with property on it's own, and accompanied by text.
*/
private void testPropertyValue( Object propObject )
private void testPropertyValue( final Object propObject )
throws Exception
{
m_context.setProperty( "typedProp", propObject );


Loading…
Cancel
Save