Ignore:
Timestamp:
2023-03-21T14:49:10+01:00 (23 months ago)
Author:
taylor.smock
Message:

See #16567: Convert most plugin tests to JUnit 5

Location:
applications/editors/josm/plugins/eventbus/test/unit/org/openstreetmap/josm/eventbus
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/eventbus/test/unit/org/openstreetmap/josm/eventbus/AsyncEventBusTest.java

    r34000 r36064  
    1717package org.openstreetmap.josm.eventbus;
    1818
    19 import static org.junit.Assert.assertEquals;
    20 import static org.junit.Assert.assertTrue;
     19import static org.junit.jupiter.api.Assertions.assertEquals;
     20import static org.junit.jupiter.api.Assertions.assertTrue;
    2121
    2222import java.util.ArrayList;
     
    2424import java.util.concurrent.Executor;
    2525
    26 import org.junit.Before;
    27 import org.junit.Test;
    28 import org.openstreetmap.josm.eventbus.AsyncEventBus;
     26import org.junit.jupiter.api.BeforeEach;
     27import org.junit.jupiter.api.Test;
    2928
    3029/**
     
    3332 * @author Cliff Biffle
    3433 */
    35 public class AsyncEventBusTest {
     34class AsyncEventBusTest {
    3635  private static final String EVENT = "Hello";
    3736
     
    4140  private AsyncEventBus bus;
    4241
    43   @Before
    44   public void setUp() throws Exception {
     42  @BeforeEach
     43  void setUp() {
    4544    executor = new FakeExecutor();
    4645    bus = new AsyncEventBus(executor);
     
    4847
    4948  @Test
    50   public void testBasicDistribution() {
     49  void testBasicDistribution() {
    5150    StringCatcher catcher = new StringCatcher();
    5251    bus.register(catcher);
     
    5655
    5756    List<String> events = catcher.getEvents();
    58     assertTrue("No events should be delivered synchronously.", events.isEmpty());
     57    assertTrue(events.isEmpty(), "No events should be delivered synchronously.");
    5958
    6059    // Now we find the task in our Executor and explicitly activate it.
    6160    List<Runnable> tasks = executor.getTasks();
    62     assertEquals("One event dispatch task should be queued.", 1, tasks.size());
     61    assertEquals(1, tasks.size(), "One event dispatch task should be queued.");
    6362
    6463    tasks.get(0).run();
    6564
    66     assertEquals("One event should be delivered.", 1, events.size());
    67     assertEquals("Correct string should be delivered.", EVENT, events.get(0));
     65    assertEquals(1, events.size(), "One event should be delivered.");
     66    assertEquals(EVENT, events.get(0), "Correct string should be delivered.");
    6867  }
    6968
  • applications/editors/josm/plugins/eventbus/test/unit/org/openstreetmap/josm/eventbus/DispatcherTest.java

    r34000 r36064  
    2727import java.util.concurrent.CyclicBarrier;
    2828
    29 import org.junit.Ignore;
    30 import org.junit.Test;
    31 import org.openstreetmap.josm.eventbus.Dispatcher;
    32 import org.openstreetmap.josm.eventbus.EventBus;
    33 import org.openstreetmap.josm.eventbus.Subscribe;
    34 import org.openstreetmap.josm.eventbus.Subscriber;
     29import org.junit.jupiter.api.Disabled;
     30import org.junit.jupiter.api.Test;
    3531
    3632/**
     
    4036 */
    4137
    42 public class DispatcherTest {
     38class DispatcherTest {
    4339
    4440  private final EventBus bus = new EventBus();
     
    6662
    6763  @Test
    68   @Ignore("FIXME")
    69   public void testPerThreadQueuedDispatcher() {
     64  @Disabled("FIXME")
     65  void testPerThreadQueuedDispatcher() {
    7066    dispatcher = Dispatcher.perThreadDispatchQueue();
    7167    dispatcher.dispatch(1, integerSubscribers.iterator());
     
    8783
    8884  @Test
    89   @Ignore("FIXME")
    90   public void testLegacyAsyncDispatcher() {
     85  @Disabled("FIXME")
     86  void testLegacyAsyncDispatcher() {
    9187    dispatcher = Dispatcher.legacyAsync();
    9288
     
    9591
    9692    new Thread(
    97             new Runnable() {
    98               @Override
    99               public void run() {
    100                 try {
    101                   barrier.await();
    102                 } catch (Exception e) {
    103                   throw new AssertionError(e);
    104                 }
     93            () -> {
     94              try {
     95                barrier.await();
     96              } catch (Exception e) {
     97                throw new AssertionError(e);
     98              }
    10599
    106                 dispatcher.dispatch(2, integerSubscribers.iterator());
    107                 latch.countDown();
    108               }
     100              dispatcher.dispatch(2, integerSubscribers.iterator());
     101              latch.countDown();
    109102            })
    110103        .start();
    111104
    112105    new Thread(
    113             new Runnable() {
    114               @Override
    115               public void run() {
    116                 try {
    117                   barrier.await();
    118                 } catch (Exception e) {
    119                   throw new AssertionError(e);
    120                 }
     106            () -> {
     107              try {
     108                barrier.await();
     109              } catch (Exception e) {
     110                throw new AssertionError(e);
     111              }
    121112
    122                 dispatcher.dispatch("foo", stringSubscribers.iterator());
    123                 latch.countDown();
    124               }
     113              dispatcher.dispatch("foo", stringSubscribers.iterator());
     114              latch.countDown();
    125115            })
    126116        .start();
     
    135125
    136126  @Test
    137   @Ignore("FIXME")
    138   public void testImmediateDispatcher() {
     127  @Disabled("FIXME")
     128  void testImmediateDispatcher() {
    139129    dispatcher = Dispatcher.immediate();
    140130    dispatcher.dispatch(1, integerSubscribers.iterator());
  • applications/editors/josm/plugins/eventbus/test/unit/org/openstreetmap/josm/eventbus/EventBusTest.java

    r34000 r36064  
    1717package org.openstreetmap.josm.eventbus;
    1818
    19 import static org.junit.Assert.assertEquals;
    20 import static org.junit.Assert.fail;
     19import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
     20import static org.junit.jupiter.api.Assertions.assertEquals;
     21import static org.junit.jupiter.api.Assertions.assertThrows;
    2122
    2223import java.util.ArrayList;
    23 import java.util.Arrays;
     24import java.util.Collections;
    2425import java.util.List;
    2526import java.util.concurrent.CopyOnWriteArrayList;
     
    2930import java.util.concurrent.atomic.AtomicInteger;
    3031
    31 import org.junit.Before;
    32 import org.junit.Test;
    33 import org.openstreetmap.josm.eventbus.DeadEvent;
    34 import org.openstreetmap.josm.eventbus.EventBus;
    35 import org.openstreetmap.josm.eventbus.Subscribe;
    36 import org.openstreetmap.josm.eventbus.SubscriberExceptionContext;
    37 import org.openstreetmap.josm.eventbus.SubscriberExceptionHandler;
     32import org.junit.jupiter.api.BeforeEach;
     33import org.junit.jupiter.api.Test;
    3834
    3935/**
     
    4238 * @author Cliff Biffle
    4339 */
    44 public class EventBusTest {
     40class EventBusTest {
    4541  private static final String EVENT = "Hello";
    4642  private static final String BUS_IDENTIFIER = "test-bus";
     
    4844  private EventBus bus;
    4945
    50   @Before
    51   public void setUp() throws Exception {
     46  @BeforeEach
     47  void setUp() {
    5248    bus = new EventBus(BUS_IDENTIFIER);
    5349  }
    5450
    5551  @Test
    56   public void testBasicCatcherDistribution() {
     52  void testBasicCatcherDistribution() {
    5753    StringCatcher catcher = new StringCatcher();
    5854    bus.register(catcher);
     
    6056
    6157    List<String> events = catcher.getEvents();
    62     assertEquals("Only one event should be delivered.", 1, events.size());
    63     assertEquals("Correct string should be delivered.", EVENT, events.get(0));
     58    assertEquals(1, events.size(), "Only one event should be delivered.");
     59    assertEquals(EVENT, events.get(0), "Correct string should be delivered.");
    6460  }
    6561
     
    7167   */
    7268  @Test
    73   public void testPolymorphicDistribution() {
     69  void testPolymorphicDistribution() {
    7470    // Three catchers for related types String, Object, and Comparable<?>.
    7571    // String isa Object
     
    10197    // Two additional event types: Object and Comparable<?> (played by Integer)
    10298    Object objEvent = new Object();
    103     Object compEvent = new Integer(6);
     99    Object compEvent = 6;
    104100
    105101    bus.post(EVENT);
     
    109105    // Check the StringCatcher...
    110106    List<String> stringEvents = stringCatcher.getEvents();
    111     assertEquals("Only one String should be delivered.", 1, stringEvents.size());
    112     assertEquals("Correct string should be delivered.", EVENT, stringEvents.get(0));
     107    assertEquals(1, stringEvents.size(), "Only one String should be delivered.");
     108    assertEquals(EVENT, stringEvents.get(0), "Correct string should be delivered.");
    113109
    114110    // Check the Catcher<Object>...
    115     assertEquals("Three Objects should be delivered.", 3, objectEvents.size());
    116     assertEquals("String fixture must be first object delivered.", EVENT, objectEvents.get(0));
    117     assertEquals("Object fixture must be second object delivered.", objEvent, objectEvents.get(1));
    118     assertEquals(
    119         "Comparable fixture must be thirdobject delivered.", compEvent, objectEvents.get(2));
     111    assertEquals(3, objectEvents.size(), "Three Objects should be delivered.");
     112    assertEquals(EVENT, objectEvents.get(0), "String fixture must be first object delivered.");
     113    assertEquals(objEvent, objectEvents.get(1), "Object fixture must be second object delivered.");
     114    assertEquals(compEvent, objectEvents.get(2), "Comparable fixture must be thirdobject delivered.");
    120115
    121116    // Check the Catcher<Comparable<?>>...
    122     assertEquals("Two Comparable<?>s should be delivered.", 2, compEvents.size());
    123     assertEquals("String fixture must be first comparable delivered.", EVENT, compEvents.get(0));
    124     assertEquals(
    125         "Comparable fixture must be second comparable delivered.", compEvent, compEvents.get(1));
    126   }
    127 
    128   @Test
    129   public void testSubscriberThrowsException() throws Exception {
     117    assertEquals(2, compEvents.size(), "Two Comparable<?>s should be delivered.");
     118    assertEquals(EVENT, compEvents.get(0), "String fixture must be first comparable delivered.");
     119    assertEquals(compEvent, compEvents.get(1), "Comparable fixture must be second comparable delivered.");
     120  }
     121
     122  @Test
     123  void testSubscriberThrowsException() throws Exception {
    130124    final RecordingSubscriberExceptionHandler handler = new RecordingSubscriberExceptionHandler();
    131125    final EventBus eventBus = new EventBus(handler);
     
    142136    eventBus.post(EVENT);
    143137
    144     assertEquals("Cause should be available.", exception, handler.exception);
    145     assertEquals("EventBus should be available.", eventBus, handler.context.getEventBus());
    146     assertEquals("Event should be available.", EVENT, handler.context.getEvent());
    147     assertEquals("Subscriber should be available.", subscriber, handler.context.getSubscriber());
    148     assertEquals(
    149         "Method should be available.",
    150         subscriber.getClass().getMethod("throwExceptionOn", String.class),
    151         handler.context.getSubscriberMethod());
    152   }
    153 
    154   @Test
    155   public void testSubscriberThrowsExceptionHandlerThrowsException() throws Exception {
     138    assertEquals(exception, handler.exception, "Cause should be available.");
     139    assertEquals(eventBus, handler.context.getEventBus(), "EventBus should be available.");
     140    assertEquals(EVENT, handler.context.getEvent(), "Event should be available.");
     141    assertEquals(subscriber, handler.context.getSubscriber(), "Subscriber should be available.");
     142    assertEquals(subscriber.getClass().getMethod("throwExceptionOn", String.class), handler.context.getSubscriberMethod(), "Method should be available.");
     143  }
     144
     145  @Test
     146  void testSubscriberThrowsExceptionHandlerThrowsException() throws Exception {
    156147    final EventBus eventBus =
    157148        new EventBus(
    158             new SubscriberExceptionHandler() {
    159               @Override
    160               public void handleException(Throwable exception, SubscriberExceptionContext context) {
    161                 throw new RuntimeException("testSubscriberThrowsExceptionHandlerThrowsException_1. This is a normal exception");
    162               }
    163             });
     149                (exception, context) -> {
     150                  throw new RuntimeException("testSubscriberThrowsExceptionHandlerThrowsException_1. This is a normal exception");
     151                });
    164152    final Object subscriber =
    165153        new Object() {
     
    170158        };
    171159    eventBus.register(subscriber);
    172     try {
    173       eventBus.post(EVENT);
    174     } catch (RuntimeException e) {
    175       fail("Exception should not be thrown.");
    176     }
    177   }
    178 
    179   @Test
    180   public void testDeadEventForwarding() {
     160    assertDoesNotThrow(() -> eventBus.post(EVENT));
     161  }
     162
     163  @Test
     164  void testDeadEventForwarding() {
    181165    GhostCatcher catcher = new GhostCatcher();
    182166    bus.register(catcher);
     
    186170
    187171    List<DeadEvent> events = catcher.getEvents();
    188     assertEquals("One dead event should be delivered.", 1, events.size());
    189     assertEquals("The dead event should wrap the original event.", EVENT, events.get(0).getEvent());
    190   }
    191 
    192   @Test
    193   public void testDeadEventPosting() {
     172    assertEquals(1, events.size(), "One dead event should be delivered.");
     173    assertEquals(EVENT, events.get(0).getEvent(), "The dead event should wrap the original event.");
     174  }
     175
     176  @Test
     177  void testDeadEventPosting() {
    194178    GhostCatcher catcher = new GhostCatcher();
    195179    bus.register(catcher);
     
    198182
    199183    List<DeadEvent> events = catcher.getEvents();
    200     assertEquals("The explicit DeadEvent should be delivered.", 1, events.size());
    201     assertEquals("The dead event must not be re-wrapped.", EVENT, events.get(0).getEvent());
    202   }
    203 
    204   @Test
    205   public void testMissingSubscribe() {
     184    assertEquals(1, events.size(), "The explicit DeadEvent should be delivered.");
     185    assertEquals(EVENT, events.get(0).getEvent(), "The dead event must not be re-wrapped.");
     186  }
     187
     188  @Test
     189  void testMissingSubscribe() {
    206190    bus.register(new Object());
    207191  }
    208192
    209193  @Test
    210   public void testUnregister() {
     194  void testUnregister() {
    211195    StringCatcher catcher1 = new StringCatcher();
    212196    StringCatcher catcher2 = new StringCatcher();
    213     try {
    214       bus.unregister(catcher1);
    215       fail("Attempting to unregister an unregistered object succeeded");
    216     } catch (IllegalArgumentException expected) {
    217       // OK.
    218     }
     197    assertThrows(IllegalArgumentException.class, () -> bus.unregister(catcher1), "Attempting to unregister an unregistered object succeeded");
    219198
    220199    bus.register(catcher1);
     
    227206    expectedEvents.add(EVENT);
    228207
    229     assertEquals("Two correct events should be delivered.", expectedEvents, catcher1.getEvents());
    230 
    231     assertEquals(
    232         "One correct event should be delivered.", Arrays.asList(EVENT), catcher2.getEvents());
     208    assertEquals(expectedEvents, catcher1.getEvents(), "Two correct events should be delivered.");
     209
     210    assertEquals(Collections.singletonList(EVENT), catcher2.getEvents(), "One correct event should be delivered.");
    233211
    234212    bus.unregister(catcher1);
    235213    bus.post(EVENT);
    236214
    237     assertEquals(
    238         "Shouldn't catch any more events when unregistered.", expectedEvents, catcher1.getEvents());
    239     assertEquals("Two correct events should be delivered.", expectedEvents, catcher2.getEvents());
    240 
    241     try {
    242       bus.unregister(catcher1);
    243       fail("Attempting to unregister an unregistered object succeeded");
    244     } catch (IllegalArgumentException expected) {
    245       // OK.
    246     }
     215    assertEquals(expectedEvents, catcher1.getEvents(), "Shouldn't catch any more events when unregistered.");
     216    assertEquals(expectedEvents, catcher2.getEvents(), "Two correct events should be delivered.");
     217
     218    assertThrows(IllegalArgumentException.class, () -> bus.unregister(catcher1), "Attempting to unregister an unregistered object succeeded");
    247219
    248220    bus.unregister(catcher2);
    249221    bus.post(EVENT);
    250     assertEquals(
    251         "Shouldn't catch any more events when unregistered.", expectedEvents, catcher1.getEvents());
    252     assertEquals(
    253         "Shouldn't catch any more events when unregistered.", expectedEvents, catcher2.getEvents());
     222    assertEquals(expectedEvents, catcher1.getEvents(), "Shouldn't catch any more events when unregistered.");
     223    assertEquals(expectedEvents, catcher2.getEvents(), "Shouldn't catch any more events when unregistered.");
    254224  }
    255225
     
    258228
    259229  @Test
    260   public void testRegisterThreadSafety() throws Exception {
     230  void testRegisterThreadSafety() throws Exception {
    261231    List<StringCatcher> catchers = new CopyOnWriteArrayList<>();
    262232    List<Future<?>> futures = new ArrayList<>();
     
    269239      futures.get(i).get();
    270240    }
    271     assertEquals("Unexpected number of catchers in the list", numberOfCatchers, catchers.size());
    272     bus.post(EVENT);
    273     List<String> expectedEvents = Arrays.asList(EVENT);
     241    assertEquals(numberOfCatchers, catchers.size(), "Unexpected number of catchers in the list");
     242    bus.post(EVENT);
     243    List<String> expectedEvents = Collections.singletonList(EVENT);
    274244    for (StringCatcher catcher : catchers) {
    275       assertEquals(
    276           "One of the registered catchers did not receive an event.",
    277           expectedEvents,
    278           catcher.getEvents());
    279     }
    280   }
    281 
    282   @Test
    283   public void testToString() throws Exception {
     245      assertEquals(expectedEvents, catcher.getEvents(), "One of the registered catchers did not receive an event.");
     246    }
     247  }
     248
     249  @Test
     250  void testToString() {
    284251    EventBus eventBus = new EventBus("a b ; - \" < > / \\ €");
    285252    assertEquals("EventBus [a b ; - \" < > / \\ €]", eventBus.toString());
     
    293260   */
    294261  @Test
    295   public void testRegistrationWithBridgeMethod() {
     262  void testRegistrationWithBridgeMethod() {
    296263    final AtomicInteger calls = new AtomicInteger();
    297264    bus.register(
     
    347314   */
    348315  public static class GhostCatcher {
    349     private List<DeadEvent> events = new ArrayList<>();
     316    private final List<DeadEvent> events = new ArrayList<>();
    350317
    351318    @Subscribe
  • applications/editors/josm/plugins/eventbus/test/unit/org/openstreetmap/josm/eventbus/ReentrantEventsTest.java

    r34000 r36064  
    1717package org.openstreetmap.josm.eventbus;
    1818
    19 import static org.junit.Assert.assertEquals;
    20 import static org.junit.Assert.assertTrue;
     19import static org.junit.jupiter.api.Assertions.assertEquals;
    2120
    2221import java.util.ArrayList;
     
    2423import java.util.List;
    2524
    26 import org.junit.Test;
    27 import org.openstreetmap.josm.eventbus.EventBus;
    28 import org.openstreetmap.josm.eventbus.Subscribe;
     25import org.junit.jupiter.api.Assertions;
     26import org.junit.jupiter.api.Test;
    2927
    3028/**
     
    3331 * @author Jesse Wilson
    3432 */
    35 public class ReentrantEventsTest {
     33class ReentrantEventsTest {
    3634
    3735  static final String FIRST = "one";
     
    4139
    4240  @Test
    43   public void testNoReentrantEvents() {
     41  void testNoReentrantEvents() {
    4442    ReentrantEventsHater hater = new ReentrantEventsHater();
    4543    bus.register(hater);
     
    4745    bus.post(FIRST);
    4846
    49     assertEquals(
    50         "ReentrantEventHater expected 2 events",
    51         Arrays.asList(FIRST, SECOND),
    52         hater.eventsReceived);
     47    assertEquals(Arrays.asList(FIRST, SECOND), hater.eventsReceived, "ReentrantEventHater expected 2 events");
    5348  }
    5449
     
    7065    @Subscribe
    7166    public void listenForDoubles(Double event) {
    72       assertTrue("I received an event when I wasn't ready!", ready);
     67      Assertions.assertTrue(ready, "I received an event when I wasn't ready!");
    7368      eventsReceived.add(event);
    7469    }
     
    7671
    7772  @Test
    78   public void testEventOrderingIsPredictable() {
     73  void testEventOrderingIsPredictable() {
    7974    EventProcessor processor = new EventProcessor();
    8075    bus.register(processor);
     
    8580    bus.post(FIRST);
    8681
    87     assertEquals(
    88         "EventRecorder expected events in order",
    89         Arrays.asList(FIRST, SECOND),
    90         recorder.eventsReceived);
     82    assertEquals(Arrays.asList(FIRST, SECOND), recorder.eventsReceived, "EventRecorder expected events in order");
    9183  }
    9284
  • applications/editors/josm/plugins/eventbus/test/unit/org/openstreetmap/josm/eventbus/StringCatcher.java

    r34000 r36064  
    1717package org.openstreetmap.josm.eventbus;
    1818
    19 import static org.junit.Assert.fail;
     19import static org.junit.jupiter.api.Assertions.fail;
    2020
    2121import java.util.ArrayList;
    2222import java.util.List;
    23 
    24 import org.openstreetmap.josm.eventbus.Subscribe;
    2523
    2624/**
     
    3331 */
    3432public class StringCatcher {
    35   private List<String> events = new ArrayList<>();
     33  private final List<String> events = new ArrayList<>();
    3634
    3735  @Subscribe
  • applications/editors/josm/plugins/eventbus/test/unit/org/openstreetmap/josm/eventbus/SubscriberRegistryTest.java

    r34000 r36064  
    1717package org.openstreetmap.josm.eventbus;
    1818
    19 import static org.junit.Assert.assertEquals;
    20 import static org.junit.Assert.assertFalse;
    21 import static org.junit.Assert.assertTrue;
    22 import static org.junit.Assert.fail;
     19import static org.junit.jupiter.api.Assertions.assertEquals;
     20import static org.junit.jupiter.api.Assertions.assertFalse;
     21import static org.junit.jupiter.api.Assertions.assertThrows;
     22import static org.junit.jupiter.api.Assertions.assertTrue;
    2323
    2424import java.util.Arrays;
     
    2626import java.util.Iterator;
    2727
    28 import org.junit.Ignore;
    29 import org.junit.Test;
    30 import org.openstreetmap.josm.eventbus.EventBus;
    31 import org.openstreetmap.josm.eventbus.Subscribe;
    32 import org.openstreetmap.josm.eventbus.Subscriber;
    33 import org.openstreetmap.josm.eventbus.SubscriberRegistry;
     28import org.junit.jupiter.api.Assertions;
     29import org.junit.jupiter.api.Disabled;
     30import org.junit.jupiter.api.Test;
     31
    3432
    3533/**
     
    4341
    4442  @Test
    45   public void testRegister() {
     43  void testRegister() {
    4644    assertEquals(0, registry.getSubscribersForTesting(String.class).size());
    4745
     
    5856
    5957  @Test
    60   public void testUnregister() {
     58  void testUnregister() {
    6159    StringSubscriber s1 = new StringSubscriber();
    6260    StringSubscriber s2 = new StringSubscriber();
     
    7371
    7472  @Test
    75   public void testUnregister_notRegistered() {
    76     try {
    77       registry.unregister(new StringSubscriber());
    78       fail();
    79     } catch (IllegalArgumentException expected) {
    80     }
     73  void testUnregisterNotRegistered() {
     74    StringSubscriber temp = new StringSubscriber();
     75    assertThrows(IllegalArgumentException.class, () -> registry.unregister(temp));
    8176
    8277    StringSubscriber s1 = new StringSubscriber();
    8378    registry.register(s1);
    84     try {
    85       registry.unregister(new StringSubscriber());
    86       fail();
    87     } catch (IllegalArgumentException expected) {
    88       // a StringSubscriber was registered, but not the same one we tried to unregister
    89     }
     79    // a StringSubscriber was registered, but not the same one we tried to unregister
     80    assertThrows(IllegalArgumentException.class, () -> registry.unregister(temp));
    9081
    9182    registry.unregister(s1);
    92 
    93     try {
    94       registry.unregister(s1);
    95       fail();
    96     } catch (IllegalArgumentException expected) {
    97     }
    98   }
    99 
    100   @Test
    101   public void testGetSubscribers() {
     83    assertThrows(IllegalArgumentException.class, () -> registry.unregister(s1));
     84  }
     85
     86  @Test
     87  void testGetSubscribers() {
    10288    assertEquals(0, size(registry.getSubscribers("")));
    10389
     
    120106
    121107  @Test
    122   @Ignore("FIXME")
    123   public void testGetSubscribers_returnsImmutableSnapshot() {
     108  @Disabled("FIXME")
     109  void testGetSubscribersReturnsImmutableSnapshot() {
    124110    StringSubscriber s1 = new StringSubscriber();
    125111    StringSubscriber s2 = new StringSubscriber();
     
    186172
    187173  @Test
    188   public void testFlattenHierarchy() {
    189     assertEquals(
    190         new HashSet<>(Arrays.asList(
    191             Object.class,
    192             HierarchyFixtureInterface.class,
    193             HierarchyFixtureSubinterface.class,
    194             HierarchyFixtureParent.class,
    195             HierarchyFixture.class)),
    196         SubscriberRegistry.flattenHierarchy(HierarchyFixture.class));
     174  void testFlattenHierarchy() {
     175    assertEquals(new HashSet<>(Arrays.asList(
     176        Object.class,
     177        HierarchyFixtureInterface.class,
     178        HierarchyFixtureSubinterface.class,
     179        HierarchyFixtureParent.class,
     180        HierarchyFixture.class)), SubscriberRegistry.flattenHierarchy(HierarchyFixture.class));
    197181  }
    198182
  • applications/editors/josm/plugins/eventbus/test/unit/org/openstreetmap/josm/eventbus/SubscriberTest.java

    r34000 r36064  
    1818
    1919//import com.google.common.testing.EqualsTester;
     20
     21import static org.junit.jupiter.api.Assertions.assertSame;
     22import static org.junit.jupiter.api.Assertions.assertThrows;
     23import static org.junit.jupiter.api.Assertions.assertTrue;
     24
    2025import java.lang.reflect.InvocationTargetException;
    2126import java.lang.reflect.Method;
    2227
    23 import org.junit.Ignore;
    24 import org.junit.Test;
    25 import org.openstreetmap.josm.eventbus.AllowConcurrentEvents;
    26 import org.openstreetmap.josm.eventbus.EventBus;
    27 import org.openstreetmap.josm.eventbus.Subscribe;
    28 import org.openstreetmap.josm.eventbus.Subscriber;
    29 
    30 import junit.framework.TestCase;
     28import org.junit.jupiter.api.Assertions;
     29import org.junit.jupiter.api.BeforeEach;
     30import org.junit.jupiter.api.Disabled;
     31import org.junit.jupiter.api.Test;
    3132
    3233/**
     
    3637 * @author Colin Decker
    3738 */
    38 public class SubscriberTest extends TestCase {
     39class SubscriberTest {
    3940
    4041  private static final Object FIXTURE_ARGUMENT = new Object();
     
    4445  private Object methodArgument;
    4546
    46   @Override
    47   protected void setUp() throws Exception {
     47  @BeforeEach
     48  protected void setUp() {
    4849    bus = new EventBus();
    4950    methodCalled = false;
     
    5253
    5354  @Test
    54   public void testCreate() {
     55  void testCreate() {
    5556    Subscriber s1 = Subscriber.create(bus, this, getTestSubscriberMethod("recordingMethod"));
    5657    assertTrue(s1 instanceof Subscriber.SynchronizedSubscriber);
     
    5859    // a thread-safe method should not create a synchronized subscriber
    5960    Subscriber s2 = Subscriber.create(bus, this, getTestSubscriberMethod("threadSafeMethod"));
    60     assertFalse(s2 instanceof Subscriber.SynchronizedSubscriber);
     61    Assertions.assertFalse(s2 instanceof Subscriber.SynchronizedSubscriber);
    6162  }
    6263
    6364  @Test
    64   public void testInvokeSubscriberMethod_basicMethodCall() throws Throwable {
     65  void testInvokeSubscriberMethodBasicMethodCall() throws Throwable {
    6566    Method method = getTestSubscriberMethod("recordingMethod");
    6667    Subscriber subscriber = Subscriber.create(bus, this, method);
     
    6869    subscriber.invokeSubscriberMethod(FIXTURE_ARGUMENT);
    6970
    70     assertTrue("Subscriber must call provided method", methodCalled);
    71     assertTrue(
    72         "Subscriber argument must be exactly the provided object.",
    73         methodArgument == FIXTURE_ARGUMENT);
     71    assertTrue(methodCalled, "Subscriber must call provided method");
     72    assertSame(methodArgument, FIXTURE_ARGUMENT, "Subscriber argument must be exactly the provided object.");
    7473  }
    7574
    7675  @Test
    77   public void testInvokeSubscriberMethod_exceptionWrapping() throws Throwable {
     76  void testInvokeSubscriberMethodExceptionWrapping() {
    7877    Method method = getTestSubscriberMethod("exceptionThrowingMethod");
    7978    Subscriber subscriber = Subscriber.create(bus, this, method);
    8079
    81     try {
    82       subscriber.invokeSubscriberMethod(FIXTURE_ARGUMENT);
    83       fail("Subscribers whose methods throw must throw InvocationTargetException");
    84     } catch (InvocationTargetException expected) {
    85       assertTrue(expected.getCause() instanceof IntentionalException);
    86     }
     80    InvocationTargetException ite = assertThrows(InvocationTargetException.class, () -> subscriber.invokeSubscriberMethod(FIXTURE_ARGUMENT),
     81            "Subscribers whose methods throw must throw InvocationTargetException");
     82    assertTrue(ite.getCause() instanceof IntentionalException);
    8783  }
    8884
    8985  @Test
    90   public void testInvokeSubscriberMethod_errorPassthrough() throws Throwable {
     86  void testInvokeSubscriberMethodErrorPassthrough() {
    9187    Method method = getTestSubscriberMethod("errorThrowingMethod");
    9288    Subscriber subscriber = Subscriber.create(bus, this, method);
    9389
    94     try {
    95       subscriber.invokeSubscriberMethod(FIXTURE_ARGUMENT);
    96       fail("Subscribers whose methods throw Errors must rethrow them");
    97     } catch (JudgmentError expected) {
    98     }
     90    assertThrows(JudgmentError.class, () -> subscriber.invokeSubscriberMethod(FIXTURE_ARGUMENT),
     91      "Subscribers whose methods throw Errors must rethrow them");
    9992  }
    10093
    10194  @Test
    102   @Ignore("FIXME")
    103   public void testEquals() throws Exception {
     95  @Disabled("FIXME")
     96  void testEquals() {
    10497    /*Method charAt = String.class.getMethod("charAt", int.class);
    10598    Method concat = String.class.getMethod("concat", String.class);
     
    128121  @Subscribe
    129122  public void recordingMethod(Object arg) {
    130     assertFalse(methodCalled);
     123    Assertions.assertFalse(methodCalled);
    131124    methodCalled = true;
    132125    methodArgument = arg;
     
    139132
    140133  /** Local exception subclass to check variety of exception thrown. */
    141   class IntentionalException extends Exception {
     134  static class IntentionalException extends Exception {
    142135
    143136    private static final long serialVersionUID = -2500191180248181379L;
  • applications/editors/josm/plugins/eventbus/test/unit/org/openstreetmap/josm/eventbus/outside/AnnotatedSubscriberFinderTests.java

    r34000 r36064  
    1717package org.openstreetmap.josm.eventbus.outside;
    1818
    19 import static org.junit.Assert.assertTrue;
     19
     20import static org.junit.jupiter.api.Assertions.assertTrue;
    2021
    2122import java.util.ArrayList;
    2223import java.util.List;
    2324
    24 import org.junit.After;
    25 import org.junit.Before;
    26 import org.junit.Test;
     25import org.junit.jupiter.api.AfterEach;
     26import org.junit.jupiter.api.BeforeEach;
     27import org.junit.jupiter.api.Test;
    2728import org.openstreetmap.josm.eventbus.EventBus;
    2829import org.openstreetmap.josm.eventbus.Subscribe;
     
    4849    }
    4950
    50     @Before
    51     public void setUp() throws Exception {
     51    @BeforeEach
     52    public void setUp() {
    5253      subscriber = createSubscriber();
    5354      EventBus bus = new EventBus();
     
    5657    }
    5758
    58     @After
    59     public void tearDown() throws Exception {
     59    @AfterEach
     60    public void tearDown() {
    6061      subscriber = null;
    6162    }
     
    6566   * We break the tests up based on whether they are annotated or abstract in the superclass.
    6667   */
    67   public static class BaseSubscriberFinderTest
     68  static class BaseSubscriberFinderTest
    6869      extends AbstractEventBusTestParent<BaseSubscriberFinderTest.Subscriber> {
    6970    static class Subscriber {
     
    8283
    8384    @Test
    84     public void testNonSubscriber() {
     85    void testNonSubscriber() {
    8586      assertTrue(getSubscriber().nonSubscriberEvents.isEmpty());
    8687    }
    8788
    8889    @Test
    89     public void testSubscriber() {
     90    void testSubscriber() {
    9091      assertTrue(getSubscriber().subscriberEvents.contains(EVENT));
    9192    }
     
    9798  }
    9899
    99   public static class AnnotatedAndAbstractInSuperclassTest
     100  static class AnnotatedAndAbstractInSuperclassTest
    100101      extends AbstractEventBusTestParent<AnnotatedAndAbstractInSuperclassTest.SubClass> {
    101102    abstract static class SuperClass {
     
    124125
    125126    @Test
    126     public void testOverriddenAndAnnotatedInSubclass() {
     127    void testOverriddenAndAnnotatedInSubclass() {
    127128      assertTrue(getSubscriber().overriddenAndAnnotatedInSubclassEvents.contains(EVENT));
    128129    }
    129130
    130131    @Test
    131     public void testOverriddenNotAnnotatedInSubclass() {
     132    void testOverriddenNotAnnotatedInSubclass() {
    132133      assertTrue(getSubscriber().overriddenInSubclassEvents.contains(EVENT));
    133134    }
     
    139140  }
    140141
    141   public static class AnnotatedNotAbstractInSuperclassTest
     142  static class AnnotatedNotAbstractInSuperclassTest
    142143      extends AbstractEventBusTestParent<AnnotatedNotAbstractInSuperclassTest.SubClass> {
    143144    static class SuperClass {
     
    206207
    207208    @Test
    208     public void testNotOverriddenInSubclass() {
     209    void testNotOverriddenInSubclass() {
    209210      assertTrue(getSubscriber().notOverriddenInSubclassEvents.contains(EVENT));
    210211    }
    211212
    212213    @Test
    213     public void testOverriddenNotAnnotatedInSubclass() {
     214    void testOverriddenNotAnnotatedInSubclass() {
    214215      assertTrue(getSubscriber().overriddenNotAnnotatedInSubclassEvents.contains(EVENT));
    215216    }
    216217
    217218    @Test
    218     public void testDifferentlyOverriddenNotAnnotatedInSubclass() {
     219    void testDifferentlyOverriddenNotAnnotatedInSubclass() {
    219220      assertTrue(getSubscriber().differentlyOverriddenNotAnnotatedInSubclassGoodEvents
    220221          .contains(EVENT));
     
    223224
    224225    @Test
    225     public void testOverriddenAndAnnotatedInSubclass() {
     226    void testOverriddenAndAnnotatedInSubclass() {
    226227      assertTrue(getSubscriber().overriddenAndAnnotatedInSubclassEvents.contains(EVENT));
    227228    }
    228229
    229230    @Test
    230     public void testDifferentlyOverriddenAndAnnotatedInSubclass() {
     231    void testDifferentlyOverriddenAndAnnotatedInSubclass() {
    231232      assertTrue(getSubscriber().differentlyOverriddenAnnotatedInSubclassGoodEvents
    232233          .contains(EVENT));
     
    240241  }
    241242
    242   public static class AbstractNotAnnotatedInSuperclassTest
     243  static class AbstractNotAnnotatedInSuperclassTest
    243244      extends AbstractEventBusTestParent<AbstractNotAnnotatedInSuperclassTest.SubClass> {
    244245    abstract static class SuperClass {
     
    265266
    266267    @Test
    267     public void testOverriddenAndAnnotatedInSubclass() {
     268    void testOverriddenAndAnnotatedInSubclass() {
    268269      assertTrue(getSubscriber().overriddenAndAnnotatedInSubclassEvents.contains(EVENT));
    269270    }
    270271
    271272    @Test
    272     public void testOverriddenInSubclassNowhereAnnotated() {
     273    void testOverriddenInSubclassNowhereAnnotated() {
    273274      assertTrue(getSubscriber().overriddenInSubclassNowhereAnnotatedEvents.isEmpty());
    274275    }
     
    280281  }
    281282
    282   public static class NeitherAbstractNorAnnotatedInSuperclassTest
     283  static class NeitherAbstractNorAnnotatedInSuperclassTest
    283284      extends AbstractEventBusTestParent<NeitherAbstractNorAnnotatedInSuperclassTest.SubClass> {
    284285    static class SuperClass {
     
    314315
    315316    @Test
    316     public void testNeitherOverriddenNorAnnotated() {
     317    void testNeitherOverriddenNorAnnotated() {
    317318      assertTrue(getSubscriber().neitherOverriddenNorAnnotatedEvents.isEmpty());
    318319    }
    319320
    320321    @Test
    321     public void testOverriddenInSubclassNowhereAnnotated() {
     322    void testOverriddenInSubclassNowhereAnnotated() {
    322323      assertTrue(getSubscriber().overriddenInSubclassNowhereAnnotatedEvents.isEmpty());
    323324    }
    324325
    325326    @Test
    326     public void testOverriddenAndAnnotatedInSubclass() {
     327    void testOverriddenAndAnnotatedInSubclass() {
    327328      assertTrue(getSubscriber().overriddenAndAnnotatedInSubclassEvents.contains(EVENT));
    328329    }
     
    334335  }
    335336
    336   public static class DeepInterfaceTest
     337  static class DeepInterfaceTest
    337338      extends AbstractEventBusTestParent<DeepInterfaceTest.SubscriberClass> {
    338339    interface Interface1 {
     
    427428
    428429    @Test
    429     public void testAnnotatedIn1() {
     430    void testAnnotatedIn1() {
    430431      assertTrue(getSubscriber().annotatedIn1Events.contains(EVENT));
    431432    }
    432433
    433434    @Test
    434     public void testAnnotatedIn2() {
     435    void testAnnotatedIn2() {
    435436      assertTrue(getSubscriber().annotatedIn2Events.contains(EVENT));
    436437    }
    437438
    438439    @Test
    439     public void testAnnotatedIn1And2() {
     440    void testAnnotatedIn1And2() {
    440441      assertTrue(getSubscriber().annotatedIn1And2Events.contains(EVENT));
    441442    }
    442443
    443444    @Test
    444     public void testAnnotatedIn1And2AndClass() {
     445    void testAnnotatedIn1And2AndClass() {
    445446      assertTrue(getSubscriber().annotatedIn1And2AndClassEvents.contains(EVENT));
    446447    }
    447448
    448449    @Test
    449     public void testDeclaredIn1AnnotatedIn2() {
     450    void testDeclaredIn1AnnotatedIn2() {
    450451      assertTrue(getSubscriber().declaredIn1AnnotatedIn2Events.contains(EVENT));
    451452    }
    452453
    453454    @Test
    454     public void testDeclaredIn1AnnotatedInClass() {
     455    void testDeclaredIn1AnnotatedInClass() {
    455456      assertTrue(getSubscriber().declaredIn1AnnotatedInClassEvents.contains(EVENT));
    456457    }
    457458
    458459    @Test
    459     public void testDeclaredIn2AnnotatedInClass() {
     460    void testDeclaredIn2AnnotatedInClass() {
    460461      assertTrue(getSubscriber().declaredIn2AnnotatedInClassEvents.contains(EVENT));
    461462    }
    462463
    463464    @Test
    464     public void testNowhereAnnotated() {
     465    void testNowhereAnnotated() {
    465466      assertTrue(getSubscriber().nowhereAnnotatedEvents.isEmpty());
    466467    }
  • applications/editors/josm/plugins/eventbus/test/unit/org/openstreetmap/josm/eventbus/outside/OutsideEventBusTest.java

    r34000 r36064  
    1717package org.openstreetmap.josm.eventbus.outside;
    1818
    19 import static org.junit.Assert.assertEquals;
     19import static org.junit.jupiter.api.Assertions.assertEquals;
    2020
    2121import java.util.concurrent.atomic.AtomicInteger;
    2222import java.util.concurrent.atomic.AtomicReference;
    2323
    24 import org.junit.Test;
     24import org.junit.jupiter.api.Test;
    2525import org.openstreetmap.josm.eventbus.EventBus;
    2626import org.openstreetmap.josm.eventbus.Subscribe;
     
    3131 * @author Louis Wasserman
    3232 */
    33 public class OutsideEventBusTest {
     33class OutsideEventBusTest {
    3434
    3535  /*
     
    3939   */
    4040  @Test
    41   public void testAnonymous() {
     41  void testAnonymous() {
    4242    final AtomicReference<String> holder = new AtomicReference<>();
    4343    final AtomicInteger deliveries = new AtomicInteger();
     
    5555    bus.post(EVENT);
    5656
    57     assertEquals("Only one event should be delivered.", 1, deliveries.get());
    58     assertEquals("Correct string should be delivered.", EVENT, holder.get());
     57    assertEquals(1, deliveries.get(), "Only one event should be delivered.");
     58    assertEquals(EVENT, holder.get(), "Correct string should be delivered.");
    5959  }
    6060}
Note: See TracChangeset for help on using the changeset viewer.