Précédent: Using
a Plexus Component from a Maven Mojo. |
Haut: Background |
The contents of this document are a work in progress
Unit testing allows us to test out our Mojo implmentation without requiring a project to be set up.
Maven provides a Plugin Test Harness to enable testing of Mojos.
Some relatively comprehensive notes on the Maven Plugin Harness are available here.
This step is needed as the Mojo Archetype (as of this writing) does not sets up test folder when we created our Maven plugin project.
<dependencies>
.
.
.
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-plugin-testing-harness</artifactId>
<version>1.0-beta-1</version>
<scope>test</scope>
</dependency>
</dependencies>
mvn eclipse:clean eclipse:eclipse
public class MonitorMojoTest
extends AbstractMojoTestCase
{
public void testMojoLookup()
throws Exception
{
File pluginXml = new File( getBasedir(), "src/test/resources/unit/plugin-config.xml" );
MonitorMojo mojo = (MonitorMojo) lookupMojo( "monitor", pluginXml );
assertNotNull( mojo );
}
public void testMojoExecution()
throws Exception
{
File pluginXml = new File( getBasedir(), "src/test/resources/unit/plugin-config.xml" );
MonitorMojo mojo = (MonitorMojo) lookupMojo( "monitor", pluginXml );
assertNotNull( mojo.getWebsites() );
assertEquals( 1, mojo.getWebsites().size() );
assertNotNull( mojo.getMonitor() );
try
{
mojo.execute();
}
catch ( Exception e )
{
fail( "Unexpected exception with test data." );
}
}
}
<project>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-website-monitor-plugin</artifactId>
<configuration>
<websites>
<website>http://plexus.codehaus.org/</website>
</websites>
</configuration>
</plugin>
</plugins>
</build>
</project>
public class MonitorMojo
extends AbstractMojo
{
/**
* List of websites to monitor.
* @parameter
*/
private List websites;
/**
* The website monitor component instance that will be injected
* by the Plexus runtime.
* @component
*/
private WebsiteMonitor monitor;
public void execute()
throws MojoExecutionException, MojoFailureException
{
if ( !hasWebsites() )
{
if ( getLog().isWarnEnabled() )
getLog().warn( "No websites specified to be monitored." );
return;
}
if ( getLog().isDebugEnabled() )
showMonitoredWebsites();
monitor.addWebsites( websites );
try
{
monitor.monitor();
}
catch ( Exception e )
{
if ( getLog().isErrorEnabled() )
getLog().error( "Error monitoring websites.", e );
}
}
private void showMonitoredWebsites()
{
getLog().debug( "Monitoring following websites:" );
for ( Iterator it = websites.iterator(); it.hasNext(); )
{
String website = (String) it.next();
getLog().debug( "\t" + website );
}
}
/**
* Determines if websites were specified to the Mojo.
*
* @return <code>true</code> if there were websites specified.
*/
private boolean hasWebsites()
{
return ( null != websites && websites.size() > 0 );
}
/**
* Returns the list of websites.<p>
* <em>Not public API. For unit tests only.</em>
*
* @return List of websites specified via Mojo configuration.
*/
protected List getWebsites()
{
return this.websites;
}
/**
* Returns the {@link WebsiteMonitor} component instance.<p>
* <em>Not public API. For unit tests only.</em>
*
* @return the {@link WebsiteMonitor} instance.
*/
protected WebsiteMonitor getMonitor()
{
return monitor;
}
}
mvn clean test
Précédent: Using
a Plexus Component from a Maven Mojo. |
Haut: Background |