BAEL-3592 : updated tests, added getter and setter for name (#8787)

* BAEL-3592: Comparison of Spring Beans and Java Enterprise Beans

* BAEL-3592 : updated tests, added getter and setter for name

* BAEL-3592 : removed unnecessary file
This commit is contained in:
Sampada 2020-02-29 04:57:31 +05:30 committed by GitHub
parent 0a6afb4528
commit b1feb51814
8 changed files with 88 additions and 50 deletions

View File

@ -6,9 +6,18 @@ import javax.ejb.Singleton;
public class CounterEJB implements CounterEJBRemote {
private int count = 1;
private String name;
public int count() {
return count++;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

View File

@ -5,4 +5,6 @@ import javax.ejb.Remote;
@Remote
public interface CounterEJBRemote {
int count();
String getName();
void setName(String name);
}

View File

@ -7,7 +7,7 @@ import javax.ejb.Stateful;
@Stateful
public class ShoppingCartEJB implements ShoppingCartEJBRemote {
private String name;
private List<String> shoppingCart;
public ShoppingCartEJB() {
@ -22,4 +22,11 @@ public class ShoppingCartEJB implements ShoppingCartEJBRemote {
return shoppingCart;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}

View File

@ -10,4 +10,8 @@ public interface ShoppingCartEJBRemote {
void addItem(String item);
List<String> getItems();
void setName(String name);
String getName();
}

View File

@ -5,8 +5,18 @@ import org.springframework.stereotype.Component;
@Component
public class CounterBean {
private int count = 1;
private String name;
public int count() {
return count++;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

View File

@ -11,6 +11,7 @@ import org.springframework.stereotype.Component;
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class ShoppingCartBean {
private String name;
private List<String> shoppingCart;
public ShoppingCartBean() {
@ -25,4 +26,11 @@ public class ShoppingCartBean {
return shoppingCart;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}

View File

@ -1,9 +1,7 @@
package com.baeldung.ejb.spring.comparison.ejb;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import javax.annotation.Resource;
import javax.ejb.EJB;
@ -51,7 +49,7 @@ public class EJBUnitTest {
public static void start() throws NamingException {
ejbContainer = EJBContainer.createEJBContainer();
}
@Before
public void initializeContext() throws NamingException {
context = ejbContainer.getContext();
@ -60,42 +58,44 @@ public class EJBUnitTest {
@Test
public void givenSingletonBean_whenCounterInvoked_thenCountIsIncremented() throws NamingException {
int count = 0;
CounterEJBRemote counterEJB = (CounterEJBRemote) context.lookup("java:global/ejb-beans/CounterEJB");
CounterEJBRemote firstCounter = (CounterEJBRemote) context.lookup("java:global/ejb-beans/CounterEJB");
firstCounter.setName("first");
for (int i = 0; i < 10; i++)
count = counterEJB.count();
assertThat(count, is(not(1)));
}
@Test
public void givenSingletonBean_whenCounterInvokedAgain_thenCountIsIncremented() throws NamingException {
CounterEJBRemote counterEJB = (CounterEJBRemote) context.lookup("java:global/ejb-beans/CounterEJB");
for (int i = 0; i < 10; i++) {
count = firstCounter.count();
}
int count = 0;
for (int i = 0; i < 10; i++)
count = counterEJB.count();
assertEquals(10, count);
assertEquals("first", firstCounter.getName());
CounterEJBRemote secondCounter = (CounterEJBRemote) context.lookup("java:global/ejb-beans/CounterEJB");
int count2 = 0;
for (int i = 0; i < 10; i++) {
count2 = secondCounter.count();
}
assertEquals(20, count2);
assertEquals("first", secondCounter.getName());
assertThat(count, is(not(1)));
}
@Test
public void givenStatefulBean_whenBathingCartWithThreeItemsAdded_thenItemsSizeIsThree() throws NamingException {
ShoppingCartEJBRemote bathingCart = (ShoppingCartEJBRemote) context.lookup("java:global/ejb-beans/ShoppingCartEJB");
bathingCart.setName("bathingCart");
bathingCart.addItem("soap");
bathingCart.addItem("shampoo");
bathingCart.addItem("oil");
assertEquals(3, bathingCart.getItems()
.size());
}
assertEquals("bathingCart", bathingCart.getName());
@Test
public void givenStatefulBean_whenFruitCartWithTwoItemsAdded_thenItemsSizeIsTwo() throws NamingException {
ShoppingCartEJBRemote fruitCart = (ShoppingCartEJBRemote) context.lookup("java:global/ejb-beans/ShoppingCartEJB");
fruitCart.addItem("apples");
@ -103,6 +103,7 @@ public class EJBUnitTest {
assertEquals(2, fruitCart.getItems()
.size());
assertNull(fruitCart.getName());
}
@Test
@ -131,10 +132,7 @@ public class EJBUnitTest {
}
@AfterClass
public static void checkTotalCountAndcloseContext() throws NamingException {
CounterEJBRemote counterEJB = (CounterEJBRemote) context.lookup("java:global/ejb-beans/CounterEJB");
assertEquals(21, counterEJB.count());
public static void closeContext() throws NamingException {
context.close();
ejbContainer.close();
}

View File

@ -1,9 +1,7 @@
package com.baeldung.ejb.spring.comparison.spring;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import javax.naming.NamingException;
@ -46,40 +44,44 @@ public class SpringUnitTest {
@Test
public void whenCounterInvoked_thenCountIsIncremented() throws NamingException {
CounterBean counterBean = context.getBean(CounterBean.class);
CounterBean firstCounter = context.getBean(CounterBean.class);
firstCounter.setName("first");
int count = 0;
for (int i = 0; i < 10; i++)
count = counterBean.count();
for (int i = 0; i < 10; i++) {
count = firstCounter.count();
}
assertThat(count, is(not(1)));
}
assertEquals(10, count);
assertEquals("first", firstCounter.getName());
@Test
public void whenCounterInvokedAgain_thenCountIsIncremented() throws NamingException {
CounterBean counterBean = context.getBean(CounterBean.class);
CounterBean secondCounter = context.getBean(CounterBean.class);
int count = 0;
for (int i = 0; i < 10; i++)
count = counterBean.count();
int count2 = 0;
for (int i = 0; i < 10; i++) {
count2 = secondCounter.count();
}
assertEquals(20, count2);
assertEquals("first", secondCounter.getName());
assertThat(count, is(not(1)));
}
@Test
public void whenBathingCartWithThreeItemsAdded_thenItemsSizeIsThree() throws NamingException {
ShoppingCartBean bathingCart = context.getBean(ShoppingCartBean.class);
bathingCart.setName("bathingCart");
bathingCart.addItem("soap");
bathingCart.addItem("shampoo");
bathingCart.addItem("oil");
assertEquals(3, bathingCart.getItems()
.size());
}
@Test
public void whenFruitCartWithTwoItemsAdded_thenItemsSizeIsTwo() throws NamingException {
assertEquals("bathingCart", bathingCart.getName());
ShoppingCartBean fruitCart = context.getBean(ShoppingCartBean.class);
fruitCart.addItem("apples");
@ -87,6 +89,7 @@ public class SpringUnitTest {
assertEquals(2, fruitCart.getItems()
.size());
assertNull(fruitCart.getName());
}
@Test
@ -98,10 +101,7 @@ public class SpringUnitTest {
}
@AfterClass
public static void checkTotalCountAndcloseContext() throws NamingException {
CounterBean counterBean = context.getBean(CounterBean.class);
int count = counterBean.count();
assertEquals(21, count);
public static void closeContext() throws NamingException {
context.close();
}