Skip to content

Commit 7d6225d

Browse files
Favouring injection over InitialContext for #75 EJB stateless and statefull tests
1 parent 2d74f53 commit 7d6225d

File tree

2 files changed

+56
-52
lines changed

2 files changed

+56
-52
lines changed

ejb/stateful/src/test/java/org/javaee7/ejb/stateful/CartBeanStatefulnessTest.java

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@
77
import org.jboss.shrinkwrap.api.ShrinkWrap;
88
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
99
import org.jboss.shrinkwrap.api.spec.JavaArchive;
10-
import org.junit.Before;
1110
import org.junit.Test;
1211
import org.junit.runner.RunWith;
1312

14-
import javax.naming.InitialContext;
15-
import javax.naming.NamingException;
13+
import javax.ejb.EJB;
1614

1715
import static org.hamcrest.CoreMatchers.*;
1816
import static org.hamcrest.MatcherAssert.*;
@@ -24,7 +22,12 @@
2422
public class CartBeanStatefulnessTest {
2523

2624
final private String item_to_add = "apple";
27-
private CartBean cartBean;
25+
26+
@EJB
27+
private CartBean bean1;
28+
29+
@EJB
30+
private CartBean bean2;
2831

2932
@Deployment
3033
public static Archive<?> deployment() {
@@ -33,35 +36,35 @@ public static Archive<?> deployment() {
3336
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
3437
}
3538

36-
@Before
37-
public void setup() throws NamingException {
38-
InitialContext ctx = new InitialContext();
39-
Object object = ctx.lookup("java:global/test/CartBean");
40-
assertThat(object, instanceOf(CartBean.class));
41-
42-
CartBean cartBean = (CartBean) object;
43-
if (this.cartBean != null) {
44-
assertThat("Expect different instances",
45-
cartBean.hashCode(),
46-
is(not(equalTo(this.cartBean.hashCode()))));
47-
}
48-
49-
this.cartBean = cartBean;
39+
/**
40+
* JSR 318: Enterprise JavaBeans, Version 3.1
41+
* 3.4.7.1 Session Object Identity / Stateful Session Beans
42+
*
43+
* A stateful session object has a unique identity that is assigned by
44+
* the container at the time the object is created. A client of the stateful
45+
* session bean business interface can determine if two business interface
46+
* or no-interface view references refer to the same session object
47+
* by use of the equals method
48+
*/
49+
@Test
50+
@InSequence(1)
51+
public void should_not_be_identical_beans() {
52+
assertThat("Expect different instances", bean1, is(not(bean2)));
5053
}
5154

5255
@Test
53-
@InSequence(1)
54-
public void should_add_items_to_cart() {
56+
@InSequence(2)
57+
public void should_add_items_to_first_cart() {
5558
// when
56-
cartBean.addItem(item_to_add);
59+
bean1.addItem(item_to_add);
5760

5861
// then
59-
assertThat(cartBean.getItems(), hasItem(item_to_add));
62+
assertThat(bean1.getItems(), hasItem(item_to_add));
6063
}
6164

6265
@Test
63-
@InSequence(2)
64-
public void should_not_contain_any_items() {
65-
assertThat(cartBean.getItems().isEmpty(), is(true));
66+
@InSequence(3)
67+
public void should_not_contain_any_items_in_second_cart() {
68+
assertThat(bean2.getItems().isEmpty(), is(true));
6669
}
6770
}

ejb/stateless/src/test/java/org/javaee7/ejb/stateless/AccountSessionStatelessnessTest.java

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@
77
import org.jboss.shrinkwrap.api.ShrinkWrap;
88
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
99
import org.jboss.shrinkwrap.api.spec.JavaArchive;
10-
import org.junit.Before;
1110
import org.junit.Test;
1211
import org.junit.runner.RunWith;
1312

14-
import javax.naming.InitialContext;
15-
import javax.naming.NamingException;
13+
import javax.ejb.EJB;
1614

1715
import static org.hamcrest.MatcherAssert.*;
1816
import static org.hamcrest.Matchers.*;
@@ -24,7 +22,12 @@
2422
public class AccountSessionStatelessnessTest {
2523

2624
final private float deposit_amount = 10f;
27-
private AccountSessionBean account;
25+
26+
@EJB
27+
AccountSessionBean account1;
28+
29+
@EJB
30+
AccountSessionBean account2;
2831

2932
@Deployment
3033
public static Archive<?> deployment() {
@@ -33,37 +36,35 @@ public static Archive<?> deployment() {
3336
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
3437
}
3538

36-
@Before
37-
public void setup() throws NamingException {
38-
InitialContext ctx = new InitialContext();
39-
Object object = ctx.lookup("java:global/test/AccountSessionBean");
40-
41-
assertThat(object, instanceOf(AccountSessionBean.class));
42-
43-
AccountSessionBean account = (AccountSessionBean) object;
44-
if (this.account != null) {
45-
assertThat("Expect same instance",
46-
account.hashCode(),
47-
is(equalTo(this.account.hashCode())));
48-
}
49-
50-
this.account = account;
39+
/**
40+
* JSR 318: Enterprise JavaBeans, Version 3.1
41+
* 3.4.7.2 Session Object Identity / Stateless Session Beans
42+
*
43+
* All business object references of the same interface type for the same
44+
* stateless session bean have the same object identity, which is assigned
45+
* by the container. All references to the no-interface view of the same
46+
* stateless session bean have the same object identity.
47+
*/
48+
@Test
49+
@InSequence(1)
50+
public void should_be_identical_beans() {
51+
assertThat("Expect same instances", account1, is(account2));
5152
}
5253

5354
@Test
54-
@InSequence(1)
55-
public void should_deposit_amount() {
56-
assertThat(account.getAmount(), is(equalTo(0f)));
55+
@InSequence(2)
56+
public void should_deposit_amount_on_first_account() {
57+
assertThat(account1.getAmount(), is(equalTo(0f)));
5758

58-
String actual = account.deposit(deposit_amount);
59+
String actual = account1.deposit(deposit_amount);
5960

6061
assertThat(actual, is(equalTo("Deposited: " + deposit_amount)));
61-
assertThat(account.getAmount(), is(equalTo(deposit_amount)));
62+
assertThat(account1.getAmount(), is(equalTo(deposit_amount)));
6263
}
6364

6465
@Test
65-
@InSequence(2)
66-
public void should_contain_already_deposited_amount() {
67-
assertThat(account.getAmount(), is(equalTo(deposit_amount)));
66+
@InSequence(3)
67+
public void should_contain_already_deposited_amount_on_second_account() {
68+
assertThat(account2.getAmount(), is(equalTo(deposit_amount)));
6869
}
6970
}

0 commit comments

Comments
 (0)