Testing EventBusBuilder Configuration and Error Handling in greenrobot/EventBus
This test suite evaluates the EventBusBuilder configuration and error handling capabilities in the greenrobot EventBus library. It verifies various builder patterns for customizing event bus behavior, exception handling, and event inheritance features.
Test Coverage Overview
Implementation Analysis
Technical Details
Best Practices Demonstrated
greenrobot/eventbus
EventBusTestJava/src/main/java/org/greenrobot/eventbus/EventBusBuilderTest.java
/*
* Copyright (C) 2012-2016 Markus Junginger, greenrobot (http://greenrobot.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.greenrobot.eventbus;
import org.junit.Assert;
import org.junit.Test;
import static org.junit.Assert.fail;
/**
* @author Markus Junginger, greenrobot
*/
public class EventBusBuilderTest extends AbstractEventBusTest {
@Test
public void testThrowSubscriberException() {
eventBus = EventBus.builder().throwSubscriberException(true).build();
eventBus.register(new SubscriberExceptionEventTracker());
eventBus.register(new ThrowingSubscriber());
try {
eventBus.post("Foo");
fail("Should have thrown");
} catch (EventBusException e) {
// Expected
}
}
@Test
public void testDoNotSendSubscriberExceptionEvent() {
eventBus = EventBus.builder().logSubscriberExceptions(false).sendSubscriberExceptionEvent(false).build();
eventBus.register(new SubscriberExceptionEventTracker());
eventBus.register(new ThrowingSubscriber());
eventBus.post("Foo");
assertEventCount(0);
}
@Test
public void testDoNotSendNoSubscriberEvent() {
eventBus = EventBus.builder().logNoSubscriberMessages(false).sendNoSubscriberEvent(false).build();
eventBus.register(new NoSubscriberEventTracker());
eventBus.post("Foo");
assertEventCount(0);
}
@Test
public void testInstallDefaultEventBus() {
EventBusBuilder builder = EventBus.builder();
try {
// Either this should throw when another unit test got the default event bus...
eventBus = builder.installDefaultEventBus();
Assert.assertEquals(eventBus, EventBus.getDefault());
// ...or this should throw
eventBus = builder.installDefaultEventBus();
fail("Should have thrown");
} catch (EventBusException e) {
// Expected
}
}
@Test
public void testEventInheritance() {
eventBus = EventBus.builder().eventInheritance(false).build();
eventBus.register(new ThrowingSubscriber());
eventBus.post("Foo");
}
public class SubscriberExceptionEventTracker {
@Subscribe
public void onEvent(SubscriberExceptionEvent event) {
trackEvent(event);
}
}
public class NoSubscriberEventTracker {
@Subscribe
public void onEvent(NoSubscriberEvent event) {
trackEvent(event);
}
}
public class ThrowingSubscriber {
@Subscribe
public void onEvent(Object event) {
throw new RuntimeException();
}
}
}