Skip to content

Commit b3dd29b

Browse files
committed
Merge pull request javaee-samples#307 from rhanus/master
fixed test of both cdi/instance and cdi/instance-qualifiers in accord…
2 parents 2f4e791 + c7e69f5 commit b3dd29b

File tree

6 files changed

+131
-35
lines changed

6 files changed

+131
-35
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package org.javaee7.cdi.instance;
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.spec.JavaArchive;
8+
import org.junit.Test;
9+
import org.junit.runner.RunWith;
10+
11+
import javax.enterprise.inject.Any;
12+
import javax.enterprise.inject.Default;
13+
import javax.enterprise.inject.Instance;
14+
import javax.enterprise.util.AnnotationLiteral;
15+
import javax.inject.Inject;
16+
17+
import static org.hamcrest.CoreMatchers.instanceOf;
18+
import static org.junit.Assert.*;
19+
20+
/**
21+
* @author Radim Hanus
22+
*/
23+
@RunWith(Arquillian.class)
24+
public class AnyGreetingTest {
25+
@Deployment
26+
public static Archive<?> deploy() {
27+
return ShrinkWrap.create(JavaArchive.class)
28+
.addClasses(Greeting.class, SimpleGreeting.class, FormalGreeting.class, Business.class, Personal.class)
29+
.addAsManifestResource("beans.xml");
30+
}
31+
32+
/**
33+
* Built-in qualifier @Any is assumed on each bean regardless other qualifiers specified.
34+
*/
35+
@Inject @Any
36+
private Instance<Greeting> instance;
37+
38+
/**
39+
* Both bean instances of Greeting interface should be available.<br/>
40+
*
41+
* When dependent scoped bean is retrieved via an instance then explicit destroy action should be taken.
42+
* This is a known memory leak in CDI 1.0 fixed in CDI 1.1 see the link bellow for details.
43+
*
44+
* @see <a href="https://issues.jboss.org/browse/CDI-139">CDI-139</a>
45+
*/
46+
@Test
47+
public void test() throws Exception {
48+
assertFalse(instance.isUnsatisfied());
49+
assertTrue(instance.isAmbiguous());
50+
51+
// use Instance<T>#select()
52+
Greeting businessBean = instance.select(new AnnotationLiteral<Business>() {}).get();
53+
assertThat(businessBean, instanceOf(FormalGreeting.class));
54+
instance.destroy(businessBean);
55+
56+
Greeting defaultBean = instance.select(new AnnotationLiteral<Default>() {}).get();
57+
assertThat(defaultBean, instanceOf(SimpleGreeting.class));
58+
instance.destroy(defaultBean);
59+
}
60+
}
61+

cdi/instance-qualifiers/src/test/java/org/javaee7/cdi/instance/GreetingTest.java

+21-27
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
import org.junit.Test;
99
import org.junit.runner.RunWith;
1010

11-
import javax.enterprise.inject.Any;
1211
import javax.enterprise.inject.Default;
1312
import javax.enterprise.inject.Instance;
1413
import javax.enterprise.util.AnnotationLiteral;
1514
import javax.inject.Inject;
1615

1716
import static org.hamcrest.CoreMatchers.instanceOf;
18-
import static org.junit.Assert.*;
17+
import static org.junit.Assert.assertFalse;
18+
import static org.junit.Assert.assertThat;
1919

2020
/**
2121
* @author Radim Hanus
@@ -33,36 +33,30 @@ public static Archive<?> deploy() {
3333
* Container will assume built-in @Default qualifier here as well as for beans that don't declare a qualifier.
3434
*/
3535
@Inject
36-
private Instance<Greeting> defaultInstance;
36+
private Instance<Greeting> instance;
3737

3838
/**
39-
* Qualifier @Personal is not qualifying any bean.
39+
* Only instance of SimpleGreeting class should be available.<br/>
40+
*
41+
* When dependent scoped bean is retrieved via an instance then explicit destroy action should be taken.
42+
* This is a known memory leak in CDI 1.0 fixed in CDI 1.1 see the link bellow for details.
43+
*
44+
* @see <a href="https://issues.jboss.org/browse/CDI-139">CDI-139</a>
4045
*/
41-
@Inject @Personal
42-
private Instance<Greeting> personalInstance;
43-
44-
/**
45-
* Built-in qualifier @Any is assumed on each bean regardless other qualifiers specified.
46-
*/
47-
@Inject @Any
48-
private Instance<Greeting> anyInstance;
49-
5046
@Test
5147
public void test() throws Exception {
52-
// only SimpleGreeting instance should be available
53-
assertFalse(defaultInstance.isUnsatisfied());
54-
assertFalse(defaultInstance.isAmbiguous());
55-
assertThat(defaultInstance.get(), instanceOf(SimpleGreeting.class));
56-
assertThat(defaultInstance.select(new AnnotationLiteral<Default>() {}).get(), instanceOf(SimpleGreeting.class));
57-
58-
// no instance should be available
59-
assertTrue(personalInstance.isUnsatisfied());
60-
61-
// both Greeting instances should be available
62-
assertFalse(anyInstance.isUnsatisfied());
63-
assertTrue(anyInstance.isAmbiguous());
64-
assertThat(anyInstance.select(new AnnotationLiteral<Business>() {}).get(), instanceOf(FormalGreeting.class));
65-
assertThat(anyInstance.select(new AnnotationLiteral<Default>() {}).get(), instanceOf(SimpleGreeting.class));
48+
assertFalse(instance.isUnsatisfied());
49+
assertFalse(instance.isAmbiguous());
50+
51+
// use Instance<T>#get()
52+
Greeting bean = instance.get();
53+
assertThat(bean, instanceOf(SimpleGreeting.class));
54+
instance.destroy(bean);
55+
56+
// use Instance<T>#select()
57+
Greeting anotherBean = instance.select(new AnnotationLiteral<Default>() {}).get();
58+
assertThat(anotherBean, instanceOf(SimpleGreeting.class));
59+
instance.destroy(anotherBean);
6660
}
6761
}
6862

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package org.javaee7.cdi.instance;
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.spec.JavaArchive;
8+
import org.junit.Test;
9+
import org.junit.runner.RunWith;
10+
11+
import javax.enterprise.inject.Instance;
12+
import javax.inject.Inject;
13+
14+
import static org.junit.Assert.assertTrue;
15+
16+
/**
17+
* @author Radim Hanus
18+
*/
19+
@RunWith(Arquillian.class)
20+
public class PersonalGreetingTest {
21+
@Deployment
22+
public static Archive<?> deploy() {
23+
return ShrinkWrap.create(JavaArchive.class)
24+
.addClasses(Greeting.class, SimpleGreeting.class, FormalGreeting.class, Business.class, Personal.class)
25+
.addAsManifestResource("beans.xml");
26+
}
27+
28+
/**
29+
* Qualifier @Personal is not qualifying any bean.
30+
*/
31+
@Inject @Personal
32+
private Instance<Greeting> instance;
33+
34+
@Test
35+
public void test() throws Exception {
36+
// no instance should be available
37+
assertTrue(instance.isUnsatisfied());
38+
}
39+
}
40+

cdi/instance/src/main/java/org/javaee7/cdi/instance/FancyGreeting.java

+3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package org.javaee7.cdi.instance;
22

3+
import javax.enterprise.context.RequestScoped;
4+
35
/**
46
* @author Arun Gupta
57
* @author Radim Hanus
68
*/
9+
@RequestScoped
710
public class FancyGreeting implements Greeting {
811
@Override
912
public String greet(String name) {

cdi/instance/src/main/java/org/javaee7/cdi/instance/SimpleGreeting.java

+3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package org.javaee7.cdi.instance;
22

3+
import javax.enterprise.context.RequestScoped;
4+
35
/**
46
* @author Arun Gupta
57
* @author Radim Hanus
68
*/
9+
@RequestScoped
710
public class SimpleGreeting implements Greeting {
811
@Override
912
public String greet(String name) {

cdi/instance/src/test/java/org/javaee7/cdi/instance/GreetingTest.java

+3-8
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@
1111
import javax.enterprise.inject.Instance;
1212
import javax.inject.Inject;
1313

14-
import static org.hamcrest.CoreMatchers.either;
1514
import static org.hamcrest.CoreMatchers.instanceOf;
16-
import static org.junit.Assert.assertEquals;
15+
import static org.hamcrest.Matchers.containsInAnyOrder;
1716
import static org.junit.Assert.assertThat;
1817

1918
/**
@@ -33,12 +32,8 @@ public static Archive<?> deploy() {
3332

3433
@Test
3534
public void test() throws Exception {
36-
int instanceCount = 0;
37-
for (Greeting greeting : instance) {
38-
assertThat(greeting, either(instanceOf(SimpleGreeting.class)).or(instanceOf(FancyGreeting.class)));
39-
instanceCount++;
40-
}
41-
assertEquals(instanceCount, 2);
35+
// there should be both request scoped bean instances available
36+
assertThat(instance, containsInAnyOrder(instanceOf(SimpleGreeting.class), instanceOf(FancyGreeting.class)));
4237
}
4338
}
4439

0 commit comments

Comments
 (0)