Skip to content

Commit 262b142

Browse files
committed
Added test for decorating a dynamically added CDI bean
1 parent 9996843 commit 262b142

File tree

8 files changed

+146
-0
lines changed

8 files changed

+146
-0
lines changed

cdi/dynamic-bean-decorated/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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"> <modelVersion>4.0.0</modelVersion>
3+
4+
<parent>
5+
<groupId>org.javaee8</groupId>
6+
<artifactId>cdi</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
</parent>
9+
10+
<artifactId>dynamic-bean-decorated</artifactId>
11+
<name>Java EE 8 Samples: CDI - Dynamic Bean Decorated</name>
12+
</project>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.javaee8.cdi.dynamic.bean.decorated;
2+
3+
import javax.enterprise.context.ApplicationScoped;
4+
import javax.enterprise.event.Observes;
5+
import javax.enterprise.inject.spi.AfterBeanDiscovery;
6+
import javax.enterprise.inject.spi.Extension;
7+
8+
/**
9+
*
10+
* @author Arjan Tijms
11+
*
12+
*/
13+
public class CdiExtension implements Extension {
14+
15+
public void afterBean(final @Observes AfterBeanDiscovery afterBeanDiscovery) {
16+
afterBeanDiscovery
17+
.addBean()
18+
.scope(ApplicationScoped.class)
19+
.types(MyBean.class)
20+
.id("Created by " + CdiExtension.class)
21+
.createWith(e -> new MyBeanImpl("Hi!"));
22+
}
23+
24+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.javaee8.cdi.dynamic.bean.decorated;
2+
3+
/**
4+
*
5+
* @author Arjan Tijms
6+
*
7+
*/
8+
public interface MyBean {
9+
String sayHi();
10+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.javaee8.cdi.dynamic.bean.decorated;
2+
3+
import javax.enterprise.inject.Typed;
4+
5+
/**
6+
*
7+
* @author Arjan Tijms
8+
*
9+
*/
10+
// Typed: Extra guard so that MyBeanImpl has no types of itself, but extension archive is not scanned
11+
// so not strictly needed.
12+
@Typed
13+
public class MyBeanImpl implements MyBean {
14+
15+
private final String greet;
16+
17+
// Note: There's no default ctor, so CDI cannot directly inject an instance of this
18+
// bean.
19+
20+
public MyBeanImpl(String greet) {
21+
this.greet = greet;
22+
}
23+
24+
@Override
25+
public String sayHi() {
26+
return greet;
27+
}
28+
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
org.javaee8.cdi.dynamic.bean.decorated.CdiExtension
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package org.javaee8.cdi.dynamic.bean.decorated;
2+
3+
import static org.jboss.shrinkwrap.api.ShrinkWrap.create;
4+
import static org.junit.Assert.assertEquals;
5+
6+
import javax.inject.Inject;
7+
8+
import org.jboss.arquillian.container.test.api.Deployment;
9+
import org.jboss.arquillian.junit.Arquillian;
10+
import org.jboss.shrinkwrap.api.spec.JavaArchive;
11+
import org.jboss.shrinkwrap.api.spec.WebArchive;
12+
import org.junit.Test;
13+
import org.junit.runner.RunWith;
14+
15+
/**
16+
*
17+
* @author Arjan Tijms
18+
*
19+
*/
20+
@RunWith(Arquillian.class)
21+
public class DynamicBeanTest {
22+
23+
@Deployment
24+
public static WebArchive deploy() {
25+
return create(WebArchive.class)
26+
.addAsLibraries(
27+
create(JavaArchive.class)
28+
.addClasses(CdiExtension.class, MyBean.class, MyBeanImpl.class)
29+
.addAsResource("META-INF/services/javax.enterprise.inject.spi.Extension"))
30+
.addClass(MyDecorator.class)
31+
.addAsManifestResource("beans.xml");
32+
}
33+
34+
@Inject
35+
private MyBean myBean;
36+
37+
@Test
38+
public void test() {
39+
assertEquals("Hi! decorated", myBean.sayHi());
40+
}
41+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.javaee8.cdi.dynamic.bean.decorated;
2+
3+
import javax.annotation.Priority;
4+
import javax.decorator.Decorator;
5+
import javax.decorator.Delegate;
6+
import javax.inject.Inject;
7+
8+
@Decorator
9+
@Priority(100)
10+
public class MyDecorator implements MyBean {
11+
12+
@Inject
13+
@Delegate
14+
MyBean mybean;
15+
16+
@Override
17+
public String sayHi() {
18+
return mybean.sayHi() + " decorated";
19+
}
20+
21+
}
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_2_0.xsd"
6+
bean-discovery-mode="all">
7+
8+
</beans>

0 commit comments

Comments
 (0)