Skip to content

Commit

Permalink
Merge pull request #184 from klieber/issue/184_https_support
Browse files Browse the repository at this point in the history
adding support for changing the jetty connector
  • Loading branch information
klieber committed Jan 9, 2014
2 parents 5425526 + d7caabd commit 6e27461
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
import com.github.searls.jasmine.runner.SpecRunnerTemplate;

public interface JasmineConfiguration {
File getBasedir();

File getBasedir();
File getJasmineTargetDir();

String getSrcDirectoryName();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package com.github.searls.jasmine.mojo;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import com.github.searls.jasmine.config.JasmineConfiguration;
import com.github.searls.jasmine.exception.StringifiesStackTraces;
import com.github.searls.jasmine.io.ScansDirectory;
import com.github.searls.jasmine.model.ScriptSearch;
import com.github.searls.jasmine.runner.SpecRunnerTemplate;
import org.apache.commons.lang3.StringUtils;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
Expand All @@ -15,12 +14,14 @@
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.resource.ResourceManager;
import org.codehaus.plexus.resource.loader.FileResourceLoader;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.nio.SelectChannelConnector;

import com.github.searls.jasmine.config.JasmineConfiguration;
import com.github.searls.jasmine.exception.StringifiesStackTraces;
import com.github.searls.jasmine.io.ScansDirectory;
import com.github.searls.jasmine.model.ScriptSearch;
import com.github.searls.jasmine.runner.SpecRunnerTemplate;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public abstract class AbstractJasmineMojo extends AbstractMojo implements JasmineConfiguration {

Expand Down Expand Up @@ -369,6 +370,14 @@ public abstract class AbstractJasmineMojo extends AbstractMojo implements Jasmin
@Parameter(property="jasmine.serverPort", defaultValue="8234")
protected int serverPort;

/**
* <p>Specify the URI scheme in which to access the SpecRunner.</p>
*
* @since 1.3.1.4
*/
@Parameter(property="jasmine.uriScheme", defaultValue="http")
protected String uriScheme;

/**
* <p>Not used by the <code>jasmine:bdd</code> goal.</p>
*
Expand Down Expand Up @@ -421,6 +430,20 @@ public abstract class AbstractJasmineMojo extends AbstractMojo implements Jasmin
@Parameter(property="coffeeScriptCompilationEnabled", defaultValue="true")
protected boolean coffeeScriptCompilationEnabled;

/**
* <p>Type of {@link org.eclipse.jetty.server.Connector} to use on the jetty server.</p>
*
* <p>Most users won't need to change this from the default value. It should only be used
* by advanced users.</p>
*
* @since 1.3.1.4
*/
@Parameter(
property="jasmine.connectorClass",
defaultValue="org.eclipse.jetty.server.nio.SelectChannelConnector"
)
protected String connectorClass;

@Parameter(defaultValue="${project}", readonly=true)
protected MavenProject mavenProject;

Expand Down Expand Up @@ -544,6 +567,20 @@ public File getBasedir() {
return this.mavenProject.getBasedir();
}

protected Connector getConnector() throws MojoExecutionException {
try {
@SuppressWarnings("unchecked")
Class<? extends Connector> c = (Class<? extends Connector>) Class.forName(connectorClass);
return c.newInstance();
} catch(InstantiationException e) {
throw new MojoExecutionException("Unable to instantiate.",e);
} catch (IllegalAccessException e) {
throw new MojoExecutionException("Unable to instantiate.",e);
} catch (ClassNotFoundException e) {
throw new MojoExecutionException("Unable to instantiate.",e);
}
}

protected boolean isSkipTests() {
return this.skipTests || this.mvnTestSkip || this.skipJasmineTests;
}
Expand Down
20 changes: 11 additions & 9 deletions src/main/java/com/github/searls/jasmine/mojo/ServerMojo.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package com.github.searls.jasmine.mojo;

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

import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugins.annotations.Mojo;

import com.github.searls.jasmine.NullLog;
import com.github.searls.jasmine.io.RelativizesFilePaths;
import com.github.searls.jasmine.runner.CreatesRunner;
import com.github.searls.jasmine.runner.ReporterType;
import com.github.searls.jasmine.server.ResourceHandlerConfigurator;
import com.github.searls.jasmine.server.ServerManager;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugins.annotations.Mojo;
import org.eclipse.jetty.server.Server;

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

/**
* Execute specs in a web browser. Monitors your sources/specs for changes as you develop.
Expand All @@ -22,7 +23,7 @@ public class ServerMojo extends AbstractJasmineMojo {
public static final String INSTRUCTION_FORMAT =
"\n\n" +
"Server started--it's time to spec some JavaScript! You can run your specs as you develop by visiting this URL in a web browser: \n\n" +
" http://localhost:%s"+
" %s://localhost:%s"+
"\n\n" +
"The server will monitor these two directories for scripts that you add, remove, and change:\n\n" +
" source directory: %s\n\n"+
Expand All @@ -39,6 +40,7 @@ public ServerMojo() {
private String buildServerInstructions() throws IOException {
return String.format(
INSTRUCTION_FORMAT,
this.uriScheme,
this.serverPort,
this.getRelativePath(this.sources.getDirectory()),
this.getRelativePath(this.specs.getDirectory()));
Expand All @@ -53,7 +55,7 @@ public void run() throws Exception {
serverManager.join();
}

private ServerManager getServerManager() {
private ServerManager getServerManager() throws MojoExecutionException {
Log log = this.debug ? this.getLog() : new NullLog();

CreatesRunner createsRunner = new CreatesRunner(
Expand All @@ -67,7 +69,7 @@ private ServerManager getServerManager() {
this.relativizesFilePaths,
createsRunner);

return new ServerManager(configurator);
return new ServerManager(new Server(), getConnector(), configurator);
}

private String getRelativePath(File absolutePath) throws IOException {
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/com/github/searls/jasmine/mojo/TestMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.eclipse.jetty.server.Server;
import org.openqa.selenium.WebDriver;

import com.github.searls.jasmine.NullLog;
Expand Down Expand Up @@ -49,7 +50,7 @@ public void run() throws Exception {
int port = serverManager.start();
setPortProperty(port);
this.getLog().info("Executing Jasmine Specs");
JasmineResult result = this.executeSpecs(new URL("http://" + this.serverHostname + ":" + port));
JasmineResult result = this.executeSpecs(new URL(this.uriScheme+"://" + this.serverHostname + ":" + port));
this.logResults(result);
this.throwAnySpecFailures(result);
} finally {
Expand All @@ -59,7 +60,7 @@ public void run() throws Exception {
}
}

private ServerManager getServerManager() {
private ServerManager getServerManager() throws MojoExecutionException {
Log log = this.debug ? this.getLog() : new NullLog();

CreatesRunner createsRunner = new CreatesRunner(
Expand All @@ -73,7 +74,7 @@ private ServerManager getServerManager() {
this.relativizesFilePaths,
createsRunner);

return new ServerManager(configurator);
return new ServerManager(new Server(), getConnector(), configurator);
}

private void setPortProperty(int port) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,21 @@

import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.nio.SelectChannelConnector;

public class ServerManager {

private static final int ANY_PORT = 0;

private final Server server;
private final Connector connector;
private final ResourceHandlerConfigurator configurator;

public ServerManager(Server server, ResourceHandlerConfigurator configurator) {
this.configurator = configurator;
public ServerManager(Server server,
Connector connector,
ResourceHandlerConfigurator configurator) {
this.server = server;
}

public ServerManager(ResourceHandlerConfigurator configurator) {
this(new Server(),configurator);
this.connector = connector;
this.configurator = configurator;
}

public int start() throws Exception {
Expand All @@ -29,7 +28,6 @@ public void start(int port) throws Exception {
}

private int startServer(int port) throws Exception {
Connector connector = new SelectChannelConnector();
connector.setPort(port);

this.server.setHandler(this.configurator.createHandler());
Expand Down
40 changes: 25 additions & 15 deletions src/test/java/com/github/searls/jasmine/mojo/ServerMojoTest.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package com.github.searls.jasmine.mojo;

import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.powermock.api.mockito.PowerMockito.whenNew;

import java.io.File;

import com.github.searls.jasmine.io.RelativizesFilePaths;
import com.github.searls.jasmine.model.ScriptSearch;
import com.github.searls.jasmine.runner.CreatesRunner;
import com.github.searls.jasmine.runner.ReporterType;
import com.github.searls.jasmine.runner.SpecRunnerTemplate;
import com.github.searls.jasmine.server.ResourceHandlerConfigurator;
import com.github.searls.jasmine.server.ServerManager;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.project.MavenProject;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ssl.SslSelectChannelConnector;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -16,25 +20,29 @@
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import com.github.searls.jasmine.io.RelativizesFilePaths;
import com.github.searls.jasmine.model.ScriptSearch;
import com.github.searls.jasmine.runner.CreatesRunner;
import com.github.searls.jasmine.runner.ReporterType;
import com.github.searls.jasmine.runner.SpecRunnerTemplate;
import com.github.searls.jasmine.server.ResourceHandlerConfigurator;
import com.github.searls.jasmine.server.ServerManager;
import java.io.File;

import static org.mockito.Matchers.eq;
import static org.mockito.Matchers.isA;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.powermock.api.mockito.PowerMockito.whenNew;

@RunWith(PowerMockRunner.class)
@PrepareForTest(ServerMojo.class)
public class ServerMojoTest {

private static final String SPECS_DIR = "spec dir";
private static final String SOURCE_DIR = "source dir";
private static final String SCHEME = "http";
private static final int PORT = 8923;
private static final String RELATIVE_TARGET_DIR = "some dir";
private static final String MANUAL_SPEC_RUNNER_NAME = "nacho specs";
private static final String BASE_DIR = "my-base-dir";

private static final String connectorClassString = "org.eclipse.jetty.server.nio.SelectChannelConnector";
private static final Class<? extends Connector> connectorClass = org.eclipse.jetty.server.nio.SelectChannelConnector.class;

@InjectMocks private final ServerMojo subject = new ServerMojo();

@Mock private Log log;
Expand All @@ -55,11 +63,13 @@ public void arrangeAndAct() throws Exception {
this.subject.sources = this.sources;
this.subject.specs = this.specs;
this.subject.setLog(this.log);
this.subject.uriScheme = SCHEME;
this.subject.serverPort = PORT;
this.subject.jasmineTargetDir = this.targetDir;
this.subject.manualSpecRunnerHtmlFileName = MANUAL_SPEC_RUNNER_NAME;
this.subject.specRunnerTemplate = SpecRunnerTemplate.DEFAULT;
this.subject.debug = true;
this.subject.connectorClass = connectorClassString;
when(this.sourceDir.getAbsolutePath()).thenReturn(SOURCE_DIR);
when(this.specDir.getAbsolutePath()).thenReturn(SPECS_DIR);
when(this.sources.getDirectory()).thenReturn(this.sourceDir);
Expand All @@ -81,14 +91,14 @@ public void arrangeAndAct() throws Exception {
this.relativizesFilePaths,
createsRunner).thenReturn(configurator);

whenNew(ServerManager.class).withArguments(configurator).thenReturn(serverManager);
whenNew(ServerManager.class).withArguments(isA(Server.class), isA(connectorClass), eq(configurator)).thenReturn(serverManager);

this.subject.run();
}

@Test
public void logsInstructions() {
verify(this.log).info(String.format(ServerMojo.INSTRUCTION_FORMAT, PORT, SOURCE_DIR, SPECS_DIR));
verify(this.log).info(String.format(ServerMojo.INSTRUCTION_FORMAT, SCHEME, PORT, SOURCE_DIR, SPECS_DIR));
}

@Test
Expand Down
Loading

0 comments on commit 6e27461

Please sign in to comment.