From 0ebaefcb958ce6e5fdcbb3be22c0f2aec0184b74 Mon Sep 17 00:00:00 2001 From: Pritam Banerjee Date: Thu, 18 May 2017 23:56:44 -0700 Subject: [PATCH] session beans --- ejb/ejb-session-beans-client/pom.xml | 54 ------ .../ejb/session/client/EJBStatefulClient.java | 85 --------- .../session/client/EJBStatelessClient.java | 88 ---------- .../resources/jboss-ejb-client.properties | 8 - .../client/test/EJBStatefulClientTest.java | 17 -- .../client/test/EJBStatelessClientTest.java | 16 -- ejb/ejb-session-beans/pom.xml | 163 ++++++++++-------- .../ejb/stateful/beans/ItemStateful.java | 27 --- .../stateful/beans/ItemStatefulRemote.java | 14 -- .../ejb/stateless/beans/ItemStateless.java | 29 ---- .../stateless/beans/ItemStatelessRemote.java | 13 -- .../readlearncode/stateful/EJBClient1.java | 17 ++ .../readlearncode/stateful/EJBClient2.java | 17 ++ .../readlearncode/stateful/StatefulEJB.java | 16 ++ .../readlearncode/stateless/EJBClient1.java | 16 ++ .../readlearncode/stateless/EJBClient2.java | 17 ++ .../readlearncode/stateless/StatelessEJB.java | 16 ++ .../src/main/resources/META-INF/ejb-jar.xml | 6 - .../stateful/StatefulEJBTest.java | 52 ++++++ .../stateless/StatelessEJBTest.java | 63 +++++++ 20 files changed, 301 insertions(+), 433 deletions(-) delete mode 100644 ejb/ejb-session-beans-client/pom.xml delete mode 100644 ejb/ejb-session-beans-client/src/main/java/com/baeldung/ejb/session/client/EJBStatefulClient.java delete mode 100644 ejb/ejb-session-beans-client/src/main/java/com/baeldung/ejb/session/client/EJBStatelessClient.java delete mode 100755 ejb/ejb-session-beans-client/src/main/resources/jboss-ejb-client.properties delete mode 100644 ejb/ejb-session-beans-client/src/test/java/com/baeldung/ejb/session/client/test/EJBStatefulClientTest.java delete mode 100644 ejb/ejb-session-beans-client/src/test/java/com/baeldung/ejb/session/client/test/EJBStatelessClientTest.java delete mode 100644 ejb/ejb-session-beans/src/main/java/com/baeldung/ejb/stateful/beans/ItemStateful.java delete mode 100644 ejb/ejb-session-beans/src/main/java/com/baeldung/ejb/stateful/beans/ItemStatefulRemote.java delete mode 100644 ejb/ejb-session-beans/src/main/java/com/baeldung/ejb/stateless/beans/ItemStateless.java delete mode 100644 ejb/ejb-session-beans/src/main/java/com/baeldung/ejb/stateless/beans/ItemStatelessRemote.java create mode 100644 ejb/ejb-session-beans/src/main/java/com/readlearncode/stateful/EJBClient1.java create mode 100644 ejb/ejb-session-beans/src/main/java/com/readlearncode/stateful/EJBClient2.java create mode 100644 ejb/ejb-session-beans/src/main/java/com/readlearncode/stateful/StatefulEJB.java create mode 100644 ejb/ejb-session-beans/src/main/java/com/readlearncode/stateless/EJBClient1.java create mode 100644 ejb/ejb-session-beans/src/main/java/com/readlearncode/stateless/EJBClient2.java create mode 100644 ejb/ejb-session-beans/src/main/java/com/readlearncode/stateless/StatelessEJB.java delete mode 100755 ejb/ejb-session-beans/src/main/resources/META-INF/ejb-jar.xml create mode 100644 ejb/ejb-session-beans/src/test/java/com/readlearncode/stateful/StatefulEJBTest.java create mode 100644 ejb/ejb-session-beans/src/test/java/com/readlearncode/stateless/StatelessEJBTest.java diff --git a/ejb/ejb-session-beans-client/pom.xml b/ejb/ejb-session-beans-client/pom.xml deleted file mode 100644 index b5e107f23a..0000000000 --- a/ejb/ejb-session-beans-client/pom.xml +++ /dev/null @@ -1,54 +0,0 @@ - - - 4.0.0 - - com.baeldung.ejb - ejb - 1.0-SNAPSHOT - - ejb-session-beans-client - EJB3 Client Maven - EJB3 Client Maven - - - 4.12 - 2.19 - - - - - org.wildfly - wildfly-ejb-client-bom - pom - import - - - - com.baeldung.ejb - ejb-session-beans - ejb - - - - junit - junit - 4.12 - test - - - - - - org.apache.maven.plugins - maven-surefire-plugin - 2.19 - - - **/*EJBSetupTest.java - - - - - - \ No newline at end of file diff --git a/ejb/ejb-session-beans-client/src/main/java/com/baeldung/ejb/session/client/EJBStatefulClient.java b/ejb/ejb-session-beans-client/src/main/java/com/baeldung/ejb/session/client/EJBStatefulClient.java deleted file mode 100644 index b2f79a2b13..0000000000 --- a/ejb/ejb-session-beans-client/src/main/java/com/baeldung/ejb/session/client/EJBStatefulClient.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.baeldung.ejb.session.client; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.Properties; - -import javax.naming.Context; -import javax.naming.InitialContext; -import javax.naming.NamingException; - -import com.baeldung.ejb.stateful.beans.ItemStatefulRemote; - -public class EJBStatefulClient { - - public EJBStatefulClient() { - } - - private Context context = null; - - public Boolean getEJBRemoteMessage() { - EJBStatefulClient ejb = new EJBStatefulClient(); - Boolean result = true; - try { - // 1. Obtaining Context - ejb.createInitialContext(); - // 2. Generate JNDI Lookup name and caste - ItemStatefulRemote itemStatefulOne = ejb.lookup(); - ItemStatefulRemote itemStatefulTwo = ejb.lookup(); - - itemStatefulOne.addItem("Book"); - itemStatefulOne.addItem("Pen"); - itemStatefulOne.addItem("Copy"); - itemStatefulOne.addItem("Pencil"); - - result = itemStatefulOne.getItemList().equals(itemStatefulTwo.getItemList()); - - return result; - } catch (NamingException e) { - e.printStackTrace(); - return false; - } finally { - try { - ejb.closeContext(); - } catch (NamingException e) { - e.printStackTrace(); - - } - } - } - - public ItemStatefulRemote lookup() throws NamingException { - - // The app name is the EAR name of the deployed EJB without .ear suffix. - // Since we haven't deployed the application as a .ear, the app name for - // us will be an empty string - final String appName = ""; - final String moduleName = "session-beans"; - final String distinctName = ""; - final String beanName = "ItemStatefulRemote"; - final String viewClassName = ItemStatefulRemote.class.getName() + "?stateful"; - final String toLookup = String.format("ejb:%s/%s/%s/%s!%s", appName, moduleName, distinctName, beanName, - viewClassName); - return (ItemStatefulRemote) context.lookup(toLookup); - } - - public void createInitialContext() throws NamingException { - Properties prop = new Properties(); - prop.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming"); - prop.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory"); - prop.put(Context.PROVIDER_URL, "http-remoting://127.0.0.1:8080"); - prop.put(Context.SECURITY_PRINCIPAL, "testUser"); - prop.put(Context.SECURITY_CREDENTIALS, "admin1234!"); - prop.put("jboss.naming.client.ejb.context", false); - - context = new InitialContext(prop); - } - - public void closeContext() throws NamingException { - if (context != null) { - context.close(); - } - } - -} diff --git a/ejb/ejb-session-beans-client/src/main/java/com/baeldung/ejb/session/client/EJBStatelessClient.java b/ejb/ejb-session-beans-client/src/main/java/com/baeldung/ejb/session/client/EJBStatelessClient.java deleted file mode 100644 index 3d00771a07..0000000000 --- a/ejb/ejb-session-beans-client/src/main/java/com/baeldung/ejb/session/client/EJBStatelessClient.java +++ /dev/null @@ -1,88 +0,0 @@ -package com.baeldung.ejb.session.client; - -import java.util.List; -import java.util.Properties; - -import javax.naming.Context; -import javax.naming.InitialContext; -import javax.naming.NamingException; - -import com.baeldung.ejb.stateless.beans.ItemStatelessRemote; - -public class EJBStatelessClient { - - public EJBStatelessClient() { - } - - private Context context = null; - - public static void main(String[] arg) { - EJBStatelessClient ejb = new EJBStatelessClient(); - System.out.println(ejb.getEJBRemoteMessage()); - - } - - public Boolean getEJBRemoteMessage() { - EJBStatelessClient main = new EJBStatelessClient(); - Boolean result = true; - try { - // 1. Obtaining Context - main.createInitialContext(); - // 2. Generate JNDI Lookup name and caste - ItemStatelessRemote itemStatelessOne = main.lookup(); - ItemStatelessRemote itemStatelessTwo = main.lookup(); - - itemStatelessOne.addItem("Book"); - itemStatelessOne.addItem("Pen"); - itemStatelessOne.addItem("Pencil"); - itemStatelessOne.addItem("Eraser"); - - result = itemStatelessOne.getItemList().equals(itemStatelessTwo.getItemList()); - - return result; - } catch (NamingException e) { - e.printStackTrace(); - return false; - } finally { - try { - main.closeContext(); - } catch (NamingException e) { - e.printStackTrace(); - } - } - } - - public ItemStatelessRemote lookup() throws NamingException { - - // The app name is the EAR name of the deployed EJB without .ear suffix. - // Since we haven't deployed the application as a .ear, the app name for - // us will be an empty string - final String appName = ""; - final String moduleName = "session-beans"; - final String distinctName = ""; - final String beanName = "ItemStatelessRemote"; - final String viewClassName = ItemStatelessRemote.class.getName(); - final String toLookup = String.format("ejb:%s/%s/%s/%s!%s", appName, moduleName, distinctName, beanName, viewClassName); - - return (ItemStatelessRemote) context.lookup(toLookup); - } - - public void createInitialContext() throws NamingException { - Properties prop = new Properties(); - prop.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming"); - prop.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory"); - prop.put(Context.PROVIDER_URL, "http-remoting://127.0.0.1:8080"); - prop.put(Context.SECURITY_PRINCIPAL, "testUser"); - prop.put(Context.SECURITY_CREDENTIALS, "admin1234!"); - prop.put("jboss.naming.client.ejb.context", false); - - context = new InitialContext(prop); - } - - public void closeContext() throws NamingException { - if (context != null) { - context.close(); - } - } - -} diff --git a/ejb/ejb-session-beans-client/src/main/resources/jboss-ejb-client.properties b/ejb/ejb-session-beans-client/src/main/resources/jboss-ejb-client.properties deleted file mode 100755 index 077cd7583f..0000000000 --- a/ejb/ejb-session-beans-client/src/main/resources/jboss-ejb-client.properties +++ /dev/null @@ -1,8 +0,0 @@ -remote.connections=default -remote.connection.default.host=127.0.0.1 -remote.connection.default.port=8080 -remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS=false -remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOPLAINTEXT=false -remote.connection.default.connect.options.org.xnio.Options.SASL_DISALLOWED_MECHANISMS=${host.auth:JBOSS-LOCAL-USER} -remote.connection.default.username=testUser -remote.connection.default.password=admin1234! \ No newline at end of file diff --git a/ejb/ejb-session-beans-client/src/test/java/com/baeldung/ejb/session/client/test/EJBStatefulClientTest.java b/ejb/ejb-session-beans-client/src/test/java/com/baeldung/ejb/session/client/test/EJBStatefulClientTest.java deleted file mode 100644 index 670dacc7aa..0000000000 --- a/ejb/ejb-session-beans-client/src/test/java/com/baeldung/ejb/session/client/test/EJBStatefulClientTest.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.baeldung.ejb.session.client.test; - -import static org.junit.Assert.*; - -import org.junit.Test; - -import com.baeldung.ejb.session.client.EJBStatefulClient; - -public class EJBStatefulClientTest { - - @Test - public void EJBClientTest() { - EJBStatefulClient ejbStatefulClient = new EJBStatefulClient(); - assertFalse(ejbStatefulClient.getEJBRemoteMessage()); - } - -} diff --git a/ejb/ejb-session-beans-client/src/test/java/com/baeldung/ejb/session/client/test/EJBStatelessClientTest.java b/ejb/ejb-session-beans-client/src/test/java/com/baeldung/ejb/session/client/test/EJBStatelessClientTest.java deleted file mode 100644 index 90e3106130..0000000000 --- a/ejb/ejb-session-beans-client/src/test/java/com/baeldung/ejb/session/client/test/EJBStatelessClientTest.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.baeldung.ejb.session.client.test; - -import static org.junit.Assert.assertTrue; - -import org.junit.Test; -import com.baeldung.ejb.session.client.EJBStatelessClient; - -public class EJBStatelessClientTest { - - @Test - public void EJBClientTest() { - EJBStatelessClient ejbStatelessClient = new EJBStatelessClient(); - assertTrue(ejbStatelessClient.getEJBRemoteMessage()); - } - -} diff --git a/ejb/ejb-session-beans/pom.xml b/ejb/ejb-session-beans/pom.xml index c1b908c0aa..a9b667baac 100644 --- a/ejb/ejb-session-beans/pom.xml +++ b/ejb/ejb-session-beans/pom.xml @@ -1,95 +1,106 @@ - - - 4.0.0 + + 4.0.0 + + com.baeldung.ejb + ejb + 1.0-SNAPSHOT + + ejb-session-beans + + + UTF-8 + 1.1.12.Final + 2.2.6 + 1.1.12.Final + 1.0.0.Final + 4.12 + 7.0 + - - com.baeldung.ejb - ejb - 1.0-SNAPSHOT - - - ejb-session-beans - ejb + + + + org.jboss.arquillian + arquillian-bom + 1.1.13.Final + import + pom + + + + javax javaee-api ${javaee-api.version} provided + + + + junit + junit + 4.12 + test + + + + + org.jboss.arquillian.junit + arquillian-junit-container + test + + - - - wildfly-standalone + arquillian-glassfish-embedded true - - - - org.codehaus.cargo - cargo-maven2-plugin - ${cargo-maven2-plugin.version} - - - - - wildfly10x - - http://download.jboss.org/wildfly/10.1.0.Final/wildfly-10.1.0.Final.zip - - - - - - 127.0.0.1 - 9990 - testUser:admin1234! - - - - - - - + + + org.jboss.arquillian.container + arquillian-glassfish-embedded-3.1 + 1.0.0.CR4 + test + + + org.glassfish.main.extras + glassfish-embedded-all + 3.1.2 + test + + - - - - wildfly-runtime - - false - - - - - org.wildfly.plugins - wildfly-maven-plugin - 1.1.0.Alpha5 - - 127.0.0.1 - 9990 - testUser - admin1234! - ${build.finalName}.jar - - - - - - - - 7.0 - 1.6.1 - - - - - + + + + maven-compiler-plugin + 3.1 + + 1.8 + 1.8 + + + + maven-war-plugin + 2.4 + + false + + + + maven-surefire-plugin + 2.17 + + + + + + \ No newline at end of file diff --git a/ejb/ejb-session-beans/src/main/java/com/baeldung/ejb/stateful/beans/ItemStateful.java b/ejb/ejb-session-beans/src/main/java/com/baeldung/ejb/stateful/beans/ItemStateful.java deleted file mode 100644 index faab0bd4b1..0000000000 --- a/ejb/ejb-session-beans/src/main/java/com/baeldung/ejb/stateful/beans/ItemStateful.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.baeldung.ejb.stateful.beans; - -import java.util.ArrayList; -import java.util.List; - -import javax.ejb.Stateful; - -@Stateful(name = "ItemStatefulRemote") -public class ItemStateful implements ItemStatefulRemote { - - private List itemList; - - public ItemStateful() { - itemList = new ArrayList(); - } - - @Override - public void addItem(String itemName) { - itemList.add(itemName); - } - - @Override - public List getItemList() { - return itemList; - } - -} diff --git a/ejb/ejb-session-beans/src/main/java/com/baeldung/ejb/stateful/beans/ItemStatefulRemote.java b/ejb/ejb-session-beans/src/main/java/com/baeldung/ejb/stateful/beans/ItemStatefulRemote.java deleted file mode 100644 index 0d5313772d..0000000000 --- a/ejb/ejb-session-beans/src/main/java/com/baeldung/ejb/stateful/beans/ItemStatefulRemote.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.baeldung.ejb.stateful.beans; - -import java.util.List; - -import javax.ejb.Remote; - -@Remote -public interface ItemStatefulRemote { - - void addItem(String itemName); - - List getItemList(); - -} diff --git a/ejb/ejb-session-beans/src/main/java/com/baeldung/ejb/stateless/beans/ItemStateless.java b/ejb/ejb-session-beans/src/main/java/com/baeldung/ejb/stateless/beans/ItemStateless.java deleted file mode 100644 index 23d24a5f21..0000000000 --- a/ejb/ejb-session-beans/src/main/java/com/baeldung/ejb/stateless/beans/ItemStateless.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.baeldung.ejb.stateless.beans; - -import java.util.ArrayList; -import java.util.List; - -import javax.ejb.Stateless; - -@Stateless(name = "ItemStatelessRemote") -public class ItemStateless implements ItemStatelessRemote { - - private List itemList; - - public ItemStateless() { - itemList = new ArrayList(); - } - - @Override - public void addItem(String itemName) { - - itemList.add(itemName); - } - - @Override - public List getItemList() { - - return itemList; - } - -} diff --git a/ejb/ejb-session-beans/src/main/java/com/baeldung/ejb/stateless/beans/ItemStatelessRemote.java b/ejb/ejb-session-beans/src/main/java/com/baeldung/ejb/stateless/beans/ItemStatelessRemote.java deleted file mode 100644 index 2ed2029cc8..0000000000 --- a/ejb/ejb-session-beans/src/main/java/com/baeldung/ejb/stateless/beans/ItemStatelessRemote.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.baeldung.ejb.stateless.beans; - -import java.util.List; - -import javax.ejb.Remote; - -@Remote -public interface ItemStatelessRemote { - - void addItem(String itemName); - - List getItemList(); -} diff --git a/ejb/ejb-session-beans/src/main/java/com/readlearncode/stateful/EJBClient1.java b/ejb/ejb-session-beans/src/main/java/com/readlearncode/stateful/EJBClient1.java new file mode 100644 index 0000000000..a61a3fef0c --- /dev/null +++ b/ejb/ejb-session-beans/src/main/java/com/readlearncode/stateful/EJBClient1.java @@ -0,0 +1,17 @@ +package com.readlearncode.stateful; + +import javax.ejb.EJB; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class EJBClient1 { + + @EJB + public StatefulEJB statefulEJB; + + +} \ No newline at end of file diff --git a/ejb/ejb-session-beans/src/main/java/com/readlearncode/stateful/EJBClient2.java b/ejb/ejb-session-beans/src/main/java/com/readlearncode/stateful/EJBClient2.java new file mode 100644 index 0000000000..350df5ff86 --- /dev/null +++ b/ejb/ejb-session-beans/src/main/java/com/readlearncode/stateful/EJBClient2.java @@ -0,0 +1,17 @@ +package com.readlearncode.stateful; + +import javax.ejb.EJB; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class EJBClient2 { + + @EJB + public StatefulEJB statefulEJB; + + +} \ No newline at end of file diff --git a/ejb/ejb-session-beans/src/main/java/com/readlearncode/stateful/StatefulEJB.java b/ejb/ejb-session-beans/src/main/java/com/readlearncode/stateful/StatefulEJB.java new file mode 100644 index 0000000000..771508584e --- /dev/null +++ b/ejb/ejb-session-beans/src/main/java/com/readlearncode/stateful/StatefulEJB.java @@ -0,0 +1,16 @@ +package com.readlearncode.stateful; + +import javax.ejb.Stateful; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +@Stateful +public class StatefulEJB { + + public String name; + +} \ No newline at end of file diff --git a/ejb/ejb-session-beans/src/main/java/com/readlearncode/stateless/EJBClient1.java b/ejb/ejb-session-beans/src/main/java/com/readlearncode/stateless/EJBClient1.java new file mode 100644 index 0000000000..e8e6e79a2d --- /dev/null +++ b/ejb/ejb-session-beans/src/main/java/com/readlearncode/stateless/EJBClient1.java @@ -0,0 +1,16 @@ +package com.readlearncode.stateless; + +import javax.ejb.EJB; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class EJBClient1 { + + @EJB + public StatelessEJB statelessEJB; + +} \ No newline at end of file diff --git a/ejb/ejb-session-beans/src/main/java/com/readlearncode/stateless/EJBClient2.java b/ejb/ejb-session-beans/src/main/java/com/readlearncode/stateless/EJBClient2.java new file mode 100644 index 0000000000..ff90f2fcf1 --- /dev/null +++ b/ejb/ejb-session-beans/src/main/java/com/readlearncode/stateless/EJBClient2.java @@ -0,0 +1,17 @@ +package com.readlearncode.stateless; + +import javax.ejb.EJB; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class EJBClient2 { + + @EJB + public StatelessEJB statelessEJB; + + +} \ No newline at end of file diff --git a/ejb/ejb-session-beans/src/main/java/com/readlearncode/stateless/StatelessEJB.java b/ejb/ejb-session-beans/src/main/java/com/readlearncode/stateless/StatelessEJB.java new file mode 100644 index 0000000000..1f60d12e71 --- /dev/null +++ b/ejb/ejb-session-beans/src/main/java/com/readlearncode/stateless/StatelessEJB.java @@ -0,0 +1,16 @@ +package com.readlearncode.stateless; + +import javax.ejb.Stateless; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +@Stateless +public class StatelessEJB { + + public String name; + +} \ No newline at end of file diff --git a/ejb/ejb-session-beans/src/main/resources/META-INF/ejb-jar.xml b/ejb/ejb-session-beans/src/main/resources/META-INF/ejb-jar.xml deleted file mode 100755 index 7ae4711863..0000000000 --- a/ejb/ejb-session-beans/src/main/resources/META-INF/ejb-jar.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - session-beans - \ No newline at end of file diff --git a/ejb/ejb-session-beans/src/test/java/com/readlearncode/stateful/StatefulEJBTest.java b/ejb/ejb-session-beans/src/test/java/com/readlearncode/stateful/StatefulEJBTest.java new file mode 100644 index 0000000000..04d01975f6 --- /dev/null +++ b/ejb/ejb-session-beans/src/test/java/com/readlearncode/stateful/StatefulEJBTest.java @@ -0,0 +1,52 @@ +package com.readlearncode.stateful; + +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.junit.Arquillian; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.asset.EmptyAsset; +import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; + +import javax.inject.Inject; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +@RunWith(Arquillian.class) +public class StatefulEJBTest { + + @Inject + private EJBClient1 ejbClient1; + + @Inject + private EJBClient2 ejbClient2; + + @Test + public void givenOneStatefulBean_whenTwoClientsSetValueOnBean_thenClientStateIsMaintained() { + + // act + ejbClient1.statefulEJB.name = "Client 1"; + ejbClient2.statefulEJB.name = "Client 2"; + + // assert + Assert.assertNotEquals(ejbClient1.statefulEJB.name, ejbClient2.statefulEJB.name); + Assert.assertEquals("Client 1", ejbClient1.statefulEJB.name); + Assert.assertEquals("Client 2", ejbClient2.statefulEJB.name); + + } + + @Deployment + public static JavaArchive createDeployment() { + return ShrinkWrap.create(JavaArchive.class) + .addClass(StatefulEJB.class) + .addClass(EJBClient1.class) + .addClass(EJBClient2.class) + .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"); + } + +} diff --git a/ejb/ejb-session-beans/src/test/java/com/readlearncode/stateless/StatelessEJBTest.java b/ejb/ejb-session-beans/src/test/java/com/readlearncode/stateless/StatelessEJBTest.java new file mode 100644 index 0000000000..8c7d3e1bc9 --- /dev/null +++ b/ejb/ejb-session-beans/src/test/java/com/readlearncode/stateless/StatelessEJBTest.java @@ -0,0 +1,63 @@ +package com.readlearncode.stateless; + +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.junit.Arquillian; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.asset.EmptyAsset; +import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; + +import javax.inject.Inject; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +@RunWith(Arquillian.class) +public class StatelessEJBTest { + + @Inject + private EJBClient1 ejbClient1; + + @Inject + private EJBClient2 ejbClient2; + + @Test + public void givenOneStatelessBean_whenStateIsSetInOneBean_secondBeanShouldHaveSameState() { + + // act + ejbClient1.statelessEJB.name = "Client 1"; + + // assert + Assert.assertEquals("Client 1", ejbClient1.statelessEJB.name); + Assert.assertEquals("Client 1", ejbClient2.statelessEJB.name); + + } + + + @Test + public void givenOneStatelessBean_whenStateIsSetInBothBeans_secondBeanShouldHaveSecondBeanState() { + + // act + ejbClient1.statelessEJB.name = "Client 1"; + ejbClient2.statelessEJB.name = "Client 2"; + + // assert + Assert.assertEquals("Client 2", ejbClient2.statelessEJB.name); + + } + + @Deployment + public static JavaArchive createDeployment() { + return ShrinkWrap.create(JavaArchive.class) + .addClass(StatelessEJB.class) + .addClass(EJBClient1.class) + .addClass(EJBClient2.class) + .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"); + } + +}