Back to Repositories

Validating EventBus Inheritance-Disabled Behavior in greenrobot/EventBus

This test suite validates EventBus behavior when inheritance is disabled, focusing on event class hierarchies and subscriber interactions. It ensures proper event handling and subscription mechanisms work correctly when inheritance-based event dispatch is turned off.

Test Coverage Overview

The test suite provides comprehensive coverage of EventBus functionality with disabled inheritance:
  • Event class hierarchy handling
  • Sticky event posting and subscription
  • Interface hierarchy behavior
  • Subscriber class inheritance scenarios

Implementation Analysis

The testing approach utilizes JUnit framework features to verify EventBus behavior with inheritance disabled. It implements systematic test cases that validate event posting, subscription handling, and hierarchy relationships using both standard and sticky events.

Key patterns include subscriber method annotations, event class hierarchies, and interface implementations.

Technical Details

Testing infrastructure includes:
  • JUnit test framework
  • EventBus builder configuration with disabled inheritance
  • Custom event classes and interfaces
  • Subscriber implementations with @Subscribe annotations
  • Event counting mechanisms for verification

Best Practices Demonstrated

The test suite exemplifies several testing best practices:
  • Isolated test setup using @Before annotation
  • Clear test method naming conventions
  • Comprehensive assertion checking
  • Hierarchical test organization
  • Proper encapsulation of test components

greenrobot/eventbus

EventBusTestJava/src/main/java/org/greenrobot/eventbus/EventBusInheritanceDisabledTest.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 junit.framework.Assert.assertEquals;

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

    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 = EventBus.builder().eventInheritance(false).build();
    }

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

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

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

        eventBus.post(new MyEventExtended());
        assertEquals(0, countObjectEvent);
        assertEquals(1, 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(1, countMyEvent);
        assertEquals(0, countObjectEvent);
    }

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

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

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

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

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

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

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

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

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

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

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

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

        eventBus.post(new MyEventExtended());
        assertEquals(0, subscriber.countObjectEvent);
        assertEquals(1, 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++;
        }
    }

}