Author: tchemit Date: 2010-04-04 12:21:09 +0200 (Sun, 04 Apr 2010) New Revision: 682 Log: improve tests Modified: trunk/src/test/java/org/nuiton/plugin/AbstractMojoTest.java trunk/src/test/java/org/nuiton/plugin/PluginHelperTest.java trunk/src/test/java/org/nuiton/plugin/TestHelper.java Modified: trunk/src/test/java/org/nuiton/plugin/AbstractMojoTest.java =================================================================== --- trunk/src/test/java/org/nuiton/plugin/AbstractMojoTest.java 2010-04-04 10:18:46 UTC (rev 681) +++ trunk/src/test/java/org/nuiton/plugin/AbstractMojoTest.java 2010-04-04 10:21:09 UTC (rev 682) @@ -20,9 +20,6 @@ */ package org.nuiton.plugin; -import java.beans.Introspector; -import java.io.File; - import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.maven.plugin.Mojo; @@ -30,34 +27,32 @@ 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.Assert; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TestName; import org.junit.runners.model.FrameworkMethod; +import java.beans.Introspector; +import java.io.File; + /** * Base test class for a mojo. * <p/> - * <b>Note:</b> replace the previous class - * {@code org.nuiton.util.BasePluginTestCase}. + * <b>Note:</b> replace the previous class {@code org.nuiton.util.BasePluginTestCase}. * <p/> * Inside each test method, we can use the following objects : * <p/> - * <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> + * <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> + * following methods : <ul> <li>{@link #getBasedir()} </li> <li>{@link + * #getTestBasedir()} </li> <li>{@link #getTestDir(String, String)}</li> + * <li>{@link #getPomFile(File, String, String)}</li> <li>{@link + * #createMojo(File, String)}</li> <li>{@link #setUpMojo(Plugin, File)}</li> * </ul> * * @author chemit @@ -66,19 +61,15 @@ */ public abstract class AbstractMojoTest<P extends Plugin> { - /** - * Test logger - */ + /** Test logger */ protected static final Log log = LogFactory.getLog(AbstractMojoTest.class); - /** - * the basedir of the project - */ + + /** the basedir of the project */ protected static File basedir; - /** - * the basedir of all tests (by convention - * {@code getBasedir()/target/test-classes}). - */ + + /** 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. @@ -98,7 +89,10 @@ public File getTestBasedir() { if (testBasedir == null) { - testBasedir = TestHelper.getFile(getBasedir(), "target", "test-classes"); + testBasedir = TestHelper.getFile(getBasedir(), + "target", + "test-classes" + ); if (log.isDebugEnabled()) { log.debug("testBasedir = " + testBasedir); } @@ -138,9 +132,10 @@ */ protected File getTestDir(String methodName, String goalName) { - //TC-20091101 use a decipatilize simple name to avoid conflict of package with - // existing class name. - String rep = getClass().getPackage().getName() + "." + Introspector.decapitalize(getClass().getSimpleName()); + //TC-20091101 use a decipatilize simple name to avoid conflict of + // package with existing class name. + String rep = getClass().getPackage().getName() + "." + + Introspector.decapitalize(getClass().getSimpleName()); rep = rep.replaceAll("\\.", File.separator); File testDir = new File(getTestBasedir(), rep); if (isVerbose()) { @@ -158,8 +153,9 @@ * 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 testDir the location of resources for the next test (is the + * result of the method {@link #getTestDir(String, + * 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. @@ -177,8 +173,8 @@ } /** - * Create the mojo base on the given {@code pomFile} for the - * given {@code goalName}. + * 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 @@ -205,8 +201,12 @@ if (project == null) { log.debug("init maven project"); - MavenProjectBuilder projectBuilder = (MavenProjectBuilder) TestHelper.getDelegateMojoTest().getContainer().lookup(MavenProjectBuilder.ROLE); - ProjectBuilderConfiguration projectBuilderConfiguration = new DefaultProjectBuilderConfiguration(); + MavenProjectBuilder projectBuilder = (MavenProjectBuilder) + TestHelper.getDelegateMojoTest().getContainer().lookup( + MavenProjectBuilder.ROLE + ); + ProjectBuilderConfiguration projectBuilderConfiguration = + new DefaultProjectBuilderConfiguration(); project = projectBuilder.build(pomFile, projectBuilderConfiguration); // project = new MavenProject(); @@ -238,28 +238,23 @@ } /** - * To offer inside each test method (annotated by a {@link org.junit.Test}) the + * To offer inside each test method (annotated by a {@link Test}) the * following properties : * <p/> - * <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> + * <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 - */ + /** location of the pom to use */ private File pomFile; - /** - * the mojo to used in the test method (based on the {@link #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 - */ + + /** the directory where the resources of the test are */ private File testDir; public MojoTestRule() { @@ -275,7 +270,8 @@ if (isVerbose()) { log.info("=============================================================================================="); } - log.info("NEW Mojo test starting : " + getTestClass().getName() + "#" + getMethodName()); + log.info("NEW Mojo test starting : " + getTestClass().getName() + + "#" + getMethodName()); String goalName = getGoalName(name.getMethodName()); @@ -284,14 +280,17 @@ pomFile = getTest().getPomFile(testDir, getMethodName(), goalName); try { - Assert.assertTrue("could not find pom " + pomFile.getAbsoluteFile(), pomFile.exists()); + Assert.assertTrue("could not find pom " + + pomFile.getAbsoluteFile(), pomFile.exists()); mojo = createMojo(pomFile, goalName); getTest().setUpMojo(mojo, pomFile); } catch (Exception ex) { - throw new IllegalStateException("could not init test " + getClass() + " - " + getMethodName() + " for reason " + ex.getMessage(), ex); + throw new IllegalStateException( + "could not init test " + getClass() + " - " + + getMethodName() + " for reason " + ex.getMessage(), ex); } } Modified: trunk/src/test/java/org/nuiton/plugin/PluginHelperTest.java =================================================================== --- trunk/src/test/java/org/nuiton/plugin/PluginHelperTest.java 2010-04-04 10:18:46 UTC (rev 681) +++ trunk/src/test/java/org/nuiton/plugin/PluginHelperTest.java 2010-04-04 10:21:09 UTC (rev 682) @@ -23,10 +23,8 @@ Assert.assertNotNull(lines); Assert.assertEquals(1, lines.length); Assert.assertEquals("file:///file.txt", lines[0]); - } - @Test public void testGetLinesAsURL() throws Exception { URL resource = getClass().getResource("urls.txt"); @@ -36,10 +34,8 @@ Assert.assertNotNull(urls); Assert.assertEquals(1, urls.length); Assert.assertEquals(new URL("file:///file.txt"), urls[0]); - } - @Test public void testGetLinesAsFiles() throws Exception { URL resource = getClass().getResource("urls.txt"); @@ -49,6 +45,6 @@ Assert.assertNotNull(files); Assert.assertEquals(1, files.length); Assert.assertEquals(new File("file:///file.txt"), files[0]); - } + } Modified: trunk/src/test/java/org/nuiton/plugin/TestHelper.java =================================================================== --- trunk/src/test/java/org/nuiton/plugin/TestHelper.java 2010-04-04 10:18:46 UTC (rev 681) +++ trunk/src/test/java/org/nuiton/plugin/TestHelper.java 2010-04-04 10:21:09 UTC (rev 682) @@ -83,11 +83,11 @@ } public static DelegateMojoTestCase getDelegateMojoTest() throws Exception { - if (TestHelper.delegateMojoTest == null) { - TestHelper.delegateMojoTest = new DelegateMojoTestCase(); + if (delegateMojoTest == null) { + delegateMojoTest = new DelegateMojoTestCase(); TestHelper.delegateMojoTest.setUp(); } - return TestHelper.delegateMojoTest; + return delegateMojoTest; } public static void setDelegateMojoTest(DelegateMojoTestCase delegateMojoTest) {