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