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.commons.lang.StringUtils;
28  import org.apache.maven.artifact.DefaultArtifact;
29  import org.apache.maven.plugin.MojoExecutionException;
30  import org.apache.maven.plugin.MojoFailureException;
31  import org.apache.maven.project.MavenProject;
32  import org.codehaus.mojo.javascript.archive.JavascriptArtifactManager;
33  import org.codehaus.plexus.archiver.ArchiverException;
34  
35  import searls.jasmine.runner.SpecRunnerHtmlGenerator;
36  import searls.jasmine.runner.SpecRunnerHtmlGenerator.ReporterType;
37  
38  /**
39   * @component
40   * @goal generateManualRunner
41   * @phase generate-test-sources
42   * @requiresDependencyResolution test
43   */
44  public class GenerateManualRunnerMojo extends AbstractJasmineMojo {
45      /**
46       * The maven project.
47       *
48       * @parameter expression="${project}"
49       * @required
50       * @readonly
51       */
52      private MavenProject project;
53  
54      /**
55       * @component
56       */
57      private JavascriptArtifactManager javascriptArtifactManager;
58  
59  
60  	public void execute() throws MojoExecutionException, MojoFailureException {
61  		if(jasmineTestSourceDirectory.exists()) {
62              try
63              {
64                  javascriptArtifactManager.unpack( project, DefaultArtifact.SCOPE_TEST, new File(
65                      jasmineTargetDir, libsDirectory ), useArtifactId );
66              }
67              catch ( ArchiverException e )
68              {
69                  throw new MojoExecutionException( "Failed to unpack javascript dependencies", e );
70              }
71              
72  			getLog().info("Generating runner '"+manualSpecRunnerHtmlFileName+"' in the Jasmine plugin's target directory to open in a browser to facilitate faster feedback.");
73  			try {
74  				writeSpecRunnerToSourceSpecDirectory();
75  			} catch (Exception e) {
76  				throw new MojoFailureException(e,"JavaScript Test execution failed.","Failed to execute generated SpecRunner.html");
77  			}
78  		} else {
79              if( jasmineTestSourceDirectory.equals(jsunitTestSourceDirectory) ) {
80                  getLog().warn("Skipping manual spec runner generation. Check to make sure that `"+ jasmineTestSourceDirectory.getAbsolutePath()+"` exist.");
81              }
82  		}
83  	}
84  
85  	private void writeSpecRunnerToSourceSpecDirectory() throws IOException {
86  		SpecRunnerHtmlGenerator htmlGenerator = new SpecRunnerHtmlGenerator(preloadSources, sourceDirectory,
87              jasmineTestSourceDirectory, new File(jasmineTargetDir,libsDirectory), jasmineTargetDir);
88  		String runner = htmlGenerator.generate(pluginArtifacts, ReporterType.TrivialReporter);
89  		
90  		File destination = new File(jasmineTargetDir,manualSpecRunnerHtmlFileName);
91  		String existingRunner = loadExistingManualRunner(destination);
92  		
93  		if(!StringUtils.equals(runner, existingRunner)) {
94  			FileUtils.writeStringToFile(destination, runner);
95  		} else {
96  			getLog().info("Skipping spec runner generation, because an identical spec runner already exists.");
97  		}
98  	}
99  
100 	private String loadExistingManualRunner(File destination) {
101 		String existingRunner = null;
102 		try {
103 			if(destination.exists()) {
104 				existingRunner = FileUtils.readFileToString(destination);
105 			}
106 		} catch(Exception e) {
107 			getLog().warn("An error occurred while trying to open an existing manual spec runner. Continuing");
108 		}
109 		return existingRunner;
110 	}
111 
112 }