Back to Repositories

Testing Generic Event Handling Implementation in greenrobot/EventBus

This test suite validates the generic event handling capabilities of EventBus, focusing on type safety and event dispatch with generic parameters. It verifies proper handling of generic event types, type erasure, and inheritance hierarchies in event subscribers.

Test Coverage Overview

The test suite provides comprehensive coverage of generic event handling in EventBus.

  • Tests generic event subscribers with type parameters
  • Validates type erasure behavior with generic events
  • Verifies inheritance handling with numeric type constraints
  • Tests subclass implementations of generic subscribers

Implementation Analysis

The testing approach uses JUnit to validate different aspects of generic event handling.

Key patterns include:
  • Generic class definitions with type parameters
  • Subscriber methods annotated with @Subscribe
  • Type-bounded generic constraints
  • Event posting and tracking verification

Technical Details

Testing infrastructure includes:
  • JUnit test framework
  • EventBus core library
  • Custom event tracking mechanism
  • AbstractEventBusTest base class for common functionality
  • Generic event and subscriber class hierarchies

Best Practices Demonstrated

The test suite exemplifies high-quality testing practices:

  • Systematic testing of type-safety mechanisms
  • Clear separation of test cases for different generic scenarios
  • Proper inheritance testing with generic types
  • Comprehensive verification of edge cases
  • Well-structured test organization

greenrobot/eventbus

EventBusTestJava/src/main/java/org/greenrobot/eventbus/EventBusGenericsTest.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.Test;

public class EventBusGenericsTest extends AbstractEventBusTest {
    public static class GenericEvent<T> {
        T value;
    }

    public class GenericEventSubscriber<T> {
        @Subscribe
        public void onGenericEvent(GenericEvent<T> event) {
            trackEvent(event);
        }
    }

    public class FullGenericEventSubscriber<T> {
        @Subscribe
        public void onGenericEvent(T event) {
            trackEvent(event);
        }
    }

    public class GenericNumberEventSubscriber<T extends Number> {
        @Subscribe
        public void onGenericEvent(T event) {
            trackEvent(event);
        }
    }

    public class GenericFloatEventSubscriber extends GenericNumberEventSubscriber<Float> {
    }

    @Test
    public void testGenericEventAndSubscriber() {
        GenericEventSubscriber<IntTestEvent> genericSubscriber = new GenericEventSubscriber<IntTestEvent>();
        eventBus.register(genericSubscriber);
        eventBus.post(new GenericEvent<Integer>());
        assertEventCount(1);
    }

    @Test
    public void testGenericEventAndSubscriber_TypeErasure() {
        FullGenericEventSubscriber<IntTestEvent> genericSubscriber = new FullGenericEventSubscriber<IntTestEvent>();
        eventBus.register(genericSubscriber);
        eventBus.post(new IntTestEvent(42));
        eventBus.post("Type erasure!");
        assertEventCount(2);
    }

    @Test
    public void testGenericEventAndSubscriber_BaseType() {
        GenericNumberEventSubscriber<Float> genericSubscriber = new GenericNumberEventSubscriber<>();
        eventBus.register(genericSubscriber);
        eventBus.post(new Float(42));
        eventBus.post(new Double(23));
        assertEventCount(2);
        eventBus.post("Not the same base type");
        assertEventCount(2);
    }

    @Test
    public void testGenericEventAndSubscriber_Subclass() {
        GenericFloatEventSubscriber genericSubscriber = new GenericFloatEventSubscriber();
        eventBus.register(genericSubscriber);
        eventBus.post(new Float(42));
        eventBus.post(new Double(77));
        assertEventCount(2);
        eventBus.post("Not the same base type");
        assertEventCount(2);
    }
}