Skip to content

Commit 0dae775

Browse files
committed
added cdi/events-conditional-reception, clarified cdi/events test
1 parent 557de80 commit 0dae775

File tree

9 files changed

+132
-6
lines changed

9 files changed

+132
-6
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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.javaee7</groupId>
7+
<artifactId>cdi</artifactId>
8+
<version>1.0-SNAPSHOT</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>cdi-events-conditional-reception</artifactId>
12+
<name>Java EE 7 Sample: conditional reception of cdi events</name>
13+
</project>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package org.javaee7.cdi.events.conditional;
2+
3+
/**
4+
* @author Radim Hanus
5+
*/
6+
public interface EventReceiver {
7+
String getGreet();
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package org.javaee7.cdi.events.conditional;
2+
3+
/**
4+
* @author Radim Hanus
5+
*/
6+
public interface EventSender {
7+
void send(String message);
8+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.javaee7.cdi.events.conditional;
2+
3+
import javax.enterprise.context.SessionScoped;
4+
import javax.enterprise.event.Observes;
5+
import javax.enterprise.event.Reception;
6+
import java.io.Serializable;
7+
8+
/**
9+
* @author Radim Hanus
10+
*/
11+
@SessionScoped
12+
public class GreetingReceiver implements EventReceiver, Serializable {
13+
private String greet = "Willkommen";
14+
15+
void receive(@Observes(notifyObserver = Reception.IF_EXISTS) String greet) {
16+
this.greet = greet;
17+
}
18+
19+
@Override
20+
public String getGreet() {
21+
return greet;
22+
}
23+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.javaee7.cdi.events.conditional;
2+
3+
import javax.enterprise.event.Event;
4+
import javax.inject.Inject;
5+
6+
/**
7+
* @author Radim Hanus
8+
*/
9+
public class GreetingSender implements EventSender {
10+
@Inject
11+
private Event<String> event;
12+
13+
@Override
14+
public void send(String message) {
15+
event.fire(message);
16+
}
17+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package org.javaee7.cdi.events.conditional;
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.inject.Inject;
12+
13+
import static org.hamcrest.CoreMatchers.*;
14+
import static org.junit.Assert.assertEquals;
15+
import static org.junit.Assert.assertThat;
16+
17+
/**
18+
* @author Radim Hanus
19+
*/
20+
@RunWith(Arquillian.class)
21+
public class GreetingTest {
22+
@Deployment
23+
public static Archive<?> deploy() {
24+
return ShrinkWrap.create(JavaArchive.class)
25+
.addClasses(EventReceiver.class, EventSender.class, GreetingReceiver.class, GreetingSender.class)
26+
.addAsManifestResource("beans.xml");
27+
}
28+
29+
@Inject
30+
private EventSender sender;
31+
32+
@Inject
33+
private EventReceiver receiver;
34+
35+
@Test
36+
public void test() throws Exception {
37+
assertThat(sender, is(notNullValue()));
38+
assertThat(sender, instanceOf(GreetingSender.class));
39+
40+
assertThat(receiver, is(notNullValue()));
41+
assertThat(receiver, instanceOf(GreetingReceiver.class));
42+
43+
// send a new greet but the receiver is not instantiated yet
44+
sender.send("Welcome");
45+
// default greet should be available (note that receiver has just been instantiated)
46+
assertEquals("Willkommen", receiver.getGreet());
47+
// send a new greet again
48+
sender.send("Welcome");
49+
// observer method was called so that new greet should be available
50+
assertEquals("Welcome", receiver.getGreet());
51+
}
52+
}
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/events/src/test/java/org/javaee7/cdi/events/GreetingTest.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010

1111
import javax.inject.Inject;
1212

13-
import static org.hamcrest.CoreMatchers.instanceOf;
14-
import static org.hamcrest.CoreMatchers.is;
15-
import static org.hamcrest.CoreMatchers.notNullValue;
13+
import static org.hamcrest.CoreMatchers.*;
1614
import static org.junit.Assert.assertEquals;
1715
import static org.junit.Assert.assertThat;
1816

@@ -42,9 +40,7 @@ public void test() throws Exception {
4240
assertThat(receiver, is(notNullValue()));
4341
assertThat(receiver, instanceOf(GreetingReceiver.class));
4442

45-
// default greet
46-
assertEquals("Willkommen", receiver.getGreet());
47-
// send a new greet
43+
// send a new greet, default greet "Willkommen" should be overwritten
4844
sender.send("Welcome");
4945
// receiver must not belongs to the dependent pseudo-scope since we are checking the result
5046
assertEquals("Welcome", receiver.getGreet());

cdi/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
<module>nobeans-el-injection</module>
3636
<module>nobeans-el-injection-flowscoped</module>
3737
<module>events</module>
38+
<module>events-conditional-reception</module>
3839
</modules>
3940

4041
<dependencies>

0 commit comments

Comments
 (0)