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