Skip to content

Commit

Permalink
Merge pull request #6 from ydylla/JettySpec
Browse files Browse the repository at this point in the history
Add JettySpec
  • Loading branch information
fkretzer authored Oct 26, 2018
2 parents fbcd60e + 0f7ac0b commit eca4837
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 24 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
description = "Utilities for Geb-based browser tests of Tapestry applications"

group = "de.eddyson"
version = "0.42.0"
version = "0.43.0"

apply plugin: 'groovy'
apply plugin: 'maven'
Expand Down
12 changes: 3 additions & 9 deletions src/main/groovy/de/eddyson/tapestrygeb/JettyGebSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ package de.eddyson.tapestrygeb

import static org.openqa.selenium.logging.LogType.BROWSER

import javax.servlet.ServletContext;

import org.apache.tapestry5.TapestryFilter;
import org.apache.tapestry5.ioc.Registry;
import org.eclipse.jetty.webapp.WebAppContext;
import org.apache.tapestry5.ioc.Registry
import org.slf4j.Logger
import org.slf4j.LoggerFactory

Expand All @@ -20,13 +16,11 @@ abstract class JettyGebSpec extends GebReportingSpec {
protected static Logger logger = LoggerFactory.getLogger(JettyGebSpec)

Registry getRegistry(){
WebAppContext webappContext = (WebAppContext) JettyExtension.runner.server.handler
ServletContext servletContext = webappContext.getServletContext()
(Registry) servletContext.getAttribute(TapestryFilter.REGISTRY_CONTEXT_NAME)
return JettyExtension.getRegistry()
}

def getService(Class serviceInterface){
getRegistry().getService(serviceInterface)
return getRegistry().getService(serviceInterface)
}

def setup() {
Expand Down
24 changes: 24 additions & 0 deletions src/main/groovy/de/eddyson/tapestrygeb/JettySpec.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package de.eddyson.tapestrygeb

import spock.lang.Specification
import org.apache.tapestry5.ioc.Registry

@RunJetty
abstract class JettySpec extends Specification {

Registry getRegistry(){
return JettyExtension.getRegistry()
}

def getService(Class serviceInterface){
return getRegistry().getService(serviceInterface)
}

def getJettyPort() {
return Integer.parseInt(System.getProperty("jettyPort"))
}

def getJettyBaseUrl() {
return "http://localhost:" + getJettyPort()
}
}
16 changes: 2 additions & 14 deletions src/main/java/de/eddyson/tapestrygeb/InjectInterceptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,10 @@
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;

import javax.servlet.ServletContext;

import org.apache.tapestry5.TapestryFilter;
import org.apache.tapestry5.ioc.AnnotationProvider;
import org.apache.tapestry5.ioc.Registry;
import org.apache.tapestry5.ioc.annotations.Autobuild;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.ioc.annotations.InjectService;
import org.eclipse.jetty.webapp.WebAppContext;
import org.spockframework.runtime.extension.AbstractMethodInterceptor;
import org.spockframework.runtime.extension.IMethodInvocation;
import org.spockframework.runtime.model.FieldInfo;
Expand Down Expand Up @@ -48,25 +43,18 @@ private void injectServices(final Object target, final boolean sharedFields) thr
|| ReflectionUtil.isAnnotationPresent(rawField, "javax.inject.Inject")
|| rawField.isAnnotationPresent(Autobuild.class))
&& rawField.isAnnotationPresent(Shared.class) == sharedFields) {
Object value = getRegistry().getObject(rawField.getType(), createAnnotationProvider(field));
Object value = JettyExtension.getRegistry().getObject(rawField.getType(), createAnnotationProvider(field));
rawField.setAccessible(true);
rawField.set(target, value);
} else if (rawField.isAnnotationPresent(InjectService.class)) {
String serviceName = rawField.getAnnotation(InjectService.class).value();
Object value = getRegistry().getService(serviceName, rawField.getType());
Object value = JettyExtension.getRegistry().getService(serviceName, rawField.getType());
rawField.setAccessible(true);
rawField.set(target, value);
}
}
}

private Registry getRegistry() {
WebAppContext webappContext = (WebAppContext) JettyExtension.runner.getServer().getHandler();
ServletContext servletContext = webappContext.getServletContext();
Registry registry = (Registry) servletContext.getAttribute(TapestryFilter.REGISTRY_CONTEXT_NAME);
return registry;
}

private static AnnotationProvider createAnnotationProvider(final FieldInfo field) {
return new AnnotationProvider() {
@Override
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/de/eddyson/tapestrygeb/JettyExtension.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package de.eddyson.tapestrygeb;

import org.apache.tapestry5.TapestryFilter;
import org.apache.tapestry5.ioc.Registry;
import org.apache.tapestry5.test.Jetty7Runner;
import org.eclipse.jetty.webapp.WebAppContext;
import org.spockframework.runtime.AbstractRunListener;
import org.spockframework.runtime.extension.AbstractAnnotationDrivenExtension;
import org.spockframework.runtime.extension.IMethodInterceptor;
import org.spockframework.runtime.model.FeatureInfo;
import org.spockframework.runtime.model.SpecInfo;

import javax.servlet.ServletContext;

public class JettyExtension extends AbstractAnnotationDrivenExtension<RunJetty> {

private boolean isSpecAnnotated;
Expand All @@ -15,6 +20,12 @@ public class JettyExtension extends AbstractAnnotationDrivenExtension<RunJetty>

private static boolean shutdownHookAdded = false;

static Registry getRegistry() {
WebAppContext webappContext = (WebAppContext) runner.getServer().getHandler();
ServletContext servletContext = webappContext.getServletContext();
return (Registry) servletContext.getAttribute(TapestryFilter.REGISTRY_CONTEXT_NAME);
}

@Override
public void visitSpec(final SpecInfo spec) {

Expand Down

0 comments on commit eca4837

Please # to comment.