Skip to content

Commit

Permalink
closes #159, refs #151; customRunnerTemplate and customRunnerConfigur…
Browse files Browse the repository at this point in the history
…ation parameters now support passing URL or file on the classpath
  • Loading branch information
klieber committed Apr 5, 2013
1 parent fc6bf5d commit c629a0f
Show file tree
Hide file tree
Showing 18 changed files with 340 additions and 86 deletions.
14 changes: 14 additions & 0 deletions features/custom_runners.feature
Expand Up @@ -9,6 +9,20 @@ Feature: using a custom runner template
When I run "mvn clean test"
Then the build should succeed
And the file "target/jasmine/SpecRunner.html" should contain "Copyright Acme, Inc."

Scenario: using a custom runner from the classpath

Given I am currently in the "jasmine-webapp-custom-runner-classpath" project
When I run "mvn clean install"
Then the build should succeed
And the file "jasmine-webapp/target/jasmine/SpecRunner.html" should contain "Copyright Acme, Inc."

Scenario: using a custom runner from a remote url

Given I am currently in the "jasmine-webapp-custom-runner-remote" project
When I run "mvn clean test"
Then the build should succeed
And the file "target/jasmine/SpecRunner.html" should contain "Copyright Acme, Inc."

@server
Scenario: using a custom runner with a footer and jQuery in it and running in browser test
Expand Down
5 changes: 5 additions & 0 deletions pom.xml
Expand Up @@ -118,6 +118,11 @@
<artifactId>plexus-utils</artifactId>
<version>3.0.10</version>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-resources</artifactId>
<version>1.0-alpha-7</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
Expand Down
@@ -1,6 +1,7 @@
package com.github.searls.jasmine.config;

import java.io.File;
import java.io.InputStream;
import java.util.List;

import org.apache.maven.plugin.logging.Log;
Expand All @@ -26,8 +27,8 @@ public interface JasmineConfiguration {

SpecRunnerTemplate getSpecRunnerTemplate();

File getCustomRunnerTemplate();
File getCustomRunnerConfiguration();
InputStream getCustomRunnerTemplate();
InputStream getCustomRunnerConfiguration();

String getScriptLoaderPath();

Expand Down
@@ -1,6 +1,7 @@
package com.github.searls.jasmine.mojo;

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand All @@ -9,8 +10,11 @@
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.resource.ResourceManager;
import org.codehaus.plexus.resource.loader.FileResourceLoader;

import com.github.searls.jasmine.config.JasmineConfiguration;
import com.github.searls.jasmine.exception.StringifiesStackTraces;
Expand Down Expand Up @@ -134,7 +138,7 @@ public abstract class AbstractJasmineMojo extends AbstractMojo implements Jasmin
* @since 1.1.0
*/
@Parameter
protected File customRunnerTemplate;
protected String customRunnerTemplate;

/**
* <p>Sometimes you want to have full control over how scriptloaders are configured. In order to interpolate custom configuration
Expand All @@ -148,7 +152,7 @@ public abstract class AbstractJasmineMojo extends AbstractMojo implements Jasmin
* @since 1.1.0
*/
@Parameter
protected File customRunnerConfiguration;
protected String customRunnerConfiguration;

/**
* Target directory for files created by the plugin.
Expand Down Expand Up @@ -357,14 +361,20 @@ public abstract class AbstractJasmineMojo extends AbstractMojo implements Jasmin
@Parameter(defaultValue="${project}", readonly=true)
protected MavenProject mavenProject;

@Component
protected ResourceManager locator;

protected ScriptSearch sources;
protected ScriptSearch specs;

protected StringifiesStackTraces stringifiesStackTraces = new StringifiesStackTraces();

private InputStream customRunnerTemplateStream;
private InputStream customRunnerConfigurationStream;

@Override
public final void execute() throws MojoExecutionException, MojoFailureException {
validateParameters();
loadResources();

this.sources = new ScriptSearch(this.jsSrcDir,this.sourceIncludes,this.sourceExcludes);
this.specs = new ScriptSearch(this.jsTestSrcDir,this.specIncludes,this.specExcludes);
Expand All @@ -386,8 +396,8 @@ public String getSourceEncoding() {
}

@Override
public File getCustomRunnerTemplate() {
return this.customRunnerTemplate;
public InputStream getCustomRunnerTemplate() {
return this.customRunnerTemplateStream;
}

@Override
Expand Down Expand Up @@ -450,8 +460,8 @@ public MavenProject getMavenProject() {
}

@Override
public File getCustomRunnerConfiguration() {
return this.customRunnerConfiguration;
public InputStream getCustomRunnerConfiguration() {
return this.customRunnerConfigurationStream;
}

@Deprecated
Expand All @@ -465,14 +475,30 @@ public File getBasedir() {
return this.mavenProject.getBasedir();
}

private void validateParameters() throws MojoExecutionException {
this.fileExists("customRunnerConfiguration",this.customRunnerConfiguration);
this.fileExists("customRunnerTemplate",this.customRunnerTemplate);
private void loadResources() throws MojoExecutionException {
this.customRunnerTemplateStream = this.getResourceAsInputStream("customRunnerTemplate", this.customRunnerTemplate);
this.customRunnerConfigurationStream = this.getResourceAsInputStream("customRunnerConfiguration", this.customRunnerConfiguration);
}

private void fileExists(String parameter, File file) throws MojoExecutionException {
if (file != null && (!file.exists() || !file.canRead())) {
throw new MojoExecutionException(String.format(ERROR_FILE_DNE,parameter,file));
private InputStream getResourceAsInputStream(String parameter, String resourceLocation) throws MojoExecutionException {
InputStream inputStream = null;
if (resourceLocation != null) {
locator.addSearchPath( "url", "" );
locator.addSearchPath( FileResourceLoader.ID, mavenProject.getFile().getParentFile().getAbsolutePath() );

ClassLoader origLoader = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
try {
inputStream = locator.getResourceAsInputStream(resourceLocation);
} catch (Exception e) {
throw new MojoExecutionException(String.format(ERROR_FILE_DNE,parameter,resourceLocation));
}
}
finally {
Thread.currentThread().setContextClassLoader( origLoader );
}
}
return inputStream;
}
}
@@ -1,9 +1,7 @@
package com.github.searls.jasmine.runner;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import java.io.InputStream;

import com.github.searls.jasmine.config.JasmineConfiguration;
import com.github.searls.jasmine.io.IOUtilsWrapper;
Expand All @@ -12,25 +10,29 @@
public class HtmlGeneratorConfiguration {
private final String sourceEncoding;
private final ReporterType reporterType;
private final File customRunnerTemplate;
private IOUtilsWrapper ioUtilsWrapper;
private final InputStream customRunnerTemplate;
private final IOUtilsWrapper ioUtilsWrapper;
private final SpecRunnerTemplate specRunnerTemplate;
private final ScriptResolver scriptResolver;
private final String scriptLoaderPath;
private final File customRunnerConfiguration;
private final InputStream customRunnerConfiguration;
private final String srcDirectoryName;
private final String specDirectoryName;
private final int autoRefreshInterval;
private final boolean autoRefresh;

public HtmlGeneratorConfiguration(ReporterType reporterType, JasmineConfiguration configuration, ScriptResolver scriptResolver) throws IOException {
this(new IOUtilsWrapper(), reporterType, configuration, scriptResolver);
}

public HtmlGeneratorConfiguration(IOUtilsWrapper ioUtilsWrapper, ReporterType reporterType, JasmineConfiguration configuration, ScriptResolver scriptResolver) throws IOException {
this.ioUtilsWrapper = ioUtilsWrapper;
this.sourceEncoding = configuration.getSourceEncoding();
this.reporterType = reporterType;
this.customRunnerTemplate = configuration.getCustomRunnerTemplate();
this.specRunnerTemplate = configuration.getSpecRunnerTemplate();
this.scriptResolver = scriptResolver;
this.customRunnerConfiguration = configuration.getCustomRunnerConfiguration();
this.ioUtilsWrapper = new IOUtilsWrapper();
this.scriptLoaderPath = configuration.getScriptLoaderPath();
this.srcDirectoryName = configuration.getSrcDirectoryName();
this.specDirectoryName = configuration.getSpecDirectoryName();
Expand All @@ -46,12 +48,12 @@ public ReporterType getReporterType() {
return this.reporterType;
}

public File getCustomRunnerTemplate() {
public InputStream getCustomRunnerTemplate() {
return this.customRunnerTemplate;
}

public String readFileToString(File customRunnerTemplate) throws IOException {
return FileUtils.readFileToString(customRunnerTemplate);
public String readFileToString(InputStream customRunnerTemplate) throws IOException {
return ioUtilsWrapper.toString(customRunnerTemplate);
}

public String IOtoString(String defaultHtmlTemplatePath) throws IOException {
Expand Down Expand Up @@ -107,15 +109,7 @@ public int hashCode() {
}

public String getCustomRunnerConfiguration() throws IOException {
if(null != this.customRunnerConfiguration) {
return FileUtils.readFileToString(this.customRunnerConfiguration);
} else {
return null;
}
}

public void setIoUtilsWrapper(IOUtilsWrapper ioUtilsWrapper) {
this.ioUtilsWrapper = ioUtilsWrapper;
return this.customRunnerConfiguration == null ? null : ioUtilsWrapper.toString(this.customRunnerConfiguration);
}

public String getScriptLoaderPath() {
Expand Down
Expand Up @@ -5,13 +5,18 @@
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.io.File;
import java.io.InputStream;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.resource.ResourceManager;
import org.codehaus.plexus.resource.loader.ResourceNotFoundException;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
Expand All @@ -29,6 +34,7 @@ public class AbstractJasmineMojoTest {

private static final String ENCODING = "UTF-8";
private static final String SCRIPT_LOADER_PATH = "scriptloaderpath";
private static final String PARENT_PROJECT_PATH = "/parent/project/path";

@InjectMocks @Spy
private final AbstractJasmineMojo subject = new AbstractJasmineMojo() {
Expand All @@ -40,27 +46,30 @@ public void run() throws Exception {}

@Rule public ExpectedException expectedException = ExpectedException.none();

@Mock
private File customRunnerConfiguration;
private static final String CUSTOM_RUNNER_CONFIG = "customRunnerConfiguration";

@Mock
private File customRunnerTemplate;
private static final String CUSTOM_RUNNER_TEMPLATE = "customRunnerTemplate";

@Mock
private File baseDir;

@Mock
private MavenProject mavenProject;

@Mock
private File projectFile;

@Mock
private File parentProjectFile;

@Mock
private ResourceManager locator;

@Before
public void before() {
subject.sourceEncoding = ENCODING;
subject.scriptLoaderPath = null;

when(customRunnerConfiguration.exists()).thenReturn(true);
when(customRunnerConfiguration.canRead()).thenReturn(true);
when(customRunnerTemplate.exists()).thenReturn(true);
when(customRunnerTemplate.canRead()).thenReturn(true);
subject.locator = locator;
}

@Test
Expand Down Expand Up @@ -121,13 +130,37 @@ public void testGetSourceEncoding() {
}

@Test
public void testGetCustomRunnerConfiguration() {
assertThat(subject.getCustomRunnerConfiguration(), is(this.customRunnerConfiguration));
public void testGetCustomRunnerConfiguration() throws ResourceNotFoundException, MojoExecutionException, MojoFailureException {
InputStream customRunnerConfigurationStream = mock(InputStream.class);
subject.customRunnerConfiguration = CUSTOM_RUNNER_CONFIG;
when(mavenProject.getFile()).thenReturn(projectFile);
when(projectFile.getParentFile()).thenReturn(parentProjectFile);
when(parentProjectFile.getAbsolutePath()).thenReturn(PARENT_PROJECT_PATH);
when(locator.getResourceAsInputStream(CUSTOM_RUNNER_CONFIG)).thenReturn(customRunnerConfigurationStream);
subject.execute();
assertThat(subject.getCustomRunnerConfiguration(), is(customRunnerConfigurationStream));
}

@Test
public void testGetCustomRunnerTemplate() {
assertThat(subject.getCustomRunnerTemplate(), is(this.customRunnerTemplate));
public void testGetCustomRunnerTemplate() throws ResourceNotFoundException, MojoExecutionException, MojoFailureException {
InputStream customRunnerTemplateStream = mock(InputStream.class);
subject.customRunnerTemplate = CUSTOM_RUNNER_TEMPLATE;
when(mavenProject.getFile()).thenReturn(projectFile);
when(projectFile.getParentFile()).thenReturn(parentProjectFile);
when(parentProjectFile.getAbsolutePath()).thenReturn(PARENT_PROJECT_PATH);
when(locator.getResourceAsInputStream(CUSTOM_RUNNER_TEMPLATE)).thenReturn(customRunnerTemplateStream);
subject.execute();
assertThat(subject.getCustomRunnerTemplate(), is(customRunnerTemplateStream));
}

@Test(expected=MojoExecutionException.class)
public void testGetCustomRunnerTemplateNotFound() throws ResourceNotFoundException, MojoExecutionException, MojoFailureException {
subject.customRunnerTemplate = CUSTOM_RUNNER_TEMPLATE;
when(mavenProject.getFile()).thenReturn(projectFile);
when(projectFile.getParentFile()).thenReturn(parentProjectFile);
when(parentProjectFile.getAbsolutePath()).thenReturn(PARENT_PROJECT_PATH);
when(locator.getResourceAsInputStream(CUSTOM_RUNNER_TEMPLATE)).thenThrow(new ResourceNotFoundException(CUSTOM_RUNNER_TEMPLATE));
subject.execute();
}

@Test
Expand Down

0 comments on commit c629a0f

Please sign in to comment.