Initial Contribution Signed-off-by: Vinay Vishal <vinay.vishal@oracle.com>
diff --git a/appserver/tests/appserv-tests/devtests/cdi/smoke-tests/simple-managed-bean-interceptor/WebTest.java b/appserver/tests/appserv-tests/devtests/cdi/smoke-tests/simple-managed-bean-interceptor/WebTest.java new file mode 100644 index 0000000..343d171 --- /dev/null +++ b/appserver/tests/appserv-tests/devtests/cdi/smoke-tests/simple-managed-bean-interceptor/WebTest.java
@@ -0,0 +1,104 @@ +/* + * Copyright (c) 2010, 2018 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0, which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +import java.io.*; +import java.net.*; +import com.sun.ejte.ccl.reporter.*; + +/* + * Unit test for @WebServlet + */ +public class WebTest { + + private static SimpleReporterAdapter stat + = new SimpleReporterAdapter("appserv-tests"); + private static final String TEST_NAME = "simple-managed-bean-interceptor"; + private static final String EXPECTED_RESPONSE = "Hello from Servlet 3.0. initParams: n1=v1, n2=v2"; + + private String host; + private String port; + private String contextRoot; + + public WebTest(String[] args) { + host = args[0]; + port = args[1]; + contextRoot = args[2]; + } + + public static void main(String[] args) { + stat.addDescription("Unit test for @WebServlet"); + WebTest webTest = new WebTest(args); + webTest.doTest(); + stat.printSummary(TEST_NAME); + } + + public void doTest() { + try { + invoke(); + } catch (Exception ex) { + System.out.println(TEST_NAME + " test failed"); + stat.addStatus(TEST_NAME, stat.FAIL); + ex.printStackTrace(); + } + } + + private void invoke() throws Exception { + + String url = "http://" + host + ":" + port + contextRoot + + "/myurl"; + System.out.println("opening connection to " + url); + HttpURLConnection conn = (HttpURLConnection) + (new URL(url)).openConnection(); + + int code = conn.getResponseCode(); + if (code != 200) { + System.out.println("Unexpected return code: " + code); + stat.addStatus(TEST_NAME, stat.FAIL); + } else { + InputStream is = null; + BufferedReader input = null; + String line = null; + try { + is = conn.getInputStream(); + input = new BufferedReader(new InputStreamReader(is)); + line = input.readLine(); + System.out.println("line = " + line); + } finally { + try { + if (is != null) { + is.close(); + } + } catch(IOException ioe) { + // ignore + } + try { + if (input != null) { + input.close(); + } + } catch(IOException ioe) { + // ignore + } + } + if (EXPECTED_RESPONSE.equals(line)) { + stat.addStatus(TEST_NAME, stat.PASS); + } else { + System.out.println("Wrong response. Expected: " + + EXPECTED_RESPONSE + ", received: " + line); + stat.addStatus(TEST_NAME, stat.FAIL); + } + } + } +}
diff --git a/appserver/tests/appserv-tests/devtests/cdi/smoke-tests/simple-managed-bean-interceptor/build.properties b/appserver/tests/appserv-tests/devtests/cdi/smoke-tests/simple-managed-bean-interceptor/build.properties new file mode 100644 index 0000000..4809e75 --- /dev/null +++ b/appserver/tests/appserv-tests/devtests/cdi/smoke-tests/simple-managed-bean-interceptor/build.properties
@@ -0,0 +1,24 @@ +<!-- + + Copyright (c) 2018 Oracle and/or its affiliates. All rights reserved. + + This program and the accompanying materials are made available under the + terms of the Eclipse Public License v. 2.0, which is available at + http://www.eclipse.org/legal/epl-2.0. + + This Source Code may also be made available under the following Secondary + Licenses when the conditions for such availability set forth in the + Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + version 2 with the GNU Classpath Exception, which is available at + https://www.gnu.org/software/classpath/license.html. + + SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + +--> + + +<property name="module" value="cdi"/> +<property name="appname" value="${module}-simple-managed-bean-interceptor"/> +<property name="assemble" value="${build.classes.dir}/archive"/> +<property name="contextroot" value="/${appname}"/> +<property name="beans.xml" value="descriptor/beans.xml"/>
diff --git a/appserver/tests/appserv-tests/devtests/cdi/smoke-tests/simple-managed-bean-interceptor/build.xml b/appserver/tests/appserv-tests/devtests/cdi/smoke-tests/simple-managed-bean-interceptor/build.xml new file mode 100644 index 0000000..83ad666 --- /dev/null +++ b/appserver/tests/appserv-tests/devtests/cdi/smoke-tests/simple-managed-bean-interceptor/build.xml
@@ -0,0 +1,86 @@ +<?xml version="1.0" encoding="ISO-8859-1"?> +<!-- + + Copyright (c) 2010, 2018 Oracle and/or its affiliates. All rights reserved. + + This program and the accompanying materials are made available under the + terms of the Eclipse Public License v. 2.0, which is available at + http://www.eclipse.org/legal/epl-2.0. + + This Source Code may also be made available under the following Secondary + Licenses when the conditions for such availability set forth in the + Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + version 2 with the GNU Classpath Exception, which is available at + https://www.gnu.org/software/classpath/license.html. + + SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + +--> + +<!DOCTYPE project [ +<!ENTITY commonSetup SYSTEM "./../../../../config/properties.xml"> +<!ENTITY commonBuild SYSTEM "./../../../../config/common.xml"> +<!ENTITY run SYSTEM "./../../../../config/run.xml"> +<!ENTITY testproperties SYSTEM "./build.properties"> +]> + +<project name="webcontainer_unittest" default="all" basedir="."> + + &commonSetup; + &commonBuild; + &testproperties; + &run; + + <target name="all" depends="build,deploy,run,undeploy"/> + + <target name="clean" depends="init-common"> + <antcall target="clean-common"/> + <delete> + <fileset dir="." includes="*.class"/> + </delete> + </target> + + <target name="compile" depends="clean"> + <antcall target="compile-common"> + <param name="src" value="servlet"/> + </antcall> + </target> + + <target name="build" depends="compile"> + <property name="hasWebclient" value="yes"/> + <antcall target="webclient-war-common"> + <param name="hasWebclient" value="yes"/> + <param name="webclient.war.classes" value="**/*.class"/> + </antcall> + + <javac srcdir="." classpath="${env.APS_HOME}/lib/reportbuilder.jar" + includes="WebTest.java"/> + + </target> + + <target name="build-publish-war" depends="build, publish-war-common" /> + + <target name="deploy" depends="init-common"> + <antcall target="deploy-war-common"/> + </target> + + <target name="run" depends="init-common"> + <java classname="WebTest"> + <arg value="${http.host}"/> + <arg value="${http.port}"/> + <arg value="${contextroot}"/> + <classpath> + <pathelement location="${env.APS_HOME}/lib/reportbuilder.jar"/> + <pathelement location="."/> + </classpath> + </java> + </target> + + <target name="undeploy" depends="init-common"> + <antcall target="undeploy-war-common"/> + </target> + + <target name="usage"> + <antcall target="usage-common"/> + </target> +</project>
diff --git a/appserver/tests/appserv-tests/devtests/cdi/smoke-tests/simple-managed-bean-interceptor/descriptor/beans.xml b/appserver/tests/appserv-tests/devtests/cdi/smoke-tests/simple-managed-bean-interceptor/descriptor/beans.xml new file mode 100644 index 0000000..173bc72 --- /dev/null +++ b/appserver/tests/appserv-tests/devtests/cdi/smoke-tests/simple-managed-bean-interceptor/descriptor/beans.xml
@@ -0,0 +1,24 @@ +<!-- + + Copyright (c) 2017, 2018 Oracle and/or its affiliates. All rights reserved. + + This program and the accompanying materials are made available under the + terms of the Eclipse Public License v. 2.0, which is available at + http://www.eclipse.org/legal/epl-2.0. + + This Source Code may also be made available under the following Secondary + Licenses when the conditions for such availability set forth in the + Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + version 2 with the GNU Classpath Exception, which is available at + https://www.gnu.org/software/classpath/license.html. + + SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + +--> + +<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation=" http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd"> + <interceptors> + <class>TestAroundInvokeInterceptor</class> + </interceptors> +</beans>
diff --git a/appserver/tests/appserv-tests/devtests/cdi/smoke-tests/simple-managed-bean-interceptor/servlet/TestAroundInvokeInterceptor.java b/appserver/tests/appserv-tests/devtests/cdi/smoke-tests/simple-managed-bean-interceptor/servlet/TestAroundInvokeInterceptor.java new file mode 100644 index 0000000..8b2414e --- /dev/null +++ b/appserver/tests/appserv-tests/devtests/cdi/smoke-tests/simple-managed-bean-interceptor/servlet/TestAroundInvokeInterceptor.java
@@ -0,0 +1,35 @@ +/* + * Copyright (c) 2010, 2018 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0, which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +import javax.interceptor.*; + +@Tester @Interceptor +public class TestAroundInvokeInterceptor { + public static int aroundInvokeCount = 0; + + public static void reset(){ + aroundInvokeCount = 0; + } + + @AroundInvoke + public Object testMethod(InvocationContext ctx) throws Exception { + System.out.println("TestAroundInvokeIntercetpr:: aroundInvoke called - target:" + ctx.getTarget() + " , params:"+ ctx.getParameters()); + aroundInvokeCount ++; + ctx.proceed(); + return null; + } + +}
diff --git a/appserver/tests/appserv-tests/devtests/cdi/smoke-tests/simple-managed-bean-interceptor/servlet/TestBean.java b/appserver/tests/appserv-tests/devtests/cdi/smoke-tests/simple-managed-bean-interceptor/servlet/TestBean.java new file mode 100644 index 0000000..3dd49b9 --- /dev/null +++ b/appserver/tests/appserv-tests/devtests/cdi/smoke-tests/simple-managed-bean-interceptor/servlet/TestBean.java
@@ -0,0 +1,19 @@ +/* + * Copyright (c) 2010, 2018 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0, which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +//Simple Bean to test injection into ManagedBeans +public class TestBean { +}
diff --git a/appserver/tests/appserv-tests/devtests/cdi/smoke-tests/simple-managed-bean-interceptor/servlet/TestManagedBean.java b/appserver/tests/appserv-tests/devtests/cdi/smoke-tests/simple-managed-bean-interceptor/servlet/TestManagedBean.java new file mode 100644 index 0000000..4905710 --- /dev/null +++ b/appserver/tests/appserv-tests/devtests/cdi/smoke-tests/simple-managed-bean-interceptor/servlet/TestManagedBean.java
@@ -0,0 +1,54 @@ +/* + * Copyright (c) 2010, 2018 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0, which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +//Simple TestBean to test CDI. +//This bean implements Serializable as it needs to be placed into a Stateful Bean +@javax.annotation.ManagedBean +public class TestManagedBean { + TestBean tb; + boolean postConstructCalled = false; + + + //A Managed Bean needs to have a no-arg constructor + public TestManagedBean() {} + @javax.inject.Inject //Constructor based Injection + public TestManagedBean(TestBean tb){ + this.tb = tb; + } + + + @javax.annotation.PostConstruct + public void init(){ + System.out.println("In ManagedBean:: PostConstruct"); + postConstructCalled = true; + } + + @Tester + public void foo(){ + System.out.println("foo called"); + } + + public boolean testPostConstructCalled(){ + return this.postConstructCalled; + } + + public boolean testInjection(){ + System.out.println("In ManagedBean:: tb=" + tb); + postConstructCalled = true; + return this.tb != null; + } + +}
diff --git a/appserver/tests/appserv-tests/devtests/cdi/smoke-tests/simple-managed-bean-interceptor/servlet/TestServlet.java b/appserver/tests/appserv-tests/devtests/cdi/smoke-tests/simple-managed-bean-interceptor/servlet/TestServlet.java new file mode 100644 index 0000000..431cdfc --- /dev/null +++ b/appserver/tests/appserv-tests/devtests/cdi/smoke-tests/simple-managed-bean-interceptor/servlet/TestServlet.java
@@ -0,0 +1,73 @@ +/* + * Copyright (c) 2010, 2018 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0, which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +import java.io.IOException; +import java.io.PrintWriter; +import java.util.Enumeration; + +import javax.servlet.ServletException; +import javax.servlet.annotation.WebInitParam; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +@WebServlet(name="mytest", + urlPatterns={"/myurl"}, + initParams={ @WebInitParam(name="n1", value="v1"), @WebInitParam(name="n2", value="v2") } ) +public class TestServlet extends HttpServlet { + @javax.inject.Inject TestManagedBean tb1; + @javax.annotation.Resource TestManagedBean tb; + + public void service(HttpServletRequest req, HttpServletResponse res) + throws IOException, ServletException { + + PrintWriter writer = res.getWriter(); + writer.write("Hello from Servlet 3.0. "); + String msg = "n1=" + getInitParameter("n1") + + ", n2=" + getInitParameter("n2"); + + //ManagedBean testing .. add additional messages in the "msg" string, so that the test will FAIL in the client + msg += testManagedBean(tb, " | TestManagedBean injected via @Resource"); + msg += testManagedBean(tb1, " | TestManagedBean injected via @Inject"); + msg += testInterceptors(); + + writer.write("initParams: " + msg + "\n"); + } + + private String testManagedBean(TestManagedBean tb, String info) { + String msg = ""; + if (tb == null) msg += info + " is null!"; + if (tb != null && !tb.testPostConstructCalled()) msg += info + " postConstruct not called"; + if (tb != null && !tb.testInjection()) msg += info + "Bean Injection into ManagedBean failed"; + return msg; + } + + private String testInterceptors(){ + System.out.println("calling foo on an @Resource injected Managed Bean"); + tb.foo(); + System.out.println("calling foo on an @Inject Managed Bean"); + tb1.foo(); //TestAroundInvokeInterceptor is not called in this case + + int count = TestAroundInvokeInterceptor.aroundInvokeCount; + System.out.println("TestAroundInvokeInterceptor called " + count + " times"); + TestAroundInvokeInterceptor.reset(); + if (count == 1) return ""; + else return "Interceptor invocation count" + count + " invalid"; + } + + +}
diff --git a/appserver/tests/appserv-tests/devtests/cdi/smoke-tests/simple-managed-bean-interceptor/servlet/Tester.java b/appserver/tests/appserv-tests/devtests/cdi/smoke-tests/simple-managed-bean-interceptor/servlet/Tester.java new file mode 100644 index 0000000..0555ac6 --- /dev/null +++ b/appserver/tests/appserv-tests/devtests/cdi/smoke-tests/simple-managed-bean-interceptor/servlet/Tester.java
@@ -0,0 +1,26 @@ +/* + * Copyright (c) 2010, 2018 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0, which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +import javax.interceptor.*; +import java.lang.annotation.*; +import static java.lang.annotation.ElementType.*; +import static java.lang.annotation.RetentionPolicy.*; + + +@InterceptorBinding +@Target({METHOD, TYPE}) +@Retention(RUNTIME) +public @interface Tester {}