forked from phoedos/pmd
Refactor PMDTaskTest in java, too using BuildFileRule
This commit is contained in:
@ -7,9 +7,9 @@ import org.junit.Test;
|
||||
|
||||
public class PMDTaskTest extends AbstractAntTestHelper {
|
||||
|
||||
public PMDTaskTest() {
|
||||
super.antTestScriptFilename = "pmdtasktest.xml";
|
||||
}
|
||||
public PMDTaskTest() {
|
||||
super.antTestScriptFilename = "pmdtasktest.xml";
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoFormattersValidation() {
|
||||
|
@ -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,39 +21,51 @@ import org.apache.tools.ant.BuildFileTest;
|
||||
* @author Romain Pelisse <belaran@gmail.com>
|
||||
*
|
||||
*/
|
||||
public abstract class AbstractAntTestHelper extends BuildFileTest {
|
||||
public abstract class AbstractAntTestHelper {
|
||||
|
||||
protected String pathToTestScript;
|
||||
protected String antTestScriptFilename;
|
||||
public String mvnWorkaround;
|
||||
@Rule
|
||||
public final BuildFileRule buildRule = new BuildFileRule();
|
||||
|
||||
public AbstractAntTestHelper() {
|
||||
mvnWorkaround = "pmd/ant/xml";
|
||||
if (new File("target/clover/test-classes").exists()) {
|
||||
pathToTestScript = "target/clover/test-classes/net/sourceforge/" + mvnWorkaround;
|
||||
} else {
|
||||
protected String pathToTestScript;
|
||||
protected String antTestScriptFilename;
|
||||
public String mvnWorkaround;
|
||||
|
||||
public AbstractAntTestHelper() {
|
||||
mvnWorkaround = "pmd/ant/xml";
|
||||
if (new File("target/clover/test-classes").exists()) {
|
||||
pathToTestScript = "target/clover/test-classes/net/sourceforge/" + mvnWorkaround;
|
||||
} else {
|
||||
pathToTestScript = "target/test-classes/net/sourceforge/" + mvnWorkaround;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setUp() {
|
||||
validatePostConstruct();
|
||||
// initialize Ant
|
||||
configureProject(pathToTestScript + separator + antTestScriptFilename);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
private void validatePostConstruct() {
|
||||
if ( pathToTestScript == null || "".equals(pathToTestScript) ||
|
||||
antTestScriptFilename == null || "".equals(antTestScriptFilename) ||
|
||||
mvnWorkaround == null || "".equals(mvnWorkaround) ) {
|
||||
throw new IllegalStateException("Unit tests for Ant script badly initialized");
|
||||
}
|
||||
}
|
||||
@Before
|
||||
public void setUp() {
|
||||
validatePostConstruct();
|
||||
// initialize Ant
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
private void validatePostConstruct() {
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user