package org.codehaus.castor; import java.io.File; import java.util.Iterator; import junit.framework.Test; import org.apache.maven.artifact.DependencyResolutionRequiredException; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.project.MavenProject; import org.castor.xmlctf.TestCaseAggregator; /** * Abstract Mojo that initialises the junit test cases from xml * Subclasses implement the runJUnit method to provide the Runner (eg. text, swing, ..) * * @requiresProject true * @requiresDependencyResolution runtime */ public abstract class AbstractTestSuiteMojo extends AbstractMojo{ private static final String TEST_ROOT_PROPERTY = "castor.xmlctf.root"; /** * The target dir used for the testclasses. * * @parameter expression="./target/xmlctf" */ private String outputRoot; /** * The project whose project files to create. * * @parameter expression="${project}" * @required */ private MavenProject project; public void execute() throws MojoExecutionException, MojoFailureException { getLog().info("Starting Xastor Mastertestsuite"); // testRoot checks String _testRoot = System.getProperty(TEST_ROOT_PROPERTY); if (_testRoot == null) { throw new MojoExecutionException("No testroot found, please specify property -Dcastor.xmlctf.root"); } if (_testRoot.equals(".") || _testRoot.equals("..")) { //-- convert relative directories "." and ".." to a Canonical path File tmp = new File(_testRoot); try { _testRoot = tmp.getCanonicalPath(); } catch (java.io.IOException iox) { } } else if (_testRoot.startsWith("./") || _testRoot.startsWith(".\\")) { //-- Remove leading ./ or .\ -- URLClassLoader can't handle such file URLs _testRoot = _testRoot.substring(2); } File testRoot = new File(_testRoot); if (!testRoot.exists()) { throw new MojoExecutionException("Root not found:" + _testRoot); } // set classpath for testcompiler String classpath = System.getProperty("java.home") + "/lib/tools.jar:"; try { for (Iterator iter = project.getTestClasspathElements().iterator(); iter.hasNext();) { classpath += iter.next() + ":"; } } catch (DependencyResolutionRequiredException e) { throw new MojoExecutionException("Error setting classpath:" + classpath); } System.setProperty("xmlctf.classpath.override",classpath); getLog().debug("classpath for sourcegenerator is:" + classpath); // run testCase runJUnit(new TestCaseAggregator(testRoot, outputRoot).suite()); } public abstract void runJUnit(Test testSuite )throws MojoExecutionException; }