Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 50AB1200B29 for ; Thu, 30 Jun 2016 16:01:16 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 4F4AF160A52; Thu, 30 Jun 2016 14:01:16 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 507B7160A06 for ; Thu, 30 Jun 2016 16:01:15 +0200 (CEST) Received: (qmail 1832 invoked by uid 500); 30 Jun 2016 14:01:14 -0000 Mailing-List: contact commits-help@openwebbeans.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@openwebbeans.apache.org Delivered-To: mailing list commits@openwebbeans.apache.org Received: (qmail 1819 invoked by uid 99); 30 Jun 2016 14:01:14 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd4-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 30 Jun 2016 14:01:14 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd4-us-west.apache.org (ASF Mail Server at spamd4-us-west.apache.org) with ESMTP id 13FEFC00EA for ; Thu, 30 Jun 2016 14:01:14 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd4-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: 0.374 X-Spam-Level: X-Spam-Status: No, score=0.374 tagged_above=-999 required=6.31 tests=[KAM_ASCII_DIVIDERS=0.8, KAM_LAZY_DOMAIN_SECURITY=1, RP_MATCHES_RCVD=-1.426] autolearn=disabled Received: from mx1-lw-us.apache.org ([10.40.0.8]) by localhost (spamd4-us-west.apache.org [10.40.0.11]) (amavisd-new, port 10024) with ESMTP id ODn2oD41WOMw for ; Thu, 30 Jun 2016 14:01:11 +0000 (UTC) Received: from mailrelay1-us-west.apache.org (mailrelay1-us-west.apache.org [209.188.14.139]) by mx1-lw-us.apache.org (ASF Mail Server at mx1-lw-us.apache.org) with ESMTP id 79B8A5F4E3 for ; Thu, 30 Jun 2016 14:01:11 +0000 (UTC) Received: from svn01-us-west.apache.org (svn.apache.org [10.41.0.6]) by mailrelay1-us-west.apache.org (ASF Mail Server at mailrelay1-us-west.apache.org) with ESMTP id B21A0E0098 for ; Thu, 30 Jun 2016 14:01:10 +0000 (UTC) Received: from svn01-us-west.apache.org (localhost [127.0.0.1]) by svn01-us-west.apache.org (ASF Mail Server at svn01-us-west.apache.org) with ESMTP id 897EF3A019C for ; Thu, 30 Jun 2016 14:01:10 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1750799 - in /openwebbeans/trunk/webbeans-tomcat7: ./ src/test/java/org/apache/webbeans/web/tomcat7/test/ Date: Thu, 30 Jun 2016 14:01:09 -0000 To: commits@openwebbeans.apache.org From: rmannibucau@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20160630140110.897EF3A019C@svn01-us-west.apache.org> archived-at: Thu, 30 Jun 2016 14:01:16 -0000 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 @@ openwebbeans-web ${project.version} + + + junit + junit + + + org.apache.tomcat.embed + tomcat-embed-core + ${tomcat7.version} + test + + + org.apache.geronimo.specs + geronimo-interceptor_1.2_spec + 1.0 + test + + + org.apache.tomcat + tomcat-jasper-el + ${tomcat7.version} + test + + + org.apache.tomcat + tomcat-el-api + ${tomcat7.version} + test + + + org.apache.tomcat + tomcat-jsp-api + ${tomcat7.version} + test + + + org.apache.tomcat + tomcat-jasper + ${tomcat7.version} + test + 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(""); + w.close(); + return test; + } + + private static File dir(final File file) + { + file.mkdirs(); + return file; + } +}