Refactor PMDTaskTest in java, too using BuildFileRule

This commit is contained in:
Andreas Dangel
2016-05-05 19:06:31 +02:00
parent 334cd5e0f5
commit 4405312c1b
2 changed files with 49 additions and 33 deletions

View File

@ -7,7 +7,11 @@ import static java.io.File.separator;
import java.io.File;
import org.apache.tools.ant.BuildFileTest;
import org.apache.tools.ant.BuildFileRule;
import org.apache.tools.ant.Project;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
/**
* Quite an ugly classe, arguably useful for just 2 units test - nevertheless as
@ -17,7 +21,10 @@ import org.apache.tools.ant.BuildFileTest;
* @author Romain Pelisse <belaran@gmail.com>
*
*/
public abstract class AbstractAntTestHelper extends BuildFileTest {
public abstract class AbstractAntTestHelper {
@Rule
public final BuildFileRule buildRule = new BuildFileRule();
protected String pathToTestScript;
protected String antTestScriptFilename;
@ -32,24 +39,33 @@ public abstract class AbstractAntTestHelper extends BuildFileTest {
}
}
@Override
@Before
public void setUp() {
validatePostConstruct();
// initialize Ant
configureProject(pathToTestScript + separator + antTestScriptFilename);
buildRule.configureProject(pathToTestScript + separator + antTestScriptFilename);
Project project = buildRule.getProject();
if (!project.getBaseDir().toString().endsWith(mvnWorkaround)) {
// when running from maven, the path needs to be adapted...
// FIXME: this is more a workaround than a good solution...
project.setBasedir(project.getBaseDir().toString()
+ separator + pathToTestScript);
project.setBasedir(project.getBaseDir().toString() + separator + pathToTestScript);
}
}
private void validatePostConstruct() {
if ( pathToTestScript == null || "".equals(pathToTestScript) ||
antTestScriptFilename == null || "".equals(antTestScriptFilename) ||
mvnWorkaround == null || "".equals(mvnWorkaround) ) {
if (pathToTestScript == null || "".equals(pathToTestScript) || antTestScriptFilename == null
|| "".equals(antTestScriptFilename) || mvnWorkaround == null || "".equals(mvnWorkaround)) {
throw new IllegalStateException("Unit tests for Ant script badly initialized");
}
}
public void executeTarget(String target) {
buildRule.executeTarget(target);
}
public void assertOutputContaining(String text) {
Assert.assertTrue("Expected to find \"" + text + "\" in the output, but it's missing",
buildRule.getOutput().contains(text));
}
}