View Javadoc

1   /*
2    * Derivative Work
3    * Copyright 2010 SOFTEC sa. All rights reserved.
4    *
5    * Original Work
6    * Copyright 2010 Justin Searls
7    *
8    * Licensed under the Apache License, Version 2.0 (the "License");
9    * you may not use this file except in compliance with the License.
10   * You may obtain a copy of the License at
11   *
12   *      http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package searls.jasmine;
22  
23  import java.io.File;
24  import java.io.IOException;
25  
26  import org.apache.commons.io.FileUtils;
27  import org.apache.maven.plugin.MojoExecutionException;
28  import org.apache.maven.plugin.MojoFailureException;
29  
30  import searls.jasmine.format.JasmineResultLogger;
31  import searls.jasmine.model.JasmineResult;
32  import searls.jasmine.runner.SpecRunnerExecutor;
33  import searls.jasmine.runner.SpecRunnerHtmlGenerator;
34  import searls.jasmine.runner.SpecRunnerHtmlGenerator.ReporterType;
35  
36  /**
37   * @goal jasmine
38   * @phase test
39   */
40  public class JasmineMojo extends AbstractJasmineMojo {
41  
42  	public void execute() throws MojoExecutionException, MojoFailureException {
43          if(!jasmineTestSourceDirectory.exists()) {
44              getLog().info("No Jasmine tests, skipping Jasmine tests execution.");
45              return;
46          }
47  
48          if (skipTests)
49          {
50              getLog().warn( "Skipping Jasmine tests." );
51              return;
52          }
53  
54          getLog().info("Executing Jasmine Tests");
55          JasmineResult result;
56          if(browsers == null) {
57              browsers = new String[] { "FF3.6" };
58          }
59          for(String browser : browsers) {
60              try {
61                  File runnerFile = writeSpecRunnerToOutputDirectory();
62                  result = new SpecRunnerExecutor().execute(runnerFile.toURI().toURL(),browser);
63              } catch (Exception e) {
64                  throw new MojoExecutionException("There was a problem executing Jasmine specs",e);
65              }
66              logResults(result,browser);
67              if(!testFailureIgnore && !result.didPass()) {
68                  throw new MojoFailureException("There were Jasmine spec failures.");
69              }
70          }
71  	}
72  
73  	private void logResults(JasmineResult result, String browser) {
74  		JasmineResultLogger resultLogger = new JasmineResultLogger();
75          resultLogger.setBrowser(browser);
76  		resultLogger.setLog(getLog());
77  		resultLogger.log(result);
78  	}
79  
80  	private File writeSpecRunnerToOutputDirectory() throws IOException {
81  		SpecRunnerHtmlGenerator htmlGenerator = new SpecRunnerHtmlGenerator(preloadSources,new File(jasmineTargetDir,srcDirectoryName),new File(
82              jasmineTargetDir,specDirectoryName),new File(jasmineTargetDir,libsDirectory), jasmineTargetDir);
83  		String html = htmlGenerator.generate(pluginArtifacts, ReporterType.JsApiReporter);
84  		
85  		getLog().debug("Writing out Spec Runner HTML " + html + " to directory " + jasmineTargetDir);
86  		File runnerFile = new File(jasmineTargetDir,specRunnerHtmlFileName);
87  		FileUtils.writeStringToFile(runnerFile, html);
88  		return runnerFile;
89  	}
90  
91  }