Author: tchemit Date: 2009-09-27 21:11:50 +0200 (Sun, 27 Sep 2009) New Revision: 595 Modified: trunk/src/test/java/org/nuiton/plugin/AbstractMojoTest.java Log: add javadoc on AbstractMojoTest + improve design Modified: trunk/src/test/java/org/nuiton/plugin/AbstractMojoTest.java =================================================================== --- trunk/src/test/java/org/nuiton/plugin/AbstractMojoTest.java 2009-09-27 19:09:25 UTC (rev 594) +++ trunk/src/test/java/org/nuiton/plugin/AbstractMojoTest.java 2009-09-27 19:11:50 UTC (rev 595) @@ -21,71 +21,193 @@ package org.nuiton.plugin; import java.io.File; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.apache.maven.plugin.Mojo; -import org.apache.maven.plugin.logging.Log; -import org.apache.maven.plugin.logging.SystemStreamLog; +import org.apache.maven.project.DefaultProjectBuilderConfiguration; import org.apache.maven.project.MavenProject; +import org.apache.maven.project.MavenProjectBuilder; +import org.apache.maven.project.ProjectBuilderConfiguration; import org.junit.rules.TestName; import org.junit.Rule; -import org.junit.AfterClass; import org.junit.Assert; -import org.junit.BeforeClass; import org.junit.runners.model.FrameworkMethod; /** + * Base test class for a mojo. * + * <b>Note:</b> replace the previous class + * {@code org.nuiton.util.BasePluginTestCase}. + * <p/> + * Inside each test method, we can use the following objects : + * + * <ul> + * <li>{@link #getTestDir()} : location of mojo resources (where the pom file for example)</li> + * <li>{@link #getPomFile()} : location of the pom file</li> + * <li>{@link #getMojo()} : the instanciated and ready to execute mojo</li> + * </ul> + * <p/> + * To change the behaviour of initialization of mojo, you can override the + * following methods : + * <ul> + * <li>{@link #getBasedir()} </li> + * <li>{@link #getTestBasedir()} </li> + * <li>{@link #getTestDir(java.lang.String, java.lang.String)}</li> + * <li>{@link #getPomFile(java.io.File, java.lang.String, java.lang.String)}</li> + * <li>{@link #createMojo(java.io.File, java.lang.String)}</li> + * <li>{@link #setUpMojo(org.nuiton.plugin.Plugin, java.io.File)}</li> + * </ul> + * * @param <P> type of goal to test * @author chemit * @since 1.0.3 */ public abstract class AbstractMojoTest<P extends Plugin> { - protected static Log log; - protected static File testDir; + /** + * Test logger + */ + protected static final Log log = LogFactory.getLog(AbstractMojoTest.class); + /** + * the basedir of the project + */ + protected static File basedir; + /** + * the basedir of all tests (by convention + * {@code getBasedir()/target/test-classes}). + */ + protected static File testBasedir; + /** + * Your test rule which offers methodName, testDir, pomFile and mojo inside + * test methods. + */ @Rule - public MojoTestName name = new MojoTestName(); + public MojoTestRule name = new MojoTestRule(); - @BeforeClass - public static void beforeClass() throws Exception { - log = new SystemStreamLog(); - TestHelper.getBasedir(); + public File getBasedir() { + if (basedir == null) { + basedir = TestHelper.getBasedir(); + if (log.isDebugEnabled()) { + log.debug("basedir = " + basedir); + } + } + return basedir; } - @AfterClass - public static void afterClass() throws Exception { - testDir = null; - log = null; + public File getTestBasedir() { + if (testBasedir == null) { + testBasedir = TestHelper.getFile(getBasedir(), "target", "test-classes"); + if (log.isDebugEnabled()) { + log.debug("testBasedir = " + testBasedir); + } + } + return testBasedir; } + public String getRelativePathFromBasedir(File f) { + return TestHelper.getRelativePath(getBasedir(), f); + } + + public String getRelativePathFromTestBasedir(File f) { + return TestHelper.getRelativePath(getTestBasedir(), f); + } + + /** + * Obtain the name of the goal according to the methodName. + * <p/> + * By convention, we should consider that a test class use always the same + * goal's name. + * + * @param methodName the name of the next test to execute. + * + * @return the name of the goal to test for the given method test name. + */ protected abstract String getGoalName(String methodName); + /** + * Obtain the location of the directory where to find resources for the next + * test. + * <p/> + * By convention, will be the package named by the test class name from the + * {@link #getTestBasedir()}. + * + * @param methodName the method of the next test to execute + * @param goalName the common goal name to use + * @return the directory where to find resources for the test + */ + protected File getTestDir(String methodName, String goalName) { + + String rep = getClass().getName(); + rep = rep.replaceAll("\\.", File.separator); + + File testDir = new File(getTestBasedir(), rep); + if (isVerbose()) { + log.info("test dir = " + getRelativePathFromBasedir(testDir)); + } else if (log.isDebugEnabled()) { + log.debug("test dir = " + getRelativePathFromBasedir(testDir)); + } + + return testDir; + } + + /** + * Obtain the location of the pom file to use for next mojo test. + * <p/> + * By default, the pom file is the file with name {@code methodName+".xml"} + * in the {@code testDir}. + * + * @param testDir the location of resources for the next test (is the result + * of the method {@link #getTestDir(java.lang.String, java.lang.String)}. + * @param methodName the name of the next test + * @param goalName the name of the common goal + * @return the location of the pom file for the next mojo test. + */ protected File getPomFile(File testDir, String methodName, String goalName) { File pom = new File(testDir, methodName + ".xml"); if (isVerbose()) { - log.info("getPomFile = " + TestHelper.getRelativePath(getBasedir(), pom)); + log.info("pom file = " + getRelativePathFromBasedir(pom)); + } else if (log.isDebugEnabled()) { + log.debug("pom file = " + getRelativePathFromBasedir(pom)); } return pom; } + /** + * Create the mojo base on the given {@code pomFile} for the + * given {@code goalName}. + * + * @param pomFile the location of the pom file + * @param goalName the name of the goal to lookup + * @return the instanciated mojo + * @throws Exception if any problem while creating the mojo + */ @SuppressWarnings("unchecked") protected P createMojo(File pomFile, String goalName) throws Exception { Mojo lookupMojo = TestHelper.lookupMojo(goalName, pomFile); - P mojo = (P) lookupMojo; - return mojo; + P result = (P) lookupMojo; + return result; } + /** + * Initialize the given mojo. + * + * @param mojo the instanciate mojo + * @param pomFile the pom file used to instanciate the mojo + * @throws Exception if any pb + */ protected void setUpMojo(P mojo, File pomFile) throws Exception { MavenProject project = mojo.getProject(); if (project == null) { -// MavenProjectBuilder projectBuilder = (MavenProjectBuilder) delegate.getContainer().lookup(MavenProjectBuilder.ROLE); -// ProjectBuilderConfiguration projectBuilderConfiguration = null; -// project = projectBuilder.build(pomFile, projectBuilderConfiguration); + log.debug("init maven project"); + MavenProjectBuilder projectBuilder = (MavenProjectBuilder) TestHelper.getDelegateMojoTest().getContainer().lookup(MavenProjectBuilder.ROLE); + ProjectBuilderConfiguration projectBuilderConfiguration = new DefaultProjectBuilderConfiguration(); + project = projectBuilder.build(pomFile, projectBuilderConfiguration); - project = new MavenProject(); +// project = new MavenProject(); mojo.setProject(project); } @@ -105,40 +227,43 @@ return name.getPomFile(); } - protected File getBasedir() { - return TestHelper.getBasedir(); - } - protected File getTestDir() { - if (testDir == null) { - - String rep = getClass().getName(); - rep = rep.replaceAll("\\.", File.separator); - - File f = TestHelper.getFile(getBasedir(), "target", "test-classes"); - - testDir = new File(f, rep); - if (isVerbose()) { - log.info("test dir = " + TestHelper.getRelativePath(getBasedir(), testDir)); - } - } - return testDir; + return name.getTestDir(); } protected boolean isVerbose() { return TestHelper.isVerbose(); } - public class MojoTestName extends TestName { + /** + * To offer inside each test method (annotated by a {@link Test}) the + * following properties : + * + * <ul> + * <li>{@link #testDir} : location where to find resources for the test</li> + * <li>{@link #pomFile} : location of the pom file to use to build the mojo</li> + * <li>{@link #mojo} : the instanciated and initialized mojo</li> + * </ul> + */ + protected class MojoTestRule extends TestName { + /** + * location of the pom to use + */ private File pomFile; + /** + * the mojo to used in the test method (based on the {@link #pomFile}). + */ private P mojo; + /** + * the directory where the resources of the test are + */ + private File testDir; - public MojoTestName() { + public MojoTestRule() { if (log.isDebugEnabled()) { - log.debug("NEW MojotestName instance for " + AbstractMojoTest.this); + log.debug("NEW MojoTest instance for " + getTestClass()); } - getTestDir(); } @Override @@ -148,17 +273,20 @@ if (isVerbose()) { log.info("=============================================================================================="); } - log.info("NEW Mojo test starting : " + AbstractMojoTest.this.getClass().getName() + "#" + getMethodName()); + log.info("NEW Mojo test starting : " + getTestClass().getName() + "#" + getMethodName()); + String goalName = getGoalName(name.getMethodName()); - pomFile = AbstractMojoTest.this.getPomFile(getTestDir(), getMethodName(), goalName); + testDir = getTest().getTestDir(getMethodName(), goalName); + pomFile = getTest().getPomFile(testDir, getMethodName(), goalName); + try { Assert.assertTrue("could not find pom " + pomFile.getAbsoluteFile(), pomFile.exists()); mojo = createMojo(pomFile, goalName); - AbstractMojoTest.this.setUpMojo(mojo, pomFile); + getTest().setUpMojo(mojo, pomFile); } catch (Exception ex) { throw new IllegalStateException("could not init test " + getClass() + " - " + getMethodName() + " for reason " + ex.getMessage(), ex); @@ -170,6 +298,7 @@ super.finished(method); pomFile = null; mojo = null; + testDir = null; } public P getMojo() { @@ -179,5 +308,17 @@ public File getPomFile() { return pomFile; } + + public File getTestDir() { + return testDir; + } + + public AbstractMojoTest<P> getTest() { + return AbstractMojoTest.this; + } + + public Class<?> getTestClass() { + return getTest().getClass(); + } } }
participants (1)
-
tchemit@users.nuiton.org