All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.searls.jasmine.server.JasmineResourceHandler Maven / Gradle / Ivy

Go to download

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

There is a newer version: 1.1.2
Show newest version
package com.github.searls.jasmine.server;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.ResourceHandler;
import org.eclipse.jetty.util.resource.Resource;

import com.github.searls.jasmine.AbstractJasmineMojo;
import com.github.searls.jasmine.CreatesManualRunner;
import com.github.searls.jasmine.NullLog;
import com.github.searls.jasmine.coffee.DetectsCoffee;
import com.github.searls.jasmine.coffee.HandlesRequestsForCoffee;

public class JasmineResourceHandler extends ResourceHandler {

	private DetectsCoffee detectsCoffee = new DetectsCoffee();
	private HandlesRequestsForCoffee handlesRequestsForCoffee = new HandlesRequestsForCoffee();
	private CreatesManualRunner createsManualRunner;

	public JasmineResourceHandler(AbstractJasmineMojo config) {
		createsManualRunner = new CreatesManualRunner(config);
		createsManualRunner.setLog(new NullLog());
	}

	@Override
	public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		createManualSpecRunnerIfNecessary(target);
		Resource resource = getResource(baseRequest);
		if (detectsCoffee.detect(target) && weCanHandleIt(baseRequest, resource)) {
			handlesRequestsForCoffee.handle(baseRequest, response, resource);
		} else {
			//Not testable. Who knew test-driving an LSP violation would be this hard. Sigh.  :-(
			super.handle(target, baseRequest, baseRequest, response);
		}
	}

	private void createManualSpecRunnerIfNecessary(String target) throws IOException {
		if ("/".equals(target)) {
			createsManualRunner.create();
		}
	}

	private boolean weCanHandleIt(Request baseRequest, Resource resource) {
		return !baseRequest.isHandled() && resource != null && resource.exists();
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy