Skip to content

Commit e7c9cca

Browse files
authored
Merge pull request #15 from MattGill98/cdi-discovery-modes
Added bean-discovery-mode tests
2 parents bf2d498 + 4c7b7c5 commit e7c9cca

File tree

11 files changed

+217
-0
lines changed

11 files changed

+217
-0
lines changed

cdi/bean-discovery-modes/pom.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<parent>
6+
<groupId>org.javaee8</groupId>
7+
<artifactId>cdi</artifactId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
11+
<artifactId>bean-discovery-modes</artifactId>
12+
<packaging>war</packaging>
13+
14+
<name>Java EE 8 Samples: CDI - Discovery Modes</name>
15+
</project>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package org.javaee8.cdi.bean.discovery.disabled;
2+
3+
public class CdiDisabledBean {
4+
5+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package org.javaee8.cdi.bean.discovery.enabled;
2+
3+
import javax.enterprise.context.RequestScoped;
4+
5+
@RequestScoped
6+
public class CdiEnabledBean {
7+
8+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package org.javaee8.cdi.bean.discovery.all;
2+
3+
import static org.junit.Assert.assertFalse;
4+
5+
import java.util.Set;
6+
7+
import javax.enterprise.inject.spi.Bean;
8+
import javax.enterprise.inject.spi.BeanManager;
9+
import javax.inject.Inject;
10+
11+
import org.javaee8.cdi.bean.discovery.disabled.CdiDisabledBean;
12+
import org.javaee8.cdi.bean.discovery.enabled.CdiEnabledBean;
13+
import org.jboss.arquillian.container.test.api.Deployment;
14+
import org.jboss.arquillian.junit.Arquillian;
15+
import org.jboss.shrinkwrap.api.Archive;
16+
import org.jboss.shrinkwrap.api.ShrinkWrap;
17+
import org.jboss.shrinkwrap.api.spec.WebArchive;
18+
import org.junit.Test;
19+
import org.junit.runner.RunWith;
20+
21+
@RunWith(Arquillian.class)
22+
public class CdiEnabledTest {
23+
24+
@Deployment
25+
public static Archive<?> deploy() {
26+
return ShrinkWrap.create(WebArchive.class).addClasses(CdiDisabledBean.class, CdiEnabledBean.class)
27+
.addAsWebInfResource("all-beans.xml", "beans.xml");
28+
}
29+
30+
@Inject
31+
BeanManager beanManager;
32+
33+
/**
34+
* Both of the beans should be found.
35+
*/
36+
@Test
37+
public void should_beans_be_injected() throws Exception {
38+
Set<Bean<?>> disabledBeans = beanManager.getBeans(CdiDisabledBean.class);
39+
assertFalse("Instances of disabled bean expected.", disabledBeans.isEmpty());
40+
41+
Set<Bean<?>> enabledBeans = beanManager.getBeans(CdiEnabledBean.class);
42+
assertFalse("Instances of enabled bean expected.", enabledBeans.isEmpty());
43+
}
44+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package org.javaee8.cdi.bean.discovery.annotated;
2+
3+
import static org.junit.Assert.assertFalse;
4+
import static org.junit.Assert.assertTrue;
5+
6+
import java.util.Set;
7+
8+
import javax.enterprise.inject.spi.Bean;
9+
import javax.enterprise.inject.spi.BeanManager;
10+
import javax.inject.Inject;
11+
12+
import org.javaee8.cdi.bean.discovery.disabled.CdiDisabledBean;
13+
import org.javaee8.cdi.bean.discovery.enabled.CdiEnabledBean;
14+
import org.jboss.arquillian.container.test.api.Deployment;
15+
import org.jboss.arquillian.junit.Arquillian;
16+
import org.jboss.shrinkwrap.api.Archive;
17+
import org.jboss.shrinkwrap.api.ShrinkWrap;
18+
import org.jboss.shrinkwrap.api.spec.WebArchive;
19+
import org.junit.Test;
20+
import org.junit.runner.RunWith;
21+
22+
@RunWith(Arquillian.class)
23+
public class CdiAnnotatedTest {
24+
25+
@Deployment
26+
public static Archive<?> deploy() {
27+
return ShrinkWrap.create(WebArchive.class).addClasses(CdiDisabledBean.class, CdiEnabledBean.class)
28+
.addAsWebInfResource("annotated-beans.xml", "beans.xml");
29+
}
30+
31+
@Inject
32+
BeanManager beanManager;
33+
34+
/**
35+
* Only the explicit CDI bean should be found.
36+
*/
37+
@Test
38+
public void should_beans_be_injected() throws Exception {
39+
Set<Bean<?>> disabledBeans = beanManager.getBeans(CdiDisabledBean.class);
40+
assertTrue("No instances of disabled bean expected.", disabledBeans.isEmpty());
41+
42+
Set<Bean<?>> enabledBeans = beanManager.getBeans(CdiEnabledBean.class);
43+
assertFalse("Instances of enabled bean expected.", enabledBeans.isEmpty());
44+
}
45+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package org.javaee8.cdi.bean.discovery.base;
2+
3+
import static org.junit.Assert.assertFalse;
4+
import static org.junit.Assert.assertTrue;
5+
6+
import java.util.Set;
7+
8+
import javax.enterprise.inject.spi.Bean;
9+
import javax.enterprise.inject.spi.BeanManager;
10+
import javax.inject.Inject;
11+
12+
import org.javaee8.cdi.bean.discovery.disabled.CdiDisabledBean;
13+
import org.javaee8.cdi.bean.discovery.enabled.CdiEnabledBean;
14+
import org.jboss.arquillian.container.test.api.Deployment;
15+
import org.jboss.arquillian.junit.Arquillian;
16+
import org.jboss.shrinkwrap.api.Archive;
17+
import org.jboss.shrinkwrap.api.ShrinkWrap;
18+
import org.jboss.shrinkwrap.api.spec.WebArchive;
19+
import org.junit.Test;
20+
import org.junit.runner.RunWith;
21+
22+
@RunWith(Arquillian.class)
23+
public class CdiDefaultTest {
24+
25+
@Deployment
26+
public static Archive<?> deploy() {
27+
return ShrinkWrap.create(WebArchive.class).addClasses(CdiDisabledBean.class, CdiEnabledBean.class);
28+
}
29+
30+
@Inject
31+
BeanManager beanManager;
32+
33+
/**
34+
* Should work the same as annotated.
35+
*/
36+
@Test
37+
public void should_beans_be_injected() throws Exception {
38+
Set<Bean<?>> disabledBeans = beanManager.getBeans(CdiDisabledBean.class);
39+
assertTrue("No instances of disabled bean expected.", disabledBeans.isEmpty());
40+
41+
Set<Bean<?>> enabledBeans = beanManager.getBeans(CdiEnabledBean.class);
42+
assertFalse("Instances of enabled bean expected.", enabledBeans.isEmpty());
43+
}
44+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package org.javaee8.cdi.bean.discovery.none;
2+
3+
import static org.junit.Assert.assertTrue;
4+
5+
import javax.enterprise.inject.spi.BeanManager;
6+
import javax.inject.Inject;
7+
8+
import org.javaee8.cdi.bean.discovery.disabled.CdiDisabledBean;
9+
import org.javaee8.cdi.bean.discovery.enabled.CdiEnabledBean;
10+
import org.jboss.arquillian.container.test.api.Deployment;
11+
import org.jboss.arquillian.junit.Arquillian;
12+
import org.jboss.shrinkwrap.api.Archive;
13+
import org.jboss.shrinkwrap.api.ShrinkWrap;
14+
import org.jboss.shrinkwrap.api.spec.WebArchive;
15+
import org.junit.Test;
16+
import org.junit.runner.RunWith;
17+
18+
@RunWith(Arquillian.class)
19+
public class CdiDisabledTest {
20+
21+
@Deployment
22+
public static Archive<?> deploy() {
23+
return ShrinkWrap.create(WebArchive.class).addClasses(CdiDisabledBean.class, CdiEnabledBean.class)
24+
.addAsWebInfResource("none-beans.xml", "beans.xml");
25+
}
26+
27+
@Inject
28+
BeanManager beanManager;
29+
30+
/**
31+
* The BeanManager should be null.
32+
*/
33+
@Test
34+
public void should_bean_manager_be_injected() throws Exception {
35+
assertTrue("BeanManager shouldn't be present for a CDI disabled archive", beanManager == null);
36+
}
37+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_2_0.xsd"
5+
version="2.0" bean-discovery-mode="all">
6+
</beans>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_2_0.xsd"
5+
version="2.0" bean-discovery-mode="annotated">
6+
</beans>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_2_0.xsd"
5+
version="2.0" bean-discovery-mode="none">
6+
</beans>

cdi/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<module>events-priority</module>
1818
<module>interception-factory</module>
1919
<module>qualified-lookup</module>
20+
<module>bean-discovery-modes</module>
2021
</modules>
2122

2223
<dependencies>

0 commit comments

Comments
 (0)