Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions cdi/events/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>
<parent>
<groupId>org.javaee7.cdi</groupId>
<artifactId>cdi-samples</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>events</artifactId>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.javaee7.cdi.events;

/**
* @author Radim Hanus
*/
public interface EventReceiver {
String getGreet();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.javaee7.cdi.events;

/**
* @author Radim Hanus
*/
public interface EventSender {
void send(String message);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.javaee7.cdi.events;

import javax.enterprise.context.SessionScoped;
import javax.enterprise.event.Observes;
import java.io.Serializable;

/**
* @author Radim Hanus
*/
@SessionScoped
public class GreetingReceiver implements EventReceiver, Serializable {
private String greet = "Willkommen";

void receive(@Observes String greet) {
this.greet = greet;
}

@Override
public String getGreet() {
return greet;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.javaee7.cdi.events;

import javax.enterprise.event.Event;
import javax.inject.Inject;

/**
* @author Radim Hanus
*/
public class GreetingSender implements EventSender {
@Inject
private Event<String> event;

@Override
public void send(String message) {
event.fire(message);
}
}
52 changes: 52 additions & 0 deletions cdi/events/src/test/java/org/javaee7/cdi/events/GreetingTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.javaee7.cdi.events;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.Test;
import org.junit.runner.RunWith;

import javax.inject.Inject;

import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;

/**
* @author Radim Hanus
*/
@RunWith(Arquillian.class)
public class GreetingTest {
@Deployment
public static Archive<?> deploy() {
return ShrinkWrap.create(JavaArchive.class)
.addClasses(EventReceiver.class, EventSender.class, GreetingReceiver.class, GreetingSender.class)
.addAsManifestResource("beans.xml");
}

@Inject
private EventSender sender;

@Inject
private EventReceiver receiver;

@Test
public void test() throws Exception {
assertThat(sender, is(notNullValue()));
assertThat(sender, instanceOf(GreetingSender.class));

assertThat(receiver, is(notNullValue()));
assertThat(receiver, instanceOf(GreetingReceiver.class));

// default greet
assertEquals("Willkommen", receiver.getGreet());
// send a new greet
sender.send("Welcome");
// receiver must not belongs to the dependent pseudo-scope since we are checking the result
assertEquals("Welcome", receiver.getGreet());
}
}
16 changes: 16 additions & 0 deletions cdi/events/src/test/resources/arquillian.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>

<arquillian xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://jboss.org/schema/arquillian"
xsi:schemaLocation="http://jboss.org/schema/arquillian http://jboss.org/schema/arquillian/arquillian_1_0.xsd">

<defaultProtocol type="Servlet 3.0"/>

<container qualifier="test" default="true">
<configuration>
<property name="jbossHome">${serverRoot:target/wildfly-8.0.0.CR1}</property>
<property name="serverConfig">${serverProfile:standalone-full.xml}</property>
</configuration>
</container>

</arquillian>
8 changes: 8 additions & 0 deletions cdi/events/src/test/resources/beans.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
bean-discovery-mode="all">

</beans>
1 change: 1 addition & 0 deletions cdi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,6 @@
<module>alternatives-priority</module>
<module>nobeans-el-injection</module>
<module>nobeans-el-injection-flowscoped</module>
<module>events</module>
</modules>
</project>