1
2
3
4
5
6
7
8
9
10
11
12
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
32
33
34
35
36 public abstract class AbstractTestSuiteMojo extends AbstractMojo {
37
38
39
40
41 private static final String TEST_ROOT_PROPERTY = "castor.xmlctf.root";
42
43
44
45
46 @Parameter(property = "outputRoot", defaultValue = "./target/xmlctf")
47 private String outputRoot;
48
49
50
51
52 @Parameter(property = "testRoot", defaultValue = "${basedir}/tests/MasterTestSuite/")
53 private String testRoot;
54
55
56
57
58 @Parameter(property = "pathToTools")
59 private String pathToTools;
60
61
62
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
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
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
104
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
117
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
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
160 TestCaseAggregator aggregator = new TestCaseAggregator(testRootFile, outputRoot);
161
162 runJUnit(aggregator.suite());
163 }
164
165
166
167
168
169
170
171 public abstract void runJUnit(Test testSuite) throws MojoExecutionException;
172
173 }