View Javadoc

1   package net.sourceforge.pmd.ant;
2   
3   import static java.io.File.separator;
4   
5   import java.io.File;
6   
7   import org.apache.tools.ant.BuildFileTest;
8   
9   /**
10   * Quite an ugly classe, arguably useful for just 2 units test - nevertheless as
11   * there is a workaround that must be shared by both tests (PMD and CPD's) I felt
12   * compelled to move it to a single classes.
13   *
14   * @author Romain Pelisse <belaran@gmail.com>
15   *
16   */
17  public abstract class AbstractAntTestHelper extends BuildFileTest {
18  
19  	protected String pathToTestScript;
20  	protected String antTestScriptFilename;
21  	public String mvnWorkaround;
22  
23  	public AbstractAntTestHelper() {
24  		mvnWorkaround = "pmd/ant/xml";
25  		if (new File("target/clover/test-classes").exists()) {
26  		    pathToTestScript = "target/clover/test-classes/net/sourceforge/" + mvnWorkaround;
27  		} else {
28              pathToTestScript = "target/test-classes/net/sourceforge/" + mvnWorkaround;
29  		}
30  	}
31  
32      @Override
33      public void setUp() {
34      	validatePostConstruct();
35          // initialize Ant
36          configureProject(pathToTestScript + separator + antTestScriptFilename);
37          if (!project.getBaseDir().toString().endsWith(mvnWorkaround)) {
38              // when running from maven, the path needs to be adapted...
39              // FIXME: this is more a workaround than a good solution...
40              project.setBasedir(project.getBaseDir().toString()
41          	    + separator + pathToTestScript);
42          }
43      }
44  
45  	private void validatePostConstruct() {
46  		if ( pathToTestScript == null || "".equals(pathToTestScript) ||
47  			 antTestScriptFilename == null || "".equals(antTestScriptFilename) ||
48  			 mvnWorkaround == null || "".equals(mvnWorkaround) )
49  			throw new IllegalStateException("Unit tests for Ant script badly initialized");
50  
51  	}
52  }