Skip to content

Commit 0b69631

Browse files
committed
Merge pull request #305 from rhanus/master
added cdi/instance
2 parents 5dc6143 + 70bf0f8 commit 0b69631

File tree

15 files changed

+263
-0
lines changed

15 files changed

+263
-0
lines changed

cdi/instance-qualifiers/pom.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<parent>
8+
<artifactId>cdi</artifactId>
9+
<groupId>org.javaee7</groupId>
10+
<version>1.0-SNAPSHOT</version>
11+
<relativePath>../pom.xml</relativePath>
12+
</parent>
13+
14+
<artifactId>cdi-instance-qualifiers</artifactId>
15+
<name>Java EE 7 Sample: cdi/instance-qualifiers</name>
16+
17+
</project>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.javaee7.cdi.instance;
2+
3+
import javax.inject.Qualifier;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.Target;
6+
7+
import static java.lang.annotation.ElementType.*;
8+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
9+
10+
/**
11+
* @author Radim Hanus
12+
*/
13+
@Qualifier
14+
@Retention(RUNTIME)
15+
@Target({TYPE, METHOD, FIELD, PARAMETER})
16+
public @interface Business {
17+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.javaee7.cdi.instance;
2+
3+
/**
4+
* @author Radim Hanus
5+
*/
6+
@Business
7+
public class FormalGreeting implements Greeting {
8+
@Override
9+
public String greet(String name) {
10+
return "Good morning " + name;
11+
}
12+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.javaee7.cdi.instance;
2+
3+
/**
4+
* @author Arun Gupta
5+
* @author Radim Hanus
6+
*/
7+
public interface Greeting {
8+
String greet(String name);
9+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.javaee7.cdi.instance;
2+
3+
import javax.inject.Qualifier;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.Target;
6+
7+
import static java.lang.annotation.ElementType.*;
8+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
9+
10+
/**
11+
* @author Radim Hanus
12+
*/
13+
@Qualifier
14+
@Retention(RUNTIME)
15+
@Target({TYPE, METHOD, FIELD, PARAMETER})
16+
public @interface Personal {
17+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.javaee7.cdi.instance;
2+
3+
/**
4+
* @author Arun Gupta
5+
* @author Radim Hanus
6+
*/
7+
public class SimpleGreeting implements Greeting {
8+
@Override
9+
public String greet(String name) {
10+
return "Hello " + name;
11+
}
12+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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 GreetingTest {
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+
* Container will assume built-in @Default qualifier here as well as for beans that don't declare a qualifier.
34+
*/
35+
@Inject
36+
private Instance<Greeting> defaultInstance;
37+
38+
/**
39+
* Qualifier @Personal is not qualifying any bean.
40+
*/
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+
@Test
51+
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));
66+
}
67+
}
68+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
6+
bean-discovery-mode="all">
7+
8+
</beans>

cdi/instance/pom.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<parent>
8+
<artifactId>cdi</artifactId>
9+
<groupId>org.javaee7</groupId>
10+
<version>1.0-SNAPSHOT</version>
11+
</parent>
12+
13+
<artifactId>cdi-instance</artifactId>
14+
<name>Java EE 7 Sample: cdi/instance</name>
15+
16+
</project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.javaee7.cdi.instance;
2+
3+
/**
4+
* @author Arun Gupta
5+
* @author Radim Hanus
6+
*/
7+
public class FancyGreeting implements Greeting {
8+
@Override
9+
public String greet(String name) {
10+
return "Nice to meet you, hello" + name;
11+
}
12+
}

0 commit comments

Comments
 (0)