Created
September 23, 2013 20:37
-
-
Save johnament/6676584 to your computer and use it in GitHub Desktop.
Some CDI events playing around
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.tad.arquillian.wls.hibernate.test; | |
import javax.enterprise.event.Event; | |
import javax.inject.Inject; | |
import junit.framework.Assert; | |
import org.jboss.arquillian.container.test.api.Deployment; | |
import org.jboss.arquillian.junit.Arquillian; | |
import org.jboss.shrinkwrap.api.ShrinkWrap; | |
import org.jboss.shrinkwrap.api.asset.EmptyAsset; | |
import org.jboss.shrinkwrap.api.spec.JavaArchive; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
@RunWith(Arquillian.class) | |
public class ArquillianTest { | |
@Deployment | |
public static JavaArchive createDeployment() { | |
JavaArchive ja = ShrinkWrap.create(JavaArchive.class, "foo.jar") | |
.addAsResource(EmptyAsset.INSTANCE, "beans.xml") | |
.addClass(EventListener.class) | |
; | |
return ja; | |
} | |
@Inject | |
@QualOne @QualTwo | |
private Event<String> strEventBoth; | |
@Inject | |
@QualOne | |
private Event<String> strEventOne; | |
@Inject | |
private EventListener listener; | |
@Test | |
public void testFireEvents() { | |
strEventBoth.fire("Hello"); | |
Assert.assertEquals(1,listener.getOne()); | |
Assert.assertEquals(1,listener.getTwo()); | |
Assert.assertEquals(1,listener.getBoth()); | |
Assert.assertEquals(1,listener.getNone()); | |
strEventOne.fire("Hello"); | |
Assert.assertEquals(2,listener.getOne()); | |
Assert.assertEquals(1,listener.getTwo()); | |
Assert.assertEquals(1,listener.getBoth()); | |
Assert.assertEquals(2,listener.getNone()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.tad.arquillian.wls.hibernate.test; | |
import javax.enterprise.context.ApplicationScoped; | |
import javax.enterprise.event.Observes; | |
/** | |
* | |
*/ | |
@ApplicationScoped | |
public class EventListener { | |
int one, two, both,none; | |
public void handleOne(@Observes @QualOne String string) { | |
one++; | |
} | |
public void handleTwo(@Observes @QualTwo String string) { | |
two++; | |
} | |
public void handleBoth(@Observes @QualOne @QualTwo String string) { | |
both++; | |
} | |
public void handleNone(@Observes String string) { | |
none++; | |
} | |
public int getOne() { | |
return one; | |
} | |
public int getTwo() { | |
return two; | |
} | |
public int getBoth() { | |
return both; | |
} | |
public int getNone() { | |
return none; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment