com.github.searls.jasmine.ServerMojo Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jasmine-maven-plugin Show documentation
Show all versions of jasmine-maven-plugin Show documentation
A JavaScript unit test plugin that processes JavaScript sources and Jasmine specs, prepares test runner HTML files, executes Jasmine specs headlessly with HtmlUnit, and produces JUnit XML reports
package com.github.searls.jasmine;
import java.io.File;
import java.io.IOException;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.DefaultHandler;
import org.eclipse.jetty.server.handler.HandlerList;
import org.eclipse.jetty.server.handler.ResourceHandler;
import org.eclipse.jetty.server.nio.SelectChannelConnector;
import com.github.searls.jasmine.io.RelativizesFilePaths;
import com.github.searls.jasmine.server.JasmineResourceHandler;
/**
* @goal bdd
* @execute lifecycle="jasmine-lifecycle" phase="generate-sources"
* @requiresDirectInvocation true
*/
public class ServerMojo extends AbstractJasmineMojo {
private Server server = new Server();
private RelativizesFilePaths relativizesFilePaths = new RelativizesFilePaths();
@Override
public void run() throws Exception {
addConnectorToServer();
addHandlersToServer();
startServer();
}
private void addConnectorToServer() {
SelectChannelConnector connector = new SelectChannelConnector();
connector.setPort(serverPort);
server.addConnector(connector);
}
private void addHandlersToServer() throws IOException {
ResourceHandler resourceHandler = createResourceHandler();
HandlerList handlers = new HandlerList();
handlers.setHandlers(new Handler[] { resourceHandler, new DefaultHandler() });
server.setHandler(handlers);
}
private ResourceHandler createResourceHandler() throws IOException {
ResourceHandler resourceHandler = new JasmineResourceHandler(this);
resourceHandler.setDirectoriesListed(true);
resourceHandler.setWelcomeFiles(new String[]{ manualSpecRunnerPath() });
resourceHandler.setResourceBase(mavenProject.getBasedir().getAbsolutePath());
return resourceHandler;
}
private void startServer() throws Exception {
server.start();
getLog().info(buildServerInstructions());
server.join();
}
private String buildServerInstructions() {
return "\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\t" +
"http://localhost:"+serverPort+
"\n\n" +
"Just leave this process running as you test-drive your code, refreshing your browser window to re-run your specs. You can kill the server with Ctrl-C when you're done.";
}
private String manualSpecRunnerPath() throws IOException {
return relativizesFilePaths.relativize(mavenProject.getBasedir(), jasmineTargetDir) + File.separator +manualSpecRunnerHtmlFileName;
}
}