Skip to content

Commit b243e10

Browse files
committed
Merge pull request #166 from kubamarchwicki/ejb_stateless_stateful
#75 EJB stateless and stateful tests
2 parents 21558bc + 7d6225d commit b243e10

File tree

7 files changed

+149
-376
lines changed

7 files changed

+149
-376
lines changed

ejb/stateful/src/main/java/org/javaee7/ejb/stateful/TestServlet.java

Lines changed: 0 additions & 136 deletions
This file was deleted.

ejb/stateful/src/main/webapp/index.jsp

Lines changed: 0 additions & 57 deletions
This file was deleted.
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package org.javaee7.ejb.stateful;
2+
3+
import org.jboss.arquillian.container.test.api.Deployment;
4+
import org.jboss.arquillian.junit.Arquillian;
5+
import org.jboss.arquillian.junit.InSequence;
6+
import org.jboss.shrinkwrap.api.Archive;
7+
import org.jboss.shrinkwrap.api.ShrinkWrap;
8+
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
9+
import org.jboss.shrinkwrap.api.spec.JavaArchive;
10+
import org.junit.Test;
11+
import org.junit.runner.RunWith;
12+
13+
import javax.ejb.EJB;
14+
15+
import static org.hamcrest.CoreMatchers.*;
16+
import static org.hamcrest.MatcherAssert.*;
17+
18+
/**
19+
* @author Jakub Marchwicki
20+
*/
21+
@RunWith(Arquillian.class)
22+
public class CartBeanStatefulnessTest {
23+
24+
final private String item_to_add = "apple";
25+
26+
@EJB
27+
private CartBean bean1;
28+
29+
@EJB
30+
private CartBean bean2;
31+
32+
@Deployment
33+
public static Archive<?> deployment() {
34+
return ShrinkWrap.create(JavaArchive.class, "test.jar")
35+
.addClass(CartBean.class)
36+
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
37+
}
38+
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)));
53+
}
54+
55+
@Test
56+
@InSequence(2)
57+
public void should_add_items_to_first_cart() {
58+
// when
59+
bean1.addItem(item_to_add);
60+
61+
// then
62+
assertThat(bean1.getItems(), hasItem(item_to_add));
63+
}
64+
65+
@Test
66+
@InSequence(3)
67+
public void should_not_contain_any_items_in_second_cart() {
68+
assertThat(bean2.getItems().isEmpty(), is(true));
69+
}
70+
}

ejb/stateless/src/main/java/org/javaee7/ejb/stateless/AccountSessionBean.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,19 @@
4747
@Stateless
4848
public class AccountSessionBean {
4949

50+
private float amount = 0;
51+
5052
public String withdraw(float amount) {
53+
this.amount -= amount;
5154
return "Withdrawn: " + amount;
5255
}
5356

54-
public String deposit(float amount) {
57+
public String deposit(float amount) {
58+
this.amount += amount;
5559
return "Deposited: " + amount;
5660
}
61+
62+
public float getAmount() {
63+
return this.amount;
64+
}
5765
}

0 commit comments

Comments
 (0)