HandlerRegistry { public HandlerMethod get(final String name) { final HandlerMethod handlerMethod = handlerMethods.getOrDefault(name, handlerMethods.get("*")); if (handlerMethod != null) { return handlerMethod; } else { throw new MissingHandlerException( format("No handler registered to handle action %s", name)); } } HandlerRegistry(final Logger logger); HandlerMethod get(final String name); void register(final Object handlerInstance);  }
@Test public void shouldReturnMethodOfTheRegisteredAsynchronousHandler() { final TestCommandHandler testCommandHandler = new TestCommandHandler(); createRegistryWith(testCommandHandler); final HandlerMethod handlerMethod = registry.get(COMMAND_NAME); assertHandlerMethodInvokesHandler(handlerMethod, testCommandHandler); assertLogStatement(COMMAND_NAME); }
@Test public void shouldReturnMethodOfTheRegisteredSynchronousHandler() { final TestCommandHandlerWithSynchronousHandler testCommandHandlerWithSynchronousHandler = new TestCommandHandlerWithSynchronousHandler(); createRegistryWith(testCommandHandlerWithSynchronousHandler); final HandlerMethod handlerMethod = registry.get(COMMAND_NAME); assertHandlerMethodInvokesHandler(handlerMethod, testCommandHandlerWithSynchronousHandler); assertLogStatement(COMMAND_NAME); }
@Test public void shouldReturnMethodOfTheAllEventsHandler() { final TestAllEventsHandler testAllEventsHandler = new TestAllEventsHandler(); createRegistryWith(testAllEventsHandler); final HandlerMethod handlerMethod = registry.get("some.name"); assertHandlerMethodInvokesHandler(handlerMethod, testAllEventsHandler); assertLogStatement("*"); }
@Test public void namedHandlerShouldTakePriorityOverAllHandler() { final TestAllEventsHandler testAllEventsHandler = new TestAllEventsHandler(); final TestCommandHandler testCommandHandler = new TestCommandHandler(); createRegistryWith(testAllEventsHandler, testCommandHandler); final HandlerMethod handlerMethod = registry.get(COMMAND_NAME); assertHandlerMethodInvokesHandler(handlerMethod, testCommandHandler); assertThat(testAllEventsHandler.firstRecordedEnvelope(), nullValue()); assertLogStatement("*"); }
@Test public void directHandlerShouldReplaceNonDirectHandler() { TestComponentAHandler testComponentAHandler = new TestComponentAHandler(); TestDirectComponentAHandler testDirectComponentAHandler = new TestDirectComponentAHandler(); createRegistryWith(testComponentAHandler, testDirectComponentAHandler); final HandlerMethod handlerMethod = registry.get(COMMAND_NAME); assertHandlerMethodInvokesHandler(handlerMethod, testDirectComponentAHandler); }
@Test public void shouldIgnoreNonDirectHandlerIfDirectOneRegistered() throws Exception { TestComponentAHandler testComponentAHandler = new TestComponentAHandler(); TestDirectComponentAHandler testDirectComponentAHandler = new TestDirectComponentAHandler(); createRegistryWith(testDirectComponentAHandler, testComponentAHandler); final HandlerMethod handlerMethod = registry.get(COMMAND_NAME); assertHandlerMethodInvokesHandler(handlerMethod, testDirectComponentAHandler); }