Author: mazi
Date: 2010-04-13 11:10:51 -0400 (Tue, 13 Apr 2010)
New Revision: 6103
Added:
servlet/trunk/tests/src/test/java/org/jboss/weld/test/tomcat/lookup/BatListener.java
servlet/trunk/tests/src/test/java/org/jboss/weld/test/tomcat/lookup/BatServlet.java
servlet/trunk/tests/src/test/java/org/jboss/weld/test/tomcat/lookup/CatFilter.java
servlet/trunk/tests/src/test/java/org/jboss/weld/test/tomcat/lookup/FilterInjectionTest.java
servlet/trunk/tests/src/test/java/org/jboss/weld/test/tomcat/lookup/ListenerInjectionTest.java
servlet/trunk/tests/src/test/resources/org/jboss/weld/test/tomcat/lookup/web-filter-injection.xml
servlet/trunk/tests/src/test/resources/org/jboss/weld/test/tomcat/lookup/web-listener-injection.xml
Log:
WELDX-83: Tests for injection into Filters and (some) Listeners on Tomcat standalone
Added:
servlet/trunk/tests/src/test/java/org/jboss/weld/test/tomcat/lookup/BatListener.java
===================================================================
--- servlet/trunk/tests/src/test/java/org/jboss/weld/test/tomcat/lookup/BatListener.java
(rev 0)
+++
servlet/trunk/tests/src/test/java/org/jboss/weld/test/tomcat/lookup/BatListener.java 2010-04-13
15:10:51 UTC (rev 6103)
@@ -0,0 +1,64 @@
+package org.jboss.weld.test.tomcat.lookup;
+
+import org.jboss.weld.servlet.api.ServletListener;
+
+import javax.inject.Inject;
+import javax.servlet.ServletContextEvent;
+import javax.servlet.ServletRequestEvent;
+import javax.servlet.http.HttpSessionEvent;
+
+public class BatListener implements ServletListener
+{
+ public static final String BAT_ATTRIBUTE_NAME = "batAttribute";
+
+ @Inject Sewer sewer;
+
+ public void contextInitialized(ServletContextEvent sce)
+ {
+ if (isSewerNameOk()) {
+ sce.getServletContext().setAttribute(BAT_ATTRIBUTE_NAME, Boolean.TRUE);
+ }
+ }
+
+ public void contextDestroyed(ServletContextEvent sce)
+ {
+ isSewerNameOk();
+ }
+
+ public void requestInitialized(ServletRequestEvent sre)
+ {
+ if (isSewerNameOk()) {
+ sre.getServletRequest().setAttribute(BAT_ATTRIBUTE_NAME, Boolean.TRUE);
+ }
+ }
+
+ public void requestDestroyed(ServletRequestEvent sre)
+ {
+ isSewerNameOk();
+ }
+
+ public void sessionCreated(HttpSessionEvent se)
+ {
+ if (isSewerNameOk()) {
+ se.getSession().setAttribute(BAT_ATTRIBUTE_NAME, Boolean.TRUE);
+ }
+ }
+
+ public void sessionDestroyed(HttpSessionEvent se)
+ {
+ isSewerNameOk();
+ }
+
+ private boolean isSewerNameOk() throws NullPointerException
+ {
+ try
+ {
+ return Sewer.NAME.equals(sewer.getName());
+ }
+ catch (NullPointerException e)
+ {
+ e.printStackTrace();
+ return false;
+ }
+ }
+}
Added:
servlet/trunk/tests/src/test/java/org/jboss/weld/test/tomcat/lookup/BatServlet.java
===================================================================
--- servlet/trunk/tests/src/test/java/org/jboss/weld/test/tomcat/lookup/BatServlet.java
(rev 0)
+++
servlet/trunk/tests/src/test/java/org/jboss/weld/test/tomcat/lookup/BatServlet.java 2010-04-13
15:10:51 UTC (rev 6103)
@@ -0,0 +1,26 @@
+package org.jboss.weld.test.tomcat.lookup;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+
+public class BatServlet extends HttpServlet
+{
+ @Override
+ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws
ServletException, IOException
+ {
+ resp.setStatus(getStatus(req));
+ }
+
+ private int getStatus(HttpServletRequest req)
+ {
+ return req.getAttribute(BatListener.BAT_ATTRIBUTE_NAME) == Boolean.TRUE
&&
+ req.getSession().getAttribute(BatListener.BAT_ATTRIBUTE_NAME) ==
Boolean.TRUE &&
+
req.getSession().getServletContext().getAttribute(BatListener.BAT_ATTRIBUTE_NAME) ==
Boolean.TRUE
+ ? HttpServletResponse.SC_OK
+ : HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
+ }
+
+}
Added: servlet/trunk/tests/src/test/java/org/jboss/weld/test/tomcat/lookup/CatFilter.java
===================================================================
--- servlet/trunk/tests/src/test/java/org/jboss/weld/test/tomcat/lookup/CatFilter.java
(rev 0)
+++
servlet/trunk/tests/src/test/java/org/jboss/weld/test/tomcat/lookup/CatFilter.java 2010-04-13
15:10:51 UTC (rev 6103)
@@ -0,0 +1,32 @@
+package org.jboss.weld.test.tomcat.lookup;
+
+import javax.inject.Inject;
+import javax.servlet.*;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+
+public class CatFilter implements Filter
+{
+
+ @Inject Sewer sewer;
+
+ public void init(FilterConfig filterConfig) throws ServletException
+ {
+ isSewerNameOk();
+ }
+
+ public void doFilter(ServletRequest request, ServletResponse response, FilterChain
chain) throws IOException, ServletException
+ {
+ ((HttpServletResponse)response).setStatus(isSewerNameOk() ?
HttpServletResponse.SC_OK : HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
+ }
+
+ public void destroy()
+ {
+ isSewerNameOk();
+ }
+
+ private boolean isSewerNameOk() throws NullPointerException
+ {
+ return Sewer.NAME.equals(sewer.getName());
+ }
+}
\ No newline at end of file
Added:
servlet/trunk/tests/src/test/java/org/jboss/weld/test/tomcat/lookup/FilterInjectionTest.java
===================================================================
---
servlet/trunk/tests/src/test/java/org/jboss/weld/test/tomcat/lookup/FilterInjectionTest.java
(rev 0)
+++
servlet/trunk/tests/src/test/java/org/jboss/weld/test/tomcat/lookup/FilterInjectionTest.java 2010-04-13
15:10:51 UTC (rev 6103)
@@ -0,0 +1,52 @@
+package org.jboss.weld.test.tomcat.lookup;
+
+
+
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.HttpMethod;
+import org.apache.commons.httpclient.methods.GetMethod;
+import org.jboss.testharness.impl.packaging.Artifact;
+import org.jboss.testharness.impl.packaging.Classes;
+import org.jboss.testharness.impl.packaging.IntegrationTest;
+import org.jboss.testharness.impl.packaging.Resource;
+import org.jboss.testharness.impl.packaging.Resources;
+import org.jboss.weld.test.AbstractWeldTest;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+@Artifact(addCurrentPackage=false)
+@IntegrationTest(runLocally=true)
+@Resources({
+ @Resource(source="context-servlet-injection.xml",
destination="/META-INF/context.xml"),
+ @Resource(source="web-filter-injection.xml",
destination="/WEB-INF/web.xml")
+})
+@Classes({
+ CatFilter.class,
+ Sewer.class
+})
+public class FilterInjectionTest extends AbstractWeldTest
+{
+
+ private static final Logger log = LoggerFactory.getLogger(FilterInjectionTest.class);
+
+ @Override
+ @BeforeClass
+ public void beforeClass() throws Throwable
+ {
+ log.info("To run the servlet filter injection test you must add
tomcat-support.jar to $CATALINA_BASE/lib");
+ super.beforeClass();
+ }
+
+
+ @Test
+ public void testFilterInjection() throws Exception
+ {
+ HttpClient client = new HttpClient();
+ HttpMethod method = new GetMethod(getContextPath() + "/cat");
+ assert client.executeMethod(method) == HttpServletResponse.SC_OK;
+ }
+}
Added:
servlet/trunk/tests/src/test/java/org/jboss/weld/test/tomcat/lookup/ListenerInjectionTest.java
===================================================================
---
servlet/trunk/tests/src/test/java/org/jboss/weld/test/tomcat/lookup/ListenerInjectionTest.java
(rev 0)
+++
servlet/trunk/tests/src/test/java/org/jboss/weld/test/tomcat/lookup/ListenerInjectionTest.java 2010-04-13
15:10:51 UTC (rev 6103)
@@ -0,0 +1,57 @@
+package org.jboss.weld.test.tomcat.lookup;
+
+
+
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.HttpMethod;
+import org.apache.commons.httpclient.methods.GetMethod;
+import org.jboss.testharness.impl.packaging.Artifact;
+import org.jboss.testharness.impl.packaging.Classes;
+import org.jboss.testharness.impl.packaging.IntegrationTest;
+import org.jboss.testharness.impl.packaging.Resource;
+import org.jboss.testharness.impl.packaging.Resources;
+import org.jboss.weld.test.AbstractWeldTest;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+@Artifact(addCurrentPackage=false)
+@IntegrationTest(runLocally=true)
+@Resources({
+ @Resource(source="context-servlet-injection.xml",
destination="/META-INF/context.xml"),
+ @Resource(source="web-listener-injection.xml",
destination="/WEB-INF/web.xml")
+})
+@Classes({
+ BatListener.class,
+ BatServlet.class,
+ Sewer.class
+})
+public class ListenerInjectionTest extends AbstractWeldTest
+{
+
+ private static final Logger log =
LoggerFactory.getLogger(ListenerInjectionTest.class);
+
+ @Override
+ @BeforeClass
+ public void beforeClass() throws Throwable
+ {
+ log.info("To run the servlet listener injection test you must add
tomcat-support.jar to $CATALINA_BASE/lib");
+ super.beforeClass();
+ }
+
+
+ // This test currently fails showing that injection into Listeners doesn't
+ // work in Tomcat standalone.
+// @Test
+ public void testListenerInjection() throws Exception
+ {
+ HttpClient client = new HttpClient();
+ HttpMethod method = new GetMethod(getContextPath() + "/bat");
+ int sc = client.executeMethod(method);
+ System.out.println("sc = " + sc);
+ assert sc == HttpServletResponse.SC_OK;
+ }
+}
Added:
servlet/trunk/tests/src/test/resources/org/jboss/weld/test/tomcat/lookup/web-filter-injection.xml
===================================================================
---
servlet/trunk/tests/src/test/resources/org/jboss/weld/test/tomcat/lookup/web-filter-injection.xml
(rev 0)
+++
servlet/trunk/tests/src/test/resources/org/jboss/weld/test/tomcat/lookup/web-filter-injection.xml 2010-04-13
15:10:51 UTC (rev 6103)
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<web-app version="2.5"
+
xmlns="http://java.sun.com/xml/ns/javaee"
+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
+
+ <display-name>JBoss Test Harness</display-name>
+
+ <listener>
+
<listener-class>org.jboss.testharness.impl.runner.servlet.HarnessServletListener</listener-class>
+ </listener>
+
+ <listener>
+
<listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
+ </listener>
+
+ <filter>
+ <filter-name>Cat Filter</filter-name>
+
<filter-class>org.jboss.weld.test.tomcat.lookup.CatFilter</filter-class>
+ </filter>
+
+ <filter-mapping>
+ <filter-name>Cat Filter</filter-name>
+ <url-pattern>/cat</url-pattern>
+ </filter-mapping>
+
+</web-app>
Added:
servlet/trunk/tests/src/test/resources/org/jboss/weld/test/tomcat/lookup/web-listener-injection.xml
===================================================================
---
servlet/trunk/tests/src/test/resources/org/jboss/weld/test/tomcat/lookup/web-listener-injection.xml
(rev 0)
+++
servlet/trunk/tests/src/test/resources/org/jboss/weld/test/tomcat/lookup/web-listener-injection.xml 2010-04-13
15:10:51 UTC (rev 6103)
@@ -0,0 +1,32 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<web-app version="2.5"
+
xmlns="http://java.sun.com/xml/ns/javaee"
+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
+
+ <display-name>JBoss Test Harness</display-name>
+
+ <listener>
+
<listener-class>org.jboss.testharness.impl.runner.servlet.HarnessServletListener</listener-class>
+ </listener>
+
+ <listener>
+
<listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
+ </listener>
+
+ <listener>
+
<listener-class>org.jboss.weld.test.tomcat.lookup.BatListener</listener-class>
+ </listener>
+
+ <servlet>
+ <servlet-name>Bat Servlet</servlet-name>
+
<servlet-class>org.jboss.weld.test.tomcat.lookup.BatServlet</servlet-class>
+ </servlet>
+
+ <servlet-mapping>
+ <servlet-name>Bat Servlet</servlet-name>
+ <url-pattern>/bat</url-pattern>
+ </servlet-mapping>
+
+</web-app>