You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by jg...@apache.org on 2011/10/03 15:04:04 UTC
svn commit: r1178401 [2/3] - in /openejb/trunk/arquillian-tomee:
arquillian-tomee-common/src/main/java/org/apache/openejb/arquillian/common/
arquillian-tomee-remote/src/main/java/org/apache/openejb/arquillian/remote/
arquillian-tomee-tests/ arquillian-...
Added: openejb/trunk/arquillian-tomee/arquillian-tomee-tests/src/test/java/org/apache/openejb/arquillian/tests/ServletListenerEjbLocalInjectionTest.java
URL: http://svn.apache.org/viewvc/openejb/trunk/arquillian-tomee/arquillian-tomee-tests/src/test/java/org/apache/openejb/arquillian/tests/ServletListenerEjbLocalInjectionTest.java?rev=1178401&view=auto
==============================================================================
--- openejb/trunk/arquillian-tomee/arquillian-tomee-tests/src/test/java/org/apache/openejb/arquillian/tests/ServletListenerEjbLocalInjectionTest.java (added)
+++ openejb/trunk/arquillian-tomee/arquillian-tomee-tests/src/test/java/org/apache/openejb/arquillian/tests/ServletListenerEjbLocalInjectionTest.java Mon Oct 3 13:04:03 2011
@@ -0,0 +1,542 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.openejb.arquillian.tests;
+
+import org.apache.commons.lang.StringUtils;
+import org.jboss.arquillian.api.Deployment;
+import org.jboss.arquillian.junit.Arquillian;
+import org.jboss.shrinkwrap.api.ArchivePaths;
+import org.jboss.shrinkwrap.api.ShrinkWrap;
+import org.jboss.shrinkwrap.api.asset.EmptyAsset;
+import org.jboss.shrinkwrap.api.asset.StringAsset;
+import org.jboss.shrinkwrap.api.spec.WebArchive;
+import org.jboss.shrinkwrap.descriptor.api.Descriptors;
+import org.jboss.shrinkwrap.descriptor.api.Node;
+import org.jboss.shrinkwrap.descriptor.api.spec.servlet.web.WebAppDescriptor;
+import org.jboss.shrinkwrap.descriptor.spi.NodeProvider;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import javax.annotation.Resource;
+import javax.ejb.EJB;
+import javax.ejb.Local;
+import javax.ejb.LocalBean;
+import javax.ejb.Stateless;
+import javax.inject.Inject;
+import javax.servlet.ServletContext;
+import javax.servlet.ServletContextEvent;
+import javax.servlet.ServletContextListener;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.HttpSession;
+import javax.servlet.http.HttpSessionEvent;
+import javax.servlet.http.HttpSessionListener;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+
+import static junit.framework.Assert.assertNotNull;
+import static junit.framework.Assert.assertTrue;
+
+@RunWith(Arquillian.class)
+public class ServletListenerEjbLocalInjectionTest {
+
+ public static final String TEST_NAME = ServletListenerEjbLocalInjectionTest.class.getSimpleName();
+
+ @Test
+ public void localEjbInjectionShouldSucceedInCtxtListener() throws Exception {
+ final String expectedOutput = "Context: Local: OpenEJB is employed at TomEE Software Inc.";
+ validateTest(expectedOutput);
+ }
+
+ @Test
+ public void localBeanEjbInjectionShouldSucceedInCtxtListener() throws Exception {
+ final String expectedOutput = "Context: OpenEJB shops at Apache Marketplace";
+ validateTest(expectedOutput);
+ }
+
+// @Test
+ public void pojoInjectionShouldSucceedInCtxtListener() throws Exception {
+ final String expectedOutput = "Context: OpenEJB is on the wheel of a 2011 Lexus IS 350";
+ validateTest(expectedOutput);
+ }
+
+ @Test
+ public void stringEnvEntryInjectionShouldSucceedInCtxtListener() throws Exception {
+ final String expectedOutput = "Context: tomee@apache.org";
+ validateTest(expectedOutput);
+ }
+
+ @Test
+ public void integerTypeEnvEntryInjectionShouldSucceedInCtxtListener() throws Exception {
+ final String expectedOutput = "Context: Connection Pool: 20";
+ validateTest(expectedOutput);
+ }
+
+// @Test
+ public void longTypeEnvEntryInjectionShouldSucceedInCtxtListener() throws Exception {
+ final String expectedOutput = "Context: Start Count: 200000";
+ validateTest(expectedOutput);
+ }
+
+ @Test
+ public void shortTypeEnvEntryInjectionShouldSucceedInCtxtListener() throws Exception {
+ final String expectedOutput = "Context: Init Size: 5";
+ validateTest(expectedOutput);
+ }
+
+ @Test
+ public void byteTypeEnvEntryInjectionShouldSucceedInCtxtListener() throws Exception {
+ final String expectedOutput = "Context: Total Quantity: 5";
+ validateTest(expectedOutput);
+ }
+
+ @Test
+ public void booleanTypeEnvEntryInjectionShouldSucceedInCtxtListener() throws Exception {
+ final String expectedOutput = "Context: Enable Email: true";
+ validateTest(expectedOutput);
+ }
+
+ @Test
+ public void charTypeEnvEntryInjectionShouldSucceedInCtxtListener() throws Exception {
+ final String expectedOutput = "Context: Option Default: X";
+ validateTest(expectedOutput);
+ }
+
+// @Test
+ public void classEnvEntryInjectionShouldSucceedInCtxtListener() throws Exception {
+ final String expectedOutput = "Context: java.lang.String";
+ validateTest(expectedOutput);
+ }
+
+// @Test
+ public void enumEnvEntryInjectionShouldSucceedInCtxtListener() throws Exception {
+ final String expectedOutput = "Context: DefaultCode: OK";
+ validateTest(expectedOutput);
+ }
+
+ @Test
+ public void localEjbInjectionShouldSucceedInSessionListener() throws Exception {
+ final String expectedOutput = "Session: Local: OpenEJB is employed at TomEE Software Inc.";
+ validateTest(expectedOutput);
+ }
+
+ @Test
+ public void localBeanEjbInjectionShouldSucceedInSessionListener() throws Exception {
+ final String expectedOutput = "Session: OpenEJB shops at Apache Marketplace";
+ validateTest(expectedOutput);
+ }
+
+// @Test
+ public void pojoInjectionShouldSucceedInSessionListener() throws Exception {
+ final String expectedOutput = "Session: OpenEJB is on the wheel of a 2011 Lexus IS 350";
+ validateTest(expectedOutput);
+ }
+
+ @Test
+ public void stringEnvEntryInjectionShouldSucceedInSessionListener() throws Exception {
+ final String expectedOutput = "Session: tomee@apache.org";
+ validateTest(expectedOutput);
+ }
+
+ @Test
+ public void integerTypeEnvEntryInjectionShouldSucceedInSessionListener() throws Exception {
+ final String expectedOutput = "Session: Connection Pool: 20";
+ validateTest(expectedOutput);
+ }
+
+// @Test
+ public void longTypeEnvEntryInjectionShouldSucceedInSessionListener() throws Exception {
+ final String expectedOutput = "Session: Start Count: 200000";
+ validateTest(expectedOutput);
+ }
+
+ @Test
+ public void shortTypeEnvEntryInjectionShouldSucceedInSessionListener() throws Exception {
+ final String expectedOutput = "Session: Init Size: 5";
+ validateTest(expectedOutput);
+ }
+
+ @Test
+ public void byteTypeEnvEntryInjectionShouldSucceedInSessionListener() throws Exception {
+ final String expectedOutput = "Session: Total Quantity: 5";
+ validateTest(expectedOutput);
+ }
+
+ @Test
+ public void booleanTypeEnvEntryInjectionShouldSucceedInSessionListener() throws Exception {
+ final String expectedOutput = "Session: Enable Email: true";
+ validateTest(expectedOutput);
+ }
+
+ @Test
+ public void charTypeEnvEntryInjectionShouldSucceedInSessionListener() throws Exception {
+ final String expectedOutput = "Session: Option Default: X";
+ validateTest(expectedOutput);
+ }
+
+// @Test
+ public void classEnvEntryInjectionShouldSucceedInSessionListener() throws Exception {
+ final String expectedOutput = "Session: java.lang.String";
+ validateTest(expectedOutput);
+ }
+
+// @Test
+ public void enumEnvEntryInjectionShouldSucceedInSessionListener() throws Exception {
+ final String expectedOutput = "Session: DefaultCode: OK";
+ validateTest(expectedOutput);
+ }
+
+ @Deployment(testable = false)
+ public static WebArchive createDeployment() {
+ final WebAppDescriptor descriptor = Descriptors.create(WebAppDescriptor.class)
+ .version("3.0")
+ .listener(PojoServletContextListener.class)
+ .listener(PojoServletSessionListener.class)
+ .servlet(ServletToCheckListener.class, "/" + TEST_NAME);
+
+ addEnvEntry(descriptor, "returnEmail", "java.lang.String", "tomee@apache.org");
+ addEnvEntry(descriptor, "connectionPool", "java.lang.Integer", "20");
+ addEnvEntry(descriptor, "startCount", "java.lang.Long", "200000");
+ addEnvEntry(descriptor, "initSize", "java.lang.Short", "5");
+ addEnvEntry(descriptor, "enableEmail", "java.lang.Boolean", "true");
+ addEnvEntry(descriptor, "totalQuantity", "java.lang.Byte", "5");
+ addEnvEntry(descriptor, "optionDefault", "java.lang.Character", "X");
+ addEnvEntry(descriptor, "auditWriter", "java.lang.Class", "java.lang.String");
+ addEnvEntry(descriptor, "defaultCode", "java.lang.Enum", "org.apache.openejb.arquillian.ServletListenerPojoInjectionTest$Code.OK");
+
+ final WebArchive archive = ShrinkWrap.create(WebArchive.class, TEST_NAME + ".war")
+ .addClass(PojoServletContextListener.class)
+ .addClass(PojoServletSessionListener.class)
+ .addClass(ServletToCheckListener.class)
+ .addClass(Car.class)
+ .addClass(CompanyLocal.class)
+ .addClass(Company.class)
+ .addClass(DefaultCompany.class)
+ .addClass(SuperMarket.class)
+ .setWebXML(new StringAsset(descriptor.exportAsString()))
+ .addAsWebInfResource(EmptyAsset.INSTANCE, ArchivePaths.create("beans.xml"));
+
+ System.err.println(descriptor.exportAsString());
+
+ return archive;
+ }
+
+ public static enum Code {
+ OK,
+ ERROR;
+ }
+
+ public static enum ContextAttributeName {
+ KEY_Car,
+ KEY_LocalEjb,
+ KEY_Market,
+ KEY_ConnPool,
+ KEY_StartCount,
+ KEY_InitSize,
+ KEY_TotalQuantity,
+ KEY_EnableEmail,
+ KEY_DefaultOption,
+ KEY_ReturnEmail,
+ KEY_AuditWriter,
+ KEY_DefaultCode,;
+ }
+
+ public static class ServletToCheckListener extends HttpServlet {
+
+ @Override
+ public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+ final ServletContext ctxt = req.getServletContext();
+ for (ContextAttributeName s : ContextAttributeName.values()) {
+ resp.getOutputStream().println("Context: " + ctxt.getAttribute(s.name()));
+ }
+
+ final HttpSession session = req.getSession();
+ for (ContextAttributeName s : ContextAttributeName.values()) {
+ resp.getOutputStream().println("Session: " + session.getAttribute(s.name()));
+ }
+ }
+ }
+
+ public static class PojoServletContextListener implements ServletContextListener {
+
+ @Inject
+ private Car car;
+
+ @EJB
+ private CompanyLocal localCompany;
+
+ @EJB
+ private SuperMarket market;
+
+ @Resource(name = "returnEmail")
+ private String returnEmail;
+
+ @Resource(name = "connectionPool")
+ private Integer connectionPool;
+
+ @Resource(name = "startCount")
+ private Long startCount;
+
+ @Resource(name = "initSize")
+ private Short initSize;
+
+ @Resource(name = "totalQuantity")
+ private Byte totalQuantity;
+
+ @Resource(name = "enableEmail")
+ private Boolean enableEmail;
+
+ @Resource(name = "optionDefault")
+ private Character optionDefault;
+
+ /* TODO: Enable this resource after functionality is fixed
+ @Resource
+ */
+ private Code defaultCode;
+
+ /* TODO: Enable this resource after functionality is fixed
+ @Resource
+ @SuppressWarnings("unchecked")
+ */
+ private Class auditWriter;
+
+
+ public void contextInitialized(ServletContextEvent event) {
+ final String name = "OpenEJB";
+ final ServletContext context = event.getServletContext();
+
+ if (car != null) {
+ context.setAttribute(ContextAttributeName.KEY_Car.name(), car.drive(name));
+ }
+ if (localCompany != null) {
+ context.setAttribute(ContextAttributeName.KEY_LocalEjb.name(), "Local: " + localCompany.employ(name));
+ }
+ if (market != null) {
+ context.setAttribute(ContextAttributeName.KEY_Market.name(), market.shop(name));
+ }
+ if (connectionPool != null) {
+ context.setAttribute(ContextAttributeName.KEY_ConnPool.name(), "Connection Pool: " + connectionPool);
+ }
+ if (startCount != null) {
+ context.setAttribute(ContextAttributeName.KEY_StartCount.name(), "Start Expressions.Count: " + startCount);
+ }
+ if (initSize != null) {
+ context.setAttribute(ContextAttributeName.KEY_InitSize.name(), "Init Size: " + initSize);
+ }
+ if (totalQuantity != null) {
+ context.setAttribute(ContextAttributeName.KEY_TotalQuantity.name(), "Total Quantity: " + totalQuantity);
+ }
+ if (enableEmail != null) {
+ context.setAttribute(ContextAttributeName.KEY_EnableEmail.name(), "Enable Email: " + enableEmail);
+ }
+ if (optionDefault != null) {
+ context.setAttribute(ContextAttributeName.KEY_DefaultOption.name(), "Option Default: " + optionDefault);
+ }
+ if (StringUtils.isNotEmpty(returnEmail) && returnEmail.equals("tomee@apache.org")) {
+ context.setAttribute(ContextAttributeName.KEY_ReturnEmail.name(), returnEmail);
+ }
+ if (auditWriter != null) {
+ context.setAttribute(ContextAttributeName.KEY_AuditWriter.name(), auditWriter.getClass().getName());
+ }
+ if (defaultCode != null) {
+ context.setAttribute(ContextAttributeName.KEY_DefaultCode.name(), "DefaultCode: " + defaultCode);
+ }
+ }
+
+ public void contextDestroyed(ServletContextEvent event) {
+ }
+
+ }
+
+ public static class PojoServletSessionListener implements HttpSessionListener {
+
+ @Inject
+ private Car car;
+
+ @EJB
+ private CompanyLocal localCompany;
+
+ @EJB
+ private SuperMarket market;
+
+ @Resource(name = "returnEmail")
+ private String returnEmail;
+
+ @Resource(name = "connectionPool")
+ private Integer connectionPool;
+
+ @Resource(name = "startCount")
+ private Long startCount;
+
+ @Resource(name = "initSize")
+ private Short initSize;
+
+ @Resource(name = "totalQuantity")
+ private Byte totalQuantity;
+
+ @Resource(name = "enableEmail")
+ private Boolean enableEmail;
+
+ @Resource(name = "optionDefault")
+ private Character optionDefault;
+
+ /* TODO: Enable this resource after functionality is fixed
+ @Resource
+ */
+ private Code defaultCode;
+
+ /* TODO: Enable this resource after functionality is fixed
+ @Resource
+ @SuppressWarnings("unchecked")
+ */
+ private Class auditWriter;
+
+
+ public void sessionCreated(HttpSessionEvent event) {
+ final String name = "OpenEJB";
+ final HttpSession context = event.getSession();
+
+ if (car != null) {
+ context.setAttribute(ContextAttributeName.KEY_Car.name(), car.drive(name));
+ }
+ if (localCompany != null) {
+ context.setAttribute(ContextAttributeName.KEY_LocalEjb.name(), "Local: " + localCompany.employ(name));
+ }
+ if (market != null) {
+ context.setAttribute(ContextAttributeName.KEY_Market.name(), market.shop(name));
+ }
+ if (connectionPool != null) {
+ context.setAttribute(ContextAttributeName.KEY_ConnPool.name(), "Connection Pool: " + connectionPool);
+ }
+ if (startCount != null) {
+ context.setAttribute(ContextAttributeName.KEY_StartCount.name(), "Start Expressions.Count: " + startCount);
+ }
+ if (initSize != null) {
+ context.setAttribute(ContextAttributeName.KEY_InitSize.name(), "Init Size: " + initSize);
+ }
+ if (totalQuantity != null) {
+ context.setAttribute(ContextAttributeName.KEY_TotalQuantity.name(), "Total Quantity: " + totalQuantity);
+ }
+ if (enableEmail != null) {
+ context.setAttribute(ContextAttributeName.KEY_EnableEmail.name(), "Enable Email: " + enableEmail);
+ }
+ if (optionDefault != null) {
+ context.setAttribute(ContextAttributeName.KEY_DefaultOption.name(), "Option Default: " + optionDefault);
+ }
+ if (StringUtils.isNotEmpty(returnEmail) && returnEmail.equals("tomee@apache.org")) {
+ context.setAttribute(ContextAttributeName.KEY_ReturnEmail.name(), returnEmail);
+ }
+ if (auditWriter != null) {
+ context.setAttribute(ContextAttributeName.KEY_AuditWriter.name(), auditWriter.getClass().getName());
+ }
+ if (defaultCode != null) {
+ context.setAttribute(ContextAttributeName.KEY_DefaultCode.name(), "DefaultCode: " + defaultCode);
+ }
+ }
+
+ public void sessionDestroyed(HttpSessionEvent event) {
+ }
+
+ }
+
+ public static class Car {
+ private final String make = "Lexus", model = "IS 350";
+ private final int year = 2011;
+
+ public String drive(String name) {
+ return name + " is on the wheel of a " + year + " " + make + " " + model;
+ }
+ }
+
+
+ public static interface Company {
+ public String employ(String employeeName);
+ }
+
+ @Local
+ public static interface CompanyLocal extends Company {
+ }
+
+ @Stateless
+ public static class DefaultCompany implements CompanyLocal {
+
+ private final String name = "TomEE Software Inc.";
+
+ public String employ(String employeeName) {
+ return employeeName + " is employed at " + name;
+ }
+
+ }
+
+ @Stateless
+ @LocalBean
+ public static class SuperMarket {
+
+ private final String name = "Apache Marketplace";
+
+ public String shop(String employeeName) {
+ return employeeName + " shops at " + name;
+ }
+
+ }
+
+ private static void addEnvEntry(WebAppDescriptor descriptor, String name, String type, String value) {
+ Node rootNode = ((NodeProvider) descriptor).getRootNode();
+ Node appNode = rootNode.get("/web-app").iterator().next();
+ appNode.create("/env-entry")
+ .create("env-entry-name").text(name)
+ .parent()
+ .create("env-entry-type").text(type)
+ .parent()
+ .create("env-entry-value").text(value)
+/*
+ .parent()
+ .create("injection-target")
+ .create("injection-target-class").text("org.apache.openejb.arquillian.ServletPojoInjectionTest$PojoServletContextListener")
+ .parent()
+ .create("injection-target-name").text(name)
+*/
+ ;
+
+ }
+
+ private void validateTest(String expectedOutput) throws IOException {
+ final InputStream is = new URL("http://localhost:9080/" + TEST_NAME + "/" + TEST_NAME).openStream();
+ final ByteArrayOutputStream os = new ByteArrayOutputStream();
+
+ int bytesRead = -1;
+ byte[] buffer = new byte[8192];
+ while ((bytesRead = is.read(buffer)) > -1) {
+ os.write(buffer, 0, bytesRead);
+ }
+
+ is.close();
+ os.close();
+
+ String output = new String(os.toByteArray(), "UTF-8");
+ assertNotNull("Response shouldn't be null", output);
+ assertTrue("Output should contain: " + expectedOutput, output.contains(expectedOutput));
+ }
+
+}
+
+
+
Added: openejb/trunk/arquillian-tomee/arquillian-tomee-tests/src/test/java/org/apache/openejb/arquillian/tests/ServletListenerEjbRemoteTest.java
URL: http://svn.apache.org/viewvc/openejb/trunk/arquillian-tomee/arquillian-tomee-tests/src/test/java/org/apache/openejb/arquillian/tests/ServletListenerEjbRemoteTest.java?rev=1178401&view=auto
==============================================================================
--- openejb/trunk/arquillian-tomee/arquillian-tomee-tests/src/test/java/org/apache/openejb/arquillian/tests/ServletListenerEjbRemoteTest.java (added)
+++ openejb/trunk/arquillian-tomee/arquillian-tomee-tests/src/test/java/org/apache/openejb/arquillian/tests/ServletListenerEjbRemoteTest.java Mon Oct 3 13:04:03 2011
@@ -0,0 +1,181 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.openejb.arquillian.tests;
+
+import org.jboss.arquillian.api.Deployment;
+import org.jboss.arquillian.junit.Arquillian;
+import org.jboss.shrinkwrap.api.ArchivePaths;
+import org.jboss.shrinkwrap.api.ShrinkWrap;
+import org.jboss.shrinkwrap.api.asset.EmptyAsset;
+import org.jboss.shrinkwrap.api.asset.StringAsset;
+import org.jboss.shrinkwrap.api.spec.WebArchive;
+import org.jboss.shrinkwrap.descriptor.api.Descriptors;
+import org.jboss.shrinkwrap.descriptor.api.spec.servlet.web.WebAppDescriptor;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import javax.ejb.EJB;
+import javax.ejb.Remote;
+import javax.ejb.Stateless;
+import javax.servlet.ServletContext;
+import javax.servlet.ServletContextEvent;
+import javax.servlet.ServletContextListener;
+import javax.servlet.ServletException;
+import javax.servlet.http.*;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+
+import static junit.framework.Assert.assertNotNull;
+import static junit.framework.Assert.assertTrue;
+
+@RunWith(Arquillian.class)
+public class ServletListenerEjbRemoteTest {
+
+ public static final String TEST_NAME = ServletListenerEjbRemoteTest.class.getSimpleName();
+
+ @Test
+ public void ejbInjectionShouldSucceedInCtxtListener() throws Exception {
+ final String expectedOutput = "Context: Remote: OpenEJB is employed at TomEE Software Inc.";
+ validateTest(expectedOutput);
+ }
+
+ @Test
+ public void ejbInjectionShouldSucceedInSessionListener() throws Exception {
+ final String expectedOutput = "Session: Remote: OpenEJB is employed at TomEE Software Inc.";
+ validateTest(expectedOutput);
+ }
+
+ @Deployment(testable = false)
+ public static WebArchive createDeployment() {
+ WebAppDescriptor descriptor = Descriptors.create(WebAppDescriptor.class)
+ .version("3.0")
+ .listener(RemoteServletContextListener.class)
+ .listener(RemoteServletSessionListener.class)
+ .servlet(ServletToCheckListener.class, "/" + TEST_NAME);
+
+ WebArchive archive = ShrinkWrap.create(WebArchive.class, TEST_NAME + ".war")
+ .addClass(RemoteServletContextListener.class)
+ .addClass(RemoteServletSessionListener.class)
+ .addClass(ServletToCheckListener.class)
+ .addClass(CompanyRemote.class)
+ .addClass(DefaultCompany.class)
+ .setWebXML(new StringAsset(descriptor.exportAsString()))
+ .addAsWebInfResource(EmptyAsset.INSTANCE, ArchivePaths.create("beans.xml"));
+
+ System.err.println(descriptor.exportAsString());
+
+ return archive;
+ }
+
+ public static enum ContextAttributeName {
+ KEY_Remote,;
+ }
+
+ public static class ServletToCheckListener extends HttpServlet {
+
+ @Override
+ public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+ final ServletContext ctxt = req.getServletContext();
+ for (ContextAttributeName s : ContextAttributeName.values()) {
+ resp.getOutputStream().println("Context: " + ctxt.getAttribute(s.name()));
+ }
+
+ final HttpSession session = req.getSession();
+ for (ContextAttributeName s : ContextAttributeName.values()) {
+ resp.getOutputStream().println("Session: " + session.getAttribute(s.name()));
+ }
+ }
+ }
+
+ public static class RemoteServletContextListener implements ServletContextListener {
+
+ @EJB
+ private CompanyRemote remoteCompany;
+
+ public void contextInitialized(ServletContextEvent event) {
+ final String name = "OpenEJB";
+ final ServletContext context = event.getServletContext();
+
+ if (remoteCompany != null) {
+ context.setAttribute(ContextAttributeName.KEY_Remote.name(), "Remote: " + remoteCompany.employ(name));
+ }
+ }
+
+ public void contextDestroyed(ServletContextEvent event) {
+ }
+
+ }
+
+ public static class RemoteServletSessionListener implements HttpSessionListener {
+
+ @EJB
+ private CompanyRemote remoteCompany;
+
+ public void sessionCreated(HttpSessionEvent event) {
+ final String name = "OpenEJB";
+ final HttpSession context = event.getSession();
+
+ if (remoteCompany != null) {
+ context.setAttribute(ContextAttributeName.KEY_Remote.name(), "Remote: " + remoteCompany.employ(name));
+ }
+ }
+
+ public void sessionDestroyed(HttpSessionEvent event) {
+ }
+
+ }
+
+ @Remote
+ public static interface CompanyRemote {
+ public String employ(String employeeName);
+ }
+
+ @Stateless
+ public static class DefaultCompany implements CompanyRemote {
+
+ private final String name = "TomEE Software Inc.";
+
+ public String employ(String employeeName) {
+ return employeeName + " is employed at " + name;
+ }
+
+ }
+
+ private void validateTest(String expectedOutput) throws IOException {
+ final InputStream is = new URL("http://localhost:9080/" + TEST_NAME + "/" + TEST_NAME).openStream();
+ final ByteArrayOutputStream os = new ByteArrayOutputStream();
+
+ int bytesRead = -1;
+ byte[] buffer = new byte[8192];
+ while ((bytesRead = is.read(buffer)) > -1) {
+ os.write(buffer, 0, bytesRead);
+ }
+
+ is.close();
+ os.close();
+
+ String output = new String(os.toByteArray(), "UTF-8");
+ assertNotNull("Response shouldn't be null", output);
+ assertTrue("Output should contain: " + expectedOutput, output.contains(expectedOutput));
+ }
+
+}
+
+
+
Added: openejb/trunk/arquillian-tomee/arquillian-tomee-tests/src/test/java/org/apache/openejb/arquillian/tests/ServletListenerEnvEntryInjectionTest.java
URL: http://svn.apache.org/viewvc/openejb/trunk/arquillian-tomee/arquillian-tomee-tests/src/test/java/org/apache/openejb/arquillian/tests/ServletListenerEnvEntryInjectionTest.java?rev=1178401&view=auto
==============================================================================
--- openejb/trunk/arquillian-tomee/arquillian-tomee-tests/src/test/java/org/apache/openejb/arquillian/tests/ServletListenerEnvEntryInjectionTest.java (added)
+++ openejb/trunk/arquillian-tomee/arquillian-tomee-tests/src/test/java/org/apache/openejb/arquillian/tests/ServletListenerEnvEntryInjectionTest.java Mon Oct 3 13:04:03 2011
@@ -0,0 +1,537 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.openejb.arquillian.tests;
+
+import org.apache.commons.lang.StringUtils;
+import org.jboss.arquillian.api.Deployment;
+import org.jboss.arquillian.junit.Arquillian;
+import org.jboss.shrinkwrap.api.ArchivePaths;
+import org.jboss.shrinkwrap.api.ShrinkWrap;
+import org.jboss.shrinkwrap.api.asset.EmptyAsset;
+import org.jboss.shrinkwrap.api.asset.StringAsset;
+import org.jboss.shrinkwrap.api.spec.WebArchive;
+import org.jboss.shrinkwrap.descriptor.api.Descriptors;
+import org.jboss.shrinkwrap.descriptor.api.Node;
+import org.jboss.shrinkwrap.descriptor.api.spec.servlet.web.WebAppDescriptor;
+import org.jboss.shrinkwrap.descriptor.spi.NodeProvider;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import javax.annotation.Resource;
+import javax.ejb.EJB;
+import javax.ejb.Local;
+import javax.ejb.LocalBean;
+import javax.ejb.Stateless;
+import javax.inject.Inject;
+import javax.servlet.ServletContext;
+import javax.servlet.ServletContextEvent;
+import javax.servlet.ServletContextListener;
+import javax.servlet.ServletException;
+import javax.servlet.http.*;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+
+import static junit.framework.Assert.assertNotNull;
+import static junit.framework.Assert.assertTrue;
+
+@RunWith(Arquillian.class)
+public class ServletListenerEnvEntryInjectionTest {
+
+ public static final String TEST_NAME = ServletListenerEnvEntryInjectionTest.class.getSimpleName();
+
+ @Test
+ public void localEjbInjectionShouldSucceedInCtxtListener() throws Exception {
+ final String expectedOutput = "Context: Local: OpenEJB is employed at TomEE Software Inc.";
+ validateTest(expectedOutput);
+ }
+
+ @Test
+ public void localBeanEjbInjectionShouldSucceedInCtxtListener() throws Exception {
+ final String expectedOutput = "Context: OpenEJB shops at Apache Marketplace";
+ validateTest(expectedOutput);
+ }
+
+// @Test
+ public void pojoInjectionShouldSucceedInCtxtListener() throws Exception {
+ final String expectedOutput = "Context: OpenEJB is on the wheel of a 2011 Lexus IS 350";
+ validateTest(expectedOutput);
+ }
+
+ @Test
+ public void stringEnvEntryInjectionShouldSucceedInCtxtListener() throws Exception {
+ final String expectedOutput = "Context: tomee@apache.org";
+ validateTest(expectedOutput);
+ }
+
+ @Test
+ public void integerTypeEnvEntryInjectionShouldSucceedInCtxtListener() throws Exception {
+ final String expectedOutput = "Context: Connection Pool: 20";
+ validateTest(expectedOutput);
+ }
+
+// @Test
+ public void longTypeEnvEntryInjectionShouldSucceedInCtxtListener() throws Exception {
+ final String expectedOutput = "Context: Start Count: 200000";
+ validateTest(expectedOutput);
+ }
+
+ @Test
+ public void shortTypeEnvEntryInjectionShouldSucceedInCtxtListener() throws Exception {
+ final String expectedOutput = "Context: Init Size: 5";
+ validateTest(expectedOutput);
+ }
+
+ @Test
+ public void byteTypeEnvEntryInjectionShouldSucceedInCtxtListener() throws Exception {
+ final String expectedOutput = "Context: Total Quantity: 5";
+ validateTest(expectedOutput);
+ }
+
+ @Test
+ public void booleanTypeEnvEntryInjectionShouldSucceedInCtxtListener() throws Exception {
+ final String expectedOutput = "Context: Enable Email: true";
+ validateTest(expectedOutput);
+ }
+
+ @Test
+ public void charTypeEnvEntryInjectionShouldSucceedInCtxtListener() throws Exception {
+ final String expectedOutput = "Context: Option Default: X";
+ validateTest(expectedOutput);
+ }
+
+// @Test
+ public void classEnvEntryInjectionShouldSucceedInCtxtListener() throws Exception {
+ final String expectedOutput = "Context: java.lang.String";
+ validateTest(expectedOutput);
+ }
+
+// @Test
+ public void enumEnvEntryInjectionShouldSucceedInCtxtListener() throws Exception {
+ final String expectedOutput = "Context: DefaultCode: OK";
+ validateTest(expectedOutput);
+ }
+
+ @Test
+ public void localEjbInjectionShouldSucceedInSessionListener() throws Exception {
+ final String expectedOutput = "Session: Local: OpenEJB is employed at TomEE Software Inc.";
+ validateTest(expectedOutput);
+ }
+
+ @Test
+ public void localBeanEjbInjectionShouldSucceedInSessionListener() throws Exception {
+ final String expectedOutput = "Session: OpenEJB shops at Apache Marketplace";
+ validateTest(expectedOutput);
+ }
+
+// @Test
+ public void pojoInjectionShouldSucceedInSessionListener() throws Exception {
+ final String expectedOutput = "Session: OpenEJB is on the wheel of a 2011 Lexus IS 350";
+ validateTest(expectedOutput);
+ }
+
+ @Test
+ public void stringEnvEntryInjectionShouldSucceedInSessionListener() throws Exception {
+ final String expectedOutput = "Session: tomee@apache.org";
+ validateTest(expectedOutput);
+ }
+
+ @Test
+ public void integerTypeEnvEntryInjectionShouldSucceedInSessionListener() throws Exception {
+ final String expectedOutput = "Session: Connection Pool: 20";
+ validateTest(expectedOutput);
+ }
+
+// @Test
+ public void longTypeEnvEntryInjectionShouldSucceedInSessionListener() throws Exception {
+ final String expectedOutput = "Session: Start Count: 200000";
+ validateTest(expectedOutput);
+ }
+
+ @Test
+ public void shortTypeEnvEntryInjectionShouldSucceedInSessionListener() throws Exception {
+ final String expectedOutput = "Session: Init Size: 5";
+ validateTest(expectedOutput);
+ }
+
+ @Test
+ public void byteTypeEnvEntryInjectionShouldSucceedInSessionListener() throws Exception {
+ final String expectedOutput = "Session: Total Quantity: 5";
+ validateTest(expectedOutput);
+ }
+
+ @Test
+ public void booleanTypeEnvEntryInjectionShouldSucceedInSessionListener() throws Exception {
+ final String expectedOutput = "Session: Enable Email: true";
+ validateTest(expectedOutput);
+ }
+
+ @Test
+ public void charTypeEnvEntryInjectionShouldSucceedInSessionListener() throws Exception {
+ final String expectedOutput = "Session: Option Default: X";
+ validateTest(expectedOutput);
+ }
+
+// @Test
+ public void classEnvEntryInjectionShouldSucceedInSessionListener() throws Exception {
+ final String expectedOutput = "Session: java.lang.String";
+ validateTest(expectedOutput);
+ }
+
+// @Test
+ public void enumEnvEntryInjectionShouldSucceedInSessionListener() throws Exception {
+ final String expectedOutput = "Session: DefaultCode: OK";
+ validateTest(expectedOutput);
+ }
+
+ @Deployment(testable = false)
+ public static WebArchive createDeployment() {
+ final WebAppDescriptor descriptor = Descriptors.create(WebAppDescriptor.class)
+ .version("3.0")
+ .listener(PojoServletContextListener.class)
+ .listener(PojoServletSessionListener.class)
+ .servlet(ServletToCheckListener.class, "/" + TEST_NAME);
+
+ addEnvEntry(descriptor, "returnEmail", "java.lang.String", "tomee@apache.org");
+ addEnvEntry(descriptor, "connectionPool", "java.lang.Integer", "20");
+ addEnvEntry(descriptor, "startCount", "java.lang.Long", "200000");
+ addEnvEntry(descriptor, "initSize", "java.lang.Short", "5");
+ addEnvEntry(descriptor, "enableEmail", "java.lang.Boolean", "true");
+ addEnvEntry(descriptor, "totalQuantity", "java.lang.Byte", "5");
+ addEnvEntry(descriptor, "optionDefault", "java.lang.Character", "X");
+ addEnvEntry(descriptor, "auditWriter", "java.lang.Class", "java.lang.String");
+ addEnvEntry(descriptor, "defaultCode", "java.lang.Enum", "org.apache.openejb.arquillian.ServletListenerPojoInjectionTest$Code.OK");
+
+ final WebArchive archive = ShrinkWrap.create(WebArchive.class, TEST_NAME + ".war")
+ .addClass(PojoServletContextListener.class)
+ .addClass(PojoServletSessionListener.class)
+ .addClass(ServletToCheckListener.class)
+ .addClass(Car.class)
+ .addClass(CompanyLocal.class)
+ .addClass(Company.class)
+ .addClass(DefaultCompany.class)
+ .addClass(SuperMarket.class)
+ .setWebXML(new StringAsset(descriptor.exportAsString()))
+ .addAsWebInfResource(EmptyAsset.INSTANCE, ArchivePaths.create("beans.xml"));
+
+ System.err.println(descriptor.exportAsString());
+
+ return archive;
+ }
+
+ public static enum Code {
+ OK,
+ ERROR;
+ }
+
+ public static enum ContextAttributeName {
+ KEY_Car,
+ KEY_LocalEjb,
+ KEY_Market,
+ KEY_ConnPool,
+ KEY_StartCount,
+ KEY_InitSize,
+ KEY_TotalQuantity,
+ KEY_EnableEmail,
+ KEY_DefaultOption,
+ KEY_ReturnEmail,
+ KEY_AuditWriter,
+ KEY_DefaultCode,;
+ }
+
+ public static class ServletToCheckListener extends HttpServlet {
+
+ @Override
+ public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+ final ServletContext ctxt = req.getServletContext();
+ for (ContextAttributeName s : ContextAttributeName.values()) {
+ resp.getOutputStream().println("Context: " + ctxt.getAttribute(s.name()));
+ }
+
+ final HttpSession session = req.getSession();
+ for (ContextAttributeName s : ContextAttributeName.values()) {
+ resp.getOutputStream().println("Session: " + session.getAttribute(s.name()));
+ }
+ }
+ }
+
+ public static class PojoServletContextListener implements ServletContextListener {
+
+ @Inject
+ private Car car;
+
+ @EJB
+ private CompanyLocal localCompany;
+
+ @EJB
+ private SuperMarket market;
+
+ @Resource(name = "returnEmail")
+ private String returnEmail;
+
+ @Resource(name = "connectionPool")
+ private Integer connectionPool;
+
+ @Resource(name = "startCount")
+ private Long startCount;
+
+ @Resource(name = "initSize")
+ private Short initSize;
+
+ @Resource(name = "totalQuantity")
+ private Byte totalQuantity;
+
+ @Resource(name = "enableEmail")
+ private Boolean enableEmail;
+
+ @Resource(name = "optionDefault")
+ private Character optionDefault;
+
+ /* TODO: Enable this resource after functionality is fixed
+ @Resource
+ */
+ private Code defaultCode;
+
+ /* TODO: Enable this resource after functionality is fixed
+ @Resource
+ @SuppressWarnings("unchecked")
+ */
+ private Class auditWriter;
+
+
+ public void contextInitialized(ServletContextEvent event) {
+ final String name = "OpenEJB";
+ final ServletContext context = event.getServletContext();
+
+ if (car != null) {
+ context.setAttribute(ContextAttributeName.KEY_Car.name(), car.drive(name));
+ }
+ if (localCompany != null) {
+ context.setAttribute(ContextAttributeName.KEY_LocalEjb.name(), "Local: " + localCompany.employ(name));
+ }
+ if (market != null) {
+ context.setAttribute(ContextAttributeName.KEY_Market.name(), market.shop(name));
+ }
+ if (connectionPool != null) {
+ context.setAttribute(ContextAttributeName.KEY_ConnPool.name(), "Connection Pool: " + connectionPool);
+ }
+ if (startCount != null) {
+ context.setAttribute(ContextAttributeName.KEY_StartCount.name(), "Start Expressions.Count: " + startCount);
+ }
+ if (initSize != null) {
+ context.setAttribute(ContextAttributeName.KEY_InitSize.name(), "Init Size: " + initSize);
+ }
+ if (totalQuantity != null) {
+ context.setAttribute(ContextAttributeName.KEY_TotalQuantity.name(), "Total Quantity: " + totalQuantity);
+ }
+ if (enableEmail != null) {
+ context.setAttribute(ContextAttributeName.KEY_EnableEmail.name(), "Enable Email: " + enableEmail);
+ }
+ if (optionDefault != null) {
+ context.setAttribute(ContextAttributeName.KEY_DefaultOption.name(), "Option Default: " + optionDefault);
+ }
+ if (StringUtils.isNotEmpty(returnEmail) && returnEmail.equals("tomee@apache.org")) {
+ context.setAttribute(ContextAttributeName.KEY_ReturnEmail.name(), returnEmail);
+ }
+ if (auditWriter != null) {
+ context.setAttribute(ContextAttributeName.KEY_AuditWriter.name(), auditWriter.getClass().getName());
+ }
+ if (defaultCode != null) {
+ context.setAttribute(ContextAttributeName.KEY_DefaultCode.name(), "DefaultCode: " + defaultCode);
+ }
+ }
+
+ public void contextDestroyed(ServletContextEvent event) {
+ }
+
+ }
+
+ public static class PojoServletSessionListener implements HttpSessionListener {
+
+ @Inject
+ private Car car;
+
+ @EJB
+ private CompanyLocal localCompany;
+
+ @EJB
+ private SuperMarket market;
+
+ @Resource(name = "returnEmail")
+ private String returnEmail;
+
+ @Resource(name = "connectionPool")
+ private Integer connectionPool;
+
+ @Resource(name = "startCount")
+ private Long startCount;
+
+ @Resource(name = "initSize")
+ private Short initSize;
+
+ @Resource(name = "totalQuantity")
+ private Byte totalQuantity;
+
+ @Resource(name = "enableEmail")
+ private Boolean enableEmail;
+
+ @Resource(name = "optionDefault")
+ private Character optionDefault;
+
+ /* TODO: Enable this resource after functionality is fixed
+ @Resource
+ */
+ private Code defaultCode;
+
+ /* TODO: Enable this resource after functionality is fixed
+ @Resource
+ @SuppressWarnings("unchecked")
+ */
+ private Class auditWriter;
+
+
+ public void sessionCreated(HttpSessionEvent event) {
+ final String name = "OpenEJB";
+ final HttpSession context = event.getSession();
+
+ if (car != null) {
+ context.setAttribute(ContextAttributeName.KEY_Car.name(), car.drive(name));
+ }
+ if (localCompany != null) {
+ context.setAttribute(ContextAttributeName.KEY_LocalEjb.name(), "Local: " + localCompany.employ(name));
+ }
+ if (market != null) {
+ context.setAttribute(ContextAttributeName.KEY_Market.name(), market.shop(name));
+ }
+ if (connectionPool != null) {
+ context.setAttribute(ContextAttributeName.KEY_ConnPool.name(), "Connection Pool: " + connectionPool);
+ }
+ if (startCount != null) {
+ context.setAttribute(ContextAttributeName.KEY_StartCount.name(), "Start Expressions.Count: " + startCount);
+ }
+ if (initSize != null) {
+ context.setAttribute(ContextAttributeName.KEY_InitSize.name(), "Init Size: " + initSize);
+ }
+ if (totalQuantity != null) {
+ context.setAttribute(ContextAttributeName.KEY_TotalQuantity.name(), "Total Quantity: " + totalQuantity);
+ }
+ if (enableEmail != null) {
+ context.setAttribute(ContextAttributeName.KEY_EnableEmail.name(), "Enable Email: " + enableEmail);
+ }
+ if (optionDefault != null) {
+ context.setAttribute(ContextAttributeName.KEY_DefaultOption.name(), "Option Default: " + optionDefault);
+ }
+ if (StringUtils.isNotEmpty(returnEmail) && returnEmail.equals("tomee@apache.org")) {
+ context.setAttribute(ContextAttributeName.KEY_ReturnEmail.name(), returnEmail);
+ }
+ if (auditWriter != null) {
+ context.setAttribute(ContextAttributeName.KEY_AuditWriter.name(), auditWriter.getClass().getName());
+ }
+ if (defaultCode != null) {
+ context.setAttribute(ContextAttributeName.KEY_DefaultCode.name(), "DefaultCode: " + defaultCode);
+ }
+ }
+
+ public void sessionDestroyed(HttpSessionEvent event) {
+ }
+
+ }
+
+ public static class Car {
+ private final String make = "Lexus", model = "IS 350";
+ private final int year = 2011;
+
+ public String drive(String name) {
+ return name + " is on the wheel of a " + year + " " + make + " " + model;
+ }
+ }
+
+
+ public static interface Company {
+ public String employ(String employeeName);
+ }
+
+ @Local
+ public static interface CompanyLocal extends Company {
+ }
+
+ @Stateless
+ public static class DefaultCompany implements CompanyLocal {
+
+ private final String name = "TomEE Software Inc.";
+
+ public String employ(String employeeName) {
+ return employeeName + " is employed at " + name;
+ }
+
+ }
+
+ @Stateless
+ @LocalBean
+ public static class SuperMarket {
+
+ private final String name = "Apache Marketplace";
+
+ public String shop(String employeeName) {
+ return employeeName + " shops at " + name;
+ }
+
+ }
+
+ private static void addEnvEntry(WebAppDescriptor descriptor, String name, String type, String value) {
+ Node rootNode = ((NodeProvider) descriptor).getRootNode();
+ Node appNode = rootNode.get("/web-app").iterator().next();
+ appNode.create("/env-entry")
+ .create("env-entry-name").text(name)
+ .parent()
+ .create("env-entry-type").text(type)
+ .parent()
+ .create("env-entry-value").text(value)
+/*
+ .parent()
+ .create("injection-target")
+ .create("injection-target-class").text("org.apache.openejb.arquillian.ServletPojoInjectionTest$PojoServletContextListener")
+ .parent()
+ .create("injection-target-name").text(name)
+*/
+ ;
+
+ }
+
+ private void validateTest(String expectedOutput) throws IOException {
+ final InputStream is = new URL("http://localhost:9080/" + TEST_NAME + "/" + TEST_NAME).openStream();
+ final ByteArrayOutputStream os = new ByteArrayOutputStream();
+
+ int bytesRead = -1;
+ byte[] buffer = new byte[8192];
+ while ((bytesRead = is.read(buffer)) > -1) {
+ os.write(buffer, 0, bytesRead);
+ }
+
+ is.close();
+ os.close();
+
+ String output = new String(os.toByteArray(), "UTF-8");
+ assertNotNull("Response shouldn't be null", output);
+ assertTrue("Output should contain: " + expectedOutput, output.contains(expectedOutput));
+ }
+
+}
+
+
+
Added: openejb/trunk/arquillian-tomee/arquillian-tomee-tests/src/test/java/org/apache/openejb/arquillian/tests/ServletListenerPersistenceInjectionTest.java
URL: http://svn.apache.org/viewvc/openejb/trunk/arquillian-tomee/arquillian-tomee-tests/src/test/java/org/apache/openejb/arquillian/tests/ServletListenerPersistenceInjectionTest.java?rev=1178401&view=auto
==============================================================================
--- openejb/trunk/arquillian-tomee/arquillian-tomee-tests/src/test/java/org/apache/openejb/arquillian/tests/ServletListenerPersistenceInjectionTest.java (added)
+++ openejb/trunk/arquillian-tomee/arquillian-tomee-tests/src/test/java/org/apache/openejb/arquillian/tests/ServletListenerPersistenceInjectionTest.java Mon Oct 3 13:04:03 2011
@@ -0,0 +1,296 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.openejb.arquillian.tests;
+
+import org.jboss.arquillian.api.Deployment;
+import org.jboss.arquillian.junit.Arquillian;
+import org.jboss.shrinkwrap.api.ArchivePaths;
+import org.jboss.shrinkwrap.api.ShrinkWrap;
+import org.jboss.shrinkwrap.api.asset.EmptyAsset;
+import org.jboss.shrinkwrap.api.asset.StringAsset;
+import org.jboss.shrinkwrap.api.spec.WebArchive;
+import org.jboss.shrinkwrap.descriptor.api.Descriptors;
+import org.jboss.shrinkwrap.descriptor.api.spec.servlet.web.WebAppDescriptor;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import javax.annotation.Resource;
+import javax.persistence.*;
+import javax.servlet.ServletContext;
+import javax.servlet.ServletContextEvent;
+import javax.servlet.ServletContextListener;
+import javax.servlet.ServletException;
+import javax.servlet.http.*;
+import javax.transaction.UserTransaction;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+
+import static junit.framework.Assert.assertNotNull;
+import static junit.framework.Assert.assertTrue;
+
+@RunWith(Arquillian.class)
+public class ServletListenerPersistenceInjectionTest {
+
+ public static final String TEST_NAME = ServletListenerPersistenceInjectionTest.class.getSimpleName();
+
+ @Test
+ public void transactionInjectionShouldSucceedInCtxtListener() throws Exception {
+ final String expectedOutput = "Context: Transaction injection successful";
+ validateTest(expectedOutput);
+ }
+
+ @Test
+ public void persistentContextInjectionShouldSucceedInCtxtListener() throws Exception {
+ final String expectedOutput = "Context: Transaction manager injection successful";
+ validateTest(expectedOutput);
+ }
+
+ @Test
+ public void persistenceUnitInjectionShouldSucceedInCtxtListener() throws Exception {
+ final String expectedOutput = "Context: Transaction manager factory injection successful";
+ validateTest(expectedOutput);
+ }
+
+ @Test
+ public void transactionInjectionShouldSucceedInSessionListener() throws Exception {
+ final String expectedOutput = "Session: Transaction injection successful";
+ validateTest(expectedOutput);
+ }
+
+ @Test
+ public void persistentContextInjectionShouldSucceedInSessionListener() throws Exception {
+ final String expectedOutput = "Session: Transaction manager injection successful";
+ validateTest(expectedOutput);
+ }
+
+ @Test
+ public void persistenceUnitInjectionShouldSucceedInSessionListener() throws Exception {
+ final String expectedOutput = "Session: Transaction manager factory injection successful";
+ validateTest(expectedOutput);
+ }
+
+ @Deployment(testable = false)
+ public static WebArchive createDeployment() {
+ WebAppDescriptor descriptor = Descriptors.create(WebAppDescriptor.class)
+ .version("3.0")
+ .listener(PersistenceServletContextListener.class)
+ .listener(PersistenceServletSessionListener.class)
+ .servlet(ServletToCheckListener.class, "/" + TEST_NAME);
+
+ WebArchive archive = ShrinkWrap.create(WebArchive.class, TEST_NAME + ".war")
+ .addClass(PersistenceServletContextListener.class)
+ .addClass(PersistenceServletSessionListener.class)
+ .addClass(ServletToCheckListener.class)
+ .addClass(Address.class)
+ .addAsManifestResource("persistence.xml", ArchivePaths.create("persistence.xml"))
+ .setWebXML(new StringAsset(descriptor.exportAsString()))
+ .addAsWebInfResource(EmptyAsset.INSTANCE, ArchivePaths.create("beans.xml"));
+
+ System.err.println(descriptor.exportAsString());
+
+ return archive;
+ }
+
+ public static enum ContextAttributeName {
+ KEY_EntityManagerFactory,
+ KEY_EntityManager,
+ KEY_Transaction,;
+ }
+
+ public static class ServletToCheckListener extends HttpServlet {
+
+ @Override
+ public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+ final ServletContext ctxt = req.getServletContext();
+ for (ContextAttributeName s : ContextAttributeName.values()) {
+ resp.getOutputStream().println("Context: " + ctxt.getAttribute(s.name()));
+ }
+
+ final HttpSession session = req.getSession();
+ for (ContextAttributeName s : ContextAttributeName.values()) {
+ resp.getOutputStream().println("Session: " + session.getAttribute(s.name()));
+ }
+ }
+ }
+
+ public static class PersistenceServletContextListener implements ServletContextListener {
+
+ @Resource
+ private UserTransaction transaction;
+
+ @PersistenceUnit
+ private EntityManagerFactory entityMgrFactory;
+
+ @PersistenceContext
+ private EntityManager entityManager;
+
+ public void contextInitialized(ServletContextEvent event) {
+ final ServletContext context = event.getServletContext();
+
+ if (transaction != null) {
+ try {
+ transaction.begin();
+ transaction.commit();
+ context.setAttribute(ContextAttributeName.KEY_Transaction.name(), "Transaction injection successful");
+ } catch (Exception ex) {
+ ex.printStackTrace();
+ }
+ }
+ if (entityManager != null) {
+ Address a = new Address();
+ try {
+ entityManager.contains(a);
+ context.setAttribute(ContextAttributeName.KEY_EntityManager.name(), "Transaction manager injection successful");
+ } catch (Exception ex) {
+ ex.printStackTrace();
+ }
+ }
+ if (entityMgrFactory != null) {
+ Address a = new Address();
+ try {
+ EntityManager em = entityMgrFactory.createEntityManager();
+ em.contains(a);
+ context.setAttribute(ContextAttributeName.KEY_EntityManagerFactory.name(), "Transaction manager factory injection successful");
+ } catch (Exception ex) {
+ ex.printStackTrace();
+ }
+ }
+
+
+ }
+
+ public void contextDestroyed(ServletContextEvent event) {
+ }
+
+ }
+
+ public static class PersistenceServletSessionListener implements HttpSessionListener {
+
+ @Resource
+ private UserTransaction transaction;
+
+ @PersistenceUnit
+ private EntityManagerFactory entityMgrFactory;
+
+ @PersistenceContext
+ private EntityManager entityManager;
+
+ public void sessionCreated(HttpSessionEvent event) {
+ final HttpSession context = event.getSession();
+
+ if (transaction != null) {
+ try {
+ transaction.begin();
+ transaction.commit();
+ context.setAttribute(ContextAttributeName.KEY_Transaction.name(), "Transaction injection successful");
+ } catch (Exception ex) {
+ ex.printStackTrace();
+ }
+ }
+ if (entityManager != null) {
+ Address a = new Address();
+ try {
+ entityManager.contains(a);
+ context.setAttribute(ContextAttributeName.KEY_EntityManager.name(), "Transaction manager injection successful");
+ } catch (Exception ex) {
+ ex.printStackTrace();
+ }
+ }
+ if (entityMgrFactory != null) {
+ Address a = new Address();
+ try {
+ EntityManager em = entityMgrFactory.createEntityManager();
+ em.contains(a);
+ context.setAttribute(ContextAttributeName.KEY_EntityManagerFactory.name(), "Transaction manager factory injection successful");
+ } catch (Exception ex) {
+ ex.printStackTrace();
+ }
+ }
+
+
+ }
+
+ public void sessionDestroyed(HttpSessionEvent event) {
+ }
+
+ }
+
+ @Entity
+ public static class Address {
+ public String getStreet() {
+ return street;
+ }
+
+ public void setStreet(String street) {
+ this.street = street;
+ }
+
+ public String getCity() {
+ return city;
+ }
+
+ public void setCity(String city) {
+ this.city = city;
+ }
+
+ public String getState() {
+ return state;
+ }
+
+ public void setState(String state) {
+ this.state = state;
+ }
+
+ public String getZip() {
+ return zip;
+ }
+
+ public void setZip(String zip) {
+ this.zip = zip;
+ }
+
+ private String street = "123 Lakeview St.", city = "Paradise", state = "ZZ", zip = "00000";
+
+ public String toString() {
+ return "Street: " + street + ", City: " + city + ", State: " + state + ", Zip: " + zip;
+ }
+ }
+
+ private void validateTest(String expectedOutput) throws IOException {
+ final InputStream is = new URL("http://localhost:9080/" + TEST_NAME + "/" + TEST_NAME).openStream();
+ final ByteArrayOutputStream os = new ByteArrayOutputStream();
+
+ int bytesRead = -1;
+ byte[] buffer = new byte[8192];
+ while ((bytesRead = is.read(buffer)) > -1) {
+ os.write(buffer, 0, bytesRead);
+ }
+
+ is.close();
+ os.close();
+
+ String output = new String(os.toByteArray(), "UTF-8");
+ assertNotNull("Response shouldn't be null", output);
+ assertTrue("Output should contain: " + expectedOutput, output.contains(expectedOutput));
+ }
+
+}
+
+
+
Added: openejb/trunk/arquillian-tomee/arquillian-tomee-tests/src/test/java/org/apache/openejb/arquillian/tests/ServletPersistenceInjectionTest.java
URL: http://svn.apache.org/viewvc/openejb/trunk/arquillian-tomee/arquillian-tomee-tests/src/test/java/org/apache/openejb/arquillian/tests/ServletPersistenceInjectionTest.java?rev=1178401&view=auto
==============================================================================
--- openejb/trunk/arquillian-tomee/arquillian-tomee-tests/src/test/java/org/apache/openejb/arquillian/tests/ServletPersistenceInjectionTest.java (added)
+++ openejb/trunk/arquillian-tomee/arquillian-tomee-tests/src/test/java/org/apache/openejb/arquillian/tests/ServletPersistenceInjectionTest.java Mon Oct 3 13:04:03 2011
@@ -0,0 +1,192 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.openejb.arquillian.tests;
+
+import org.jboss.arquillian.api.Deployment;
+import org.jboss.arquillian.junit.Arquillian;
+import org.jboss.shrinkwrap.api.ArchivePaths;
+import org.jboss.shrinkwrap.api.ShrinkWrap;
+import org.jboss.shrinkwrap.api.asset.StringAsset;
+import org.jboss.shrinkwrap.api.spec.WebArchive;
+import org.jboss.shrinkwrap.descriptor.api.Descriptors;
+import org.jboss.shrinkwrap.descriptor.api.spec.servlet.web.WebAppDescriptor;
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import javax.annotation.Resource;
+import javax.persistence.Entity;
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.PersistenceContext;
+import javax.persistence.PersistenceUnit;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.transaction.UserTransaction;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+
+import static junit.framework.Assert.assertNotNull;
+import static junit.framework.Assert.assertTrue;
+
+@RunWith(Arquillian.class)
+public class ServletPersistenceInjectionTest {
+
+ public static final String TEST_NAME = ServletPersistenceInjectionTest.class.getSimpleName();
+
+ @Test
+ public void transactionInjectionShouldSucceed() throws Exception {
+ final String expectedOutput = "testUserTransaction=true";
+ validateTest(expectedOutput);
+ }
+
+ @Test
+ public void persistentContextInjectionShouldSucceed() throws Exception {
+ final String expectedOutput = "testEntityManager=true";
+ validateTest(expectedOutput);
+ }
+
+ @Test
+ public void persistenceUnitInjectionShouldSucceed() throws Exception {
+ final String expectedOutput = "testEntityManagerFactory=true";
+ validateTest(expectedOutput);
+ }
+
+ @Deployment(testable = false)
+ public static WebArchive createDeployment() {
+ WebAppDescriptor descriptor = Descriptors.create(WebAppDescriptor.class)
+ .version("3.0")
+ .servlet(PersistenceServlet.class, "/" + TEST_NAME);
+
+ WebArchive archive = ShrinkWrap.create(WebArchive.class, TEST_NAME + ".war")
+ .addClass(PersistenceServlet.class)
+ .addClass(Address.class)
+ .addAsManifestResource("persistence.xml", ArchivePaths.create("persistence.xml"))
+ .setWebXML(new StringAsset(descriptor.exportAsString()));
+
+ System.err.println(descriptor.exportAsString());
+
+ return archive;
+ }
+
+
+ public static class PersistenceServlet extends HttpServlet {
+
+ @Resource
+ private UserTransaction transaction;
+
+ @PersistenceUnit
+ private EntityManagerFactory entityMgrFactory;
+
+ @PersistenceContext
+ private EntityManager entityManager;
+
+ @Override
+ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+ Runner.run(req, resp, this);
+ }
+
+ public void testEntityManagerFactory() {
+ Assert.assertNotNull(entityMgrFactory);
+
+ Address a = new Address();
+ EntityManager em = entityMgrFactory.createEntityManager();
+ em.contains(a);
+ }
+
+ public void testEntityManager() {
+ Assert.assertNotNull(entityManager);
+ Address a = new Address();
+ entityManager.contains(a);
+ }
+
+ public void testUserTransaction() throws Exception{
+ Assert.assertNotNull(transaction);
+ transaction.begin();
+ transaction.commit();
+ }
+
+ }
+
+
+ @Entity
+ public static class Address {
+ public String getStreet() {
+ return street;
+ }
+
+ public void setStreet(String street) {
+ this.street = street;
+ }
+
+ public String getCity() {
+ return city;
+ }
+
+ public void setCity(String city) {
+ this.city = city;
+ }
+
+ public String getState() {
+ return state;
+ }
+
+ public void setState(String state) {
+ this.state = state;
+ }
+
+ public String getZip() {
+ return zip;
+ }
+
+ public void setZip(String zip) {
+ this.zip = zip;
+ }
+
+ private String street = "123 Lakeview St.", city = "Paradise", state = "ZZ", zip = "00000";
+
+ public String toString() {
+ return "Street: " + street + ", City: " + city + ", State: " + state + ", Zip: " + zip;
+ }
+ }
+
+ private void validateTest(String expectedOutput) throws IOException {
+ final InputStream is = new URL("http://localhost:9080/" + TEST_NAME + "/" + TEST_NAME).openStream();
+ final ByteArrayOutputStream os = new ByteArrayOutputStream();
+
+ int bytesRead = -1;
+ byte[] buffer = new byte[8192];
+ while ((bytesRead = is.read(buffer)) > -1) {
+ os.write(buffer, 0, bytesRead);
+ }
+
+ is.close();
+ os.close();
+
+ String output = new String(os.toByteArray(), "UTF-8");
+ assertNotNull("Response shouldn't be null", output);
+ assertTrue("Output should contain: " + expectedOutput, output.contains(expectedOutput));
+ }
+
+}
+
+
+
Added: openejb/trunk/arquillian-tomee/arquillian-tomee-tests/src/test/java/org/apache/openejb/arquillian/tests/ServletResourceEnvEntryInjectionTest.java
URL: http://svn.apache.org/viewvc/openejb/trunk/arquillian-tomee/arquillian-tomee-tests/src/test/java/org/apache/openejb/arquillian/tests/ServletResourceEnvEntryInjectionTest.java?rev=1178401&view=auto
==============================================================================
--- openejb/trunk/arquillian-tomee/arquillian-tomee-tests/src/test/java/org/apache/openejb/arquillian/tests/ServletResourceEnvEntryInjectionTest.java (added)
+++ openejb/trunk/arquillian-tomee/arquillian-tomee-tests/src/test/java/org/apache/openejb/arquillian/tests/ServletResourceEnvEntryInjectionTest.java Mon Oct 3 13:04:03 2011
@@ -0,0 +1,226 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.openejb.arquillian.tests;
+
+import org.jboss.arquillian.api.Deployment;
+import org.jboss.arquillian.junit.Arquillian;
+import org.jboss.shrinkwrap.api.spec.WebArchive;
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import javax.annotation.ManagedBean;
+import javax.annotation.Resource;
+import javax.ejb.Singleton;
+import javax.enterprise.inject.spi.BeanManager;
+import javax.servlet.ServletException;
+import javax.servlet.annotation.WebServlet;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.transaction.TransactionManager;
+import javax.transaction.TransactionSynchronizationRegistry;
+import javax.transaction.UserTransaction;
+import javax.validation.Validator;
+import javax.validation.ValidatorFactory;
+import java.io.IOException;
+import java.lang.reflect.Field;
+
+import static junit.framework.Assert.assertNotNull;
+import static junit.framework.Assert.assertTrue;
+
+@RunWith(Arquillian.class)
+public class ServletResourceEnvEntryInjectionTest extends TestSetup {
+
+ public static final String TEST_NAME = ServletResourceEnvEntryInjectionTest.class.getSimpleName();
+
+ @Test
+ public void testRed() throws Exception {
+ validateTest("red", "true");
+ }
+
+ @Test
+ public void testBlue() throws Exception {
+ validateTest("blue", "true");
+ }
+
+ @Deployment(testable = false)
+ public static WebArchive getArchive() {
+ return new ServletResourceEnvEntryInjectionTest().createDeployment(ServletResourceEnvEntryInjectionTest.class, Blue.class, Red.class, Green.class, Orange.class, Purple.class);
+ }
+
+ @WebServlet("/blue")
+ public static class Blue extends HttpServlet {
+
+ @Resource(name = "java:comp/Validator")
+ private Validator validator;
+
+ @Resource(name = "java:comp/ValidatorFactory")
+ private ValidatorFactory validatorFactory;
+
+ @Resource(name = "java:comp/TransactionManager")
+ private TransactionManager transactionManager;
+
+ @Resource(name = "java:comp/TransactionSynchronizationRegistry")
+ private TransactionSynchronizationRegistry transactionSynchronizationRegistry;
+
+ @Resource(name = "java:comp/UserTransaction")
+ private UserTransaction userTransaction;
+
+ @Resource(name = "java:comp/BeanManager")
+ private BeanManager beanManager;
+
+ @Resource(name = "java:app/AppName")
+ private String app;
+
+ @Resource(name = "java:module/ModuleName")
+ private String module;
+
+ @Override
+ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+ run(req, resp, this);
+ }
+
+ public void test() throws Exception {
+
+ final Field[] fields = this.getClass().getDeclaredFields();
+
+ for (Field field : fields) {
+ field.setAccessible(true);
+ Assert.assertNotNull(field.getName(), field.get(this));
+ }
+
+ final String name = ServletResourceEnvEntryInjectionTest.class.getSimpleName();
+ Assert.assertEquals("app", name, app);
+ Assert.assertEquals("module", name, module);
+ }
+
+ }
+
+
+ @WebServlet("/red")
+ public static class Red extends HttpServlet {
+
+ @Resource
+ private Validator validator;
+
+ @Resource
+ private ValidatorFactory validatorFactory;
+
+ @Resource
+ private TransactionManager transactionManager;
+
+ @Resource
+ private TransactionSynchronizationRegistry transactionSynchronizationRegistry;
+
+ @Resource
+ private UserTransaction userTransaction;
+
+ @Resource
+ private BeanManager beanManager;
+
+ @Resource
+ private Purple purple;
+
+ @Override
+ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+ run(req, resp, this);
+ }
+
+ public void test() throws Exception {
+
+ final Field[] fields = this.getClass().getDeclaredFields();
+
+ for (Field field : fields) {
+ field.setAccessible(true);
+ Assert.assertNotNull(field.getName(), field.get(this));
+ }
+
+ purple.test();
+ }
+ }
+
+ @Singleton
+ public static class Green {
+
+ public void foo() {}
+ }
+
+ @WebServlet("/orange")
+ public static class Orange extends HttpServlet {
+
+ @Resource(name = "java:app/some/longer/path/MyValidatorFactory")
+ private ValidatorFactory validatorFactory;
+
+ @Resource(name = "java:app/some/longer/path/MyTransactionManager")
+ private TransactionManager transactionManager;
+
+ @Resource(name = "java:app/some/longer/path/MyTransactionSynchronizationRegistry")
+ private TransactionSynchronizationRegistry transactionSynchronizationRegistry;
+
+ @Resource(name = "java:app/some/longer/path/MyUserTransaction")
+ private UserTransaction userTransaction;
+
+ @Resource(name = "java:app/some/longer/path/MyBeanManager")
+ private BeanManager beanManager;
+
+ @Resource(name = "java:app/AppName")
+ private String app;
+
+ @Override
+ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+ run(req, resp, this);
+ }
+
+ public void test() throws Exception {
+
+ assertFields(this);
+
+ Assert.assertEquals("app", "BuiltInEnvironmentEntriesTest", app);
+ }
+
+ }
+
+ @ManagedBean
+ public static class Purple {
+
+ @Resource
+ private Validator validator;
+
+ @Resource
+ private ValidatorFactory validatorFactory;
+
+ @Resource
+ private TransactionManager transactionManager;
+
+ @Resource
+ private TransactionSynchronizationRegistry transactionSynchronizationRegistry;
+
+ @Resource
+ private UserTransaction userTransaction;
+
+ @Resource
+ private BeanManager beanManager;
+
+ public void test() throws IllegalAccessException {
+ assertFields(this);
+ }
+ }
+}
+
+
+
Added: openejb/trunk/arquillian-tomee/arquillian-tomee-tests/src/test/java/org/apache/openejb/arquillian/tests/SharedEnvironmentTest.java
URL: http://svn.apache.org/viewvc/openejb/trunk/arquillian-tomee/arquillian-tomee-tests/src/test/java/org/apache/openejb/arquillian/tests/SharedEnvironmentTest.java?rev=1178401&view=auto
==============================================================================
--- openejb/trunk/arquillian-tomee/arquillian-tomee-tests/src/test/java/org/apache/openejb/arquillian/tests/SharedEnvironmentTest.java (added)
+++ openejb/trunk/arquillian-tomee/arquillian-tomee-tests/src/test/java/org/apache/openejb/arquillian/tests/SharedEnvironmentTest.java Mon Oct 3 13:04:03 2011
@@ -0,0 +1,351 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.openejb.arquillian.tests;
+
+import org.jboss.arquillian.api.Deployment;
+import org.jboss.arquillian.junit.Arquillian;
+import org.jboss.shrinkwrap.api.spec.WebArchive;
+import org.jboss.shrinkwrap.descriptor.api.Node;
+import org.jboss.shrinkwrap.descriptor.api.spec.servlet.web.WebAppDescriptor;
+import org.jboss.shrinkwrap.descriptor.spi.NodeProvider;
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import javax.annotation.Resource;
+import javax.ejb.EJB;
+import javax.ejb.LocalBean;
+import javax.ejb.Singleton;
+import javax.inject.Inject;
+import javax.servlet.Filter;
+import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import java.io.IOException;
+
+@RunWith(Arquillian.class)
+public class SharedEnvironmentTest extends TestSetup {
+
+ public static final String TEST_NAME = SharedEnvironmentTest.class.getSimpleName();
+
+ @Test
+ public void testCdi() throws Exception {
+ validateTest("testCdi=true");
+ }
+
+ @Test
+ public void testEjb() throws Exception {
+ validateTest("testEjb=true");
+ }
+
+ @Test
+ public void testFilter() throws Exception {
+ validateTest("testFilter=true");
+ }
+
+ @Deployment(testable = false)
+ public static WebArchive getArchive() {
+ return new SharedEnvironmentTest().createDeployment(PojoServletFilter.class, Orange.class, Green.class);
+ }
+
+ protected void decorateDescriptor(WebAppDescriptor descriptor) {
+ descriptor.filter(PojoServletFilter.class, "/" + getTestContextName());
+ addEnvEntry(descriptor, "returnEmail", "java.lang.String", "tomee@apache.org");
+ addEnvEntry(descriptor, "connectionPool", "java.lang.Integer", "20");
+ addEnvEntry(descriptor, "startCount", "java.lang.Long", "200000");
+ addEnvEntry(descriptor, "initSize", "java.lang.Short", "6");
+ addEnvEntry(descriptor, "enableEmail", "java.lang.Boolean", "true");
+ addEnvEntry(descriptor, "totalQuantity", "java.lang.Byte", "5");
+ addEnvEntry(descriptor, "optionDefault", "java.lang.Character", "X");
+ }
+
+
+ private static void addEnvEntry(WebAppDescriptor descriptor, String name, String type, String value) {
+ Node rootNode = ((NodeProvider) descriptor).getRootNode();
+ Node appNode = rootNode.get("/web-app").iterator().next();
+ appNode.create("/env-entry")
+ .create("env-entry-name").text(name)
+ .parent()
+ .create("env-entry-type").text(type)
+ .parent()
+ .create("env-entry-value").text(value)
+ ;
+
+ }
+
+ public static class PojoServletFilter implements Filter, Environment {
+
+ @Resource(name = "returnEmail")
+ private String returnEmail;
+
+ @Resource(name = "connectionPool")
+ private Integer connectionPool;
+
+ @Resource(name = "startCount")
+ private Long startCount;
+
+ @Resource(name = "initSize")
+ private Short initSize;
+
+ @Resource(name = "totalQuantity")
+ private Byte totalQuantity;
+
+ @Resource(name = "enableEmail")
+ private Boolean enableEmail;
+
+ @Resource(name = "optionDefault")
+ private Character optionDefault;
+
+ @Override
+ public String getReturnEmail() {
+ return returnEmail;
+ }
+
+ @Override
+ public Integer getConnectionPool() {
+ return connectionPool;
+ }
+
+ @Override
+ public Long getStartCount() {
+ return startCount;
+ }
+
+ @Override
+ public Short getInitSize() {
+ return initSize;
+ }
+
+ @Override
+ public Byte getTotalQuantity() {
+ return totalQuantity;
+ }
+
+ @Override
+ public Boolean getEnableEmail() {
+ return enableEmail;
+ }
+
+ @Override
+ public Character getOptionDefault() {
+ return optionDefault;
+ }
+
+ @Inject
+ private Green green;
+
+ @EJB
+ private Orange orange;
+
+ public void init(FilterConfig config) {
+ }
+
+ public void destroy() {
+ }
+
+ @Override
+ public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
+ run(req, resp, this);
+ }
+
+
+ public void testCdi() {
+ assertEnvironment(green);
+ }
+
+ public void testEjb() {
+ assertEnvironment(orange);
+ }
+
+ public void testFilter() {
+ assertEnvironment(this);
+ }
+
+ public void assertEnvironment(Environment actual) {
+ Assert.assertNotNull("component", actual);
+
+ Environment expected = new Green("tomee@apache.org", 20, 200000l, (short) 6, (byte) 5, true, 'X');
+
+ Assert.assertEquals("ReturnEmail", expected.getReturnEmail(), actual.getReturnEmail());
+ Assert.assertEquals("ConnectionPool", expected.getConnectionPool(), actual.getConnectionPool());
+ Assert.assertEquals("StartCount", expected.getStartCount(), actual.getStartCount());
+ Assert.assertEquals("InitSize", expected.getInitSize(), actual.getInitSize());
+ Assert.assertEquals("TotalQuantity", expected.getTotalQuantity(), actual.getTotalQuantity());
+ Assert.assertEquals("EnableEmail", expected.getEnableEmail(), actual.getEnableEmail());
+ Assert.assertEquals("OptionDefault", expected.getOptionDefault(), actual.getOptionDefault());
+ }
+ }
+
+ public static class Green implements Environment {
+
+ @Resource(name = "returnEmail")
+ private String returnEmail;
+
+ @Resource(name = "connectionPool")
+ private Integer connectionPool;
+
+ @Resource(name = "startCount")
+ private Long startCount;
+
+ @Resource(name = "initSize")
+ private Short initSize;
+
+ @Resource(name = "totalQuantity")
+ private Byte totalQuantity;
+
+ @Resource(name = "enableEmail")
+ private Boolean enableEmail;
+
+ @Resource(name = "optionDefault")
+ private Character optionDefault;
+
+ @Inject
+ public Green() {
+ }
+
+ public Green(String returnEmail, Integer connectionPool, Long startCount, Short initSize, Byte totalQuantity, Boolean enableEmail, Character optionDefault) {
+ this.returnEmail = returnEmail;
+ this.connectionPool = connectionPool;
+ this.startCount = startCount;
+ this.initSize = initSize;
+ this.totalQuantity = totalQuantity;
+ this.enableEmail = enableEmail;
+ this.optionDefault = optionDefault;
+ }
+
+ @Override
+ public String getReturnEmail() {
+ return returnEmail;
+ }
+
+ @Override
+ public Integer getConnectionPool() {
+ return connectionPool;
+ }
+
+ @Override
+ public Long getStartCount() {
+ return startCount;
+ }
+
+ @Override
+ public Short getInitSize() {
+ return initSize;
+ }
+
+ @Override
+ public Byte getTotalQuantity() {
+ return totalQuantity;
+ }
+
+ @Override
+ public Boolean getEnableEmail() {
+ return enableEmail;
+ }
+
+ @Override
+ public Character getOptionDefault() {
+ return optionDefault;
+ }
+ }
+
+ @Singleton
+ @LocalBean
+ public static class Orange implements Environment {
+
+ @Resource(name = "returnEmail")
+ private String returnEmail;
+
+ @Resource(name = "connectionPool")
+ private Integer connectionPool;
+
+ @Resource(name = "startCount")
+ private Long startCount;
+
+ @Resource(name = "initSize")
+ private Short initSize;
+
+ @Resource(name = "totalQuantity")
+ private Byte totalQuantity;
+
+ @Resource(name = "enableEmail")
+ private Boolean enableEmail;
+
+ @Resource(name = "optionDefault")
+ private Character optionDefault;
+
+ @Override
+ public String getReturnEmail() {
+ return returnEmail;
+ }
+
+ @Override
+ public Integer getConnectionPool() {
+ return connectionPool;
+ }
+
+ @Override
+ public Long getStartCount() {
+ return startCount;
+ }
+
+ @Override
+ public Short getInitSize() {
+ return initSize;
+ }
+
+ @Override
+ public Byte getTotalQuantity() {
+ return totalQuantity;
+ }
+
+ @Override
+ public Boolean getEnableEmail() {
+ return enableEmail;
+ }
+
+ @Override
+ public Character getOptionDefault() {
+ return optionDefault;
+ }
+ }
+
+
+ /**
+ * @version $Rev$ $Date$
+ */
+ public static interface Environment {
+ String getReturnEmail();
+
+ Integer getConnectionPool();
+
+ Long getStartCount();
+
+ Short getInitSize();
+
+ Byte getTotalQuantity();
+
+ Boolean getEnableEmail();
+
+ Character getOptionDefault();
+ }
+}
+
+
+