@Override public <H extends EventHandler> void fireEvent(Event<H> event) { super.fireEvent(event); sentEvents.add(event); }
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); }
@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(); } } });
@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); } }
@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()); }
@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"); } }); }
@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"); }
@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()); }
@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 testHandlerAddedWhileDispatchingIsNotCalled() { // GIVEN final SimpleEventBus eventBus = new SimpleEventBus(); final InvocationCountingTestEventHandler handler2 = new InvocationCountingTestEventHandler(); InvocationCountingTestEventHandler handler1 = new InvocationCountingTestEventHandler() { @Override public void handleEvent(TestEvent event) { super.handleEvent(event); eventBus.addHandler(TestEvent.class, handler2); } }; eventBus.addHandler(TestEvent.class, handler1); // WHEN eventBus.fireEvent(new TestEvent()); // THEN assertEquals(1, handler1.getInvocationCount()); assertEquals(0, handler2.getInvocationCount()); }
@Test public void testMap() throws RegistrationException { // GIVEN VirtualURIManager manager = new VirtualURIManager(moduleRegistry, new Node2BeanProcessorImpl(new TypeMappingImpl(), new Node2BeanTransformerImpl()), eventBus); // WHEN eventBus.fireEvent(new ModulesStartedEvent()); // THEN assertEquals("/1", manager.getURIMapping("/first")); assertEquals("/2", manager.getURIMapping("/second")); assertEquals(StringUtils.EMPTY, manager.getURIMapping("/unmapped")); }