BAEL-639: Guide to Guava's EventBus Tests - corrected formatting
This commit is contained in:
parent
b2fcd4a953
commit
80c07b0ab2
@ -57,6 +57,32 @@
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- logging -->
|
||||
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
<version>${org.slf4j.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-classic</artifactId>
|
||||
<version>${logback.version}</version>
|
||||
<!-- <scope>runtime</scope> -->
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>jcl-over-slf4j</artifactId>
|
||||
<version>${org.slf4j.version}</version>
|
||||
<!-- <scope>runtime</scope> --> <!-- some spring dependencies need to compile against jcl -->
|
||||
</dependency>
|
||||
<dependency> <!-- needed to bridge to slf4j for projects that use the log4j APIs directly -->
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>log4j-over-slf4j</artifactId>
|
||||
<version>${org.slf4j.version}</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
@ -105,6 +131,9 @@
|
||||
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
|
||||
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
|
||||
|
||||
<!-- logging -->
|
||||
<org.slf4j.version>1.7.21</org.slf4j.version>
|
||||
<logback.version>1.1.7</logback.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
@ -1,6 +1,5 @@
|
||||
package org.baeldung.guava;
|
||||
|
||||
|
||||
public class CustomEvent {
|
||||
private String action;
|
||||
|
||||
|
@ -6,17 +6,16 @@ class EventBusWrapper {
|
||||
|
||||
private static EventBus eventBus = new EventBus();
|
||||
|
||||
static void register(Object object){
|
||||
static void register(Object object) {
|
||||
eventBus.register(object);
|
||||
}
|
||||
|
||||
static void unregister(Object object){
|
||||
static void unregister(Object object) {
|
||||
eventBus.unregister(object);
|
||||
}
|
||||
|
||||
static void post(Object object){
|
||||
static void post(Object object) {
|
||||
eventBus.post(object);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -1,25 +1,23 @@
|
||||
package org.baeldung.guava;
|
||||
|
||||
import com.google.common.eventbus.Subscribe;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class EventListener {
|
||||
|
||||
private static int eventsHandled;
|
||||
private static final Logger LOG = LoggerFactory.getLogger(EventListener.class);
|
||||
|
||||
/**
|
||||
* Handles events of type String *
|
||||
*/
|
||||
@Subscribe
|
||||
public void stringEvent(String event){
|
||||
System.out.println("do event ["+event+"]");
|
||||
public void stringEvent(String event) {
|
||||
LOG.info("do event [" + event + "]");
|
||||
eventsHandled++;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles events of type CustomEvent
|
||||
*/
|
||||
@Subscribe
|
||||
public void someEvent(CustomEvent customEvent){
|
||||
System.out.println("do event ["+ customEvent.getAction()+"]");
|
||||
public void someCustomEvent(CustomEvent customEvent) {
|
||||
LOG.info("do event [" + customEvent.getAction() + "]");
|
||||
eventsHandled++;
|
||||
}
|
||||
|
||||
@ -27,7 +25,7 @@ public class EventListener {
|
||||
return eventsHandled;
|
||||
}
|
||||
|
||||
public void resetEventsHandled(){
|
||||
public void resetEventsHandled() {
|
||||
eventsHandled = 0;
|
||||
}
|
||||
}
|
||||
|
@ -10,32 +10,32 @@ public class GuavaEventBusTest {
|
||||
private EventListener listener;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
public void setUp() {
|
||||
listener = new EventListener();
|
||||
EventBusWrapper.register(listener);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
public void tearDown() {
|
||||
EventBusWrapper.unregister(listener);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStringEvent_whenEventHandled_thenSuccess() throws Exception {
|
||||
public void givenStringEvent_whenEventHandled_thenSuccess() {
|
||||
listener.resetEventsHandled();
|
||||
|
||||
EventBusWrapper.post("String Event");
|
||||
assertEquals(1,listener.getEventsHandled());
|
||||
assertEquals(1, listener.getEventsHandled());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCustomEvent_whenEventHandled_thenSuccess() throws Exception {
|
||||
public void givenCustomEvent_whenEventHandled_thenSuccess() {
|
||||
listener.resetEventsHandled();
|
||||
|
||||
CustomEvent customEvent = new CustomEvent("Custom Event");
|
||||
EventBusWrapper.post(customEvent);
|
||||
|
||||
assertEquals(1,listener.getEventsHandled());
|
||||
assertEquals(1, listener.getEventsHandled());
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user