Changeset 36064 in osm for applications/editors/josm/plugins/eventbus
- Timestamp:
- 2023-03-21T14:49:10+01:00 (23 months ago)
- 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 17 17 package org.openstreetmap.josm.eventbus; 18 18 19 import static org.junit. Assert.assertEquals;20 import static org.junit. Assert.assertTrue;19 import static org.junit.jupiter.api.Assertions.assertEquals; 20 import static org.junit.jupiter.api.Assertions.assertTrue; 21 21 22 22 import java.util.ArrayList; … … 24 24 import java.util.concurrent.Executor; 25 25 26 import org.junit.Before; 27 import org.junit.Test; 28 import org.openstreetmap.josm.eventbus.AsyncEventBus; 26 import org.junit.jupiter.api.BeforeEach; 27 import org.junit.jupiter.api.Test; 29 28 30 29 /** … … 33 32 * @author Cliff Biffle 34 33 */ 35 publicclass AsyncEventBusTest {34 class AsyncEventBusTest { 36 35 private static final String EVENT = "Hello"; 37 36 … … 41 40 private AsyncEventBus bus; 42 41 43 @Before 44 publicvoid setUp()throws Exception{42 @BeforeEach 43 void setUp() { 45 44 executor = new FakeExecutor(); 46 45 bus = new AsyncEventBus(executor); … … 48 47 49 48 @Test 50 publicvoid testBasicDistribution() {49 void testBasicDistribution() { 51 50 StringCatcher catcher = new StringCatcher(); 52 51 bus.register(catcher); … … 56 55 57 56 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."); 59 58 60 59 // Now we find the task in our Executor and explicitly activate it. 61 60 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."); 63 62 64 63 tasks.get(0).run(); 65 64 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."); 68 67 } 69 68 -
applications/editors/josm/plugins/eventbus/test/unit/org/openstreetmap/josm/eventbus/DispatcherTest.java
r34000 r36064 27 27 import java.util.concurrent.CyclicBarrier; 28 28 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; 29 import org.junit.jupiter.api.Disabled; 30 import org.junit.jupiter.api.Test; 35 31 36 32 /** … … 40 36 */ 41 37 42 publicclass DispatcherTest {38 class DispatcherTest { 43 39 44 40 private final EventBus bus = new EventBus(); … … 66 62 67 63 @Test 68 @ Ignore("FIXME")69 publicvoid testPerThreadQueuedDispatcher() {64 @Disabled("FIXME") 65 void testPerThreadQueuedDispatcher() { 70 66 dispatcher = Dispatcher.perThreadDispatchQueue(); 71 67 dispatcher.dispatch(1, integerSubscribers.iterator()); … … 87 83 88 84 @Test 89 @ Ignore("FIXME")90 publicvoid testLegacyAsyncDispatcher() {85 @Disabled("FIXME") 86 void testLegacyAsyncDispatcher() { 91 87 dispatcher = Dispatcher.legacyAsync(); 92 88 … … 95 91 96 92 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 } 105 99 106 dispatcher.dispatch(2, integerSubscribers.iterator()); 107 latch.countDown(); 108 } 100 dispatcher.dispatch(2, integerSubscribers.iterator()); 101 latch.countDown(); 109 102 }) 110 103 .start(); 111 104 112 105 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 } 121 112 122 dispatcher.dispatch("foo", stringSubscribers.iterator()); 123 latch.countDown(); 124 } 113 dispatcher.dispatch("foo", stringSubscribers.iterator()); 114 latch.countDown(); 125 115 }) 126 116 .start(); … … 135 125 136 126 @Test 137 @ Ignore("FIXME")138 publicvoid testImmediateDispatcher() {127 @Disabled("FIXME") 128 void testImmediateDispatcher() { 139 129 dispatcher = Dispatcher.immediate(); 140 130 dispatcher.dispatch(1, integerSubscribers.iterator()); -
applications/editors/josm/plugins/eventbus/test/unit/org/openstreetmap/josm/eventbus/EventBusTest.java
r34000 r36064 17 17 package org.openstreetmap.josm.eventbus; 18 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.fail; 19 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; 20 import static org.junit.jupiter.api.Assertions.assertEquals; 21 import static org.junit.jupiter.api.Assertions.assertThrows; 21 22 22 23 import java.util.ArrayList; 23 import java.util. Arrays;24 import java.util.Collections; 24 25 import java.util.List; 25 26 import java.util.concurrent.CopyOnWriteArrayList; … … 29 30 import java.util.concurrent.atomic.AtomicInteger; 30 31 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; 32 import org.junit.jupiter.api.BeforeEach; 33 import org.junit.jupiter.api.Test; 38 34 39 35 /** … … 42 38 * @author Cliff Biffle 43 39 */ 44 publicclass EventBusTest {40 class EventBusTest { 45 41 private static final String EVENT = "Hello"; 46 42 private static final String BUS_IDENTIFIER = "test-bus"; … … 48 44 private EventBus bus; 49 45 50 @Before 51 publicvoid setUp()throws Exception{46 @BeforeEach 47 void setUp() { 52 48 bus = new EventBus(BUS_IDENTIFIER); 53 49 } 54 50 55 51 @Test 56 publicvoid testBasicCatcherDistribution() {52 void testBasicCatcherDistribution() { 57 53 StringCatcher catcher = new StringCatcher(); 58 54 bus.register(catcher); … … 60 56 61 57 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."); 64 60 } 65 61 … … 71 67 */ 72 68 @Test 73 publicvoid testPolymorphicDistribution() {69 void testPolymorphicDistribution() { 74 70 // Three catchers for related types String, Object, and Comparable<?>. 75 71 // String isa Object … … 101 97 // Two additional event types: Object and Comparable<?> (played by Integer) 102 98 Object objEvent = new Object(); 103 Object compEvent = new Integer(6);99 Object compEvent = 6; 104 100 105 101 bus.post(EVENT); … … 109 105 // Check the StringCatcher... 110 106 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."); 113 109 114 110 // 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."); 120 115 121 116 // 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 { 130 124 final RecordingSubscriberExceptionHandler handler = new RecordingSubscriberExceptionHandler(); 131 125 final EventBus eventBus = new EventBus(handler); … … 142 136 eventBus.post(EVENT); 143 137 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 { 156 147 final EventBus eventBus = 157 148 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 }); 164 152 final Object subscriber = 165 153 new Object() { … … 170 158 }; 171 159 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() { 181 165 GhostCatcher catcher = new GhostCatcher(); 182 166 bus.register(catcher); … … 186 170 187 171 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 publicvoid 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() { 194 178 GhostCatcher catcher = new GhostCatcher(); 195 179 bus.register(catcher); … … 198 182 199 183 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 publicvoid 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() { 206 190 bus.register(new Object()); 207 191 } 208 192 209 193 @Test 210 publicvoid testUnregister() {194 void testUnregister() { 211 195 StringCatcher catcher1 = new StringCatcher(); 212 196 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"); 219 198 220 199 bus.register(catcher1); … … 227 206 expectedEvents.add(EVENT); 228 207 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."); 233 211 234 212 bus.unregister(catcher1); 235 213 bus.post(EVENT); 236 214 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"); 247 219 248 220 bus.unregister(catcher2); 249 221 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."); 254 224 } 255 225 … … 258 228 259 229 @Test 260 publicvoid testRegisterThreadSafety() throws Exception {230 void testRegisterThreadSafety() throws Exception { 261 231 List<StringCatcher> catchers = new CopyOnWriteArrayList<>(); 262 232 List<Future<?>> futures = new ArrayList<>(); … … 269 239 futures.get(i).get(); 270 240 } 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); 274 244 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() { 284 251 EventBus eventBus = new EventBus("a b ; - \" < > / \\ €"); 285 252 assertEquals("EventBus [a b ; - \" < > / \\ €]", eventBus.toString()); … … 293 260 */ 294 261 @Test 295 publicvoid testRegistrationWithBridgeMethod() {262 void testRegistrationWithBridgeMethod() { 296 263 final AtomicInteger calls = new AtomicInteger(); 297 264 bus.register( … … 347 314 */ 348 315 public static class GhostCatcher { 349 private List<DeadEvent> events = new ArrayList<>(); 316 private final List<DeadEvent> events = new ArrayList<>(); 350 317 351 318 @Subscribe -
applications/editors/josm/plugins/eventbus/test/unit/org/openstreetmap/josm/eventbus/ReentrantEventsTest.java
r34000 r36064 17 17 package org.openstreetmap.josm.eventbus; 18 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertTrue; 19 import static org.junit.jupiter.api.Assertions.assertEquals; 21 20 22 21 import java.util.ArrayList; … … 24 23 import java.util.List; 25 24 26 import org.junit.Test; 27 import org.openstreetmap.josm.eventbus.EventBus; 28 import org.openstreetmap.josm.eventbus.Subscribe; 25 import org.junit.jupiter.api.Assertions; 26 import org.junit.jupiter.api.Test; 29 27 30 28 /** … … 33 31 * @author Jesse Wilson 34 32 */ 35 publicclass ReentrantEventsTest {33 class ReentrantEventsTest { 36 34 37 35 static final String FIRST = "one"; … … 41 39 42 40 @Test 43 publicvoid testNoReentrantEvents() {41 void testNoReentrantEvents() { 44 42 ReentrantEventsHater hater = new ReentrantEventsHater(); 45 43 bus.register(hater); … … 47 45 bus.post(FIRST); 48 46 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"); 53 48 } 54 49 … … 70 65 @Subscribe 71 66 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!"); 73 68 eventsReceived.add(event); 74 69 } … … 76 71 77 72 @Test 78 publicvoid testEventOrderingIsPredictable() {73 void testEventOrderingIsPredictable() { 79 74 EventProcessor processor = new EventProcessor(); 80 75 bus.register(processor); … … 85 80 bus.post(FIRST); 86 81 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"); 91 83 } 92 84 -
applications/editors/josm/plugins/eventbus/test/unit/org/openstreetmap/josm/eventbus/StringCatcher.java
r34000 r36064 17 17 package org.openstreetmap.josm.eventbus; 18 18 19 import static org.junit. Assert.fail;19 import static org.junit.jupiter.api.Assertions.fail; 20 20 21 21 import java.util.ArrayList; 22 22 import java.util.List; 23 24 import org.openstreetmap.josm.eventbus.Subscribe;25 23 26 24 /** … … 33 31 */ 34 32 public class StringCatcher { 35 private List<String> events = new ArrayList<>(); 33 private final List<String> events = new ArrayList<>(); 36 34 37 35 @Subscribe -
applications/editors/josm/plugins/eventbus/test/unit/org/openstreetmap/josm/eventbus/SubscriberRegistryTest.java
r34000 r36064 17 17 package org.openstreetmap.josm.eventbus; 18 18 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;19 import static org.junit.jupiter.api.Assertions.assertEquals; 20 import static org.junit.jupiter.api.Assertions.assertFalse; 21 import static org.junit.jupiter.api.Assertions.assertThrows; 22 import static org.junit.jupiter.api.Assertions.assertTrue; 23 23 24 24 import java.util.Arrays; … … 26 26 import java.util.Iterator; 27 27 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; 28 import org.junit.jupiter.api.Assertions; 29 import org.junit.jupiter.api.Disabled; 30 import org.junit.jupiter.api.Test; 31 34 32 35 33 /** … … 43 41 44 42 @Test 45 publicvoid testRegister() {43 void testRegister() { 46 44 assertEquals(0, registry.getSubscribersForTesting(String.class).size()); 47 45 … … 58 56 59 57 @Test 60 publicvoid testUnregister() {58 void testUnregister() { 61 59 StringSubscriber s1 = new StringSubscriber(); 62 60 StringSubscriber s2 = new StringSubscriber(); … … 73 71 74 72 @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)); 81 76 82 77 StringSubscriber s1 = new StringSubscriber(); 83 78 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)); 90 81 91 82 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() { 102 88 assertEquals(0, size(registry.getSubscribers(""))); 103 89 … … 120 106 121 107 @Test 122 @ Ignore("FIXME")123 publicvoid testGetSubscribers_returnsImmutableSnapshot() {108 @Disabled("FIXME") 109 void testGetSubscribersReturnsImmutableSnapshot() { 124 110 StringSubscriber s1 = new StringSubscriber(); 125 111 StringSubscriber s2 = new StringSubscriber(); … … 186 172 187 173 @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)); 197 181 } 198 182 -
applications/editors/josm/plugins/eventbus/test/unit/org/openstreetmap/josm/eventbus/SubscriberTest.java
r34000 r36064 18 18 19 19 //import com.google.common.testing.EqualsTester; 20 21 import static org.junit.jupiter.api.Assertions.assertSame; 22 import static org.junit.jupiter.api.Assertions.assertThrows; 23 import static org.junit.jupiter.api.Assertions.assertTrue; 24 20 25 import java.lang.reflect.InvocationTargetException; 21 26 import java.lang.reflect.Method; 22 27 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; 28 import org.junit.jupiter.api.Assertions; 29 import org.junit.jupiter.api.BeforeEach; 30 import org.junit.jupiter.api.Disabled; 31 import org.junit.jupiter.api.Test; 31 32 32 33 /** … … 36 37 * @author Colin Decker 37 38 */ 38 publicclass SubscriberTestextends TestCase{39 class SubscriberTest { 39 40 40 41 private static final Object FIXTURE_ARGUMENT = new Object(); … … 44 45 private Object methodArgument; 45 46 46 @ Override47 protected void setUp() throws Exception{47 @BeforeEach 48 protected void setUp() { 48 49 bus = new EventBus(); 49 50 methodCalled = false; … … 52 53 53 54 @Test 54 publicvoid testCreate() {55 void testCreate() { 55 56 Subscriber s1 = Subscriber.create(bus, this, getTestSubscriberMethod("recordingMethod")); 56 57 assertTrue(s1 instanceof Subscriber.SynchronizedSubscriber); … … 58 59 // a thread-safe method should not create a synchronized subscriber 59 60 Subscriber s2 = Subscriber.create(bus, this, getTestSubscriberMethod("threadSafeMethod")); 60 assertFalse(s2 instanceof Subscriber.SynchronizedSubscriber); 61 Assertions.assertFalse(s2 instanceof Subscriber.SynchronizedSubscriber); 61 62 } 62 63 63 64 @Test 64 publicvoid testInvokeSubscriberMethod_basicMethodCall() throws Throwable {65 void testInvokeSubscriberMethodBasicMethodCall() throws Throwable { 65 66 Method method = getTestSubscriberMethod("recordingMethod"); 66 67 Subscriber subscriber = Subscriber.create(bus, this, method); … … 68 69 subscriber.invokeSubscriberMethod(FIXTURE_ARGUMENT); 69 70 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."); 74 73 } 75 74 76 75 @Test 77 publicvoid testInvokeSubscriberMethod_exceptionWrapping()throws Throwable{76 void testInvokeSubscriberMethodExceptionWrapping() { 78 77 Method method = getTestSubscriberMethod("exceptionThrowingMethod"); 79 78 Subscriber subscriber = Subscriber.create(bus, this, method); 80 79 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); 87 83 } 88 84 89 85 @Test 90 publicvoid testInvokeSubscriberMethod_errorPassthrough()throws Throwable{86 void testInvokeSubscriberMethodErrorPassthrough() { 91 87 Method method = getTestSubscriberMethod("errorThrowingMethod"); 92 88 Subscriber subscriber = Subscriber.create(bus, this, method); 93 89 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"); 99 92 } 100 93 101 94 @Test 102 @ Ignore("FIXME")103 publicvoid testEquals()throws Exception{95 @Disabled("FIXME") 96 void testEquals() { 104 97 /*Method charAt = String.class.getMethod("charAt", int.class); 105 98 Method concat = String.class.getMethod("concat", String.class); … … 128 121 @Subscribe 129 122 public void recordingMethod(Object arg) { 130 assertFalse(methodCalled); 123 Assertions.assertFalse(methodCalled); 131 124 methodCalled = true; 132 125 methodArgument = arg; … … 139 132 140 133 /** Local exception subclass to check variety of exception thrown. */ 141 class IntentionalException extends Exception { 134 static class IntentionalException extends Exception { 142 135 143 136 private static final long serialVersionUID = -2500191180248181379L; -
applications/editors/josm/plugins/eventbus/test/unit/org/openstreetmap/josm/eventbus/outside/AnnotatedSubscriberFinderTests.java
r34000 r36064 17 17 package org.openstreetmap.josm.eventbus.outside; 18 18 19 import static org.junit.Assert.assertTrue; 19 20 import static org.junit.jupiter.api.Assertions.assertTrue; 20 21 21 22 import java.util.ArrayList; 22 23 import java.util.List; 23 24 24 import org.junit. After;25 import org.junit. Before;26 import org.junit.Test; 25 import org.junit.jupiter.api.AfterEach; 26 import org.junit.jupiter.api.BeforeEach; 27 import org.junit.jupiter.api.Test; 27 28 import org.openstreetmap.josm.eventbus.EventBus; 28 29 import org.openstreetmap.josm.eventbus.Subscribe; … … 48 49 } 49 50 50 @Before 51 public void setUp() throws Exception{51 @BeforeEach 52 public void setUp() { 52 53 subscriber = createSubscriber(); 53 54 EventBus bus = new EventBus(); … … 56 57 } 57 58 58 @After 59 public void tearDown() throws Exception{59 @AfterEach 60 public void tearDown() { 60 61 subscriber = null; 61 62 } … … 65 66 * We break the tests up based on whether they are annotated or abstract in the superclass. 66 67 */ 67 publicstatic class BaseSubscriberFinderTest68 static class BaseSubscriberFinderTest 68 69 extends AbstractEventBusTestParent<BaseSubscriberFinderTest.Subscriber> { 69 70 static class Subscriber { … … 82 83 83 84 @Test 84 publicvoid testNonSubscriber() {85 void testNonSubscriber() { 85 86 assertTrue(getSubscriber().nonSubscriberEvents.isEmpty()); 86 87 } 87 88 88 89 @Test 89 publicvoid testSubscriber() {90 void testSubscriber() { 90 91 assertTrue(getSubscriber().subscriberEvents.contains(EVENT)); 91 92 } … … 97 98 } 98 99 99 publicstatic class AnnotatedAndAbstractInSuperclassTest100 static class AnnotatedAndAbstractInSuperclassTest 100 101 extends AbstractEventBusTestParent<AnnotatedAndAbstractInSuperclassTest.SubClass> { 101 102 abstract static class SuperClass { … … 124 125 125 126 @Test 126 publicvoid testOverriddenAndAnnotatedInSubclass() {127 void testOverriddenAndAnnotatedInSubclass() { 127 128 assertTrue(getSubscriber().overriddenAndAnnotatedInSubclassEvents.contains(EVENT)); 128 129 } 129 130 130 131 @Test 131 publicvoid testOverriddenNotAnnotatedInSubclass() {132 void testOverriddenNotAnnotatedInSubclass() { 132 133 assertTrue(getSubscriber().overriddenInSubclassEvents.contains(EVENT)); 133 134 } … … 139 140 } 140 141 141 publicstatic class AnnotatedNotAbstractInSuperclassTest142 static class AnnotatedNotAbstractInSuperclassTest 142 143 extends AbstractEventBusTestParent<AnnotatedNotAbstractInSuperclassTest.SubClass> { 143 144 static class SuperClass { … … 206 207 207 208 @Test 208 publicvoid testNotOverriddenInSubclass() {209 void testNotOverriddenInSubclass() { 209 210 assertTrue(getSubscriber().notOverriddenInSubclassEvents.contains(EVENT)); 210 211 } 211 212 212 213 @Test 213 publicvoid testOverriddenNotAnnotatedInSubclass() {214 void testOverriddenNotAnnotatedInSubclass() { 214 215 assertTrue(getSubscriber().overriddenNotAnnotatedInSubclassEvents.contains(EVENT)); 215 216 } 216 217 217 218 @Test 218 publicvoid testDifferentlyOverriddenNotAnnotatedInSubclass() {219 void testDifferentlyOverriddenNotAnnotatedInSubclass() { 219 220 assertTrue(getSubscriber().differentlyOverriddenNotAnnotatedInSubclassGoodEvents 220 221 .contains(EVENT)); … … 223 224 224 225 @Test 225 publicvoid testOverriddenAndAnnotatedInSubclass() {226 void testOverriddenAndAnnotatedInSubclass() { 226 227 assertTrue(getSubscriber().overriddenAndAnnotatedInSubclassEvents.contains(EVENT)); 227 228 } 228 229 229 230 @Test 230 publicvoid testDifferentlyOverriddenAndAnnotatedInSubclass() {231 void testDifferentlyOverriddenAndAnnotatedInSubclass() { 231 232 assertTrue(getSubscriber().differentlyOverriddenAnnotatedInSubclassGoodEvents 232 233 .contains(EVENT)); … … 240 241 } 241 242 242 publicstatic class AbstractNotAnnotatedInSuperclassTest243 static class AbstractNotAnnotatedInSuperclassTest 243 244 extends AbstractEventBusTestParent<AbstractNotAnnotatedInSuperclassTest.SubClass> { 244 245 abstract static class SuperClass { … … 265 266 266 267 @Test 267 publicvoid testOverriddenAndAnnotatedInSubclass() {268 void testOverriddenAndAnnotatedInSubclass() { 268 269 assertTrue(getSubscriber().overriddenAndAnnotatedInSubclassEvents.contains(EVENT)); 269 270 } 270 271 271 272 @Test 272 publicvoid testOverriddenInSubclassNowhereAnnotated() {273 void testOverriddenInSubclassNowhereAnnotated() { 273 274 assertTrue(getSubscriber().overriddenInSubclassNowhereAnnotatedEvents.isEmpty()); 274 275 } … … 280 281 } 281 282 282 publicstatic class NeitherAbstractNorAnnotatedInSuperclassTest283 static class NeitherAbstractNorAnnotatedInSuperclassTest 283 284 extends AbstractEventBusTestParent<NeitherAbstractNorAnnotatedInSuperclassTest.SubClass> { 284 285 static class SuperClass { … … 314 315 315 316 @Test 316 publicvoid testNeitherOverriddenNorAnnotated() {317 void testNeitherOverriddenNorAnnotated() { 317 318 assertTrue(getSubscriber().neitherOverriddenNorAnnotatedEvents.isEmpty()); 318 319 } 319 320 320 321 @Test 321 publicvoid testOverriddenInSubclassNowhereAnnotated() {322 void testOverriddenInSubclassNowhereAnnotated() { 322 323 assertTrue(getSubscriber().overriddenInSubclassNowhereAnnotatedEvents.isEmpty()); 323 324 } 324 325 325 326 @Test 326 publicvoid testOverriddenAndAnnotatedInSubclass() {327 void testOverriddenAndAnnotatedInSubclass() { 327 328 assertTrue(getSubscriber().overriddenAndAnnotatedInSubclassEvents.contains(EVENT)); 328 329 } … … 334 335 } 335 336 336 publicstatic class DeepInterfaceTest337 static class DeepInterfaceTest 337 338 extends AbstractEventBusTestParent<DeepInterfaceTest.SubscriberClass> { 338 339 interface Interface1 { … … 427 428 428 429 @Test 429 publicvoid testAnnotatedIn1() {430 void testAnnotatedIn1() { 430 431 assertTrue(getSubscriber().annotatedIn1Events.contains(EVENT)); 431 432 } 432 433 433 434 @Test 434 publicvoid testAnnotatedIn2() {435 void testAnnotatedIn2() { 435 436 assertTrue(getSubscriber().annotatedIn2Events.contains(EVENT)); 436 437 } 437 438 438 439 @Test 439 publicvoid testAnnotatedIn1And2() {440 void testAnnotatedIn1And2() { 440 441 assertTrue(getSubscriber().annotatedIn1And2Events.contains(EVENT)); 441 442 } 442 443 443 444 @Test 444 publicvoid testAnnotatedIn1And2AndClass() {445 void testAnnotatedIn1And2AndClass() { 445 446 assertTrue(getSubscriber().annotatedIn1And2AndClassEvents.contains(EVENT)); 446 447 } 447 448 448 449 @Test 449 publicvoid testDeclaredIn1AnnotatedIn2() {450 void testDeclaredIn1AnnotatedIn2() { 450 451 assertTrue(getSubscriber().declaredIn1AnnotatedIn2Events.contains(EVENT)); 451 452 } 452 453 453 454 @Test 454 publicvoid testDeclaredIn1AnnotatedInClass() {455 void testDeclaredIn1AnnotatedInClass() { 455 456 assertTrue(getSubscriber().declaredIn1AnnotatedInClassEvents.contains(EVENT)); 456 457 } 457 458 458 459 @Test 459 publicvoid testDeclaredIn2AnnotatedInClass() {460 void testDeclaredIn2AnnotatedInClass() { 460 461 assertTrue(getSubscriber().declaredIn2AnnotatedInClassEvents.contains(EVENT)); 461 462 } 462 463 463 464 @Test 464 publicvoid testNowhereAnnotated() {465 void testNowhereAnnotated() { 465 466 assertTrue(getSubscriber().nowhereAnnotatedEvents.isEmpty()); 466 467 } -
applications/editors/josm/plugins/eventbus/test/unit/org/openstreetmap/josm/eventbus/outside/OutsideEventBusTest.java
r34000 r36064 17 17 package org.openstreetmap.josm.eventbus.outside; 18 18 19 import static org.junit. Assert.assertEquals;19 import static org.junit.jupiter.api.Assertions.assertEquals; 20 20 21 21 import java.util.concurrent.atomic.AtomicInteger; 22 22 import java.util.concurrent.atomic.AtomicReference; 23 23 24 import org.junit.Test; 24 import org.junit.jupiter.api.Test; 25 25 import org.openstreetmap.josm.eventbus.EventBus; 26 26 import org.openstreetmap.josm.eventbus.Subscribe; … … 31 31 * @author Louis Wasserman 32 32 */ 33 publicclass OutsideEventBusTest {33 class OutsideEventBusTest { 34 34 35 35 /* … … 39 39 */ 40 40 @Test 41 publicvoid testAnonymous() {41 void testAnonymous() { 42 42 final AtomicReference<String> holder = new AtomicReference<>(); 43 43 final AtomicInteger deliveries = new AtomicInteger(); … … 55 55 bus.post(EVENT); 56 56 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."); 59 59 } 60 60 }
Note:
See TracChangeset
for help on using the changeset viewer.