Search in sources :

Example 46 with Event

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

the class ObservationTest method propertyValues.

@Test
public void propertyValues() throws RepositoryException, ExecutionException, InterruptedException {
    Node n = getNode(TEST_PATH);
    n.setProperty("toChange", new String[] { "one", "two" }, PropertyType.STRING);
    n.setProperty("toDelete", new String[] { "three", "four" }, PropertyType.STRING);
    getAdminSession().save();
    ExpectationListener listener = new ExpectationListener();
    observationManager.addEventListener(listener, ALL_EVENTS, "/", true, null, null, false);
    try {
        n.setProperty("added", new String[] { "five", "six" }, PropertyType.STRING);
        listener.expectValues(null, n.getProperty("added").getValues());
        Value[] before = n.getProperty("toChange").getValues();
        n.getProperty("toChange").setValue(new String[] { "1", "2" });
        listener.expectValues(before, n.getProperty("toChange").getValues());
        before = n.getProperty("toDelete").getValues();
        n.getProperty("toDelete").remove();
        listener.expectValues(before, null);
        getAdminSession().save();
        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());
    } finally {
        observationManager.removeEventListener(listener);
    }
}
Also used : JackrabbitNode(org.apache.jackrabbit.api.JackrabbitNode) Node(javax.jcr.Node) Value(javax.jcr.Value) Event(javax.jcr.observation.Event) AbstractRepositoryTest(org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest) Test(org.junit.Test)

Example 47 with Event

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

the class ObservationTest method testAggregate2.

@Test
public void testAggregate2() throws Exception {
    assumeTrue(observationManager instanceof ObservationManagerImpl);
    ObservationManagerImpl oManager = (ObservationManagerImpl) observationManager;
    ExpectationListener listener = new ExpectationListener();
    JackrabbitEventFilter filter = new JackrabbitEventFilter();
    filter.setAbsPath("/parent");
    filter.setIsDeep(true);
    filter.setEventTypes(ALL_EVENTS);
    filter = FilterFactory.wrap(filter).withNodeTypeAggregate(new String[] { "oak:Unstructured" }, // "file", "file/jcr:content",
    new String[] { "", "**" });
    // "file/jcr:content/**");
    oManager.addEventListener(listener, filter);
    Node parent = getAdminSession().getRootNode().addNode("parent", "nt:unstructured");
    // OAK-5096: in OR mode the following event also gets sent:
    listener.expect(parent.getPath() + "/jcr:primaryType", PROPERTY_ADDED);
    Node child = parent.addNode("child", "oak:Unstructured");
    listener.expectAdd(child);
    Node file = child.addNode("file", "nt:unstructured");
    listener.expectAdd(file);
    Node jcrContent = file.addNode("jcr:content", "nt:unstructured");
    listener.expect(jcrContent.getPath(), "/parent/child", NODE_ADDED);
    listener.expect(jcrContent.getPath() + "/jcr:primaryType", "/parent/child", PROPERTY_ADDED);
    Property jcrDataProperty = jcrContent.setProperty("jcr:data", "foo");
    listener.expect(jcrDataProperty.getPath(), "/parent/child", PROPERTY_ADDED);
    parent.getSession().save();
    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());
    file = getAdminSession().getRootNode().getNode("parent").getNode("child").getNode("file");
    jcrContent = file.getNode("jcr:content");
    Property newProperty = jcrContent.setProperty("newProperty", "foo");
    listener.expect(newProperty.getPath(), "/parent/child", PROPERTY_ADDED);
    Property lastModifiedBy = jcrContent.setProperty("jcr:lastModifiedBy", "bar");
    listener.expect(lastModifiedBy.getPath(), "/parent/child", PROPERTY_ADDED);
    jcrContent.getSession().save();
    Thread.sleep(2000);
    missing = listener.getMissing(TIME_OUT, TimeUnit.SECONDS);
    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) Property(javax.jcr.Property) AbstractRepositoryTest(org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest) Test(org.junit.Test)

Example 48 with Event

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

the class ObservationTest method testConsecutiveGlobPaths.

@Test
public void testConsecutiveGlobPaths() throws Exception {
    Node testNode = getNode(TEST_PATH);
    Node a1 = testNode.addNode("a1");
    a1.addNode("b1").addNode("c1");
    a1.addNode("b2").addNode("c2");
    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/**").withIncludeGlobPaths(TEST_PATH + "/a1/**");
    oManager.addEventListener(listener, filter);
    ChangeProcessor cp = oManager.getChangeProcessor(listener);
    assertNotNull(cp);
    FilterProvider filterProvider = cp.getFilterProvider();
    assertNotNull(filterProvider);
    assertMatches(filterProvider.getSubTrees(), TEST_PATH + "/a1", TEST_PATH + "/a2");
    listener.expectRemove(testNode.getNode("a1").getNode("b2")).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());
}
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 49 with Event

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

the class ObservationTest method pathFilterWithTrailingSlash.

@Test
public void pathFilterWithTrailingSlash() throws Exception {
    final String path = "/events/only/here";
    ExpectationListener listener = new ExpectationListener();
    listener.expect(new Expectation(path) {

        @Override
        public boolean onEvent(Event event) throws Exception {
            return PathUtils.isAncestor(path, event.getPath());
        }
    });
    observationManager.addEventListener(listener, NODE_ADDED, path + '/', true, null, null, false);
    try {
        Node root = getNode("/");
        root.addNode("events").addNode("only").addNode("here").addNode("at");
        root.getSession().save();
        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());
    } finally {
        observationManager.removeEventListener(listener);
    }
}
Also used : JackrabbitNode(org.apache.jackrabbit.api.JackrabbitNode) Node(javax.jcr.Node) Event(javax.jcr.observation.Event) 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) AbstractRepositoryTest(org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest) Test(org.junit.Test)

Example 50 with Event

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

the class ObservationTest method addSubtreeFilter.

@Test
public void addSubtreeFilter() throws RepositoryException, ExecutionException, InterruptedException {
    assumeTrue(observationManager instanceof ObservationManagerImpl);
    ObservationManagerImpl oManager = (ObservationManagerImpl) observationManager;
    ExpectationListener listener = new ExpectationListener();
    FilterBuilder builder = new FilterBuilder();
    // Only generate events for the root of added sub trees
    builder.condition(builder.addSubtree());
    oManager.addEventListener(listener, builder.build());
    Node testNode = getNode(TEST_PATH);
    Node a = listener.expectAdd(testNode.addNode("a"));
    a.addNode("c");
    testNode.getSession().save();
    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());
}
Also used : FilterBuilder(org.apache.jackrabbit.oak.plugins.observation.filter.FilterBuilder) JackrabbitNode(org.apache.jackrabbit.api.JackrabbitNode) Node(javax.jcr.Node) Event(javax.jcr.observation.Event) AbstractRepositoryTest(org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest) Test(org.junit.Test)

Aggregations

Event (javax.jcr.observation.Event)136 Node (javax.jcr.Node)103 Test (org.junit.Test)55 JackrabbitNode (org.apache.jackrabbit.api.JackrabbitNode)46 AbstractRepositoryTest (org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)40 RepositoryException (javax.jcr.RepositoryException)29 Session (javax.jcr.Session)20 JackrabbitEventFilter (org.apache.jackrabbit.api.observation.JackrabbitEventFilter)19 EventResult (org.apache.jackrabbit.test.api.observation.EventResult)17 EventIterator (javax.jcr.observation.EventIterator)15 Property (javax.jcr.Property)13 ObservationManager (javax.jcr.observation.ObservationManager)13 SlingRepository (org.apache.sling.jcr.api.SlingRepository)10 ArrayList (java.util.ArrayList)9 Scheduler (org.apache.sling.commons.scheduler.Scheduler)9 DistributionRequest (org.apache.sling.distribution.DistributionRequest)9 ResourceResolverFactory (org.apache.sling.api.resource.ResourceResolverFactory)8 PathNotFoundException (javax.jcr.PathNotFoundException)7 EventListener (javax.jcr.observation.EventListener)7 JackrabbitObservationManager (org.apache.jackrabbit.api.observation.JackrabbitObservationManager)7