Back to Repositories

Testing Event Inheritance Patterns in greenrobot/EventBus

This test suite validates the event inheritance behavior in the EventBus library, focusing on how events are handled across class and interface hierarchies. It verifies both regular and sticky event posting mechanisms while ensuring proper event propagation through inheritance chains.

Test Coverage Overview

The test suite provides comprehensive coverage of EventBus inheritance mechanisms:

  • Event class hierarchy testing with regular and sticky events
  • Interface hierarchy validation across event types
  • Subscriber class inheritance behavior verification
  • Edge cases for inheritance chains and interface implementations

Implementation Analysis

The testing approach utilizes JUnit’s framework to systematically verify event propagation patterns.

Key implementation aspects include:
  • Subscriber method annotations with @Subscribe
  • Event posting validation through count verification
  • Inheritance chain testing with extended classes and interfaces
  • Sticky event persistence verification

Technical Details

Testing infrastructure includes:

  • JUnit 4 testing framework
  • EventBus core library integration
  • Custom event hierarchies (MyEvent, MyEventExtended)
  • Interface-based event structures
  • Subscriber registration and event counting mechanisms

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Clear separation of test cases by functionality
  • Systematic setup and teardown procedures
  • Comprehensive verification of inheritance behaviors
  • Proper event hierarchy modeling
  • Effective use of assertion patterns

greenrobot/eventbus

EventBusTestJava/src/main/java/org/greenrobot/eventbus/EventBusInheritanceTest.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.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

/**
 * @author Markus Junginger, greenrobot
 */
public class EventBusInheritanceTest {

    protected EventBus eventBus;

    protected int countMyEventExtended;
    protected int countMyEvent;
    protected int countObjectEvent;
    private int countMyEventInterface;
    private int countMyEventInterfaceExtended;

    @Before
    public void setUp() throws Exception {
        eventBus = new EventBus();
    }

    @Test
    public void testEventClassHierarchy() {
        eventBus.register(this);

        eventBus.post("Hello");
        assertEquals(1, countObjectEvent);

        eventBus.post(new MyEvent());
        assertEquals(2, countObjectEvent);
        assertEquals(1, countMyEvent);

        eventBus.post(new MyEventExtended());
        assertEquals(3, countObjectEvent);
        assertEquals(2, countMyEvent);
        assertEquals(1, countMyEventExtended);
    }

    @Test
    public void testEventClassHierarchySticky() {
        eventBus.postSticky("Hello");
        eventBus.postSticky(new MyEvent());
        eventBus.postSticky(new MyEventExtended());
        eventBus.register(new StickySubscriber());
        assertEquals(1, countMyEventExtended);
        assertEquals(2, countMyEvent);
        assertEquals(3, countObjectEvent);
    }

    @Test
    public void testEventInterfaceHierarchy() {
        eventBus.register(this);

        eventBus.post(new MyEvent());
        assertEquals(1, countMyEventInterface);

        eventBus.post(new MyEventExtended());
        assertEquals(2, countMyEventInterface);
        assertEquals(1, countMyEventInterfaceExtended);
    }

    @Test
    public void testEventSuperInterfaceHierarchy() {
        eventBus.register(this);

        eventBus.post(new MyEventInterfaceExtended() {
        });
        assertEquals(1, countMyEventInterface);
        assertEquals(1, countMyEventInterfaceExtended);
    }

    @Test
    public void testSubscriberClassHierarchy() {
        EventBusInheritanceSubclassTest subscriber = new EventBusInheritanceSubclassTest();
        eventBus.register(subscriber);

        eventBus.post("Hello");
        assertEquals(1, subscriber.countObjectEvent);

        eventBus.post(new MyEvent());
        assertEquals(2, subscriber.countObjectEvent);
        assertEquals(0, subscriber.countMyEvent);
        assertEquals(1, subscriber.countMyEventOverwritten);

        eventBus.post(new MyEventExtended());
        assertEquals(3, subscriber.countObjectEvent);
        assertEquals(0, subscriber.countMyEvent);
        assertEquals(1, subscriber.countMyEventExtended);
        assertEquals(2, subscriber.countMyEventOverwritten);
    }

    @Test
    public void testSubscriberClassHierarchyWithoutNewSubscriberMethod() {
        EventBusInheritanceSubclassNoMethodTest
                subscriber = new EventBusInheritanceSubclassNoMethodTest();
        eventBus.register(subscriber);

        eventBus.post("Hello");
        assertEquals(1, subscriber.countObjectEvent);

        eventBus.post(new MyEvent());
        assertEquals(2, subscriber.countObjectEvent);
        assertEquals(1, subscriber.countMyEvent);

        eventBus.post(new MyEventExtended());
        assertEquals(3, subscriber.countObjectEvent);
        assertEquals(2, subscriber.countMyEvent);
        assertEquals(1, subscriber.countMyEventExtended);
    }

    @Subscribe
    public void onEvent(Object event) {
        countObjectEvent++;
    }

    @Subscribe
    public void onEvent(MyEvent event) {
        countMyEvent++;
    }

    @Subscribe
    public void onEvent(MyEventExtended event) {
        countMyEventExtended++;
    }

    @Subscribe
    public void onEvent(MyEventInterface event) {
        countMyEventInterface++;
    }

    @Subscribe
    public void onEvent(MyEventInterfaceExtended event) {
        countMyEventInterfaceExtended++;
    }

    public static interface MyEventInterface {
    }

    public static class MyEvent implements MyEventInterface {
    }

    public static interface MyEventInterfaceExtended extends MyEventInterface {
    }

    public static class MyEventExtended extends MyEvent implements MyEventInterfaceExtended {
    }

    public class StickySubscriber {
        @Subscribe(sticky = true)
        public void onEvent(Object event) {
            countObjectEvent++;
        }

        @Subscribe(sticky = true)
        public void onEvent(MyEvent event) {
            countMyEvent++;
        }

        @Subscribe(sticky = true)
        public void onEvent(MyEventExtended event) {
            countMyEventExtended++;
        }

        @Subscribe(sticky = true)
        public void onEvent(MyEventInterface event) {
            countMyEventInterface++;
        }

        @Subscribe(sticky = true)
        public void onEvent(MyEventInterfaceExtended event) {
            countMyEventInterfaceExtended++;
        }
    }

}