Search in sources :

Example 36 with Event

use of javax.jcr.observation.Event in project jackrabbit-oak by apache.

the class ObservationTest method doIncludeAncestorsRemove.

private FilterProvider doIncludeAncestorsRemove(JackrabbitEventFilter filter) throws Exception {
    assumeTrue(observationManager instanceof ObservationManagerImpl);
    Node testNode = getNode(TEST_PATH);
    testNode.addNode("a").addNode("b").addNode("c").addNode("d").setProperty("e", 42);
    testNode.getSession().save();
    ObservationManagerImpl oManager = (ObservationManagerImpl) observationManager;
    ExpectationListener listener = new ExpectationListener();
    oManager.addEventListener(listener, filter);
    Node d = testNode.getNode("a").getNode("b").getNode("c").getNode("d");
    Property e = d.getProperty("e");
    listener.expectRemove(e);
    // listener.expectRemove(d.getProperty("jcr:primaryType"));
    // d.remove();
    listener.expectRemove(d).remove();
    testNode.getSession().save();
    Thread.sleep(1000);
    List<Expectation> missing = listener.getMissing(TIME_OUT, TimeUnit.SECONDS);
    List<Event> unexpected = listener.getUnexpected();
    assertTrue("Unexpected events: " + unexpected, unexpected.isEmpty());
    assertTrue("Missing events: " + missing, missing.isEmpty());
    oManager.addEventListener(new EventListener() {

        @Override
        public void onEvent(EventIterator events) {
            while (events.hasNext()) {
                System.out.println("/a-listener GOT: " + events.next());
            }
        }
    }, NODE_REMOVED, TEST_PATH + "/a", false, null, null, false);
    System.out.println("REGISTERED");
    testNode = getNode(TEST_PATH);
    Node b = testNode.getNode("a").getNode("b");
    listener.expect(b.getPath(), NODE_REMOVED);
    listener.optional(new Expectation("/a/b/c is optionally sent depending on filter") {

        @Override
        public boolean onEvent(Event event) throws Exception {
            if (event.getPath().equals(TEST_PATH + "/a/b/c") && event.getType() == NODE_REMOVED) {
                return true;
            }
            return false;
        }
    });
    b.remove();
    // but not the jcr:primaryType
    testNode.getSession().save();
    Thread.sleep(1000);
    missing = listener.getMissing(TIME_OUT, TimeUnit.SECONDS);
    unexpected = listener.getUnexpected();
    assertTrue("Missing events: " + missing, missing.isEmpty());
    assertTrue("Unexpected events: " + unexpected, unexpected.isEmpty());
    Node a = testNode.getNode("a");
    listener.expect(a.getPath(), NODE_REMOVED);
    a.remove();
    // but not the jcr:primaryType
    testNode.getSession().save();
    missing = listener.getMissing(TIME_OUT, TimeUnit.SECONDS);
    unexpected = listener.getUnexpected();
    assertTrue("Unexpected events: " + unexpected, unexpected.isEmpty());
    assertTrue("Missing events: " + missing, missing.isEmpty());
    ChangeProcessor cp = oManager.getChangeProcessor(listener);
    assertNotNull(cp);
    FilterProvider filterProvider = cp.getFilterProvider();
    assertNotNull(filterProvider);
    return filterProvider;
}
Also used : JackrabbitNode(org.apache.jackrabbit.api.JackrabbitNode) Node(javax.jcr.Node) ItemExistsException(javax.jcr.ItemExistsException) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) TimeoutException(java.util.concurrent.TimeoutException) VersionException(javax.jcr.version.VersionException) InvalidItemStateException(javax.jcr.InvalidItemStateException) ReferentialIntegrityException(javax.jcr.ReferentialIntegrityException) AccessDeniedException(javax.jcr.AccessDeniedException) PathNotFoundException(javax.jcr.PathNotFoundException) RepositoryException(javax.jcr.RepositoryException) LockException(javax.jcr.lock.LockException) NoSuchNodeTypeException(javax.jcr.nodetype.NoSuchNodeTypeException) ExecutionException(java.util.concurrent.ExecutionException) FilterProvider(org.apache.jackrabbit.oak.plugins.observation.filter.FilterProvider) Event(javax.jcr.observation.Event) EventListener(javax.jcr.observation.EventListener) EventIterator(javax.jcr.observation.EventIterator) Property(javax.jcr.Property)

Example 37 with Event

use of javax.jcr.observation.Event in project jackrabbit-oak by apache.

the class ObservationTest method applyNodeTypeOnSelf.

// ------------------------------------------------------------< OakEventFilter tests >---
@Test
public void applyNodeTypeOnSelf() throws Exception {
    assumeTrue(observationManager instanceof ObservationManagerImpl);
    Node testNode = getNode(TEST_PATH);
    testNode.addNode("a", "nt:unstructured").addNode("b", "oak:Unstructured").addNode("c", "nt:unstructured");
    testNode.getSession().save();
    ObservationManagerImpl oManager = (ObservationManagerImpl) observationManager;
    ExpectationListener listener = new ExpectationListener();
    JackrabbitEventFilter filter = new JackrabbitEventFilter();
    filter.setEventTypes(ALL_EVENTS);
    filter.setAbsPath("/");
    filter.setIsDeep(true);
    filter.setNodeTypes(new String[] { "oak:Unstructured" });
    filter = FilterFactory.wrap(filter).withApplyNodeTypeOnSelf();
    oManager.addEventListener(listener, filter);
    testNode.getNode("a").getNode("b").getNode("c").remove();
    testNode.getSession().save();
    // wait 1 sec to give failures a chance (we're not expecting anything, but perhaps
    // something would come, after 1sec more likely than after 0sec)
    Thread.sleep(1000);
    List<Expectation> missing = listener.getMissing(TIME_OUT, TimeUnit.SECONDS);
    assertTrue("Missing events: " + missing, missing.isEmpty());
    List<Event> unexpected = listener.getUnexpected();
    assertTrue("Unexpected events: " + unexpected, unexpected.isEmpty());
    Node b = testNode.getNode("a").getNode("b");
    // OAK-5061 : the event NODE_REMOVED on /a/b is actually expected and was missing in the test:
    listener.expect(b.getPath(), NODE_REMOVED);
    b.remove();
    testNode.getSession().save();
    Thread.sleep(1000);
    missing = listener.getMissing(TIME_OUT, TimeUnit.SECONDS);
    assertTrue("Missing events: " + missing, missing.isEmpty());
    unexpected = listener.getUnexpected();
    assertTrue("Unexpected events: " + unexpected, unexpected.isEmpty());
}
Also used : JackrabbitNode(org.apache.jackrabbit.api.JackrabbitNode) Node(javax.jcr.Node) Event(javax.jcr.observation.Event) JackrabbitEventFilter(org.apache.jackrabbit.api.observation.JackrabbitEventFilter) AbstractRepositoryTest(org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest) Test(org.junit.Test)

Example 38 with Event

use of javax.jcr.observation.Event in project jackrabbit-oak by apache.

the class ObservationTest method testAggregate5.

/**
 * OAK-5096 : new test case for OR mode
 */
@Test
public void testAggregate5() throws Exception {
    assumeTrue(observationManager instanceof ObservationManagerImpl);
    ObservationManagerImpl oManager = (ObservationManagerImpl) observationManager;
    ExpectationListener listener = new ExpectationListener();
    JackrabbitEventFilter filter = new JackrabbitEventFilter();
    filter.setEventTypes(ALL_EVENTS);
    filter = FilterFactory.wrap(filter).withNodeTypeAggregate(new String[] { "oak:Unstructured" }, new String[] { "**/foo/**" }).withIncludeGlobPaths("/parent/**/bar/**");
    oManager.addEventListener(listener, filter);
    ChangeProcessor cp = oManager.getChangeProcessor(listener);
    assertNotNull(cp);
    FilterProvider filterProvider = cp.getFilterProvider();
    assertNotNull(filterProvider);
    assertMatches(filterProvider.getSubTrees(), "/parent");
    Node parent = getAdminSession().getRootNode().addNode("parent", "nt:unstructured");
    Node bar = parent.addNode("bar", "nt:unstructured");
    listener.expect(bar.getPath() + "/jcr:primaryType", PROPERTY_ADDED);
    Node c = bar.addNode("c", "nt:unstructured");
    listener.expectAdd(c);
    Node foo = c.addNode("foo", "nt:unstructured");
    listener.expectAdd(foo);
    Node jcrContent = foo.addNode("jcr:content", "nt:unstructured");
    listener.expectAdd(jcrContent);
    parent.getSession().save();
    Thread.sleep(1000);
    List<Expectation> missing = listener.getMissing(TIME_OUT, TimeUnit.SECONDS);
    List<Event> unexpected = listener.getUnexpected();
    assertTrue("Unexpected events: " + unexpected, unexpected.isEmpty());
    assertTrue("Missing events: " + missing, missing.isEmpty());
}
Also used : JackrabbitNode(org.apache.jackrabbit.api.JackrabbitNode) Node(javax.jcr.Node) Event(javax.jcr.observation.Event) JackrabbitEventFilter(org.apache.jackrabbit.api.observation.JackrabbitEventFilter) FilterProvider(org.apache.jackrabbit.oak.plugins.observation.filter.FilterProvider) AbstractRepositoryTest(org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest) Test(org.junit.Test)

Example 39 with Event

use of javax.jcr.observation.Event in project jackrabbit-oak by apache.

the class ObservationTest method includeGlobPaths.

@Test
public void includeGlobPaths() throws Exception {
    Node testNode = getNode(TEST_PATH);
    testNode.addNode("a1").addNode("b").addNode("c");
    testNode.addNode("a2").addNode("b").addNode("c");
    testNode.getSession().save();
    ObservationManagerImpl oManager = (ObservationManagerImpl) observationManager;
    ExpectationListener listener = new ExpectationListener();
    JackrabbitEventFilter filter = new JackrabbitEventFilter();
    filter.setEventTypes(ALL_EVENTS);
    filter = FilterFactory.wrap(filter).withIncludeGlobPaths(TEST_PATH + "/a2/**");
    oManager.addEventListener(listener, filter);
    ChangeProcessor cp = oManager.getChangeProcessor(listener);
    assertNotNull(cp);
    FilterProvider filterProvider = cp.getFilterProvider();
    assertNotNull(filterProvider);
    assertMatches(filterProvider.getSubTrees(), TEST_PATH + "/a2");
    testNode.getNode("a1").getNode("b").remove();
    listener.expectRemove(testNode.getNode("a2").getNode("b")).remove();
    testNode.getSession().save();
    Thread.sleep(1000);
    List<Expectation> missing = listener.getMissing(TIME_OUT, TimeUnit.SECONDS);
    assertTrue("Missing events: " + missing, missing.isEmpty());
    List<Event> unexpected = listener.getUnexpected();
    assertTrue("Unexpected events: " + unexpected, unexpected.isEmpty());
    Node a3 = testNode.addNode("a3");
    Node foo = a3.addNode("bar").addNode("foo");
    testNode.getSession().save();
    filter = new JackrabbitEventFilter();
    filter.setEventTypes(ALL_EVENTS);
    // filter.setAbsPath(TEST_PATH + "/a3/bar/foo/x");
    filter = FilterFactory.wrap(filter).withIncludeGlobPaths(TEST_PATH + "/a3/**/x");
    oManager.addEventListener(listener, filter);
    cp = oManager.getChangeProcessor(listener);
    assertNotNull(cp);
    filterProvider = cp.getFilterProvider();
    assertNotNull(filterProvider);
    assertMatches(filterProvider.getSubTrees(), TEST_PATH + "/a3");
    Node x = foo.addNode("x");
    listener.expect(x.getPath() + "/jcr:primaryType", PROPERTY_ADDED);
    testNode.getSession().save();
    Thread.sleep(1000);
    missing = listener.getMissing(TIME_OUT, TimeUnit.SECONDS);
    assertTrue("Missing events: " + missing, missing.isEmpty());
    unexpected = listener.getUnexpected();
    assertTrue("Unexpected events: " + unexpected, unexpected.isEmpty());
    filter = new JackrabbitEventFilter();
    filter.setEventTypes(ALL_EVENTS);
    filter = FilterFactory.wrap(filter).withIncludeGlobPaths(TEST_PATH + "/a3/**/y");
    oManager.addEventListener(listener, filter);
    cp = oManager.getChangeProcessor(listener);
    assertNotNull(cp);
    filterProvider = cp.getFilterProvider();
    assertNotNull(filterProvider);
    assertMatches(filterProvider.getSubTrees(), TEST_PATH + "/a3");
    Node y = foo.addNode("y");
    listener.expect(y.getPath() + "/jcr:primaryType", PROPERTY_ADDED);
    testNode.getSession().save();
    Thread.sleep(1000);
    missing = listener.getMissing(TIME_OUT, TimeUnit.SECONDS);
    assertTrue("Missing events: " + missing, missing.isEmpty());
    unexpected = listener.getUnexpected();
    assertTrue("Unexpected events: " + unexpected, unexpected.isEmpty());
}
Also used : JackrabbitNode(org.apache.jackrabbit.api.JackrabbitNode) Node(javax.jcr.Node) Event(javax.jcr.observation.Event) JackrabbitEventFilter(org.apache.jackrabbit.api.observation.JackrabbitEventFilter) FilterProvider(org.apache.jackrabbit.oak.plugins.observation.filter.FilterProvider) AbstractRepositoryTest(org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest) Test(org.junit.Test)

Example 40 with Event

use of javax.jcr.observation.Event in project jackrabbit-oak by apache.

the class ObservationTest method testEventRemovedNodeWhenDenyEntryIsRemoved.

/**
 * @see <a href="https://issues.apache.org/jira/browse/OAK-4196">OAK-4196</a>
 */
@Test
public void testEventRemovedNodeWhenDenyEntryIsRemoved() throws Exception {
    // withdraw the READ privilege on childNPath
    deny(childNPath, readPrivileges);
    assertFalse(testSession.nodeExists(childNPath));
    assertTrue(testSession.nodeExists(childNPath2));
    // testUser registers a event listener for changes under testRoot
    ObservationManager obsMgr = testSession.getWorkspace().getObservationManager();
    EventResult listener = new EventResult(this.log);
    try {
        obsMgr.addEventListener(listener, Event.NODE_REMOVED, testRoot, true, null, null, true);
        // superuser removes the node with childNPath & childNPath2 in
        // order to provoke events being generated
        superuser.getItem(childNPath).remove();
        superuser.getItem(childNPath2).remove();
        superuser.save();
        // since the events are generated _after_ persisting all the changes
        // and the removal also removes the permission entries denying access
        // testUser will be notified about the removal because the remaining
        // permission setup after the removal grants read access.
        Event[] evts = listener.getEvents(DEFAULT_WAIT_TIMEOUT);
        List<String> eventPaths = new ArrayList<String>();
        for (Event evt : evts) {
            if (evt.getType() == Event.NODE_REMOVED) {
                eventPaths.add(evt.getPath());
            }
        }
        assertTrue(eventPaths.contains(childNPath));
        assertTrue(eventPaths.contains(childNPath2));
    } finally {
        obsMgr.removeEventListener(listener);
    }
}
Also used : EventResult(org.apache.jackrabbit.test.api.observation.EventResult) ArrayList(java.util.ArrayList) Event(javax.jcr.observation.Event) ObservationManager(javax.jcr.observation.ObservationManager) Test(org.junit.Test)

Aggregations

Event (javax.jcr.observation.Event)138 Node (javax.jcr.Node)104 Test (org.junit.Test)56 JackrabbitNode (org.apache.jackrabbit.api.JackrabbitNode)46 AbstractRepositoryTest (org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)41 RepositoryException (javax.jcr.RepositoryException)31 Session (javax.jcr.Session)21 JackrabbitEventFilter (org.apache.jackrabbit.api.observation.JackrabbitEventFilter)19 EventResult (org.apache.jackrabbit.test.api.observation.EventResult)17 EventIterator (javax.jcr.observation.EventIterator)16 ObservationManager (javax.jcr.observation.ObservationManager)14 Property (javax.jcr.Property)13 SlingRepository (org.apache.sling.jcr.api.SlingRepository)10 ArrayList (java.util.ArrayList)9 EventListener (javax.jcr.observation.EventListener)9 Scheduler (org.apache.sling.commons.scheduler.Scheduler)9 DistributionRequest (org.apache.sling.distribution.DistributionRequest)9 ResourceResolverFactory (org.apache.sling.api.resource.ResourceResolverFactory)8 ExecutionException (java.util.concurrent.ExecutionException)7 PathNotFoundException (javax.jcr.PathNotFoundException)7