From f1129ea16bb120281be8d0df9ed67cbf3c04b923 Mon Sep 17 00:00:00 2001 From: gautamshetty Date: Sun, 20 May 2018 03:16:46 -0500 Subject: [PATCH] BAEL 717 - Singleton Session Bean (#4104) * BAEL-717: Singleton EJB Bean Files for BAEL-717:Singleton EJB Bean. * BAEL-717: Singleton EJB Bean Corrected Indentation. * BAEL-717: Singleton EJB Bean Corrected Indentation. * BAEL-717: Singleton EJB Bean Corrected Indentation. * BAEL-717: Singleton EJB Bean Corrected Indentation. * BAEL-717: Singleton EJB Bean Changed artifactId value. * BAEL-717: Singleton EJB Bean. Added module for Singleton EJB Bean. * BAEL-717: Singleton EJB Bean. Removed Singleton EJB Bean Module. * BAEL-717: Singleton EJB Bean Changed the JNDI Lookup name. * BAEL-717: Singleton EJB Bean. Added the "singleton-ejb-bean" module. * BAEL-717: Singleton EJB Bean. Corrected Indentation. * BAEL-717: Singleton EJB Bean Corrected Indentation. * BAEL-717: Singleton EJB Bean. Corrected Indentation. * BAEL-717: Singleton EJB Bean. Corrected Indentation. * BAEL-717:Singleton EJB Bean. Corrected Indentation. * BAEL-717:Singleton EJB Bean. Corrected Indentation. * BAEL-717: Singleton Session Bean. Added class for Bean-Managed concurrrency. Changed class name from CountryStateCacheBean to CountryStateContainerManagedBean. * BAEL-717: Singleton Session Bean. Changing the name of the class to CountryStateContainerManagedBean. * BAEL-717: Singleton Session Bean. Added method to test Bean-Managed concurrency. * Get Latest. * deleting CountryStateBeanManagedBean for new file. * deleting CountryStateCacheBean for new file. * deleting CountryStateContainerManagedBean for new file. * BAEL-717: Singleton Session Bean. Adding file for Bean with Bean-Managed concurrency. Changing file name for original file to CountryStateContainerManagedBean with Container-Managed concurrency. * Deleting file for new checkin. * BAEL-717: Singleton Session Bean. Added test case for Bean-Manged concurrency. Change in JNDI names. * BAEL-717: Singleton Session Bean. Changed the assert method parameter order and null check in test cases. * BAEL-717:Singleton Session Bean Removed volatile keyword for the variable countryStatesMap. Marking it as final. * BAEL 717 - Singleton EJB Bean Added new method setStates(). * BAEL-717: Singleton Session Bean. Renaming directory singleton-ejb-bean to ejb-beans. * BAEL-717: Singleton EJB Beans. Renaming directory name from singleton-ejb-bean to ejb-beans. * BAEL-717: Singleton Session Bean Renaming directory singleton-ejb-bean to ejb-beans. --- .../{singleton-ejb-bean => ejb-beans}/pom.xml | 0 .../baeldung/singletonbean/CountryState.java | 2 + .../CountryStateBeanManagedBean.java | 6 ++- .../CountryStateContainerManagedBean.java | 5 +++ .../CountryStateCacheBeanTest.java | 38 ++++++++++++++++--- spring-ejb/pom.xml | 2 +- 6 files changed, 46 insertions(+), 7 deletions(-) rename spring-ejb/{singleton-ejb-bean => ejb-beans}/pom.xml (100%) rename spring-ejb/{singleton-ejb-bean => ejb-beans}/src/main/java/com/baeldung/singletonbean/CountryState.java (72%) rename spring-ejb/{singleton-ejb-bean => ejb-beans}/src/main/java/com/baeldung/singletonbean/CountryStateBeanManagedBean.java (85%) rename spring-ejb/{singleton-ejb-bean => ejb-beans}/src/main/java/com/baeldung/singletonbean/CountryStateContainerManagedBean.java (88%) rename spring-ejb/{singleton-ejb-bean => ejb-beans}/src/test/java/com/baeldung/singletonbean/CountryStateCacheBeanTest.java (52%) diff --git a/spring-ejb/singleton-ejb-bean/pom.xml b/spring-ejb/ejb-beans/pom.xml similarity index 100% rename from spring-ejb/singleton-ejb-bean/pom.xml rename to spring-ejb/ejb-beans/pom.xml diff --git a/spring-ejb/singleton-ejb-bean/src/main/java/com/baeldung/singletonbean/CountryState.java b/spring-ejb/ejb-beans/src/main/java/com/baeldung/singletonbean/CountryState.java similarity index 72% rename from spring-ejb/singleton-ejb-bean/src/main/java/com/baeldung/singletonbean/CountryState.java rename to spring-ejb/ejb-beans/src/main/java/com/baeldung/singletonbean/CountryState.java index a9a426ae32..1f96fdba7c 100644 --- a/spring-ejb/singleton-ejb-bean/src/main/java/com/baeldung/singletonbean/CountryState.java +++ b/spring-ejb/ejb-beans/src/main/java/com/baeldung/singletonbean/CountryState.java @@ -8,5 +8,7 @@ import javax.ejb.Local; public interface CountryState { public List getStates(String country); + + public void setStates(String country, List states); } diff --git a/spring-ejb/singleton-ejb-bean/src/main/java/com/baeldung/singletonbean/CountryStateBeanManagedBean.java b/spring-ejb/ejb-beans/src/main/java/com/baeldung/singletonbean/CountryStateBeanManagedBean.java similarity index 85% rename from spring-ejb/singleton-ejb-bean/src/main/java/com/baeldung/singletonbean/CountryStateBeanManagedBean.java rename to spring-ejb/ejb-beans/src/main/java/com/baeldung/singletonbean/CountryStateBeanManagedBean.java index ee2c1f005a..3ef7a62753 100644 --- a/spring-ejb/singleton-ejb-bean/src/main/java/com/baeldung/singletonbean/CountryStateBeanManagedBean.java +++ b/spring-ejb/ejb-beans/src/main/java/com/baeldung/singletonbean/CountryStateBeanManagedBean.java @@ -19,7 +19,7 @@ public class CountryStateBeanManagedBean implements CountryState { private final Map> countryStatesMap = new HashMap>(); @PostConstruct - public synchronized void initialize() { + public void initialize() { List states = new ArrayList(); states.add("Texas"); @@ -34,4 +34,8 @@ public class CountryStateBeanManagedBean implements CountryState { public List getStates(String country) { return countryStatesMap.get(country); } + + public synchronized void setStates(String country, List states) { + countryStatesMap.put(country, states); + } } diff --git a/spring-ejb/singleton-ejb-bean/src/main/java/com/baeldung/singletonbean/CountryStateContainerManagedBean.java b/spring-ejb/ejb-beans/src/main/java/com/baeldung/singletonbean/CountryStateContainerManagedBean.java similarity index 88% rename from spring-ejb/singleton-ejb-bean/src/main/java/com/baeldung/singletonbean/CountryStateContainerManagedBean.java rename to spring-ejb/ejb-beans/src/main/java/com/baeldung/singletonbean/CountryStateContainerManagedBean.java index ee16285824..0a970f4c5c 100644 --- a/spring-ejb/singleton-ejb-bean/src/main/java/com/baeldung/singletonbean/CountryStateContainerManagedBean.java +++ b/spring-ejb/ejb-beans/src/main/java/com/baeldung/singletonbean/CountryStateContainerManagedBean.java @@ -38,4 +38,9 @@ public class CountryStateContainerManagedBean implements CountryState { public List getStates(String country) { return countryStatesMap.get(country); } + + @Lock(LockType.WRITE) + public void setStates(String country, List states) { + countryStatesMap.put(country, states); + } } diff --git a/spring-ejb/singleton-ejb-bean/src/test/java/com/baeldung/singletonbean/CountryStateCacheBeanTest.java b/spring-ejb/ejb-beans/src/test/java/com/baeldung/singletonbean/CountryStateCacheBeanTest.java similarity index 52% rename from spring-ejb/singleton-ejb-bean/src/test/java/com/baeldung/singletonbean/CountryStateCacheBeanTest.java rename to spring-ejb/ejb-beans/src/test/java/com/baeldung/singletonbean/CountryStateCacheBeanTest.java index 62412084bd..2207431702 100644 --- a/spring-ejb/singleton-ejb-bean/src/test/java/com/baeldung/singletonbean/CountryStateCacheBeanTest.java +++ b/spring-ejb/ejb-beans/src/test/java/com/baeldung/singletonbean/CountryStateCacheBeanTest.java @@ -3,6 +3,8 @@ package com.baeldung.singletonbean; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertNotNull; +import java.util.Arrays; +import java.util.Collections; import java.util.List; import javax.ejb.embeddable.EJBContainer; @@ -24,29 +26,55 @@ public class CountryStateCacheBeanTest { ejbContainer = EJBContainer.createEJBContainer(); context = ejbContainer.getContext(); } - + @Test public void whenCallGetStatesFromContainerManagedBean_ReturnsStatesForCountry() throws Exception { String[] expectedStates = { "Texas", "Alabama", "Alaska", "Arizona", "Arkansas" }; - CountryState countryStateBean = (CountryState) context.lookup("java:global/singleton-ejb-bean/CountryStateContainerManagedBean"); + CountryState countryStateBean = (CountryState) context.lookup("java:global/ejb-beans/CountryStateContainerManagedBean"); List actualStates = countryStateBean.getStates("UnitedStates"); assertNotNull(actualStates); assertArrayEquals(expectedStates, actualStates.toArray()); } - + @Test public void whenCallGetStatesFromBeanManagedBean_ReturnsStatesForCountry() throws Exception { String[] expectedStates = { "Texas", "Alabama", "Alaska", "Arizona", "Arkansas" }; - CountryState countryStateBean = (CountryState) context.lookup("java:global/singleton-ejb-bean/CountryStateBeanManagedBean"); + CountryState countryStateBean = (CountryState) context.lookup("java:global/ejb-beans/CountryStateBeanManagedBean"); List actualStates = countryStateBean.getStates("UnitedStates"); assertNotNull(actualStates); assertArrayEquals(expectedStates, actualStates.toArray()); } - + + @Test + public void whenCallSetStatesFromContainerManagedBean_SetsStatesForCountry() throws Exception { + + String[] expectedStates = { "California", "Florida", "Hawaii", "Pennsylvania", "Michigan" }; + + CountryState countryStateBean = (CountryState) context.lookup("java:global/ejb-beans/CountryStateContainerManagedBean"); + countryStateBean.setStates("UnitedStates", Arrays.asList(expectedStates)); + + List actualStates = countryStateBean.getStates("UnitedStates"); + assertNotNull(actualStates); + assertArrayEquals(expectedStates, actualStates.toArray()); + } + + @Test + public void whenCallSetStatesFromBeanManagedBean_SetsStatesForCountry() throws Exception { + + String[] expectedStates = { "California", "Florida", "Hawaii", "Pennsylvania", "Michigan" }; + + CountryState countryStateBean = (CountryState) context.lookup("java:global/ejb-beans/CountryStateBeanManagedBean"); + countryStateBean.setStates("UnitedStates", Arrays.asList(expectedStates)); + + List actualStates = countryStateBean.getStates("UnitedStates"); + assertNotNull(actualStates); + assertArrayEquals(expectedStates, actualStates.toArray()); + } + @After public void close() { if (ejbContainer != null) diff --git a/spring-ejb/pom.xml b/spring-ejb/pom.xml index bd0183d71d..188ba0b8fe 100755 --- a/spring-ejb/pom.xml +++ b/spring-ejb/pom.xml @@ -73,6 +73,6 @@ ejb-remote-for-spring spring-ejb-client - singleton-ejb-bean + ejb-beans