From d03e677b9340bd0200cc4720651834c66866d9d9 Mon Sep 17 00:00:00 2001 From: Yannick Dylla Date: Thu, 25 Oct 2018 13:04:28 +0200 Subject: [PATCH 1/2] added JettySpec for starting without Geb Useful for testing REST API endpoints --- .../eddyson/tapestrygeb/JettyGebSpec.groovy | 12 +++------- .../de/eddyson/tapestrygeb/JettySpec.groovy | 24 +++++++++++++++++++ .../tapestrygeb/InjectInterceptor.java | 16 ++----------- .../eddyson/tapestrygeb/JettyExtension.java | 11 +++++++++ 4 files changed, 40 insertions(+), 23 deletions(-) create mode 100644 src/main/groovy/de/eddyson/tapestrygeb/JettySpec.groovy diff --git a/src/main/groovy/de/eddyson/tapestrygeb/JettyGebSpec.groovy b/src/main/groovy/de/eddyson/tapestrygeb/JettyGebSpec.groovy index d578710..a4c99d1 100644 --- a/src/main/groovy/de/eddyson/tapestrygeb/JettyGebSpec.groovy +++ b/src/main/groovy/de/eddyson/tapestrygeb/JettyGebSpec.groovy @@ -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 @@ -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() { diff --git a/src/main/groovy/de/eddyson/tapestrygeb/JettySpec.groovy b/src/main/groovy/de/eddyson/tapestrygeb/JettySpec.groovy new file mode 100644 index 0000000..ca9918e --- /dev/null +++ b/src/main/groovy/de/eddyson/tapestrygeb/JettySpec.groovy @@ -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() + } +} diff --git a/src/main/java/de/eddyson/tapestrygeb/InjectInterceptor.java b/src/main/java/de/eddyson/tapestrygeb/InjectInterceptor.java index ed374e4..ea55391 100644 --- a/src/main/java/de/eddyson/tapestrygeb/InjectInterceptor.java +++ b/src/main/java/de/eddyson/tapestrygeb/InjectInterceptor.java @@ -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; @@ -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 diff --git a/src/main/java/de/eddyson/tapestrygeb/JettyExtension.java b/src/main/java/de/eddyson/tapestrygeb/JettyExtension.java index b43987e..e2750cc 100644 --- a/src/main/java/de/eddyson/tapestrygeb/JettyExtension.java +++ b/src/main/java/de/eddyson/tapestrygeb/JettyExtension.java @@ -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 { private boolean isSpecAnnotated; @@ -15,6 +20,12 @@ public class JettyExtension extends AbstractAnnotationDrivenExtension 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) { From 0f7ac0b5e3327aed37840c47c6f3cab0eb4bb9d7 Mon Sep 17 00:00:00 2001 From: Yannick Dylla Date: Thu, 25 Oct 2018 13:09:00 +0200 Subject: [PATCH 2/2] new version --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index 2864fd0..119d361 100644 --- a/build.gradle +++ b/build.gradle @@ -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'