* No longer implements Configurable, uses a set() method instead. * The property value can be included as the text content of the <property> element. * Added test-cases. * Added some alternative executeTarget() methods to AbstractProjectTest, to expect a particular set of log messages, or a particular failure. git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@271567 13f79535-47bb-0310-9956-ffa450edef68master
| @@ -454,6 +454,10 @@ Legal: | |||
| <fileset dir="${test.classes}" includes="org/apache/myrmidon/interfaces/type/MyType1.class"/> | |||
| </jar> | |||
| <!-- Prepare the project tests --> | |||
| <copy file="src/manifest/testcases-ant-descriptor.xml" tofile="${test.classes}/META-INF/ant-descriptor.xml"/> | |||
| <!-- Run all the tests --> | |||
| <junit printsummary="on" | |||
| fork="false"> | |||
| <formatter type="brief" usefile="false"/> | |||
| @@ -0,0 +1,44 @@ | |||
| <project version="2.0"> | |||
| <!-- Test setting property via attribute --> | |||
| <target name="set-attr"> | |||
| <property name="test-prop" value="some value"/> | |||
| <log>test-prop = [${test-prop}]</log> | |||
| </target> | |||
| <!-- Test setting property via content --> | |||
| <target name="set-content"> | |||
| <property name="test-prop2">some value</property> | |||
| <log>test-prop2 = [${test-prop2}]</log> | |||
| </target> | |||
| <!-- Test setting property via a nested element --> | |||
| <target name="set-element"> | |||
| <property name="test-prop3"> | |||
| <property-test-type value="some value"/> | |||
| </property> | |||
| <log>test-prop3 = [${test-prop3}]</log> | |||
| </target> | |||
| <!-- Test missing property name --> | |||
| <target name="missing-name"> | |||
| <property value="some value"/> | |||
| </target> | |||
| <!-- Test missing property value --> | |||
| <target name="missing-value"> | |||
| <property name="some-prop"/> | |||
| </target> | |||
| <!-- Test setting the value more than once --> | |||
| <target name="too-many-values1"> | |||
| <property name="some-prop" value="some value">another value</property> | |||
| </target> | |||
| <!-- Test setting the value more than once --> | |||
| <target name="too-many-values2"> | |||
| <property name="some-prop" value="some value"> | |||
| <property-test-type value="value 2"/> | |||
| </property> | |||
| </target> | |||
| </project> | |||
| @@ -9,12 +9,9 @@ package org.apache.antlib.core; | |||
| import org.apache.avalon.excalibur.i18n.ResourceManager; | |||
| import org.apache.avalon.excalibur.i18n.Resources; | |||
| import org.apache.avalon.framework.configuration.Configurable; | |||
| import org.apache.avalon.framework.configuration.Configuration; | |||
| import org.apache.avalon.framework.configuration.ConfigurationException; | |||
| import org.apache.myrmidon.api.AbstractTask; | |||
| import org.apache.myrmidon.api.TaskContext; | |||
| import org.apache.myrmidon.api.TaskException; | |||
| import org.apache.myrmidon.framework.AbstractContainerTask; | |||
| import org.apache.myrmidon.framework.DataType; | |||
| /** | |||
| @@ -27,8 +24,7 @@ import org.apache.myrmidon.framework.DataType; | |||
| * @ant:task name="property" | |||
| */ | |||
| public class Property | |||
| extends AbstractContainerTask | |||
| implements Configurable | |||
| extends AbstractTask | |||
| { | |||
| private final static Resources REZ = | |||
| ResourceManager.getPackageResources( Property.class ); | |||
| @@ -37,40 +33,37 @@ public class Property | |||
| private Object m_value; | |||
| private boolean m_localScope = true; | |||
| public void configure( final Configuration configuration ) | |||
| throws ConfigurationException | |||
| public void setName( final String name ) | |||
| { | |||
| final String[] attributes = configuration.getAttributeNames(); | |||
| for( int i = 0; i < attributes.length; i++ ) | |||
| { | |||
| final String name = attributes[ i ]; | |||
| final String value = configuration.getAttribute( name ); | |||
| configure( this, name, value ); | |||
| } | |||
| m_name = name; | |||
| } | |||
| final Configuration[] children = configuration.getChildren(); | |||
| for( int i = 0; i < children.length; i++ ) | |||
| { | |||
| try | |||
| { | |||
| final String typeName = children[ i ].getName(); | |||
| final DataType value = (DataType)newInstance( DataType.class, typeName ); | |||
| configure( value, children[ i ] ); | |||
| setValue( value ); | |||
| } | |||
| catch( final Exception e ) | |||
| { | |||
| final String message = REZ.getString( "property.no-set.error" ); | |||
| throw new ConfigurationException( message, e ); | |||
| } | |||
| } | |||
| public void setLocalScope( final boolean localScope ) | |||
| { | |||
| m_localScope = localScope; | |||
| } | |||
| public void setName( final String name ) | |||
| /** | |||
| * Sets the property value from a nested element. | |||
| */ | |||
| public void set( final DataType value ) | |||
| throws TaskException | |||
| { | |||
| m_name = name; | |||
| setValue( value ); | |||
| } | |||
| /** | |||
| * Sets the property value from text content. | |||
| */ | |||
| public void addContent( final String value ) | |||
| throws TaskException | |||
| { | |||
| setValue( value ); | |||
| } | |||
| /** | |||
| * Sets the property value from an attribute. | |||
| */ | |||
| public void setValue( final Object value ) | |||
| throws TaskException | |||
| { | |||
| @@ -83,11 +76,6 @@ public class Property | |||
| m_value = value; | |||
| } | |||
| public void setLocalScope( final boolean localScope ) | |||
| { | |||
| m_localScope = localScope; | |||
| } | |||
| public void execute() | |||
| throws TaskException | |||
| { | |||
| @@ -0,0 +1,5 @@ | |||
| <ant-lib version="1.0"> | |||
| <types> | |||
| <data-type name="property-test-type" classname="org.apache.antlib.core.PropertyTestType"/> | |||
| </types> | |||
| </ant-lib> | |||
| @@ -0,0 +1,85 @@ | |||
| /* | |||
| * 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 java.io.File; | |||
| import org.apache.avalon.excalibur.i18n.ResourceManager; | |||
| import org.apache.avalon.excalibur.i18n.Resources; | |||
| import org.apache.myrmidon.AbstractProjectTest; | |||
| import org.apache.myrmidon.LogMessageTracker; | |||
| import org.apache.myrmidon.components.configurer.DefaultConfigurer; | |||
| /** | |||
| * Test cases for <property> task. | |||
| * | |||
| * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a> | |||
| * @version $Revision$ $Date$ | |||
| */ | |||
| public class PropertyTest | |||
| extends AbstractProjectTest | |||
| { | |||
| private final static Resources REZ | |||
| = ResourceManager.getPackageResources( PropertyTest.class ); | |||
| public PropertyTest( final String name ) | |||
| { | |||
| super( name ); | |||
| } | |||
| /** | |||
| * Tests setting a property, using an attribute, text content, and | |||
| * nested element. | |||
| */ | |||
| public void testSetProperty() | |||
| throws Exception | |||
| { | |||
| final File projectFile = getTestResource( "property.ant" ); | |||
| // Set by attribute | |||
| LogMessageTracker tracker = new LogMessageTracker(); | |||
| tracker.addExpectedMessage( "set-attr", "test-prop = [some value]"); | |||
| executeTarget( projectFile, "set-attr", tracker ); | |||
| // Set by text content | |||
| tracker = new LogMessageTracker(); | |||
| tracker.addExpectedMessage( "set-content", "test-prop2 = [some value]"); | |||
| executeTarget( projectFile, "set-content", tracker ); | |||
| // Set by nested element | |||
| tracker = new LogMessageTracker(); | |||
| tracker.addExpectedMessage( "set-element", "test-prop3 = [value=[some value]]"); | |||
| executeTarget( projectFile, "set-element", tracker ); | |||
| } | |||
| /** | |||
| * Tests the validation performed by the propery task. | |||
| */ | |||
| public void testValidation() | |||
| throws Exception | |||
| { | |||
| final File projectFile = getTestResource( "property.ant" ); | |||
| // Missing name | |||
| String message = REZ.getString( "property.no-name.error" ); | |||
| executeTargetExpectError( projectFile, "missing-name", message ); | |||
| // Missing value | |||
| message = REZ.getString( "property.no-value.error" ); | |||
| executeTargetExpectError( projectFile, "missing-value", message ); | |||
| // Too many values | |||
| String[] messages = { | |||
| null, | |||
| null, | |||
| REZ.getString( "property.multi-set.error" ) | |||
| }; | |||
| executeTargetExpectError( projectFile, "too-many-values1", messages ); | |||
| executeTargetExpectError( projectFile, "too-many-values2", messages ); | |||
| } | |||
| } | |||
| @@ -0,0 +1,37 @@ | |||
| /* | |||
| * 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.myrmidon.framework.DataType; | |||
| /** | |||
| * A test data-type used by the property tests. | |||
| * | |||
| * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a> | |||
| * @version $Revision$ $Date$ | |||
| * | |||
| * @ant:data-type name="property-test-type" | |||
| */ | |||
| public class PropertyTestType | |||
| implements DataType | |||
| { | |||
| private String m_value; | |||
| public void setValue( final String value ) | |||
| { | |||
| m_value = value; | |||
| } | |||
| /** | |||
| * Used in the test project file to check the value has been set. | |||
| */ | |||
| public String toString() | |||
| { | |||
| return "value=[" + m_value + "]"; | |||
| } | |||
| } | |||
| @@ -99,6 +99,9 @@ public abstract class AbstractMyrmidonTest | |||
| /** | |||
| * Asserts that an exception chain contains the expected messages. | |||
| * | |||
| * @param messages The messages, in order. A null entry in this array | |||
| * indicates that the message should be ignored. | |||
| */ | |||
| protected void assertSameMessage( final String[] messages, final Throwable throwable ) | |||
| { | |||
| @@ -107,7 +110,10 @@ public abstract class AbstractMyrmidonTest | |||
| { | |||
| String message = messages[ i ]; | |||
| assertNotNull( current ); | |||
| assertEquals( message, current.getMessage() ); | |||
| if( message != null ) | |||
| { | |||
| assertEquals( message, current.getMessage() ); | |||
| } | |||
| if( current instanceof CascadingThrowable ) | |||
| { | |||
| @@ -8,12 +8,13 @@ | |||
| package org.apache.myrmidon; | |||
| import java.io.File; | |||
| import org.apache.avalon.framework.logger.Logger; | |||
| import org.apache.avalon.framework.parameters.Parameters; | |||
| import org.apache.myrmidon.components.embeddor.DefaultEmbeddor; | |||
| import org.apache.myrmidon.interfaces.embeddor.Embeddor; | |||
| import org.apache.myrmidon.interfaces.model.Project; | |||
| import org.apache.myrmidon.interfaces.workspace.Workspace; | |||
| import org.apache.avalon.framework.logger.Logger; | |||
| import org.apache.avalon.framework.parameters.Parameters; | |||
| import org.apache.myrmidon.listeners.ProjectListener; | |||
| /** | |||
| * A base class for test cases which need to execute projects. | |||
| @@ -69,15 +70,75 @@ public class AbstractProjectTest | |||
| } | |||
| /** | |||
| * Executes a target in a project, and asserts that it does not fail | |||
| * Executes a target in a project, and asserts that it fails with the | |||
| * given error message. | |||
| */ | |||
| protected void executeTargetExpectError( final File projectFile, | |||
| final String targetName, | |||
| final String message ) | |||
| { | |||
| executeTargetExpectError( projectFile, targetName, new String[] { message } ); | |||
| } | |||
| /** | |||
| * Executes a target in a project, and asserts that it fails with the | |||
| * given error messages. | |||
| */ | |||
| protected void executeTargetExpectError( final File projectFile, | |||
| final String targetName, | |||
| final String[] messages ) | |||
| { | |||
| try | |||
| { | |||
| executeTarget( projectFile, targetName, null ); | |||
| fail( "target execution did not fail" ); | |||
| } | |||
| catch( Exception e ) | |||
| { | |||
| assertSameMessage( messages, e ); | |||
| } | |||
| } | |||
| /** | |||
| * Executes a target in a project, and asserts that it does not fail. | |||
| */ | |||
| protected void executeTarget( final File projectFile, final String targetName ) | |||
| throws Exception | |||
| { | |||
| executeTarget( projectFile, targetName, null ); | |||
| } | |||
| /** | |||
| * Executes a target in a project, and asserts that it does not fail. | |||
| */ | |||
| protected void executeTarget( final File projectFile, | |||
| final String targetName, | |||
| final ProjectListener listener ) | |||
| throws Exception | |||
| { | |||
| // Create the project and workspace | |||
| final Embeddor embeddor = getEmbeddor(); | |||
| final Project project = embeddor.createProject( projectFile.getAbsolutePath(), null, null ); | |||
| final Workspace workspace = embeddor.createWorkspace( new Parameters() ); | |||
| // Add a listener to make sure all is good | |||
| final TrackingProjectListener tracker = new TrackingProjectListener(); | |||
| workspace.addProjectListener( tracker ); | |||
| // Add supplied listener | |||
| if( listener != null ) | |||
| { | |||
| workspace.addProjectListener( listener ); | |||
| } | |||
| // Now execute the target | |||
| workspace.executeProject( project, targetName ); | |||
| // Make sure all expected events were delivered | |||
| tracker.assertComplete(); | |||
| if( listener instanceof TrackingProjectListener ) | |||
| { | |||
| ( (TrackingProjectListener)listener ).assertComplete(); | |||
| } | |||
| } | |||
| } | |||
| @@ -0,0 +1,59 @@ | |||
| /* | |||
| * 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; | |||
| import java.util.ArrayList; | |||
| import java.util.List; | |||
| import org.apache.myrmidon.listeners.LogEvent; | |||
| /** | |||
| * Asserts that log messages are delivered in the correct order. | |||
| * | |||
| * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a> | |||
| * @version $Revision$ $Date$ | |||
| */ | |||
| public class LogMessageTracker | |||
| extends TrackingProjectListener | |||
| { | |||
| private List m_targets = new ArrayList(); | |||
| private List m_messages = new ArrayList(); | |||
| /** | |||
| * Handles a log message. | |||
| */ | |||
| public void log( final LogEvent event ) | |||
| { | |||
| super.log( event ); | |||
| // Pop the next expected message off the list, and make sure it | |||
| // matches the message in the event | |||
| assertTrue( "Unexpected log message", m_targets.size() > 0 && m_messages.size() > 0 ); | |||
| assertEquals( "Unexpected log message", m_targets.remove( 0 ), event.getTargetName() ); | |||
| assertEquals( "Unexpected log message", m_messages.remove( 0 ), event.getMessage() ); | |||
| } | |||
| /** | |||
| * Asserts that all the log messages were delivered. | |||
| */ | |||
| public void assertComplete() | |||
| { | |||
| super.assertComplete(); | |||
| // Make sure that all log messages were delivered | |||
| assertTrue( "Log message not delivered", m_targets.size() == 0 && m_messages.size() == 0 ); | |||
| } | |||
| /** | |||
| * Adds an expected log message. | |||
| */ | |||
| public void addExpectedMessage( String target, String message ) | |||
| { | |||
| m_targets.add( target ); | |||
| m_messages.add( message ); | |||
| } | |||
| } | |||
| @@ -7,9 +7,6 @@ | |||
| */ | |||
| package org.apache.myrmidon; | |||
| import java.util.ArrayList; | |||
| import java.util.HashMap; | |||
| import java.util.Map; | |||
| import junit.framework.Assert; | |||
| import org.apache.myrmidon.listeners.LogEvent; | |||
| import org.apache.myrmidon.listeners.ProjectEvent; | |||
| @@ -32,8 +29,6 @@ public class TrackingProjectListener | |||
| private String m_currentProject; | |||
| private String m_currentTarget; | |||
| private String m_currentTask; | |||
| private Map m_messages = new HashMap(); | |||
| private ArrayList m_currentMsgs; | |||
| /** | |||
| * Notify the listener that a project is about to start. | |||
| @@ -64,7 +59,6 @@ public class TrackingProjectListener | |||
| assertNull( "Target already started", m_currentTarget ); | |||
| m_currentProject = event.getProjectName(); | |||
| m_currentTarget = event.getTargetName(); | |||
| m_currentMsgs = (ArrayList)m_messages.get( m_currentTarget ); | |||
| } | |||
| /** | |||
| @@ -76,7 +70,6 @@ public class TrackingProjectListener | |||
| assertEquals( "Mismatched target name", m_currentTarget, event.getTargetName() ); | |||
| m_currentProject = null; | |||
| m_currentTarget = null; | |||
| assertTrue( "Missing log messages for target", m_currentMsgs == null || m_currentMsgs.size() == 0 ); | |||
| assertNull( "Task not finished", m_currentTask ); | |||
| } | |||
| @@ -112,9 +105,6 @@ public class TrackingProjectListener | |||
| assertEquals( "Mismatched project name", m_currentProject, event.getProjectName() ); | |||
| assertEquals( "Mismatched target name", m_currentTarget, event.getTargetName() ); | |||
| assertEquals( "Mismatched task name", m_currentTask, event.getTaskName() ); | |||
| assertNotNull( "Unexpected log message", m_currentMsgs ); | |||
| assertTrue( "Unexpected log message", m_currentMsgs.size() > 0 ); | |||
| assertEquals( "Unexpected log message", m_currentMsgs.remove( 0 ), event.getMessage() ); | |||
| assertNull( "Unexpected build error", event.getThrowable() ); | |||
| } | |||
| @@ -128,18 +118,4 @@ public class TrackingProjectListener | |||
| assertNull( "Target not finished", m_currentProject ); | |||
| assertNull( "Project not finished", m_rootProject ); | |||
| } | |||
| /** | |||
| * Adds an expected log message. | |||
| */ | |||
| public void addExpectedMessage( String target, String message ) | |||
| { | |||
| ArrayList targetMsgs = (ArrayList)m_messages.get( target ); | |||
| if( targetMsgs == null ) | |||
| { | |||
| targetMsgs = new ArrayList(); | |||
| m_messages.put( target, targetMsgs ); | |||
| } | |||
| targetMsgs.add( message ); | |||
| } | |||
| } | |||
| @@ -8,15 +8,13 @@ | |||
| package org.apache.myrmidon.components.embeddor; | |||
| import java.io.File; | |||
| import org.apache.avalon.framework.logger.Logger; | |||
| import org.apache.avalon.framework.parameters.Parameters; | |||
| import org.apache.myrmidon.AbstractMyrmidonTest; | |||
| import org.apache.myrmidon.TrackingProjectListener; | |||
| import org.apache.myrmidon.AbstractProjectTest; | |||
| import org.apache.myrmidon.LogMessageTracker; | |||
| import org.apache.myrmidon.interfaces.embeddor.Embeddor; | |||
| import org.apache.myrmidon.interfaces.model.Project; | |||
| import org.apache.myrmidon.interfaces.model.Target; | |||
| import org.apache.myrmidon.interfaces.workspace.Workspace; | |||
| import org.apache.myrmidon.interfaces.embeddor.Embeddor; | |||
| import org.apache.myrmidon.listeners.ProjectListener; | |||
| /** | |||
| @@ -81,7 +79,7 @@ public class DefaultEmbeddorTest | |||
| final Workspace workspace = embeddor.createWorkspace( new Parameters() ); | |||
| // Install a listener | |||
| final TrackingProjectListener listener = new TrackingProjectListener(); | |||
| final LogMessageTracker listener = new LogMessageTracker(); | |||
| workspace.addProjectListener( listener ); | |||
| listener.addExpectedMessage( "main-target", "A log message" ); | |||
| @@ -0,0 +1,85 @@ | |||
| /* | |||
| * 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 java.io.File; | |||
| import org.apache.avalon.excalibur.i18n.ResourceManager; | |||
| import org.apache.avalon.excalibur.i18n.Resources; | |||
| import org.apache.myrmidon.AbstractProjectTest; | |||
| import org.apache.myrmidon.LogMessageTracker; | |||
| import org.apache.myrmidon.components.configurer.DefaultConfigurer; | |||
| /** | |||
| * Test cases for <property> task. | |||
| * | |||
| * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a> | |||
| * @version $Revision$ $Date$ | |||
| */ | |||
| public class PropertyTest | |||
| extends AbstractProjectTest | |||
| { | |||
| private final static Resources REZ | |||
| = ResourceManager.getPackageResources( PropertyTest.class ); | |||
| public PropertyTest( final String name ) | |||
| { | |||
| super( name ); | |||
| } | |||
| /** | |||
| * Tests setting a property, using an attribute, text content, and | |||
| * nested element. | |||
| */ | |||
| public void testSetProperty() | |||
| throws Exception | |||
| { | |||
| final File projectFile = getTestResource( "property.ant" ); | |||
| // Set by attribute | |||
| LogMessageTracker tracker = new LogMessageTracker(); | |||
| tracker.addExpectedMessage( "set-attr", "test-prop = [some value]"); | |||
| executeTarget( projectFile, "set-attr", tracker ); | |||
| // Set by text content | |||
| tracker = new LogMessageTracker(); | |||
| tracker.addExpectedMessage( "set-content", "test-prop2 = [some value]"); | |||
| executeTarget( projectFile, "set-content", tracker ); | |||
| // Set by nested element | |||
| tracker = new LogMessageTracker(); | |||
| tracker.addExpectedMessage( "set-element", "test-prop3 = [value=[some value]]"); | |||
| executeTarget( projectFile, "set-element", tracker ); | |||
| } | |||
| /** | |||
| * Tests the validation performed by the propery task. | |||
| */ | |||
| public void testValidation() | |||
| throws Exception | |||
| { | |||
| final File projectFile = getTestResource( "property.ant" ); | |||
| // Missing name | |||
| String message = REZ.getString( "property.no-name.error" ); | |||
| executeTargetExpectError( projectFile, "missing-name", message ); | |||
| // Missing value | |||
| message = REZ.getString( "property.no-value.error" ); | |||
| executeTargetExpectError( projectFile, "missing-value", message ); | |||
| // Too many values | |||
| String[] messages = { | |||
| null, | |||
| null, | |||
| REZ.getString( "property.multi-set.error" ) | |||
| }; | |||
| executeTargetExpectError( projectFile, "too-many-values1", messages ); | |||
| executeTargetExpectError( projectFile, "too-many-values2", messages ); | |||
| } | |||
| } | |||
| @@ -0,0 +1,37 @@ | |||
| /* | |||
| * 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.myrmidon.framework.DataType; | |||
| /** | |||
| * A test data-type used by the property tests. | |||
| * | |||
| * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a> | |||
| * @version $Revision$ $Date$ | |||
| * | |||
| * @ant:data-type name="property-test-type" | |||
| */ | |||
| public class PropertyTestType | |||
| implements DataType | |||
| { | |||
| private String m_value; | |||
| public void setValue( final String value ) | |||
| { | |||
| m_value = value; | |||
| } | |||
| /** | |||
| * Used in the test project file to check the value has been set. | |||
| */ | |||
| public String toString() | |||
| { | |||
| return "value=[" + m_value + "]"; | |||
| } | |||
| } | |||
| @@ -99,6 +99,9 @@ public abstract class AbstractMyrmidonTest | |||
| /** | |||
| * Asserts that an exception chain contains the expected messages. | |||
| * | |||
| * @param messages The messages, in order. A null entry in this array | |||
| * indicates that the message should be ignored. | |||
| */ | |||
| protected void assertSameMessage( final String[] messages, final Throwable throwable ) | |||
| { | |||
| @@ -107,7 +110,10 @@ public abstract class AbstractMyrmidonTest | |||
| { | |||
| String message = messages[ i ]; | |||
| assertNotNull( current ); | |||
| assertEquals( message, current.getMessage() ); | |||
| if( message != null ) | |||
| { | |||
| assertEquals( message, current.getMessage() ); | |||
| } | |||
| if( current instanceof CascadingThrowable ) | |||
| { | |||
| @@ -8,12 +8,13 @@ | |||
| package org.apache.myrmidon; | |||
| import java.io.File; | |||
| import org.apache.avalon.framework.logger.Logger; | |||
| import org.apache.avalon.framework.parameters.Parameters; | |||
| import org.apache.myrmidon.components.embeddor.DefaultEmbeddor; | |||
| import org.apache.myrmidon.interfaces.embeddor.Embeddor; | |||
| import org.apache.myrmidon.interfaces.model.Project; | |||
| import org.apache.myrmidon.interfaces.workspace.Workspace; | |||
| import org.apache.avalon.framework.logger.Logger; | |||
| import org.apache.avalon.framework.parameters.Parameters; | |||
| import org.apache.myrmidon.listeners.ProjectListener; | |||
| /** | |||
| * A base class for test cases which need to execute projects. | |||
| @@ -69,15 +70,75 @@ public class AbstractProjectTest | |||
| } | |||
| /** | |||
| * Executes a target in a project, and asserts that it does not fail | |||
| * Executes a target in a project, and asserts that it fails with the | |||
| * given error message. | |||
| */ | |||
| protected void executeTargetExpectError( final File projectFile, | |||
| final String targetName, | |||
| final String message ) | |||
| { | |||
| executeTargetExpectError( projectFile, targetName, new String[] { message } ); | |||
| } | |||
| /** | |||
| * Executes a target in a project, and asserts that it fails with the | |||
| * given error messages. | |||
| */ | |||
| protected void executeTargetExpectError( final File projectFile, | |||
| final String targetName, | |||
| final String[] messages ) | |||
| { | |||
| try | |||
| { | |||
| executeTarget( projectFile, targetName, null ); | |||
| fail( "target execution did not fail" ); | |||
| } | |||
| catch( Exception e ) | |||
| { | |||
| assertSameMessage( messages, e ); | |||
| } | |||
| } | |||
| /** | |||
| * Executes a target in a project, and asserts that it does not fail. | |||
| */ | |||
| protected void executeTarget( final File projectFile, final String targetName ) | |||
| throws Exception | |||
| { | |||
| executeTarget( projectFile, targetName, null ); | |||
| } | |||
| /** | |||
| * Executes a target in a project, and asserts that it does not fail. | |||
| */ | |||
| protected void executeTarget( final File projectFile, | |||
| final String targetName, | |||
| final ProjectListener listener ) | |||
| throws Exception | |||
| { | |||
| // Create the project and workspace | |||
| final Embeddor embeddor = getEmbeddor(); | |||
| final Project project = embeddor.createProject( projectFile.getAbsolutePath(), null, null ); | |||
| final Workspace workspace = embeddor.createWorkspace( new Parameters() ); | |||
| // Add a listener to make sure all is good | |||
| final TrackingProjectListener tracker = new TrackingProjectListener(); | |||
| workspace.addProjectListener( tracker ); | |||
| // Add supplied listener | |||
| if( listener != null ) | |||
| { | |||
| workspace.addProjectListener( listener ); | |||
| } | |||
| // Now execute the target | |||
| workspace.executeProject( project, targetName ); | |||
| // Make sure all expected events were delivered | |||
| tracker.assertComplete(); | |||
| if( listener instanceof TrackingProjectListener ) | |||
| { | |||
| ( (TrackingProjectListener)listener ).assertComplete(); | |||
| } | |||
| } | |||
| } | |||
| @@ -0,0 +1,59 @@ | |||
| /* | |||
| * 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; | |||
| import java.util.ArrayList; | |||
| import java.util.List; | |||
| import org.apache.myrmidon.listeners.LogEvent; | |||
| /** | |||
| * Asserts that log messages are delivered in the correct order. | |||
| * | |||
| * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a> | |||
| * @version $Revision$ $Date$ | |||
| */ | |||
| public class LogMessageTracker | |||
| extends TrackingProjectListener | |||
| { | |||
| private List m_targets = new ArrayList(); | |||
| private List m_messages = new ArrayList(); | |||
| /** | |||
| * Handles a log message. | |||
| */ | |||
| public void log( final LogEvent event ) | |||
| { | |||
| super.log( event ); | |||
| // Pop the next expected message off the list, and make sure it | |||
| // matches the message in the event | |||
| assertTrue( "Unexpected log message", m_targets.size() > 0 && m_messages.size() > 0 ); | |||
| assertEquals( "Unexpected log message", m_targets.remove( 0 ), event.getTargetName() ); | |||
| assertEquals( "Unexpected log message", m_messages.remove( 0 ), event.getMessage() ); | |||
| } | |||
| /** | |||
| * Asserts that all the log messages were delivered. | |||
| */ | |||
| public void assertComplete() | |||
| { | |||
| super.assertComplete(); | |||
| // Make sure that all log messages were delivered | |||
| assertTrue( "Log message not delivered", m_targets.size() == 0 && m_messages.size() == 0 ); | |||
| } | |||
| /** | |||
| * Adds an expected log message. | |||
| */ | |||
| public void addExpectedMessage( String target, String message ) | |||
| { | |||
| m_targets.add( target ); | |||
| m_messages.add( message ); | |||
| } | |||
| } | |||
| @@ -7,9 +7,6 @@ | |||
| */ | |||
| package org.apache.myrmidon; | |||
| import java.util.ArrayList; | |||
| import java.util.HashMap; | |||
| import java.util.Map; | |||
| import junit.framework.Assert; | |||
| import org.apache.myrmidon.listeners.LogEvent; | |||
| import org.apache.myrmidon.listeners.ProjectEvent; | |||
| @@ -32,8 +29,6 @@ public class TrackingProjectListener | |||
| private String m_currentProject; | |||
| private String m_currentTarget; | |||
| private String m_currentTask; | |||
| private Map m_messages = new HashMap(); | |||
| private ArrayList m_currentMsgs; | |||
| /** | |||
| * Notify the listener that a project is about to start. | |||
| @@ -64,7 +59,6 @@ public class TrackingProjectListener | |||
| assertNull( "Target already started", m_currentTarget ); | |||
| m_currentProject = event.getProjectName(); | |||
| m_currentTarget = event.getTargetName(); | |||
| m_currentMsgs = (ArrayList)m_messages.get( m_currentTarget ); | |||
| } | |||
| /** | |||
| @@ -76,7 +70,6 @@ public class TrackingProjectListener | |||
| assertEquals( "Mismatched target name", m_currentTarget, event.getTargetName() ); | |||
| m_currentProject = null; | |||
| m_currentTarget = null; | |||
| assertTrue( "Missing log messages for target", m_currentMsgs == null || m_currentMsgs.size() == 0 ); | |||
| assertNull( "Task not finished", m_currentTask ); | |||
| } | |||
| @@ -112,9 +105,6 @@ public class TrackingProjectListener | |||
| assertEquals( "Mismatched project name", m_currentProject, event.getProjectName() ); | |||
| assertEquals( "Mismatched target name", m_currentTarget, event.getTargetName() ); | |||
| assertEquals( "Mismatched task name", m_currentTask, event.getTaskName() ); | |||
| assertNotNull( "Unexpected log message", m_currentMsgs ); | |||
| assertTrue( "Unexpected log message", m_currentMsgs.size() > 0 ); | |||
| assertEquals( "Unexpected log message", m_currentMsgs.remove( 0 ), event.getMessage() ); | |||
| assertNull( "Unexpected build error", event.getThrowable() ); | |||
| } | |||
| @@ -128,18 +118,4 @@ public class TrackingProjectListener | |||
| assertNull( "Target not finished", m_currentProject ); | |||
| assertNull( "Project not finished", m_rootProject ); | |||
| } | |||
| /** | |||
| * Adds an expected log message. | |||
| */ | |||
| public void addExpectedMessage( String target, String message ) | |||
| { | |||
| ArrayList targetMsgs = (ArrayList)m_messages.get( target ); | |||
| if( targetMsgs == null ) | |||
| { | |||
| targetMsgs = new ArrayList(); | |||
| m_messages.put( target, targetMsgs ); | |||
| } | |||
| targetMsgs.add( message ); | |||
| } | |||
| } | |||
| @@ -8,15 +8,13 @@ | |||
| package org.apache.myrmidon.components.embeddor; | |||
| import java.io.File; | |||
| import org.apache.avalon.framework.logger.Logger; | |||
| import org.apache.avalon.framework.parameters.Parameters; | |||
| import org.apache.myrmidon.AbstractMyrmidonTest; | |||
| import org.apache.myrmidon.TrackingProjectListener; | |||
| import org.apache.myrmidon.AbstractProjectTest; | |||
| import org.apache.myrmidon.LogMessageTracker; | |||
| import org.apache.myrmidon.interfaces.embeddor.Embeddor; | |||
| import org.apache.myrmidon.interfaces.model.Project; | |||
| import org.apache.myrmidon.interfaces.model.Target; | |||
| import org.apache.myrmidon.interfaces.workspace.Workspace; | |||
| import org.apache.myrmidon.interfaces.embeddor.Embeddor; | |||
| import org.apache.myrmidon.listeners.ProjectListener; | |||
| /** | |||
| @@ -81,7 +79,7 @@ public class DefaultEmbeddorTest | |||
| final Workspace workspace = embeddor.createWorkspace( new Parameters() ); | |||
| // Install a listener | |||
| final TrackingProjectListener listener = new TrackingProjectListener(); | |||
| final LogMessageTracker listener = new LogMessageTracker(); | |||
| workspace.addProjectListener( listener ); | |||
| listener.addExpectedMessage( "main-target", "A log message" ); | |||