@SuppressWarnings("unchecked") @Override public void dispatchEvent(@Nonnull Event<?> event) { AutowireCapableBeanFactory factory = applicationContext.getAutowireCapableBeanFactory(); List<Event<?>> queue = Lists.<Event<?>>newArrayList(event); while (!queue.isEmpty()) { Event<?> evt = queue.remove(0); for (Class<? extends EventHandler<?>> eventHandler : handlers.get((Class<? extends Event<?>>) evt.getClass())) { EventHandler<Event<?>> handler = (EventHandler<Event<?>>) factory.autowire(eventHandler, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true); EventResult result = handler.onEvent(evt); if (result.isAbort()) { throw new RuntimeException( String.format( "Handler %s aborted event %s with message: %s", handler, evt, result.getMessage())); } queue.addAll(result.getTriggeredEvents()); } } }
/** Tests if getType() returns the correct value. */ public void testGetType() throws RepositoryException { EventResult result = new EventResult(log); addEventListener(result, Event.NODE_ADDED); testRootNode.addNode(nodeName1, testNodeType); testRootNode.save(); Event[] events = result.getEvents(DEFAULT_WAIT_TIMEOUT); removeEventListener(result); assertEquals("Wrong number of events returned", 1, events.length); int type = events[0].getType(); assertEquals("Event did not return correct event type", Event.NODE_ADDED, type); }
/** * Tests if {@link javax.jcr.observation.Event#getUserID()} returns the same value as {@link * javax.jcr.Session#getUserID()}. */ public void testGetUserId() throws RepositoryException { EventResult result = new EventResult(log); addEventListener(result, Event.NODE_ADDED); testRootNode.addNode(nodeName1, testNodeType); testRootNode.save(); Event[] events = result.getEvents(DEFAULT_WAIT_TIMEOUT); removeEventListener(result); assertEquals("Wrong number of events returned", 1, events.length); String userId = events[0].getUserID(); String sessionUId = superuser.getUserID(); assertEquals("UserId of event is not equal to userId of session", userId, sessionUId); }
/** Tests if getPath() returns the correct path. */ public void testGetNodePath() throws RepositoryException { EventResult result = new EventResult(log); addEventListener(result, Event.NODE_ADDED); Node addedNode = testRootNode.addNode(nodeName1, testNodeType); testRootNode.save(); Event[] events = result.getEvents(DEFAULT_WAIT_TIMEOUT); removeEventListener(result); assertEquals("Wrong number of events returned", 1, events.length); String path = events[0].getPath(); String absPath = addedNode.getPath(); assertEquals("Path returned by getPath() is wrong", absPath, path); }
/** Tests if node removed and node added event is triggered when a node is moved. */ public void testMoveNode() throws RepositoryException { /** * Initial tree: + testroot + nodename1 + nodename2 * * <p>After move: + testroot + nodename1 + nodename2 */ Node n1 = testRootNode.addNode(nodeName1, testNodeType); Node n2 = n1.addNode(nodeName2, testNodeType); testRootNode.save(); EventResult addNodeListener = new EventResult(log); EventResult removeNodeListener = new EventResult(log); EventResult moveNodeListener = new EventResult(log); addEventListener(addNodeListener, Event.NODE_ADDED); addEventListener(removeNodeListener, Event.NODE_REMOVED); addEventListener(moveNodeListener, Event.NODE_MOVED); superuser.move(n2.getPath(), testRoot + "/" + nodeName2); testRootNode.save(); Event[] added = addNodeListener.getEvents(DEFAULT_WAIT_TIMEOUT); Event[] removed = removeNodeListener.getEvents(DEFAULT_WAIT_TIMEOUT); Event[] moved = moveNodeListener.getEvents(DEFAULT_WAIT_TIMEOUT); removeEventListener(addNodeListener); removeEventListener(removeNodeListener); removeEventListener(moveNodeListener); checkNodeAdded(added, new String[] {nodeName2}, null); checkNodeRemoved(removed, new String[] {nodeName1 + "/" + nodeName2}, null); checkNodeMoved(moved, nodeName1 + "/" + nodeName2, nodeName2); }
private void doTestNodeReorder(List<Event> added, List<Event> removed, List<Event> moved) throws RepositoryException, NotExecutableException { if (!testRootNode.getDefinition().getDeclaringNodeType().hasOrderableChildNodes()) { throw new NotExecutableException( "Node at '" + testRoot + "' does not support orderable child nodes."); } /** * Initial tree: + testroot + nodename1 + nodename2 + nodename3 * * <p>After reorder: + testroot + nodename1 + nodename3 + nodename2 */ testRootNode.addNode(nodeName1, testNodeType); testRootNode.addNode(nodeName2, testNodeType); testRootNode.addNode(nodeName3, testNodeType); testRootNode.getSession().save(); EventResult addNodeListener = new EventResult(log); EventResult removeNodeListener = new EventResult(log); EventResult moveNodeListener = new EventResult(log); addEventListener(addNodeListener, Event.NODE_ADDED); addEventListener(removeNodeListener, Event.NODE_REMOVED); addEventListener(moveNodeListener, Event.NODE_MOVED); testRootNode.orderBefore(nodeName3, nodeName2); testRootNode.getSession().save(); added.addAll(Arrays.asList(addNodeListener.getEvents(DEFAULT_WAIT_TIMEOUT))); removed.addAll(Arrays.asList(removeNodeListener.getEvents(DEFAULT_WAIT_TIMEOUT))); moved.addAll(Arrays.asList(moveNodeListener.getEvents(DEFAULT_WAIT_TIMEOUT))); removeEventListener(addNodeListener); removeEventListener(removeNodeListener); removeEventListener(moveNodeListener); }
/** * Tests if reordering a child node triggers a {@link Event#NODE_REMOVED} and a {@link * Event#NODE_ADDED} event with same name siblings. Furthermore a node is removed in the same save * scope. */ public void testNodeReorderSameNameWithRemove() throws RepositoryException, NotExecutableException { if (!testRootNode.getDefinition().getDeclaringNodeType().hasOrderableChildNodes()) { throw new NotExecutableException( "Node at '" + testRoot + "' does not support orderable child nodes."); } /** * Initial tree: + testroot + nodename1[1] + nodename2 + nodename1[2] + nodename1[3] + nodename3 * * <p>After reorder: + testroot + nodename1[1] + nodename2 + nodename1[2] (was 3) + nodename1[3] * (was 2) */ Node n = testRootNode.addNode(nodeName1, testNodeType); if (!n.getDefinition().allowsSameNameSiblings()) { throw new NotExecutableException( "Node at " + testRoot + " does not allow same name siblings with name " + nodeName1); } testRootNode.addNode(nodeName2, testNodeType); testRootNode.addNode(nodeName1, testNodeType); testRootNode.addNode(nodeName1, testNodeType); testRootNode.addNode(nodeName3, testNodeType); testRootNode.getSession().save(); EventResult addNodeListener = new EventResult(log); EventResult removeNodeListener = new EventResult(log); EventResult moveNodeListener = new EventResult(log); addEventListener(addNodeListener, Event.NODE_ADDED); addEventListener(removeNodeListener, Event.NODE_REMOVED); addEventListener(moveNodeListener, Event.NODE_MOVED); testRootNode.orderBefore(nodeName1 + "[2]", null); testRootNode.getNode(nodeName3).remove(); testRootNode.getSession().save(); Event[] added = addNodeListener.getEvents(DEFAULT_WAIT_TIMEOUT); Event[] removed = removeNodeListener.getEvents(DEFAULT_WAIT_TIMEOUT); Event[] moved = moveNodeListener.getEvents(DEFAULT_WAIT_TIMEOUT); removeEventListener(addNodeListener); removeEventListener(removeNodeListener); removeEventListener(moveNodeListener); // either // 1) nodename1[2] has been reordered to the end // or: // 2) nodename1[3] has been reordered before nodename1[2] // // that is, the following event sets are correct: // 1) nodename1[2]:remove, nodename1[3]:add, nodename3:remove // or: // 2) nodename1[3]:remove, nodename1[2]:add, nodename3:remove // if true, check for option 1) boolean reorderEnd = false; for (int i = 0; i < added.length; i++) { if (added[i].getPath().endsWith(nodeName1 + "[3]")) { reorderEnd = true; break; } } if (reorderEnd) { checkNodeAdded(added, new String[] {nodeName1 + "[3]"}, null); checkNodeRemoved(removed, new String[] {nodeName1 + "[2]", nodeName3}, null); checkNodeReordered(moved, nodeName1 + "[2]", nodeName1 + "[3]", null); } else { checkNodeAdded(added, new String[] {nodeName1 + "[2]"}, null); checkNodeRemoved(removed, new String[] {nodeName1 + "[3]", nodeName3}, null); checkNodeReordered(moved, nodeName1 + "[3]", nodeName1 + "[2]", nodeName1 + "[2]"); } }