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:
parent
0a6afb4528
commit
b1feb51814
|
@ -6,9 +6,18 @@ import javax.ejb.Singleton;
|
||||||
public class CounterEJB implements CounterEJBRemote {
|
public class CounterEJB implements CounterEJBRemote {
|
||||||
|
|
||||||
private int count = 1;
|
private int count = 1;
|
||||||
|
private String name;
|
||||||
|
|
||||||
public int count() {
|
public int count() {
|
||||||
return count++;
|
return count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -5,4 +5,6 @@ import javax.ejb.Remote;
|
||||||
@Remote
|
@Remote
|
||||||
public interface CounterEJBRemote {
|
public interface CounterEJBRemote {
|
||||||
int count();
|
int count();
|
||||||
|
String getName();
|
||||||
|
void setName(String name);
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@ import javax.ejb.Stateful;
|
||||||
|
|
||||||
@Stateful
|
@Stateful
|
||||||
public class ShoppingCartEJB implements ShoppingCartEJBRemote {
|
public class ShoppingCartEJB implements ShoppingCartEJBRemote {
|
||||||
|
private String name;
|
||||||
private List<String> shoppingCart;
|
private List<String> shoppingCart;
|
||||||
|
|
||||||
public ShoppingCartEJB() {
|
public ShoppingCartEJB() {
|
||||||
|
@ -22,4 +22,11 @@ public class ShoppingCartEJB implements ShoppingCartEJBRemote {
|
||||||
return shoppingCart;
|
return shoppingCart;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,4 +10,8 @@ public interface ShoppingCartEJBRemote {
|
||||||
void addItem(String item);
|
void addItem(String item);
|
||||||
|
|
||||||
List<String> getItems();
|
List<String> getItems();
|
||||||
|
|
||||||
|
void setName(String name);
|
||||||
|
|
||||||
|
String getName();
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,8 +5,18 @@ import org.springframework.stereotype.Component;
|
||||||
@Component
|
@Component
|
||||||
public class CounterBean {
|
public class CounterBean {
|
||||||
private int count = 1;
|
private int count = 1;
|
||||||
|
private String name;
|
||||||
|
|
||||||
public int count() {
|
public int count() {
|
||||||
return count++;
|
return count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,6 +11,7 @@ import org.springframework.stereotype.Component;
|
||||||
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
|
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
|
||||||
public class ShoppingCartBean {
|
public class ShoppingCartBean {
|
||||||
|
|
||||||
|
private String name;
|
||||||
private List<String> shoppingCart;
|
private List<String> shoppingCart;
|
||||||
|
|
||||||
public ShoppingCartBean() {
|
public ShoppingCartBean() {
|
||||||
|
@ -25,4 +26,11 @@ public class ShoppingCartBean {
|
||||||
return shoppingCart;
|
return shoppingCart;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,7 @@
|
||||||
package com.baeldung.ejb.spring.comparison.ejb;
|
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.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import javax.ejb.EJB;
|
import javax.ejb.EJB;
|
||||||
|
@ -62,40 +60,42 @@ public class EJBUnitTest {
|
||||||
public void givenSingletonBean_whenCounterInvoked_thenCountIsIncremented() throws NamingException {
|
public void givenSingletonBean_whenCounterInvoked_thenCountIsIncremented() throws NamingException {
|
||||||
|
|
||||||
int count = 0;
|
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++)
|
for (int i = 0; i < 10; i++) {
|
||||||
count = counterEJB.count();
|
count = firstCounter.count();
|
||||||
|
|
||||||
assertThat(count, is(not(1)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
assertEquals(10, count);
|
||||||
public void givenSingletonBean_whenCounterInvokedAgain_thenCountIsIncremented() throws NamingException {
|
assertEquals("first", firstCounter.getName());
|
||||||
|
|
||||||
CounterEJBRemote counterEJB = (CounterEJBRemote) context.lookup("java:global/ejb-beans/CounterEJB");
|
CounterEJBRemote secondCounter = (CounterEJBRemote) context.lookup("java:global/ejb-beans/CounterEJB");
|
||||||
|
|
||||||
int count = 0;
|
int count2 = 0;
|
||||||
for (int i = 0; i < 10; i++)
|
for (int i = 0; i < 10; i++) {
|
||||||
count = counterEJB.count();
|
count2 = secondCounter.count();
|
||||||
|
}
|
||||||
|
|
||||||
|
assertEquals(20, count2);
|
||||||
|
assertEquals("first", secondCounter.getName());
|
||||||
|
|
||||||
assertThat(count, is(not(1)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenStatefulBean_whenBathingCartWithThreeItemsAdded_thenItemsSizeIsThree() throws NamingException {
|
public void givenStatefulBean_whenBathingCartWithThreeItemsAdded_thenItemsSizeIsThree() throws NamingException {
|
||||||
ShoppingCartEJBRemote bathingCart = (ShoppingCartEJBRemote) context.lookup("java:global/ejb-beans/ShoppingCartEJB");
|
ShoppingCartEJBRemote bathingCart = (ShoppingCartEJBRemote) context.lookup("java:global/ejb-beans/ShoppingCartEJB");
|
||||||
|
|
||||||
|
bathingCart.setName("bathingCart");
|
||||||
|
|
||||||
bathingCart.addItem("soap");
|
bathingCart.addItem("soap");
|
||||||
bathingCart.addItem("shampoo");
|
bathingCart.addItem("shampoo");
|
||||||
bathingCart.addItem("oil");
|
bathingCart.addItem("oil");
|
||||||
|
|
||||||
assertEquals(3, bathingCart.getItems()
|
assertEquals(3, bathingCart.getItems()
|
||||||
.size());
|
.size());
|
||||||
}
|
assertEquals("bathingCart", bathingCart.getName());
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenStatefulBean_whenFruitCartWithTwoItemsAdded_thenItemsSizeIsTwo() throws NamingException {
|
|
||||||
ShoppingCartEJBRemote fruitCart = (ShoppingCartEJBRemote) context.lookup("java:global/ejb-beans/ShoppingCartEJB");
|
ShoppingCartEJBRemote fruitCart = (ShoppingCartEJBRemote) context.lookup("java:global/ejb-beans/ShoppingCartEJB");
|
||||||
|
|
||||||
fruitCart.addItem("apples");
|
fruitCart.addItem("apples");
|
||||||
|
@ -103,6 +103,7 @@ public class EJBUnitTest {
|
||||||
|
|
||||||
assertEquals(2, fruitCart.getItems()
|
assertEquals(2, fruitCart.getItems()
|
||||||
.size());
|
.size());
|
||||||
|
assertNull(fruitCart.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -131,10 +132,7 @@ public class EJBUnitTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@AfterClass
|
@AfterClass
|
||||||
public static void checkTotalCountAndcloseContext() throws NamingException {
|
public static void closeContext() throws NamingException {
|
||||||
CounterEJBRemote counterEJB = (CounterEJBRemote) context.lookup("java:global/ejb-beans/CounterEJB");
|
|
||||||
assertEquals(21, counterEJB.count());
|
|
||||||
|
|
||||||
context.close();
|
context.close();
|
||||||
ejbContainer.close();
|
ejbContainer.close();
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,7 @@
|
||||||
package com.baeldung.ejb.spring.comparison.spring;
|
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.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||||
|
|
||||||
import javax.naming.NamingException;
|
import javax.naming.NamingException;
|
||||||
|
|
||||||
|
@ -46,40 +44,44 @@ public class SpringUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenCounterInvoked_thenCountIsIncremented() throws NamingException {
|
public void whenCounterInvoked_thenCountIsIncremented() throws NamingException {
|
||||||
CounterBean counterBean = context.getBean(CounterBean.class);
|
|
||||||
|
|
||||||
|
CounterBean firstCounter = context.getBean(CounterBean.class);
|
||||||
|
firstCounter.setName("first");
|
||||||
int count = 0;
|
int count = 0;
|
||||||
for (int i = 0; i < 10; i++)
|
for (int i = 0; i < 10; i++) {
|
||||||
count = counterBean.count();
|
count = firstCounter.count();
|
||||||
|
|
||||||
assertThat(count, is(not(1)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
assertEquals(10, count);
|
||||||
public void whenCounterInvokedAgain_thenCountIsIncremented() throws NamingException {
|
assertEquals("first", firstCounter.getName());
|
||||||
CounterBean counterBean = context.getBean(CounterBean.class);
|
|
||||||
|
|
||||||
int count = 0;
|
CounterBean secondCounter = context.getBean(CounterBean.class);
|
||||||
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
|
@Test
|
||||||
public void whenBathingCartWithThreeItemsAdded_thenItemsSizeIsThree() throws NamingException {
|
public void whenBathingCartWithThreeItemsAdded_thenItemsSizeIsThree() throws NamingException {
|
||||||
ShoppingCartBean bathingCart = context.getBean(ShoppingCartBean.class);
|
ShoppingCartBean bathingCart = context.getBean(ShoppingCartBean.class);
|
||||||
|
|
||||||
|
bathingCart.setName("bathingCart");
|
||||||
|
|
||||||
bathingCart.addItem("soap");
|
bathingCart.addItem("soap");
|
||||||
bathingCart.addItem("shampoo");
|
bathingCart.addItem("shampoo");
|
||||||
bathingCart.addItem("oil");
|
bathingCart.addItem("oil");
|
||||||
|
|
||||||
assertEquals(3, bathingCart.getItems()
|
assertEquals(3, bathingCart.getItems()
|
||||||
.size());
|
.size());
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
assertEquals("bathingCart", bathingCart.getName());
|
||||||
public void whenFruitCartWithTwoItemsAdded_thenItemsSizeIsTwo() throws NamingException {
|
|
||||||
ShoppingCartBean fruitCart = context.getBean(ShoppingCartBean.class);
|
ShoppingCartBean fruitCart = context.getBean(ShoppingCartBean.class);
|
||||||
|
|
||||||
fruitCart.addItem("apples");
|
fruitCart.addItem("apples");
|
||||||
|
@ -87,6 +89,7 @@ public class SpringUnitTest {
|
||||||
|
|
||||||
assertEquals(2, fruitCart.getItems()
|
assertEquals(2, fruitCart.getItems()
|
||||||
.size());
|
.size());
|
||||||
|
assertNull(fruitCart.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -98,10 +101,7 @@ public class SpringUnitTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@AfterClass
|
@AfterClass
|
||||||
public static void checkTotalCountAndcloseContext() throws NamingException {
|
public static void closeContext() throws NamingException {
|
||||||
CounterBean counterBean = context.getBean(CounterBean.class);
|
|
||||||
int count = counterBean.count();
|
|
||||||
assertEquals(21, count);
|
|
||||||
context.close();
|
context.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue