Skip to content

Commit ad637ed

Browse files
committed
Merge pull request #144 from rafos/master
Tests for ejb/stateless & ejb/stateful
2 parents 21c1f64 + e8ce2a9 commit ad637ed

File tree

7 files changed

+354
-99
lines changed

7 files changed

+354
-99
lines changed

ejb/stateful/src/main/java/org/javaee7/ejb/stateful/remote/CartBean.java renamed to ejb/stateful/src/main/java/org/javaee7/ejb/stateful/CartBean.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
* only if the new code is made subject to such option by the copyright
3838
* holder.
3939
*/
40-
package org.javaee7.ejb.stateful.remote;
40+
package org.javaee7.ejb.stateful;
4141

4242
import java.util.ArrayList;
4343
import java.util.List;
@@ -48,30 +48,26 @@
4848
* @author Arun Gupta
4949
*/
5050
@Stateful
51-
public class CartBean implements Cart {
51+
public class CartBean {
5252

5353
List<String> items;
5454

5555
public CartBean() {
5656
items = new ArrayList<>();
5757
}
5858

59-
@Override
6059
public void addItem(String item) {
6160
items.add(item);
6261
}
6362

64-
@Override
6563
public void removeItem(String item) {
6664
items.remove(item);
6765
}
6866

69-
@Override
7067
public void purchase() {
7168
//. . .
7269
}
7370

74-
@Override
7571
public List<String> getItems() {
7672
return items;
7773
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
*/
5454
@WebServlet(urlPatterns = {"/TestServlet"})
5555
public class TestServlet extends HttpServlet {
56-
@Inject Cart bean;;
56+
@Inject CartBean bean;;
5757

5858
/**
5959
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>

ejb/stateful/src/main/java/org/javaee7/ejb/stateful/Cart.java renamed to ejb/stateful/src/main/java/org/javaee7/ejb/stateful/remote/CartBeanWithInterface.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
* only if the new code is made subject to such option by the copyright
3838
* holder.
3939
*/
40-
package org.javaee7.ejb.stateful;
40+
package org.javaee7.ejb.stateful.remote;
4141

4242
import java.util.ArrayList;
4343
import java.util.List;
@@ -48,26 +48,30 @@
4848
* @author Arun Gupta
4949
*/
5050
@Stateful
51-
public class Cart {
51+
public class CartBeanWithInterface implements Cart {
5252

5353
List<String> items;
5454

55-
public Cart() {
55+
public CartBeanWithInterface() {
5656
items = new ArrayList<>();
5757
}
5858

59+
@Override
5960
public void addItem(String item) {
6061
items.add(item);
6162
}
6263

64+
@Override
6365
public void removeItem(String item) {
6466
items.remove(item);
6567
}
6668

69+
@Override
6770
public void purchase() {
6871
//. . .
6972
}
7073

74+
@Override
7175
public List<String> getItems() {
7276
return items;
7377
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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.shrinkwrap.api.Archive;
6+
import org.jboss.shrinkwrap.api.ShrinkWrap;
7+
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
8+
import org.jboss.shrinkwrap.api.spec.JavaArchive;
9+
import org.junit.Test;
10+
import org.junit.runner.RunWith;
11+
12+
import javax.inject.Inject;
13+
14+
import java.util.Arrays;
15+
import java.util.List;
16+
17+
import static org.hamcrest.CoreMatchers.hasItem;
18+
import static org.hamcrest.CoreMatchers.hasItems;
19+
import static org.hamcrest.CoreMatchers.is;
20+
import static org.hamcrest.CoreMatchers.not;
21+
import static org.hamcrest.MatcherAssert.assertThat;
22+
23+
/**
24+
* @author Rafał Roppel
25+
*/
26+
@RunWith(Arquillian.class)
27+
public class CartBeanTest {
28+
29+
@Inject
30+
private CartBean sut;
31+
32+
@Deployment
33+
public static Archive<?> deployment() {
34+
return ShrinkWrap.create(JavaArchive.class)
35+
.addClass(CartBean.class)
36+
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
37+
}
38+
39+
/**
40+
* Test of addItem method, of class CartBean
41+
*
42+
* @throws Exception
43+
*/
44+
@Test
45+
public void shouldAddOneItem() throws Exception {
46+
// given
47+
48+
// when
49+
sut.addItem("apple");
50+
51+
// then
52+
assertThat(sut.getItems(), hasItem("apple"));
53+
}
54+
55+
/**
56+
* Test of addItem method, of class CartBean
57+
*
58+
* @throws Exception
59+
*/
60+
@Test
61+
public void shouldAddManyItems() throws Exception {
62+
// given
63+
final List<String> items = Arrays.asList("apple", "banana", "mango", "kiwi", "passion fruit");
64+
65+
// when
66+
for (final String item : items) {
67+
sut.addItem(item);
68+
}
69+
70+
// then
71+
assertThat(sut.getItems(), is(items));
72+
}
73+
74+
/**
75+
* Test of removeItem method, of class CartBean
76+
*
77+
* @throws Exception
78+
*/
79+
@Test
80+
public void shouldRemoveOneItem() throws Exception {
81+
// given
82+
final List<String> items = Arrays.asList("apple", "banana", "mango", "kiwi", "passion fruit");
83+
for (final String item : items) {
84+
sut.addItem(item);
85+
}
86+
87+
// when
88+
sut.removeItem("banana");
89+
90+
// then
91+
assertThat(sut.getItems(), not(hasItem("banana")));
92+
}
93+
94+
/**
95+
* Test of getItems method, of class CartBean
96+
*
97+
* @throws Exception
98+
*/
99+
@Test
100+
public void shouldBeEmpty() throws Exception {
101+
// given
102+
103+
// when
104+
final List<String> actual = sut.getItems();
105+
106+
// then
107+
assertThat(actual.isEmpty(), is(true));
108+
}
109+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package org.javaee7.ejb.stateful;
2+
3+
import org.javaee7.ejb.stateful.remote.Cart;
4+
import org.javaee7.ejb.stateful.remote.CartBeanWithInterface;
5+
import org.jboss.arquillian.container.test.api.Deployment;
6+
import org.jboss.arquillian.junit.Arquillian;
7+
import org.jboss.shrinkwrap.api.Archive;
8+
import org.jboss.shrinkwrap.api.ShrinkWrap;
9+
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
10+
import org.jboss.shrinkwrap.api.spec.JavaArchive;
11+
import org.junit.Test;
12+
import org.junit.runner.RunWith;
13+
14+
import javax.ejb.EJB;
15+
16+
import java.util.Arrays;
17+
import java.util.List;
18+
19+
import static org.hamcrest.CoreMatchers.hasItem;
20+
import static org.hamcrest.CoreMatchers.hasItems;
21+
import static org.hamcrest.CoreMatchers.is;
22+
import static org.hamcrest.CoreMatchers.not;
23+
import static org.hamcrest.MatcherAssert.assertThat;
24+
25+
/**
26+
* @author rafos
27+
*/
28+
@RunWith(Arquillian.class)
29+
public class CartBeanWithInterfaceTest {
30+
31+
@EJB
32+
private Cart sut;
33+
34+
@Deployment
35+
public static Archive<?> deployment() {
36+
return ShrinkWrap.create(JavaArchive.class)
37+
.addClass(Cart.class)
38+
.addClass(CartBeanWithInterface.class)
39+
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
40+
}
41+
42+
/**
43+
* Test of addItem method, of class CartBean
44+
*
45+
* @throws Exception
46+
*/
47+
@Test
48+
public void shouldAddOneItem() throws Exception {
49+
// given
50+
51+
// when
52+
sut.addItem("apple");
53+
54+
// then
55+
assertThat(sut.getItems(), hasItem("apple"));
56+
}
57+
58+
/**
59+
* Test of addItem method, of class CartBean
60+
*
61+
* @throws Exception
62+
*/
63+
@Test
64+
public void shouldAddManyItems() throws Exception {
65+
// given
66+
final List<String> items = Arrays.asList("apple", "banana", "mango", "kiwi", "passion fruit");
67+
68+
// when
69+
for (final String item : items) {
70+
sut.addItem(item);
71+
}
72+
73+
// then
74+
assertThat(sut.getItems(), is(items));
75+
}
76+
77+
/**
78+
* Test of removeItem method, of class CartBean
79+
*
80+
* @throws Exception
81+
*/
82+
@Test
83+
public void shouldRemoveOneItem() throws Exception {
84+
// given
85+
final List<String> items = Arrays.asList("apple", "banana", "mango", "kiwi", "passion fruit");
86+
for (final String item : items) {
87+
sut.addItem(item);
88+
}
89+
90+
// when
91+
sut.removeItem("banana");
92+
93+
// then
94+
assertThat(sut.getItems(), not(hasItem("banana")));
95+
}
96+
97+
/**
98+
* Test of getItems method, of class CartBean
99+
*
100+
* @throws Exception
101+
*/
102+
@Test
103+
public void shouldBeEmpty() throws Exception {
104+
// given
105+
106+
// when
107+
final List<String> actual = sut.getItems();
108+
109+
// then
110+
assertThat(actual.isEmpty(), is(true));
111+
}
112+
}

0 commit comments

Comments
 (0)