donaldp 02/04/14 03:46:51
Added: antlib/src/test/org/apache/myrmidon
AbstractMyrmidonTest.java AbstractProjectTest.java
LogMessageTracker.java TrackingProjectListener.java
Log:
Commit testinfg infrastructure. Really need to rework this so less
duplication between all the sub-projects.
Revision Changes Path
1.1
jakarta-ant-myrmidon/antlib/src/test/org/apache/myrmidon/AbstractMyrmidonTest.java
Index: AbstractMyrmidonTest.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE.txt file.
*/
package org.apache.myrmidon;
import java.io.File;
import java.io.IOException;
import junit.framework.TestCase;
import org.apache.avalon.excalibur.i18n.Resources;
import org.apache.avalon.excalibur.i18n.ResourceManager;
import org.apache.avalon.framework.ExceptionUtil;
import org.apache.avalon.framework.logger.Logger;
import org.apache.myrmidon.interfaces.BasicLogger;
/**
* A base class for Myrmidon tests. Provides utility methods for locating
* test resources.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Adam Murdoch</a>
*/
public abstract class AbstractMyrmidonTest
extends TestCase
{
private final File m_testBaseDir;
private final File m_baseDir;
private Logger m_logger;
public AbstractMyrmidonTest( final String name )
{
super( name );
final String baseDirProp = System.getProperty( "test.basedir" );
m_baseDir = getCanonicalFile( new File( baseDirProp ) );
final String packagePath = getPackageName( getClass() ).replace( '.',
File.separatorChar );
m_testBaseDir = getCanonicalFile( new File( m_baseDir, packagePath )
);
}
/**
* Locates the error message resources for a class.
*/
protected static final Resources getResourcesForTested( final Class clazz
)
{
String baseName = getPackageName( clazz );
if( baseName.endsWith( ".test" ) )
{
baseName = baseName.substring( 0, baseName.length() - 5 );
}
return ResourceManager.getBaseResources( baseName + ".Resources",
AbstractMyrmidonTest.class.getClassLoader() );
}
/**
* Returns the name of the package containing a class.
*
* @return The . delimited package name, or an empty string if the class
* is in the default package.
*/
protected static String getPackageName( final Class clazz )
{
final Package pkg = clazz.getPackage();
if( null != pkg )
{
return pkg.getName();
}
final String name = clazz.getName();
if( -1 == name.lastIndexOf( "." ) )
{
return "";
}
else
{
return name.substring( 0, name.lastIndexOf( "." ) );
}
}
/**
* Locates a test resource, and asserts that the resource exists
*
* @param name path of the resource, relative to this test's base
directory.
*/
protected File getTestResource( final String name )
{
return getTestResource( name, true );
}
/**
* Locates a test resource.
*
* @param name path of the resource, relative to this test's base
directory.
*/
protected File getTestResource( final String name, final boolean
mustExist )
{
File file = new File( m_testBaseDir, name );
file = getCanonicalFile( file );
if( mustExist )
{
assertTrue( "Test file \"" + file + "\" does not exist.",
file.exists() );
}
else
{
assertTrue( "Test file \"" + file + "\" should not exist.",
!file.exists() );
}
return file;
}
/**
* Locates the base directory for this test.
*/
protected File getTestDirectory()
{
return m_testBaseDir;
}
/**
* Locates a test directory, creating it if it does not exist.
*
* @param name path of the directory, relative to this test's base
directory.
*/
protected File getTestDirectory( final String name )
{
File file = new File( m_testBaseDir, name );
file = getCanonicalFile( file );
assertTrue( "Test directory \"" + file + "\" does not exist or is not
a directory.",
file.isDirectory() || file.mkdirs() );
return file;
}
/**
* Returns the directory containing a Myrmidon install.
*/
protected File getInstallDirectory()
{
final File file = new File( m_baseDir, "dist" );
return getCanonicalFile( file );
}
/**
* Makes a file canonical
*/
private File getCanonicalFile( final File file )
{
try
{
return file.getCanonicalFile();
}
catch( IOException e )
{
return file.getAbsoluteFile();
}
}
/**
* Creates a logger.
*/
protected Logger getLogger()
{
if( m_logger == null )
{
m_logger = new BasicLogger( "[test]", BasicLogger.LEVEL_WARN );
}
return m_logger;
}
/**
* 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 )
{
Throwable current = throwable;
for( int i = 0; i < messages.length; i++ )
{
String message = messages[ i ];
assertNotNull( current );
if( message != null )
{
assertEquals( message, current.getMessage() );
}
// Get the next exception in the chain
current = ExceptionUtil.getCause( current, true );
}
}
/**
* Asserts that an exception contains the expected message.
*/
protected void assertSameMessage( final String message, final Throwable
throwable )
{
assertSameMessage( new String[]{message}, throwable );
}
/**
* Compares 2 objects for equality, nulls are equal. Used by the test
* classes' equals() methods.
*/
public static boolean equals( final Object o1, final Object o2 )
{
if( o1 == null && o2 == null )
{
return true;
}
if( o1 == null || o2 == null )
{
return false;
}
return o1.equals( o2 );
}
}
1.1
jakarta-ant-myrmidon/antlib/src/test/org/apache/myrmidon/AbstractProjectTest.java
Index: AbstractProjectTest.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE.txt file.
*/
package org.apache.myrmidon;
import java.io.File;
import org.apache.myrmidon.interfaces.EmbeddedAnt;
import org.apache.myrmidon.listeners.ProjectListener;
import org.apache.avalon.framework.ExceptionUtil;
/**
* A base class for test cases which need to execute projects.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Adam Murdoch</a>
* @version $Revision: 1.1 $ $Date: 2002/04/14 10:46:50 $
*/
public class AbstractProjectTest
extends AbstractMyrmidonTest
{
public AbstractProjectTest( final String name )
{
super( name );
}
/**
* 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
{
final EmbeddedAnt embeddor = new EmbeddedAnt();
final TrackingProjectListener tracker = new TrackingProjectListener();
try
{
// Configure embeddor
embeddor.setHomeDirectory( getInstallDirectory() );
embeddor.enableLogging( getLogger() );
embeddor.setSharedClassLoader( getClass().getClassLoader() );
embeddor.setProjectFile( projectFile.getAbsolutePath() );
embeddor.setProjectListener( null );
// Add a listener to make sure all is good
embeddor.addProjectListener( tracker );
// Add supplied listener
if( listener != null )
{
embeddor.addProjectListener( listener );
}
// Now execute the target
embeddor.executeTargets( new String[] { targetName } );
}
finally
{
embeddor.stop();
}
// Make sure all expected events were delivered
tracker.assertComplete();
if( listener instanceof TrackingProjectListener )
{
( (TrackingProjectListener)listener ).assertComplete();
}
}
}
1.1
jakarta-ant-myrmidon/antlib/src/test/org/apache/myrmidon/LogMessageTracker.java
Index: LogMessageTracker.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE.txt file.
*/
package org.apache.myrmidon;
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:[EMAIL PROTECTED]">Adam Murdoch</a>
* @version $Revision: 1.1 $ $Date: 2002/04/14 10:46:50 $
*/
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 );
}
}
1.1
jakarta-ant-myrmidon/antlib/src/test/org/apache/myrmidon/TrackingProjectListener.java
Index: TrackingProjectListener.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE.txt file.
*/
package org.apache.myrmidon;
import junit.framework.Assert;
import org.apache.myrmidon.listeners.LogEvent;
import org.apache.myrmidon.listeners.ProjectEvent;
import org.apache.myrmidon.listeners.ProjectListener;
import org.apache.myrmidon.listeners.TargetEvent;
import org.apache.myrmidon.listeners.TaskEvent;
/**
* A project listener that asserts that it receives a particular sequence of
* events.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Adam Murdoch</a>
* @version $Revision: 1.1 $ $Date: 2002/04/14 10:46:50 $
*/
public class TrackingProjectListener
extends Assert
implements ProjectListener
{
private String m_rootProject;
private String m_currentProject;
private String m_currentTarget;
private String m_currentTask;
/**
* Notify the listener that a project is about to start.
*/
public void projectStarted( final ProjectEvent event )
{
assertNull( "Project already started", m_rootProject );
m_rootProject = event.getProjectName();
}
/**
* Notify the listener that a project has finished.
*/
public void projectFinished( final ProjectEvent event )
{
assertEquals( "Mismatched project name", m_rootProject,
event.getProjectName() );
m_rootProject = null;
assertNull( "Target not started", m_currentTarget );
}
/**
* Notify the listener that a target is about to start.
*/
public void targetStarted( final TargetEvent event )
{
assertNotNull( "Project not started", m_rootProject );
assertNull( "Target already started", m_currentTarget );
m_currentProject = event.getProjectName();
m_currentTarget = event.getTargetName();
}
/**
* Notify the listener that a target has finished.
*/
public void targetFinished( final TargetEvent event )
{
assertEquals( "Mismatched project name", m_currentProject,
event.getProjectName() );
assertEquals( "Mismatched target name", m_currentTarget,
event.getTargetName() );
m_currentProject = null;
m_currentTarget = null;
assertNull( "Task not finished", m_currentTask );
}
/**
* Notify the listener that a task is about to start.
*/
public void taskStarted( final TaskEvent event )
{
assertEquals( "Mismatched project name", m_currentProject,
event.getProjectName() );
assertEquals( "Mismatched target name", m_currentTarget,
event.getTargetName() );
assertNull( "Task already started", m_currentTask );
m_currentTask = event.getTaskName();
}
/**
* Notify the listener that a task has finished.
*/
public void taskFinished( final TaskEvent event )
{
assertEquals( "Mismatched project name", m_currentProject,
event.getProjectName() );
assertEquals( "Mismatched target name", m_currentTarget,
event.getTargetName() );
assertEquals( "Mismatched task name", m_currentTask,
event.getTaskName() );
m_currentTask = null;
}
/**
* Notify listener of log message event.
*/
public void log( final LogEvent event )
{
assertEquals( "Mismatched project name", m_currentProject,
event.getProjectName() );
assertEquals( "Mismatched target name", m_currentTarget,
event.getTargetName() );
assertEquals( "Mismatched task name", m_currentTask,
event.getTaskName() );
assertNull( "Unexpected build error", event.getThrowable() );
}
/**
* Asserts that the listener has finished.
*/
public void assertComplete()
{
assertNull( "Task not finished", m_currentTask );
assertNull( "Target not finished", m_currentTarget );
assertNull( "Target not finished", m_currentProject );
assertNull( "Project not finished", m_rootProject );
}
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>