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
@@ -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();
}
@@ -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();
}