private void bindEventBuses() { // Make event buses injectable. Use EventBusProxyScope to automatically wrap the event buses injected across // scopes with ResettableEventBus final Provider<EventBus> eventBusProvider = () -> new ResettableEventBus(new SimpleEventBus()); final Function<String, Key<EventBus>> createEventBusKey = (name) -> Key.get(EventBus.class, Names.named(name)); final Consumer<String> bindEventBus = name -> bind(createEventBusKey.apply(name)) .toProvider(eventBusProvider) .in(eventBusScopes.get(name)); bindEventBus.accept(AdmincentralEventBus.NAME); bindEventBus.accept(AppEventBus.NAME); bindEventBus.accept(SubAppEventBus.NAME); bindEventBus.accept(ChooseDialogEventBus.NAME); bindEventBus.accept("mediaeditor"); }
@Test public void testMultipleRegistrationOfTheSameHandlerWillNotResultInMultipleInvocations() { // GIVEN SimpleEventBus eventBus = new SimpleEventBus(); InvocationCountingTestEventHandler handler = new InvocationCountingTestEventHandler(); eventBus.addHandler(TestEvent.class, handler); eventBus.addHandler(TestEvent.class, handler); eventBus.addHandler(TestEvent.class, handler); eventBus.addHandler(TestEvent.class, handler); // WHEN eventBus.fireEvent(new TestEvent()); // THEN assertEquals(1, handler.getInvocationCount()); }
@Override public <H extends EventHandler> HandlerRegistration addHandler(final Class<? extends Event<H>> eventClass, final H handler) { log.debug("Adding handler {} for events of class {}", handler, eventClass); internalAddHandler(eventClass, handler); return new HandlerRegistration() { @Override public void removeHandler() { internalRemoveHandler(eventClass, handler); } }; }
private void assertSetsFragmentToOnLocationChange(Location newLocation, String expectedFragment) { // GIVEN Shell shell = mock(Shell.class); SimpleEventBus eventBus = new SimpleEventBus(); LocationController locationController = mock(LocationController.class); LocationHistoryMapper locationHistoryMapper = new MockLocationHistoryMapper(); LocationHistoryHandler locationHistoryHandler = new LocationHistoryHandler(locationHistoryMapper, shell); locationHistoryHandler.register(locationController, eventBus, Location.NOWHERE); // WHEN eventBus.fireEvent(new LocationChangedEvent(newLocation)); // THEN Mockito.verify(shell).setFragment(expectedFragment); }
@Test public void testGoToSameLocationDoesNothing() { // GIVEN SimpleEventBus eventBus = new SimpleEventBus(); LocationController locationController = new LocationController(eventBus, mock(Shell.class)); assertEquals(Location.NOWHERE, locationController.getWhere()); Location newLocation = getNewEmptyLocation(); locationController.goTo(newLocation); assertSame(newLocation, locationController.getWhere()); LocationChangeRequestedHandler requestHandler = new LocationChangeRequestedHandler(); LocationChangedHandler changeHandler = new LocationChangedHandler(); eventBus.addHandler(LocationChangeRequestedEvent.class, requestHandler); eventBus.addHandler(LocationChangedEvent.class, changeHandler); // WHEN locationController.goTo(newLocation); // THEN assertNull(requestHandler.event); assertNull(changeHandler.event); }
@Override public <H extends EventHandler> void fireEvent(Event<H> event) { super.fireEvent(event); sentEvents.add(event); }
@Override public void run() { try { startSignal.await(); for (int i = 0; i < numberOfIterations; i++) { List<InvocationCountingTestEventHandler> handlers = new ArrayList<InvocationCountingTestEventHandler>(); List<HandlerRegistration> registrations = new ArrayList<HandlerRegistration>(); // Add handlers for (int j = 0; j < numberOfHandlers; j++) { InvocationCountingTestEventHandler handler = new InvocationCountingTestEventHandler(); handlers.add(handler); registrations.add(eventBus.addHandler(TestEvent.class, handler)); } // Fire event eventBus.fireEvent(new TestEvent()); // Remove handlers for (HandlerRegistration handler : registrations) { handler.removeHandler(); } // Make sure that every handler was invoked at least once for (InvocationCountingTestEventHandler handler : handlers) { Assert.assertTrue(handler.getInvocationCount() > 0); } } } catch (Throwable e) { threadEncounteredException.set(true); e.printStackTrace(); } } });
@Override public void handleEvent(TestEvent event) { super.handleEvent(event); eventBus.addHandler(TestEvent.class, handler2); } };
@Override public <H extends EventHandler> void fireEvent(Event<H> event) { for (H eventHandler : internalGetHandlers(event)) { log.debug("Dispatching event {} with handler {}", event, eventHandler); try { event.dispatch(eventHandler); } catch (RuntimeException e) { log.error("Exception caught when dispatching {} with {} eventHandler.", event.getClass().getName(), eventHandler.getClass().getName(), e); } } }
LocationChangedHandler changeHandler = new LocationChangedHandler(); SimpleEventBus eventBus = new SimpleEventBus(); eventBus.addHandler(LocationChangeRequestedEvent.class, requestHandler); eventBus.addHandler(LocationChangedEvent.class, changeHandler);
@Override @Before public void setUp() throws Exception { super.setUp(); session = SessionTestUtil.createSession("config", config); MockUtil.setSystemContextSessionAndHierarchyManager(session); SystemContext systemContext = mock(SystemContext.class); when(systemContext.getJCRSession("config")).thenReturn(session); ComponentsTestUtil.setInstance(SystemContext.class, systemContext); ComponentsTestUtil.setImplementation(CommandsManager.class, CommandsManager.class); // few "hacks" to workaround bug in MockComponentProvider ComponentsTestUtil.setInstanceFactory(MgnlCatalog.class, new MgnlCatalogComponentFactory()); ComponentsTestUtil.setInstanceFactory(Command.class, new TestCommandComponentFactory()); ComponentsTestUtil.setInstanceFactory(ChainBase.class, new ChainBaseComponentFactory()); ComponentsTestUtil.setInstanceFactory(Command[].class, new CommandArrayComponentFactory()); ComponentsTestUtil.setInstance(EventBus.class, eventBus); Set<String> moduleNames = new LinkedHashSet<String>(); moduleNames.add("mymodule"); moduleNames.add("mymodule2"); moduleNames.add("mymodule3"); moduleNames.add("mymodule4"); ModuleRegistry moduleRegistry = mock(ModuleRegistry.class); when(moduleRegistry.getModuleNames()).thenReturn(moduleNames); ComponentsTestUtil.setInstance(ModuleRegistry.class, moduleRegistry); commandsManager = Components.getComponent(CommandsManager.class); eventBus.fireEvent(new ModulesStartedEvent()); }
@Override protected void configure() { bind(EventBus.class).annotatedWith(Names.named(SystemEventBus.NAME)).toProvider(Providers.of(new SimpleEventBus())); } }
@Test public void testHandlerRemovedWhileDispatchingIsCalled() { // GIVEN SimpleEventBus eventBus = new SimpleEventBus(); RemoveEventHandler handler1 = new RemoveEventHandler(); InvocationCountingTestEventHandler handler2 = new InvocationCountingTestEventHandler(); eventBus.addHandler(TestEvent.class, handler1); handler1.handlerRegistration = eventBus.addHandler(TestEvent.class, handler2); // WHEN eventBus.fireEvent(new TestEvent()); // THEN assertEquals(1, handler1.getInvocationCount()); assertEquals(1, handler2.getInvocationCount()); }
@Test public void testGoToWithoutWarning() { // GIVEN LocationChangeRequestedHandler requestHandler = new LocationChangeRequestedHandler(); LocationChangedHandler changeHandler = new LocationChangedHandler(); SimpleEventBus eventBus = new SimpleEventBus(); eventBus.addHandler(LocationChangeRequestedEvent.class, requestHandler); eventBus.addHandler(LocationChangedEvent.class, changeHandler); LocationController locationController = new LocationController(eventBus, mock(Shell.class)); assertEquals(Location.NOWHERE, locationController.getWhere()); Location newLocation = getNewEmptyLocation(); // WHEN locationController.goTo(newLocation); // THEN assertSame(newLocation, locationController.getWhere()); assertNotNull(requestHandler.event); assertSame(newLocation, requestHandler.event.getNewLocation()); assertNotNull(changeHandler.event); assertSame(newLocation, changeHandler.event.getNewLocation()); }
@Test public void testReloadsTemplatesOnChange() throws RepositoryException, RegistrationException, InterruptedException { // GIVEN final VirtualURIManager manager = new VirtualURIManager(moduleRegistry, new Node2BeanProcessorImpl(new TypeMappingImpl(), new Node2BeanTransformerImpl()), eventBus); // WHEN eventBus.fireEvent(new ModulesStartedEvent()); // THEN, make sure that it found mapping 'first' assertHasDefaultMapping(manager, "/first", "/1"); // WHEN we remove the node for mapping 'first' and add a new one 'third' in zedModule session.getNode("/modules/fooModule/virtualURIMapping/first").remove(); Node node = session.getNode("/modules/zedModule/").addNode("virtualURIMapping").addNode("third"); node.setProperty("fromURI", "/third"); node.setProperty("toURI", "/3"); // It's enough to fire just this event because it reloads everything for each batch of events MockObservationManager observationManager = (MockObservationManager) session.getWorkspace().getObservationManager(); observationManager.fireEvent(MockEvent.nodeAdded("/modules/zedModule/virtualURIMapping/third")); // THEN 'first' must be gone and 'third' must have been found TestUtil.delayedAssert(new Assertion() { @Override public void evaluate() throws RegistrationException { assertEquals(2, manager.getURIMappings().size()); assertHasDefaultMapping(manager, "/second", "/2"); assertHasDefaultMapping(manager, "/third", "/3"); } }); }
@Override public void setUp() throws Exception { super.setUp(); workbenchPresenter = mock(WorkbenchPresenter.class); eventBus = new SimpleEventBus(); itemIds = new HashSet<>(); // make sure that workbench view registers a content view so that restore selection doesn't fail. WorkbenchView workbenchView = mock(WorkbenchView.class); doReturn(mock(Component.class)).when(workbenchView).asVaadinComponent(); doReturn(workbenchView).when(workbenchPresenter).start(any(WorkbenchDefinition.class), any(ImageProviderDefinition.class), any(EventBus.class)); }
@Test public void testAreHandlersInvokedInOrder() { // GIVEN SimpleEventBus eventBus = new SimpleEventBus(); List<InvocationOrderTestingEventHandler> handlers = new ArrayList<InvocationOrderTestingEventHandler>(); AtomicInteger counter = new AtomicInteger(0); for (int i = 0; i < 1000; i++) { InvocationOrderTestingEventHandler handler = new InvocationOrderTestingEventHandler(i, counter); handlers.add(handler); eventBus.addHandler(TestEvent.class, handler); } // WHEN eventBus.fireEvent(new TestEvent()); // THEN for (InvocationOrderTestingEventHandler handler : handlers) { assertEquals(1, handler.getInvocationCount()); assertEquals(handler.expectedOrder, handler.actualOrder); } }
LocationChangedHandler changeHandler = new LocationChangedHandler(); SimpleEventBus eventBus = new SimpleEventBus(); eventBus.addHandler(LocationChangeRequestedEvent.class, requestHandler); eventBus.addHandler(LocationChangedEvent.class, changeHandler);
@Test public void testRegistersTemplatesOnStart() throws RegistrationException { // GIVEN VirtualURIManager manager = new VirtualURIManager(moduleRegistry, new Node2BeanProcessorImpl(new TypeMappingImpl(), new Node2BeanTransformerImpl()), eventBus); // WHEN eventBus.fireEvent(new ModulesStartedEvent()); // THEN assertEquals(2, manager.getURIMappings().size()); assertHasDefaultMapping(manager, "/first", "/1"); assertHasDefaultMapping(manager, "/second", "/2"); }
private void initWorkbenchPresenter() throws Exception { SimpleTranslator i18n = mock(SimpleTranslator.class); contentConnector = mock(ContentConnector.class); StatusBarView view = mock(StatusBarViewImpl.class); activeContentPresenter = mock(ContentPresenter.class); eventBus = new SimpleEventBus(); barPresenter = new WorkbenchStatusBarPresenter(view, contentConnector, i18n); barPresenter.start(eventBus, activeContentPresenter); }