1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
32
33
34
35
36
37
38 public abstract class AbstractTestSuiteMojo extends AbstractMojo {
39
40
41
42
43 private static final String TEST_ROOT_PROPERTY = "castor.xmlctf.root";
44
45
46
47
48
49
50 private String outputRoot;
51
52
53
54
55
56
57 private String testRoot;
58
59
60
61
62
63
64 private String pathToTools;
65
66
67
68
69
70
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
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
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
111
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
124
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
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
160 TestCaseAggregator aggregator = new TestCaseAggregator(testRootFile, outputRoot);
161
162 runJUnit(aggregator.suite());
163 }
164
165
166
167
168
169
170
171
172
173 public abstract void runJUnit(Test testSuite) throws MojoExecutionException;
174
175 }