001package edu.pdx.cs.joy.grader.poa;
002
003import com.google.common.eventbus.EventBus;
004import com.google.common.eventbus.Subscribe;
005import org.junit.jupiter.api.AfterEach;
006import org.junit.jupiter.api.BeforeEach;
007
008import java.util.function.Consumer;
009
010public class EventBusTestCase {
011  protected EventBus bus;
012  protected Consumer<UnhandledExceptionEvent> unhandledExceptionHandler;
013
014  @BeforeEach
015  public void setUp() {
016    unhandledExceptionHandler = this::failTestWhenUnhandledExceptionEncountered;
017    bus = new EventBusThatPublishesUnhandledExceptionEvents();
018    bus.register(this);
019  }
020
021  @AfterEach
022  public void tearDown() {
023    if (bus != null) {
024      bus.unregister(this);
025    }
026  }
027
028  @Subscribe
029  public void handleUnhandledException(UnhandledExceptionEvent event) {
030    unhandledExceptionHandler.accept(event);
031  }
032
033  private void failTestWhenUnhandledExceptionEncountered(UnhandledExceptionEvent event) {
034    throw new AssertionError("Unhandled Exception on event bus", event.getUnhandledException());
035  }
036
037  protected void doNotFailTestWhenUnhandledExceptionEncountered(UnhandledExceptionEvent event) {
038
039  }
040
041}