1 package com.github.searls.jasmine.mojo;
2
3 import com.github.klieber.phantomjs.locate.PhantomJsLocatorOptions;
4 import com.github.klieber.phantomjs.locate.RepositoryDetails;
5 import com.github.searls.jasmine.NullLog;
6 import com.github.searls.jasmine.driver.WebDriverFactory;
7 import com.github.searls.jasmine.format.JasmineResultLogger;
8 import com.github.searls.jasmine.io.RelativizesFilePaths;
9 import com.github.searls.jasmine.model.JasmineResult;
10 import com.github.searls.jasmine.runner.CreatesRunner;
11 import com.github.searls.jasmine.runner.ReporterType;
12 import com.github.searls.jasmine.runner.SpecRunnerExecutor;
13 import com.github.searls.jasmine.server.ResourceHandlerConfigurator;
14 import com.github.searls.jasmine.server.ServerManager;
15 import org.apache.maven.execution.MavenSession;
16 import org.apache.maven.plugin.MojoExecutionException;
17 import org.apache.maven.plugin.MojoFailureException;
18 import org.apache.maven.plugin.logging.Log;
19 import org.apache.maven.plugins.annotations.LifecyclePhase;
20 import org.apache.maven.plugins.annotations.Mojo;
21 import org.apache.maven.plugins.annotations.Parameter;
22 import org.apache.maven.plugins.annotations.ResolutionScope;
23 import org.eclipse.aether.RepositorySystem;
24 import org.eclipse.aether.RepositorySystemSession;
25 import org.eclipse.aether.repository.RemoteRepository;
26 import org.eclipse.jetty.server.Server;
27 import org.openqa.selenium.WebDriver;
28
29 import javax.inject.Inject;
30 import java.io.File;
31 import java.net.URL;
32 import java.util.Collections;
33 import java.util.List;
34 import java.util.Properties;
35
36
37
38
39 @Mojo(name = "test", defaultPhase = LifecyclePhase.TEST, requiresDependencyResolution = ResolutionScope.TEST)
40 public class TestMojo extends AbstractJasmineMojo {
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58 @Parameter(defaultValue = "org.openqa.selenium.phantomjs.PhantomJSDriver")
59 protected String webDriverClassName;
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91 @Parameter
92 protected List<Capability> webDriverCapabilities = Collections.emptyList();
93
94
95
96
97
98
99
100
101
102
103 @Parameter(defaultValue = "FIREFOX_17")
104 @Deprecated
105 protected String browserVersion;
106
107
108
109
110
111
112
113
114
115
116
117 @Parameter(defaultValue = "documentation")
118 protected String format;
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140 @Parameter(property = "phantomjs", defaultValue = "${phantomJs}")
141 protected PhantomJsOptions phantomjs;
142
143
144
145
146
147
148
149 @Parameter(property = "keepServerAlive", defaultValue = "false")
150 protected boolean keepServerAlive;
151
152 @Parameter(
153 defaultValue = "${repositorySystemSession}",
154 readonly = true
155 )
156 private RepositorySystemSession repositorySystemSession;
157
158 @Parameter(
159 defaultValue = "${project.remoteProjectRepositories}",
160 readonly = true
161 )
162 private List<RemoteRepository> remoteRepositories;
163
164 @Parameter(
165 defaultValue = "${session}",
166 readonly = true
167 )
168 private MavenSession mavenSession;
169
170 private RepositorySystem repositorySystem;
171
172 private final RelativizesFilePaths relativizesFilePaths;
173
174 @Inject
175 public TestMojo(RepositorySystem repositorySystem) {
176 this.repositorySystem = repositorySystem;
177 this.relativizesFilePaths = new RelativizesFilePaths();
178 }
179
180 @Override
181 public void execute() throws MojoExecutionException, MojoFailureException {
182 if (!this.isSkipTests()) {
183 super.execute();
184 } else {
185 this.getLog().info("Skipping Jasmine Specs");
186 }
187 }
188
189 @Override
190 public void run() throws Exception {
191 ServerManager serverManager = this.getServerManager();
192 try {
193 int port = serverManager.start();
194 setPortProperty(port);
195 this.getLog().info("Executing Jasmine Specs");
196 JasmineResult result = this.executeSpecs(new URL(this.uriScheme + "://" + this.serverHostname + ":" + port));
197 this.logResults(result);
198 this.throwAnySpecFailures(result);
199 } finally {
200 if (!keepServerAlive) {
201 serverManager.stop();
202 }
203 }
204 }
205
206 private ServerManager getServerManager() throws MojoExecutionException {
207 Log log = this.debug ? this.getLog() : new NullLog();
208
209 CreatesRunner createsRunner = new CreatesRunner(
210 this,
211 log,
212 this.specRunnerHtmlFileName,
213 ReporterType.JsApiReporter);
214
215 ResourceHandlerConfigurator configurator = new ResourceHandlerConfigurator(
216 this,
217 this.relativizesFilePaths,
218 createsRunner);
219
220 return new ServerManager(new Server(), getConnector(), configurator);
221 }
222
223 private void setPortProperty(int port) {
224 this.mavenProject.getProperties().setProperty("jasmine.serverPort", String.valueOf(port));
225 }
226
227 private JasmineResult executeSpecs(URL runner) throws Exception {
228 WebDriver driver = this.createDriver();
229 JasmineResult result = new SpecRunnerExecutor().execute(
230 runner,
231 driver,
232 this.timeout,
233 this.debug,
234 this.getLog(),
235 this.format,
236 getReporters(),
237 getFileSystemReporters()
238 );
239 return result;
240 }
241
242 private WebDriver createDriver() throws Exception {
243 RepositoryDetails details = new RepositoryDetails();
244 details.setRemoteRepositories(remoteRepositories);
245 details.setRepositorySystem(repositorySystem);
246 details.setRepositorySystemSession(repositorySystemSession);
247
248 configure(mavenSession.getUserProperties());
249
250 WebDriverFactory factory = new WebDriverFactory();
251 factory.setWebDriverCapabilities(webDriverCapabilities);
252 factory.setWebDriverClassName(webDriverClassName);
253 factory.setDebug(debug);
254 factory.setBrowserVersion(browserVersion);
255 factory.setPhantomJsLocatorOptions(phantomjs);
256 factory.setRepositoryDetails(details);
257
258 return factory.createWebDriver();
259 }
260
261 private void configure(Properties properties) {
262
263 phantomjs.setVersion(
264 properties.getProperty("phantomjs.version", phantomjs.getVersion())
265 );
266
267 phantomjs.setSource(
268 PhantomJsLocatorOptions.Source.valueOf(
269 properties.getProperty("phantomjs.source", phantomjs.getSource().toString())
270 )
271 );
272
273 phantomjs.setOutputDirectory(
274 new File(properties.getProperty("phantomjs.outputDirectory", phantomjs.getOutputDirectory().toString()))
275 );
276
277 phantomjs.setBaseUrl(
278 properties.getProperty("phantomjs.baseUrl", phantomjs.getBaseUrl())
279 );
280
281 phantomjs.setCheckSystemPath(
282 configureBoolean(properties, "phantomjs.checkSystemPath", phantomjs.isCheckSystemPath())
283 );
284
285 phantomjs.setEnforceVersion(
286 properties.getProperty("phantomjs.enforceVersion", phantomjs.getEnforceVersion())
287 );
288 }
289
290 private boolean configureBoolean(Properties properties, String property, boolean defaultValue) {
291 return Boolean.parseBoolean(properties.getProperty(property, Boolean.toString(defaultValue)));
292 }
293
294 private void logResults(JasmineResult result) {
295 JasmineResultLogger resultLogger = new JasmineResultLogger();
296 resultLogger.setLog(this.getLog());
297 resultLogger.log(result);
298 }
299
300 private void throwAnySpecFailures(JasmineResult result) throws MojoFailureException {
301 if (this.haltOnFailure && !result.didPass()) {
302 throw new MojoFailureException("There were Jasmine spec failures.");
303 }
304 }
305 }