View Javadoc
1   /*
2    * Copyright 2007 Werner Guttmann
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5    * in compliance with the License. You may obtain a copy of the License at
6    *
7    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software distributed under the License
10   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11   * or implied. See the License for the specific language governing permissions and limitations under
12   * the License.
13   */
14  package org.codehaus.castor.maven.xmlctf;
15  
16  import java.io.File;
17  import java.util.Iterator;
18  
19  import junit.framework.Test;
20  
21  import org.apache.commons.lang3.StringUtils;
22  import org.apache.maven.artifact.Artifact;
23  import org.apache.maven.plugin.AbstractMojo;
24  import org.apache.maven.plugin.MojoExecutionException;
25  import org.apache.maven.plugin.MojoFailureException;
26  import org.apache.maven.plugins.annotations.Parameter;
27  import org.apache.maven.project.MavenProject;
28  import org.castor.xmlctf.TestCaseAggregator;
29  
30  /**
31   * Abstract Maven Mojo that initialises the Junit test cases from xml Subclasses implement the
32   * runJUnit method to provide the Runner (eg. text, swing, ..)
33   * 
34   * @since 1.2
35   */
36  public abstract class AbstractTestSuiteMojo extends AbstractMojo {
37  
38    /**
39     * Property describing the root directory of the tests to be executed.
40     */
41    private static final String TEST_ROOT_PROPERTY = "castor.xmlctf.root";
42  
43    /**
44     * Target directory used for the testclasses.
45     */
46    @Parameter(property = "outputRoot", defaultValue = "./target/xmlctf")
47    private String outputRoot;
48  
49    /**
50     * The source directory of the tests.
51     */
52    @Parameter(property = "testRoot", defaultValue = "${basedir}/tests/MasterTestSuite/")
53    private String testRoot;
54  
55    /**
56     * Optional parameter to overwrite the absolute path to the java tools.jar
57     */
58    @Parameter(property = "pathToTools")
59    private String pathToTools;
60  
61    /**
62     * The Maven project whose project files to create.
63     */
64    @Parameter(defaultValue = "${project}", readonly = true, required = true)
65    private MavenProject project;
66  
67    public void execute() throws MojoExecutionException, MojoFailureException {
68  
69      boolean skipMavenTests = Boolean.getBoolean("maven.test.skip");
70      skipMavenTests |= Boolean.getBoolean("skipTests");
71      skipMavenTests |= Boolean.getBoolean("skipITs");
72  
73      if (skipMavenTests) {
74        if (getLog().isInfoEnabled()) {
75          getLog().info("Skipping XML CTF tests as per configuration.");
76        }
77        return;
78      }
79  
80      getLog().info("Starting Castor Mastertestsuite");
81  
82      // testRoot checks
83      getLog().info("testRoot = " + testRoot);
84      String testRootToUse = System.getProperty(TEST_ROOT_PROPERTY);
85      if (testRootToUse == null) {
86        testRootToUse = testRoot;
87      }
88  
89      if (testRootToUse == null) {
90        throw new MojoExecutionException(
91            "No testroot found, please specify property -Dcastor.xmlctf.root");
92      }
93  
94      if (testRootToUse.equals(".") || testRootToUse.equals("..")) {
95        // -- convert relative directories "." and ".." to a Canonical path
96        File tmp = new File(testRootToUse);
97        try {
98          testRootToUse = tmp.getCanonicalPath();
99        } catch (java.io.IOException iox) {
100 
101       }
102     } else if (testRootToUse.startsWith("./") || testRootToUse.startsWith(".\\")) {
103       // -- Remove leading ./ or .\ -- URLClassLoader can't handle such
104       // file URLs
105       testRoot = testRoot.substring(2);
106     }
107 
108     File testRootFile = new File(testRootToUse);
109     getLog().info("using testRoot: " + testRootFile.getAbsolutePath());
110 
111     if (!testRootFile.exists()) {
112       throw new MojoExecutionException("Root not found:" + testRoot);
113     }
114 
115 
116     // set classpath for testcompiler
117     // TODO
118     String dirSeparator = System.getProperty("file.separator");
119     StringBuilder classpath = new StringBuilder();
120     String pathSeparator = System.getProperty("path.separator");
121     for (@SuppressWarnings("unchecked")
122     Iterator<Artifact> iter = project.getArtifacts().iterator(); iter.hasNext();) {
123       classpath.append(((Artifact) iter.next()).getFile().getAbsolutePath());
124       classpath.append(pathSeparator);
125     }
126 
127     classpath.append(project.getBuild().getTestOutputDirectory());
128     classpath.append(pathSeparator);
129 
130     if (pathToTools != null) {
131       getLog().info("Usage of -DpathToTools !");
132       classpath.append(pathToTools + pathSeparator + "tools.jar");
133     } else {
134       String javaHome = System.getProperty("java.home");
135       classpath.append(javaHome);
136       classpath.append(dirSeparator);
137       classpath.append("lib");
138       classpath.append(dirSeparator);
139       classpath.append("tools.jar");
140       classpath.append(pathSeparator);
141       classpath.append(javaHome.substring(0, javaHome.lastIndexOf("/jre")) + dirSeparator + "lib"
142           + dirSeparator + "tools.jar");
143       classpath.append(pathSeparator);
144     }
145 
146     // set system proerties for
147     System.setProperty("xmlctf.classpath.override", classpath.toString());
148     if (getLog().isDebugEnabled()) {
149       System.setProperty(TestCaseAggregator.VERBOSE_PROPERTY, "true");
150       System.setProperty(TestCaseAggregator.PRINT_STACK_TRACE, "true");
151     }
152 
153     getLog().info("classpath for sourcegenerator is: " + classpath);
154     String[] classpathEntries = StringUtils.split(classpath.toString(), ';');
155     for (String classpathEntry : classpathEntries) {
156       getLog().info(classpathEntry);
157     }
158 
159     // run testCase
160     TestCaseAggregator aggregator = new TestCaseAggregator(testRootFile, outputRoot);
161     // aggregator.
162     runJUnit(aggregator.suite());
163   }
164 
165   /**
166    * For subclasses to implement to provide a test runner.
167    * 
168    * @param testSuite The TestSuite to be executed
169    * @throws MojoExecutionException If test execution fails.
170    */
171   public abstract void runJUnit(Test testSuite) throws MojoExecutionException;
172 
173 }