8
8
import org .junit .Test ;
9
9
import org .junit .runner .RunWith ;
10
10
11
- import javax .enterprise .inject .Any ;
12
11
import javax .enterprise .inject .Default ;
13
12
import javax .enterprise .inject .Instance ;
14
13
import javax .enterprise .util .AnnotationLiteral ;
15
14
import javax .inject .Inject ;
16
15
17
16
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 ;
19
19
20
20
/**
21
21
* @author Radim Hanus
@@ -33,36 +33,30 @@ public static Archive<?> deploy() {
33
33
* Container will assume built-in @Default qualifier here as well as for beans that don't declare a qualifier.
34
34
*/
35
35
@ Inject
36
- private Instance <Greeting > defaultInstance ;
36
+ private Instance <Greeting > instance ;
37
37
38
38
/**
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>
40
45
*/
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
-
50
46
@ Test
51
47
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 );
66
60
}
67
61
}
68
62
0 commit comments