NodeObserver implements Observer { @Override public void contentChanged(@NotNull NodeState root, @NotNull CommitInfo info) { if (previousRoot != null) { try { long start = PERF_LOGGER.start(); NamePathMapper namePathMapper = new NamePathMapperImpl( new GlobalNameMapper(RootFactory.createReadOnlyRoot(root))); Set<String> oakPropertyNames = Sets.newHashSet(); for (String name : propertyNames) { String oakName = namePathMapper.getOakNameOrNull(name); if (oakName == null) { LOG.warn("Ignoring invalid property name: {}", name); } else { oakPropertyNames.add(oakName); } } NodeState before = previousRoot; NodeState after = root; EventHandler handler = new FilteredHandler( VISIBLE_FILTER, new NodeEventHandler("/", info, namePathMapper, oakPropertyNames)); String oakPath = namePathMapper.getOakPath(path); if (oakPath == null) { LOG.warn("Cannot listen for changes on invalid path: {}", path); return; } for (String oakName : PathUtils.elements(oakPath)) { before = before.getChildNode(oakName); after = after.getChildNode(oakName); handler = handler.getChildHandler(oakName, before, after); } EventGenerator generator = new EventGenerator(before, after, handler); while (!generator.isDone()) { generator.generate(); } PERF_LOGGER.end(start, 100, "Generated events (before: {}, after: {})", previousRoot, root); } catch (Exception e) { LOG.warn("Error while dispatching observation events", e); } } previousRoot = root; } protected  NodeObserver(String path, String... propertyNames); @Override void contentChanged(@NotNull NodeState root, @NotNull CommitInfo info);  }
@Test public void addNode() { NodeBuilder builder = before.builder(); builder.getChildNode("m").getChildNode("n").setChildNode("new").setProperty("p", "3"); nodeObserver.contentChanged(builder.getNodeState(), CommitInfo.EMPTY); assertEquals(ImmutableMap.of("/m/n/new", ImmutableSet.of("p")), nodeObserver.added); assertTrue(nodeObserver.deleted.isEmpty()); assertTrue(nodeObserver.changed.isEmpty()); assertTrue(nodeObserver.properties.isEmpty()); }
@Test public void deleteNode() { NodeBuilder builder = before.builder(); builder.getChildNode("m").getChildNode("n").getChildNode("o").remove(); nodeObserver.contentChanged(builder.getNodeState(), CommitInfo.EMPTY); assertTrue(nodeObserver.added.isEmpty()); assertEquals(ImmutableMap.of("/m/n/o", ImmutableSet.of("q")), nodeObserver.deleted); assertTrue(nodeObserver.changed.isEmpty()); assertTrue(nodeObserver.properties.isEmpty()); }
@Test public void changeNode() { NodeBuilder builder = before.builder(); builder.getChildNode("m").getChildNode("n").setProperty("p", 42); nodeObserver.contentChanged(builder.getNodeState(), CommitInfo.EMPTY); assertTrue(nodeObserver.added.isEmpty()); assertTrue(nodeObserver.deleted.isEmpty()); assertEquals(ImmutableMap.of("/m/n", ImmutableSet.of("p")), nodeObserver.changed); assertEquals(ImmutableMap.of("/m/n", ImmutableMap.of("extra", "42")), nodeObserver.properties); }
@Test public void ignoreAdd() { NodeBuilder builder = before.builder(); builder.getChildNode("a").getChildNode("b").setChildNode("new").setProperty("p", "3"); nodeObserver.contentChanged(builder.getNodeState(), CommitInfo.EMPTY); assertTrue(nodeObserver.added.isEmpty()); assertTrue(nodeObserver.deleted.isEmpty()); assertTrue(nodeObserver.changed.isEmpty()); assertTrue(nodeObserver.properties.isEmpty()); }
@Test public void ignoreDelete() { NodeBuilder builder = before.builder(); builder.getChildNode("a").getChildNode("b").remove(); nodeObserver.contentChanged(builder.getNodeState(), CommitInfo.EMPTY); assertTrue(nodeObserver.added.isEmpty()); assertTrue(nodeObserver.deleted.isEmpty()); assertTrue(nodeObserver.changed.isEmpty()); assertTrue(nodeObserver.properties.isEmpty()); }
@Test public void ignoreChange() { NodeBuilder builder = before.builder(); builder.getChildNode("a").getChildNode("b").setProperty("p", 42); nodeObserver.contentChanged(builder.getNodeState(), CommitInfo.EMPTY); assertTrue(nodeObserver.added.isEmpty()); assertTrue(nodeObserver.deleted.isEmpty()); assertTrue(nodeObserver.changed.isEmpty()); assertTrue(nodeObserver.properties.isEmpty()); }