Skip to content

Commit 2d74f53

Browse files
Test same stateless bean on every ctx lookup
1 parent a742660 commit 2d74f53

File tree

2 files changed

+78
-1
lines changed

2 files changed

+78
-1
lines changed

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
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package org.javaee7.ejb.stateless;
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.Before;
11+
import org.junit.Test;
12+
import org.junit.runner.RunWith;
13+
14+
import javax.naming.InitialContext;
15+
import javax.naming.NamingException;
16+
17+
import static org.hamcrest.MatcherAssert.*;
18+
import static org.hamcrest.Matchers.*;
19+
20+
/**
21+
* @author Jakub Marchwicki
22+
*/
23+
@RunWith(Arquillian.class)
24+
public class AccountSessionStatelessnessTest {
25+
26+
final private float deposit_amount = 10f;
27+
private AccountSessionBean account;
28+
29+
@Deployment
30+
public static Archive<?> deployment() {
31+
return ShrinkWrap.create(JavaArchive.class, "test.jar")
32+
.addClass(AccountSessionBean.class)
33+
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
34+
}
35+
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;
51+
}
52+
53+
@Test
54+
@InSequence(1)
55+
public void should_deposit_amount() {
56+
assertThat(account.getAmount(), is(equalTo(0f)));
57+
58+
String actual = account.deposit(deposit_amount);
59+
60+
assertThat(actual, is(equalTo("Deposited: " + deposit_amount)));
61+
assertThat(account.getAmount(), is(equalTo(deposit_amount)));
62+
}
63+
64+
@Test
65+
@InSequence(2)
66+
public void should_contain_already_deposited_amount() {
67+
assertThat(account.getAmount(), is(equalTo(deposit_amount)));
68+
}
69+
}

0 commit comments

Comments
 (0)