Skip to content

Commit a983cad

Browse files
committed
added cdi/instance
1 parent 5dc6143 commit a983cad

File tree

7 files changed

+102
-0
lines changed

7 files changed

+102
-0
lines changed

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+
}
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: 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: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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.hamcrest.CoreMatchers.either;
15+
import static org.hamcrest.CoreMatchers.instanceOf;
16+
import static org.junit.Assert.assertEquals;
17+
import static org.junit.Assert.assertThat;
18+
19+
/**
20+
* @author Radim Hanus
21+
*/
22+
@RunWith(Arquillian.class)
23+
public class GreetingTest {
24+
@Deployment
25+
public static Archive<?> deploy() {
26+
return ShrinkWrap.create(JavaArchive.class)
27+
.addClasses(Greeting.class, SimpleGreeting.class, FancyGreeting.class)
28+
.addAsManifestResource("beans.xml");
29+
}
30+
31+
@Inject
32+
private Instance<Greeting> instance;
33+
34+
@Test
35+
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);
42+
}
43+
}
44+
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/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
<module>nobeans-el-injection-flowscoped</module>
3737
<module>events</module>
3838
<module>events-conditional-reception</module>
39+
<module>instance</module>
3940
</modules>
4041

4142
<dependencies>

0 commit comments

Comments
 (0)