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