diff --git a/spring-ejb/singleton-ejb-bean/src/main/java/com/baeldung/singletonbean/CountryStateBeanManagedBean.java b/spring-ejb/singleton-ejb-bean/src/main/java/com/baeldung/singletonbean/CountryStateBeanManagedBean.java new file mode 100644 index 0000000000..b437dc4d7f --- /dev/null +++ b/spring-ejb/singleton-ejb-bean/src/main/java/com/baeldung/singletonbean/CountryStateBeanManagedBean.java @@ -0,0 +1,39 @@ +package com.baeldung.singletonbean; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.annotation.PostConstruct; +import javax.ejb.ConcurrencyManagement; +import javax.ejb.ConcurrencyManagementType; +import javax.ejb.Lock; +import javax.ejb.LockType; +import javax.ejb.Singleton; +import javax.ejb.Startup; + +@Singleton +@Startup +@ConcurrencyManagement(ConcurrencyManagementType.BEAN) +public class CountryStateBeanManagedBean implements CountryState { + + private volatile Map> countryStatesMap = new HashMap>(); + + @PostConstruct + public synchronized void initialize() { + + List states = new ArrayList(); + states.add("Texas"); + states.add("Alabama"); + states.add("Alaska"); + states.add("Arizona"); + states.add("Arkansas"); + + countryStatesMap.put("UnitedStates", states); + } + + public List getStates(String country) { + return countryStatesMap.get(country); + } +} diff --git a/spring-ejb/singleton-ejb-bean/src/main/java/com/baeldung/singletonbean/CountryStateCacheBean.java b/spring-ejb/singleton-ejb-bean/src/main/java/com/baeldung/singletonbean/CountryStateContainerManagedBean.java similarity index 93% rename from spring-ejb/singleton-ejb-bean/src/main/java/com/baeldung/singletonbean/CountryStateCacheBean.java rename to spring-ejb/singleton-ejb-bean/src/main/java/com/baeldung/singletonbean/CountryStateContainerManagedBean.java index 3b4d80c88c..ee16285824 100644 --- a/spring-ejb/singleton-ejb-bean/src/main/java/com/baeldung/singletonbean/CountryStateCacheBean.java +++ b/spring-ejb/singleton-ejb-bean/src/main/java/com/baeldung/singletonbean/CountryStateContainerManagedBean.java @@ -16,7 +16,7 @@ import javax.ejb.Startup; @Singleton @Startup @ConcurrencyManagement(ConcurrencyManagementType.CONTAINER) -public class CountryStateCacheBean implements CountryState { +public class CountryStateContainerManagedBean implements CountryState { private Map> countryStatesMap = new HashMap>(); diff --git a/spring-ejb/singleton-ejb-bean/src/test/java/com/baeldung/singletonbean/CountryStateCacheBeanTest.java b/spring-ejb/singleton-ejb-bean/src/test/java/com/baeldung/singletonbean/CountryStateCacheBeanTest.java index 988a4eeb6d..62412084bd 100644 --- a/spring-ejb/singleton-ejb-bean/src/test/java/com/baeldung/singletonbean/CountryStateCacheBeanTest.java +++ b/spring-ejb/singleton-ejb-bean/src/test/java/com/baeldung/singletonbean/CountryStateCacheBeanTest.java @@ -1,6 +1,7 @@ package com.baeldung.singletonbean; import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertNotNull; import java.util.List; @@ -11,8 +12,6 @@ import org.junit.After; import org.junit.Before; import org.junit.Test; -import com.baeldung.singletonbean.CountryState; - public class CountryStateCacheBeanTest { private EJBContainer ejbContainer = null; @@ -27,15 +26,25 @@ public class CountryStateCacheBeanTest { } @Test - public void whenCallGetStates_ReturnsStatesForCountry() throws Exception { + public void whenCallGetStatesFromContainerManagedBean_ReturnsStatesForCountry() throws Exception { - String[] actualStates = { "Texas", "Alabama", "Alaska", "Arizona", "Arkansas" }; + String[] expectedStates = { "Texas", "Alabama", "Alaska", "Arizona", "Arkansas" }; - CountryState countryStateBean = (CountryState) context.lookup("java:global/singleton-ejb-bean/CountryStateCacheBean"); - List states = countryStateBean.getStates("UnitedStates"); - if (states != null) { - assertArrayEquals(states.toArray(), actualStates); - } + CountryState countryStateBean = (CountryState) context.lookup("java:global/singleton-ejb-bean/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"); + List actualStates = countryStateBean.getStates("UnitedStates"); + assertNotNull(actualStates); + assertArrayEquals(expectedStates, actualStates.toArray()); } @After