You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwebbeans.apache.org by rm...@apache.org on 2016/06/30 14:01:09 UTC

svn commit: r1750799 - in /openwebbeans/trunk/webbeans-tomcat7: ./ src/test/java/org/apache/webbeans/web/tomcat7/test/

Author: rmannibucau
Date: Thu Jun 30 14:01:09 2016
New Revision: 1750799

URL: http://svn.apache.org/viewvc?rev=1750799&view=rev
Log:
OWB-1123 adding a test for unload/load of tomcat and our session handling

Added:
    openwebbeans/trunk/webbeans-tomcat7/src/test/java/org/apache/webbeans/web/tomcat7/test/MySessionScoped.java
    openwebbeans/trunk/webbeans-tomcat7/src/test/java/org/apache/webbeans/web/tomcat7/test/MyWrapper.java
    openwebbeans/trunk/webbeans-tomcat7/src/test/java/org/apache/webbeans/web/tomcat7/test/TomcatNormalScopeProxyFactoryTest.java
Modified:
    openwebbeans/trunk/webbeans-tomcat7/pom.xml

Modified: openwebbeans/trunk/webbeans-tomcat7/pom.xml
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-tomcat7/pom.xml?rev=1750799&r1=1750798&r2=1750799&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-tomcat7/pom.xml (original)
+++ openwebbeans/trunk/webbeans-tomcat7/pom.xml Thu Jun 30 14:01:09 2016
@@ -62,6 +62,47 @@
             <artifactId>openwebbeans-web</artifactId>
             <version>${project.version}</version>
         </dependency>
+
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.tomcat.embed</groupId>
+            <artifactId>tomcat-embed-core</artifactId>
+            <version>${tomcat7.version}</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.geronimo.specs</groupId>
+            <artifactId>geronimo-interceptor_1.2_spec</artifactId>
+            <version>1.0</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.tomcat</groupId>
+            <artifactId>tomcat-jasper-el</artifactId>
+            <version>${tomcat7.version}</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.tomcat</groupId>
+            <artifactId>tomcat-el-api</artifactId>
+            <version>${tomcat7.version}</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.tomcat</groupId>
+            <artifactId>tomcat-jsp-api</artifactId>
+            <version>${tomcat7.version}</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.tomcat</groupId>
+            <artifactId>tomcat-jasper</artifactId>
+            <version>${tomcat7.version}</version>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 
 

Added: openwebbeans/trunk/webbeans-tomcat7/src/test/java/org/apache/webbeans/web/tomcat7/test/MySessionScoped.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-tomcat7/src/test/java/org/apache/webbeans/web/tomcat7/test/MySessionScoped.java?rev=1750799&view=auto
==============================================================================
--- openwebbeans/trunk/webbeans-tomcat7/src/test/java/org/apache/webbeans/web/tomcat7/test/MySessionScoped.java (added)
+++ openwebbeans/trunk/webbeans-tomcat7/src/test/java/org/apache/webbeans/web/tomcat7/test/MySessionScoped.java Thu Jun 30 14:01:09 2016
@@ -0,0 +1,38 @@
+/*
+ * 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.webbeans.web.tomcat7.test;
+
+import javax.enterprise.context.SessionScoped;
+import java.io.Serializable;
+
+@SessionScoped
+public class MySessionScoped implements Serializable
+{
+    private String value = "init";
+
+    public String getValue()
+    {
+        return value;
+    }
+
+    public void setValue(final String value)
+    {
+        this.value = value;
+    }
+}

Added: openwebbeans/trunk/webbeans-tomcat7/src/test/java/org/apache/webbeans/web/tomcat7/test/MyWrapper.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-tomcat7/src/test/java/org/apache/webbeans/web/tomcat7/test/MyWrapper.java?rev=1750799&view=auto
==============================================================================
--- openwebbeans/trunk/webbeans-tomcat7/src/test/java/org/apache/webbeans/web/tomcat7/test/MyWrapper.java (added)
+++ openwebbeans/trunk/webbeans-tomcat7/src/test/java/org/apache/webbeans/web/tomcat7/test/MyWrapper.java Thu Jun 30 14:01:09 2016
@@ -0,0 +1,34 @@
+/*
+ * 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.webbeans.web.tomcat7.test;
+
+import javax.enterprise.context.SessionScoped;
+import javax.inject.Inject;
+import java.io.Serializable;
+
+@SessionScoped
+public class MyWrapper implements Serializable {
+    @Inject
+    private MySessionScoped proxy;
+
+    public MySessionScoped getProxy()
+    {
+        return proxy;
+    }
+}

Added: openwebbeans/trunk/webbeans-tomcat7/src/test/java/org/apache/webbeans/web/tomcat7/test/TomcatNormalScopeProxyFactoryTest.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-tomcat7/src/test/java/org/apache/webbeans/web/tomcat7/test/TomcatNormalScopeProxyFactoryTest.java?rev=1750799&view=auto
==============================================================================
--- openwebbeans/trunk/webbeans-tomcat7/src/test/java/org/apache/webbeans/web/tomcat7/test/TomcatNormalScopeProxyFactoryTest.java (added)
+++ openwebbeans/trunk/webbeans-tomcat7/src/test/java/org/apache/webbeans/web/tomcat7/test/TomcatNormalScopeProxyFactoryTest.java Thu Jun 30 14:01:09 2016
@@ -0,0 +1,157 @@
+/*
+ * 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.webbeans.web.tomcat7.test;
+
+import org.apache.catalina.Context;
+import org.apache.catalina.connector.Request;
+import org.apache.catalina.connector.Response;
+import org.apache.catalina.startup.Tomcat;
+import org.apache.tomcat.util.http.fileupload.IOUtils;
+import org.apache.webbeans.config.WebBeansContext;
+import org.apache.webbeans.spi.ContextsService;
+import org.apache.webbeans.web.tomcat7.ContextLifecycleListener;
+import org.junit.Test;
+
+import javax.enterprise.context.RequestScoped;
+import javax.enterprise.context.SessionScoped;
+import javax.enterprise.inject.spi.BeanManager;
+import javax.enterprise.inject.spi.CDI;
+import javax.servlet.ServletRequestEvent;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.Writer;
+import java.lang.reflect.Method;
+
+import static java.util.Arrays.asList;
+import static org.junit.Assert.assertEquals;
+
+public class TomcatNormalScopeProxyFactoryTest
+{
+    @Test
+    public void checkDeserialisation() throws Exception
+    {
+        final File base = dir(new File("target/TomcatNormalScopeProxyFactoryTest-" + System.nanoTime()));
+        final File war = createWar(dir(new File(base, "test")), MyWrapper.class, MySessionScoped.class);
+
+        String sessionId = null;
+        for (final String expected : asList("init", "new"))
+        {
+            final Tomcat tomcat = new Tomcat();
+            tomcat.setPort(0);
+            tomcat.setBaseDir(base.getAbsolutePath());
+
+            final Context ctx = tomcat.addContext("/test", war.getAbsolutePath());
+            ctx.addLifecycleListener(new ContextLifecycleListener());
+
+            tomcat.start();
+
+            try
+            {
+                Thread thread = Thread.currentThread();
+                ClassLoader old = thread.getContextClassLoader();
+                final ClassLoader webappLoader = ctx.getLoader().getClassLoader();
+                thread.setContextClassLoader(webappLoader);
+                try
+                {
+                    // we don't want test type but webapp one...even if named the same
+                    final Class<?> webapptype = webappLoader.loadClass(MySessionScoped.class.getName());
+                    final Method setValue = webapptype.getMethod("setValue", String.class);
+                    final Method getValue = webapptype.getMethod("getValue");
+
+                    final Class<?> wrapperType = webappLoader.loadClass(MyWrapper.class.getName());
+                    final Method m = wrapperType.getMethod("getProxy");
+
+                    final BeanManager bm = CDI.current().getBeanManager();
+
+                    final Response response = new Response();
+                    response.setConnector(tomcat.getConnector());
+                    response.setCoyoteResponse(new org.apache.coyote.Response());
+
+                    final Request request = new Request();
+                    request.setContext(ctx);
+                    request.setResponse(response);
+                    request.setRequestedSessionId(sessionId);
+
+                    final ContextsService contextsService = WebBeansContext.currentInstance().getContextsService();
+                    final ServletRequestEvent startParameter = new ServletRequestEvent(ctx.getServletContext(), request);
+                    contextsService.startContext(RequestScoped.class, startParameter);
+                    if (request.getSession() != null)
+                    {
+                        contextsService.startContext(SessionScoped.class, request.getSession());
+                    }
+
+                    {
+                        //final Object bean = bm.getReference(bm.resolve(bm.getBeans(webapptype)), webapptype, null);
+                        final Object bean = m.invoke(bm.getReference(bm.resolve(bm.getBeans(wrapperType)), wrapperType, null));
+                        assertEquals(expected, getValue.invoke(bean));
+                        setValue.invoke(bean, "new");
+                        assertEquals("new", getValue.invoke(bean));
+                    }
+
+                    sessionId = request.getSession().getId();
+                    contextsService.endContext(RequestScoped.class, startParameter);
+
+                    // don't do to not destroy the instance
+                    // contextsService.endContext(SessionScoped.class, request.getSession());
+                }
+                finally
+                {
+                    thread.setContextClassLoader(old);
+                }
+            }
+            finally
+            {
+                tomcat.stop();
+            }
+        }
+    }
+
+    private static File createWar(final File test, final Class<?>... classes) throws IOException
+    {
+        for (final Class<?> clazz : classes)
+        {
+            final String name = clazz.getName().replace('.', '/') + ".class";
+            final InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(name);
+            if (is == null)
+            {
+                throw new IllegalArgumentException(name);
+            }
+            final File out = new File(test, "WEB-INF/classes/" + name);
+            dir(out.getParentFile());
+            final OutputStream os = new FileOutputStream(out);
+            IOUtils.copy(is, os);
+            is.close();
+            os.close();
+        }
+        final Writer w = new FileWriter(new File(test, "WEB-INF/beans.xml"));
+        w.write("<beans />");
+        w.close();
+        return test;
+    }
+
+    private static File dir(final File file)
+    {
+        file.mkdirs();
+        return file;
+    }
+}