From 065a5e755dcc0e296dd955f384cee1f4dd7e84f5 Mon Sep 17 00:00:00 2001 From: kubamarchwicki Date: Sun, 8 Dec 2013 14:14:02 +0100 Subject: [PATCH 1/4] Removing obsolete servlets and index.jsp files --- .../org/javaee7/ejb/stateful/TestServlet.java | 136 ------------------ ejb/stateful/src/main/webapp/index.jsp | 57 -------- .../javaee7/ejb/stateless/TestServlet.java | 125 ---------------- ejb/stateless/src/main/webapp/index.jsp | 57 -------- 4 files changed, 375 deletions(-) delete mode 100644 ejb/stateful/src/main/java/org/javaee7/ejb/stateful/TestServlet.java delete mode 100644 ejb/stateful/src/main/webapp/index.jsp delete mode 100644 ejb/stateless/src/main/java/org/javaee7/ejb/stateless/TestServlet.java delete mode 100644 ejb/stateless/src/main/webapp/index.jsp diff --git a/ejb/stateful/src/main/java/org/javaee7/ejb/stateful/TestServlet.java b/ejb/stateful/src/main/java/org/javaee7/ejb/stateful/TestServlet.java deleted file mode 100644 index 19f16c2ed..000000000 --- a/ejb/stateful/src/main/java/org/javaee7/ejb/stateful/TestServlet.java +++ /dev/null @@ -1,136 +0,0 @@ -/* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * - * Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved. - * - * The contents of this file are subject to the terms of either the GNU - * General Public License Version 2 only ("GPL") or the Common Development - * and Distribution License("CDDL") (collectively, the "License"). You - * may not use this file except in compliance with the License. You can - * obtain a copy of the License at - * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html - * or packager/legal/LICENSE.txt. See the License for the specific - * language governing permissions and limitations under the License. - * - * When distributing the software, include this License Header Notice in each - * file and include the License file at packager/legal/LICENSE.txt. - * - * GPL Classpath Exception: - * Oracle designates this particular file as subject to the "Classpath" - * exception as provided by Oracle in the GPL Version 2 section of the License - * file that accompanied this code. - * - * Modifications: - * If applicable, add the following below the License Header, with the fields - * enclosed by brackets [] replaced by your own identifying information: - * "Portions Copyright [year] [name of copyright owner]" - * - * Contributor(s): - * If you wish your version of this file to be governed by only the CDDL or - * only the GPL Version 2, indicate your decision by adding "[Contributor] - * elects to include this software in this distribution under the [CDDL or GPL - * Version 2] license." If you don't indicate a single choice of license, a - * recipient has the option to distribute your version of this file under - * either the CDDL, the GPL Version 2 or to extend the choice of license to - * its licensees as provided above. However, if you add GPL Version 2 code - * and therefore, elected the GPL Version 2 license, then the option applies - * only if the new code is made subject to such option by the copyright - * holder. - */ -package org.javaee7.ejb.stateful; - -import java.io.IOException; -import java.io.PrintWriter; -import javax.inject.Inject; -import javax.servlet.ServletException; -import javax.servlet.annotation.WebServlet; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -/** - * @author Arun Gupta - */ -@WebServlet(urlPatterns = {"/TestServlet"}) -public class TestServlet extends HttpServlet { - @Inject CartBean bean;; - - /** - * Processes requests for both HTTP GET and POST - * methods. - * - * @param request servlet request - * @param response servlet response - * @throws ServletException if a servlet-specific error occurs - * @throws IOException if an I/O error occurs - */ - protected void processRequest(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { - response.setContentType("text/html;charset=UTF-8"); - try (PrintWriter out = response.getWriter()) { - out.println(""); - out.println(""); - out.println(""); - out.println("Adding/Removing items from Stateful Bean (No Interface)"); - out.println(""); - out.println(""); - out.println("

Adding/Removing items from Stateful Bean (No Interface)

"); - out.println("

Adding items

"); - bean.addItem("apple"); - bean.addItem("banana"); - bean.addItem("mango"); - bean.addItem("kiwi"); - bean.addItem("passion fruit"); - out.println("added"); - out.println("

Listing items

"); - out.println(bean.getItems()); - out.println("

Removing item

"); - bean.removeItem("banana"); - out.println("removed"); - out.println("

Listing items

"); - out.println(bean.getItems()); - out.println(""); - out.println(""); - } - } - - // - /** - * Handles the HTTP GET method. - * - * @param request servlet request - * @param response servlet response - * @throws ServletException if a servlet-specific error occurs - * @throws IOException if an I/O error occurs - */ - @Override - protected void doGet(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { - processRequest(request, response); - } - - /** - * Handles the HTTP POST method. - * - * @param request servlet request - * @param response servlet response - * @throws ServletException if a servlet-specific error occurs - * @throws IOException if an I/O error occurs - */ - @Override - protected void doPost(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { - processRequest(request, response); - } - - /** - * Returns a short description of the servlet. - * - * @return a String containing servlet description - */ - @Override - public String getServletInfo() { - return "Short description"; - }// - -} diff --git a/ejb/stateful/src/main/webapp/index.jsp b/ejb/stateful/src/main/webapp/index.jsp deleted file mode 100644 index ee2c4087a..000000000 --- a/ejb/stateful/src/main/webapp/index.jsp +++ /dev/null @@ -1,57 +0,0 @@ - -<%@page contentType="text/html" pageEncoding="UTF-8"%> - - - - - - EJB : Stateful - - -

EJB : Stateful

- - Call beans no-interface - and with-interface. - - diff --git a/ejb/stateless/src/main/java/org/javaee7/ejb/stateless/TestServlet.java b/ejb/stateless/src/main/java/org/javaee7/ejb/stateless/TestServlet.java deleted file mode 100644 index 5c834f9f7..000000000 --- a/ejb/stateless/src/main/java/org/javaee7/ejb/stateless/TestServlet.java +++ /dev/null @@ -1,125 +0,0 @@ -/* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * - * Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved. - * - * The contents of this file are subject to the terms of either the GNU - * General Public License Version 2 only ("GPL") or the Common Development - * and Distribution License("CDDL") (collectively, the "License"). You - * may not use this file except in compliance with the License. You can - * obtain a copy of the License at - * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html - * or packager/legal/LICENSE.txt. See the License for the specific - * language governing permissions and limitations under the License. - * - * When distributing the software, include this License Header Notice in each - * file and include the License file at packager/legal/LICENSE.txt. - * - * GPL Classpath Exception: - * Oracle designates this particular file as subject to the "Classpath" - * exception as provided by Oracle in the GPL Version 2 section of the License - * file that accompanied this code. - * - * Modifications: - * If applicable, add the following below the License Header, with the fields - * enclosed by brackets [] replaced by your own identifying information: - * "Portions Copyright [year] [name of copyright owner]" - * - * Contributor(s): - * If you wish your version of this file to be governed by only the CDDL or - * only the GPL Version 2, indicate your decision by adding "[Contributor] - * elects to include this software in this distribution under the [CDDL or GPL - * Version 2] license." If you don't indicate a single choice of license, a - * recipient has the option to distribute your version of this file under - * either the CDDL, the GPL Version 2 or to extend the choice of license to - * its licensees as provided above. However, if you add GPL Version 2 code - * and therefore, elected the GPL Version 2 license, then the option applies - * only if the new code is made subject to such option by the copyright - * holder. - */ -package org.javaee7.ejb.stateless; - -import java.io.IOException; -import java.io.PrintWriter; -import javax.inject.Inject; -import javax.servlet.ServletException; -import javax.servlet.annotation.WebServlet; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -/** - * @author Arun Gupta - */ -@WebServlet(urlPatterns = {"/TestServlet"}) -public class TestServlet extends HttpServlet { - @Inject AccountSessionBean bean;; - - /** - * Processes requests for both HTTP GET and POST - * methods. - * - * @param request servlet request - * @param response servlet response - * @throws ServletException if a servlet-specific error occurs - * @throws IOException if an I/O error occurs - */ - protected void processRequest(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { - response.setContentType("text/html;charset=UTF-8"); - try (PrintWriter out = response.getWriter()) { - out.println(""); - out.println(""); - out.println(""); - out.println("Stateless Bean (No Interface)"); - out.println(""); - out.println(""); - out.println("

Stateless Bean (No Interface)

"); - out.println("

Withdraw and Deposit

"); - out.println(bean.deposit((float)5.0)); - out.println(bean.withdraw((float)5.0)); - out.println(""); - out.println(""); - } - } - - // - /** - * Handles the HTTP GET method. - * - * @param request servlet request - * @param response servlet response - * @throws ServletException if a servlet-specific error occurs - * @throws IOException if an I/O error occurs - */ - @Override - protected void doGet(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { - processRequest(request, response); - } - - /** - * Handles the HTTP POST method. - * - * @param request servlet request - * @param response servlet response - * @throws ServletException if a servlet-specific error occurs - * @throws IOException if an I/O error occurs - */ - @Override - protected void doPost(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { - processRequest(request, response); - } - - /** - * Returns a short description of the servlet. - * - * @return a String containing servlet description - */ - @Override - public String getServletInfo() { - return "Short description"; - }// - -} diff --git a/ejb/stateless/src/main/webapp/index.jsp b/ejb/stateless/src/main/webapp/index.jsp deleted file mode 100644 index ee2c4087a..000000000 --- a/ejb/stateless/src/main/webapp/index.jsp +++ /dev/null @@ -1,57 +0,0 @@ - -<%@page contentType="text/html" pageEncoding="UTF-8"%> - - - - - - EJB : Stateful - - -

EJB : Stateful

- - Call beans no-interface - and with-interface. - - From a7426603bb5c456ed6565cefa0f88836c0133b7f Mon Sep 17 00:00:00 2001 From: kubamarchwicki Date: Sun, 8 Dec 2013 14:33:45 +0100 Subject: [PATCH 2/4] Test new stateful bean on every ctx lookup --- .../stateful/CartBeanStatefulnessTest.java | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 ejb/stateful/src/test/java/org/javaee7/ejb/stateful/CartBeanStatefulnessTest.java diff --git a/ejb/stateful/src/test/java/org/javaee7/ejb/stateful/CartBeanStatefulnessTest.java b/ejb/stateful/src/test/java/org/javaee7/ejb/stateful/CartBeanStatefulnessTest.java new file mode 100644 index 000000000..e04e390bf --- /dev/null +++ b/ejb/stateful/src/test/java/org/javaee7/ejb/stateful/CartBeanStatefulnessTest.java @@ -0,0 +1,67 @@ +package org.javaee7.ejb.stateful; + +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.junit.Arquillian; +import org.jboss.arquillian.junit.InSequence; +import org.jboss.shrinkwrap.api.Archive; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.asset.EmptyAsset; +import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +import javax.naming.InitialContext; +import javax.naming.NamingException; + +import static org.hamcrest.CoreMatchers.*; +import static org.hamcrest.MatcherAssert.*; + +/** + * @author Jakub Marchwicki + */ +@RunWith(Arquillian.class) +public class CartBeanStatefulnessTest { + + final private String item_to_add = "apple"; + private CartBean cartBean; + + @Deployment + public static Archive deployment() { + return ShrinkWrap.create(JavaArchive.class, "test.jar") + .addClass(CartBean.class) + .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"); + } + + @Before + public void setup() throws NamingException { + InitialContext ctx = new InitialContext(); + Object object = ctx.lookup("java:global/test/CartBean"); + assertThat(object, instanceOf(CartBean.class)); + + CartBean cartBean = (CartBean) object; + if (this.cartBean != null) { + assertThat("Expect different instances", + cartBean.hashCode(), + is(not(equalTo(this.cartBean.hashCode())))); + } + + this.cartBean = cartBean; + } + + @Test + @InSequence(1) + public void should_add_items_to_cart() { + // when + cartBean.addItem(item_to_add); + + // then + assertThat(cartBean.getItems(), hasItem(item_to_add)); + } + + @Test + @InSequence(2) + public void should_not_contain_any_items() { + assertThat(cartBean.getItems().isEmpty(), is(true)); + } +} \ No newline at end of file From 2d74f53716b1fe5bb4f1a3c4591ec545ee5859da Mon Sep 17 00:00:00 2001 From: kubamarchwicki Date: Sun, 8 Dec 2013 15:08:58 +0100 Subject: [PATCH 3/4] Test same stateless bean on every ctx lookup --- .../ejb/stateless/AccountSessionBean.java | 10 ++- .../AccountSessionStatelessnessTest.java | 69 +++++++++++++++++++ 2 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 ejb/stateless/src/test/java/org/javaee7/ejb/stateless/AccountSessionStatelessnessTest.java diff --git a/ejb/stateless/src/main/java/org/javaee7/ejb/stateless/AccountSessionBean.java b/ejb/stateless/src/main/java/org/javaee7/ejb/stateless/AccountSessionBean.java index fe5c05b0b..49c344ec0 100644 --- a/ejb/stateless/src/main/java/org/javaee7/ejb/stateless/AccountSessionBean.java +++ b/ejb/stateless/src/main/java/org/javaee7/ejb/stateless/AccountSessionBean.java @@ -47,11 +47,19 @@ @Stateless public class AccountSessionBean { + private float amount = 0; + public String withdraw(float amount) { + this.amount -= amount; return "Withdrawn: " + amount; } - public String deposit(float amount) { + public String deposit(float amount) { + this.amount += amount; return "Deposited: " + amount; } + + public float getAmount() { + return this.amount; + } } diff --git a/ejb/stateless/src/test/java/org/javaee7/ejb/stateless/AccountSessionStatelessnessTest.java b/ejb/stateless/src/test/java/org/javaee7/ejb/stateless/AccountSessionStatelessnessTest.java new file mode 100644 index 000000000..e0a3df355 --- /dev/null +++ b/ejb/stateless/src/test/java/org/javaee7/ejb/stateless/AccountSessionStatelessnessTest.java @@ -0,0 +1,69 @@ +package org.javaee7.ejb.stateless; + +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.junit.Arquillian; +import org.jboss.arquillian.junit.InSequence; +import org.jboss.shrinkwrap.api.Archive; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.asset.EmptyAsset; +import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +import javax.naming.InitialContext; +import javax.naming.NamingException; + +import static org.hamcrest.MatcherAssert.*; +import static org.hamcrest.Matchers.*; + +/** + * @author Jakub Marchwicki + */ +@RunWith(Arquillian.class) +public class AccountSessionStatelessnessTest { + + final private float deposit_amount = 10f; + private AccountSessionBean account; + + @Deployment + public static Archive deployment() { + return ShrinkWrap.create(JavaArchive.class, "test.jar") + .addClass(AccountSessionBean.class) + .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"); + } + + @Before + public void setup() throws NamingException { + InitialContext ctx = new InitialContext(); + Object object = ctx.lookup("java:global/test/AccountSessionBean"); + + assertThat(object, instanceOf(AccountSessionBean.class)); + + AccountSessionBean account = (AccountSessionBean) object; + if (this.account != null) { + assertThat("Expect same instance", + account.hashCode(), + is(equalTo(this.account.hashCode()))); + } + + this.account = account; + } + + @Test + @InSequence(1) + public void should_deposit_amount() { + assertThat(account.getAmount(), is(equalTo(0f))); + + String actual = account.deposit(deposit_amount); + + assertThat(actual, is(equalTo("Deposited: " + deposit_amount))); + assertThat(account.getAmount(), is(equalTo(deposit_amount))); + } + + @Test + @InSequence(2) + public void should_contain_already_deposited_amount() { + assertThat(account.getAmount(), is(equalTo(deposit_amount))); + } +} \ No newline at end of file From 7d6225da153cf48c767496197d5a9ae72d2fdacf Mon Sep 17 00:00:00 2001 From: kubamarchwicki Date: Mon, 9 Dec 2013 21:08:50 +0100 Subject: [PATCH 4/4] Favouring injection over InitialContext for #75 EJB stateless and statefull tests --- .../stateful/CartBeanStatefulnessTest.java | 53 +++++++++--------- .../AccountSessionStatelessnessTest.java | 55 ++++++++++--------- 2 files changed, 56 insertions(+), 52 deletions(-) diff --git a/ejb/stateful/src/test/java/org/javaee7/ejb/stateful/CartBeanStatefulnessTest.java b/ejb/stateful/src/test/java/org/javaee7/ejb/stateful/CartBeanStatefulnessTest.java index e04e390bf..d521455f0 100644 --- a/ejb/stateful/src/test/java/org/javaee7/ejb/stateful/CartBeanStatefulnessTest.java +++ b/ejb/stateful/src/test/java/org/javaee7/ejb/stateful/CartBeanStatefulnessTest.java @@ -7,12 +7,10 @@ import org.jboss.shrinkwrap.api.ShrinkWrap; import org.jboss.shrinkwrap.api.asset.EmptyAsset; import org.jboss.shrinkwrap.api.spec.JavaArchive; -import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import javax.naming.InitialContext; -import javax.naming.NamingException; +import javax.ejb.EJB; import static org.hamcrest.CoreMatchers.*; import static org.hamcrest.MatcherAssert.*; @@ -24,7 +22,12 @@ public class CartBeanStatefulnessTest { final private String item_to_add = "apple"; - private CartBean cartBean; + + @EJB + private CartBean bean1; + + @EJB + private CartBean bean2; @Deployment public static Archive deployment() { @@ -33,35 +36,35 @@ public static Archive deployment() { .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"); } - @Before - public void setup() throws NamingException { - InitialContext ctx = new InitialContext(); - Object object = ctx.lookup("java:global/test/CartBean"); - assertThat(object, instanceOf(CartBean.class)); - - CartBean cartBean = (CartBean) object; - if (this.cartBean != null) { - assertThat("Expect different instances", - cartBean.hashCode(), - is(not(equalTo(this.cartBean.hashCode())))); - } - - this.cartBean = cartBean; + /** + * JSR 318: Enterprise JavaBeans, Version 3.1 + * 3.4.7.1 Session Object Identity / Stateful Session Beans + * + * A stateful session object has a unique identity that is assigned by + * the container at the time the object is created. A client of the stateful + * session bean business interface can determine if two business interface + * or no-interface view references refer to the same session object + * by use of the equals method + */ + @Test + @InSequence(1) + public void should_not_be_identical_beans() { + assertThat("Expect different instances", bean1, is(not(bean2))); } @Test - @InSequence(1) - public void should_add_items_to_cart() { + @InSequence(2) + public void should_add_items_to_first_cart() { // when - cartBean.addItem(item_to_add); + bean1.addItem(item_to_add); // then - assertThat(cartBean.getItems(), hasItem(item_to_add)); + assertThat(bean1.getItems(), hasItem(item_to_add)); } @Test - @InSequence(2) - public void should_not_contain_any_items() { - assertThat(cartBean.getItems().isEmpty(), is(true)); + @InSequence(3) + public void should_not_contain_any_items_in_second_cart() { + assertThat(bean2.getItems().isEmpty(), is(true)); } } \ No newline at end of file diff --git a/ejb/stateless/src/test/java/org/javaee7/ejb/stateless/AccountSessionStatelessnessTest.java b/ejb/stateless/src/test/java/org/javaee7/ejb/stateless/AccountSessionStatelessnessTest.java index e0a3df355..218a3f75e 100644 --- a/ejb/stateless/src/test/java/org/javaee7/ejb/stateless/AccountSessionStatelessnessTest.java +++ b/ejb/stateless/src/test/java/org/javaee7/ejb/stateless/AccountSessionStatelessnessTest.java @@ -7,12 +7,10 @@ import org.jboss.shrinkwrap.api.ShrinkWrap; import org.jboss.shrinkwrap.api.asset.EmptyAsset; import org.jboss.shrinkwrap.api.spec.JavaArchive; -import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import javax.naming.InitialContext; -import javax.naming.NamingException; +import javax.ejb.EJB; import static org.hamcrest.MatcherAssert.*; import static org.hamcrest.Matchers.*; @@ -24,7 +22,12 @@ public class AccountSessionStatelessnessTest { final private float deposit_amount = 10f; - private AccountSessionBean account; + + @EJB + AccountSessionBean account1; + + @EJB + AccountSessionBean account2; @Deployment public static Archive deployment() { @@ -33,37 +36,35 @@ public static Archive deployment() { .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"); } - @Before - public void setup() throws NamingException { - InitialContext ctx = new InitialContext(); - Object object = ctx.lookup("java:global/test/AccountSessionBean"); - - assertThat(object, instanceOf(AccountSessionBean.class)); - - AccountSessionBean account = (AccountSessionBean) object; - if (this.account != null) { - assertThat("Expect same instance", - account.hashCode(), - is(equalTo(this.account.hashCode()))); - } - - this.account = account; + /** + * JSR 318: Enterprise JavaBeans, Version 3.1 + * 3.4.7.2 Session Object Identity / Stateless Session Beans + * + * All business object references of the same interface type for the same + * stateless session bean have the same object identity, which is assigned + * by the container. All references to the no-interface view of the same + * stateless session bean have the same object identity. + */ + @Test + @InSequence(1) + public void should_be_identical_beans() { + assertThat("Expect same instances", account1, is(account2)); } @Test - @InSequence(1) - public void should_deposit_amount() { - assertThat(account.getAmount(), is(equalTo(0f))); + @InSequence(2) + public void should_deposit_amount_on_first_account() { + assertThat(account1.getAmount(), is(equalTo(0f))); - String actual = account.deposit(deposit_amount); + String actual = account1.deposit(deposit_amount); assertThat(actual, is(equalTo("Deposited: " + deposit_amount))); - assertThat(account.getAmount(), is(equalTo(deposit_amount))); + assertThat(account1.getAmount(), is(equalTo(deposit_amount))); } @Test - @InSequence(2) - public void should_contain_already_deposited_amount() { - assertThat(account.getAmount(), is(equalTo(deposit_amount))); + @InSequence(3) + public void should_contain_already_deposited_amount_on_second_account() { + assertThat(account2.getAmount(), is(equalTo(deposit_amount))); } } \ No newline at end of file