Search in sources :

Example 31 with Event

use of javax.jcr.observation.Event in project sling by apache.

the class SuperimposingManagerImplTest method prepareNodeMoveEvent.

private EventIterator prepareNodeMoveEvent(Resource pResource, String pOldPath) throws RepositoryException {
    String resourcePath = pResource.getPath();
    Event nodeRemoveEvent = mock(Event.class);
    when(nodeRemoveEvent.getType()).thenReturn(Event.NODE_REMOVED);
    when(nodeRemoveEvent.getPath()).thenReturn(pOldPath);
    Event nodeCreateEvent = mock(Event.class);
    when(nodeCreateEvent.getType()).thenReturn(Event.NODE_ADDED);
    when(nodeCreateEvent.getPath()).thenReturn(resourcePath);
    EventIterator eventIterator = mock(EventIterator.class);
    when(eventIterator.hasNext()).thenReturn(true, true, false);
    when(eventIterator.nextEvent()).thenReturn(nodeRemoveEvent, nodeCreateEvent);
    return eventIterator;
}
Also used : Event(javax.jcr.observation.Event) EventIterator(javax.jcr.observation.EventIterator)

Example 32 with Event

use of javax.jcr.observation.Event in project sling by apache.

the class SuperimposingManagerImplTest method prepareNodeCreateEvent.

private EventIterator prepareNodeCreateEvent(Resource pResource) throws RepositoryException {
    String resourcePath = pResource.getPath();
    Event nodeEvent = mock(Event.class);
    when(nodeEvent.getType()).thenReturn(Event.NODE_ADDED);
    when(nodeEvent.getPath()).thenReturn(resourcePath);
    Event propertyEvent = mock(Event.class);
    when(propertyEvent.getType()).thenReturn(Event.PROPERTY_ADDED);
    when(propertyEvent.getPath()).thenReturn(resourcePath + "/" + SuperimposingResourceProvider.PROP_SUPERIMPOSE_SOURCE_PATH);
    EventIterator eventIterator = mock(EventIterator.class);
    when(eventIterator.hasNext()).thenReturn(true, true, false);
    when(eventIterator.nextEvent()).thenReturn(nodeEvent, propertyEvent);
    return eventIterator;
}
Also used : Event(javax.jcr.observation.Event) EventIterator(javax.jcr.observation.EventIterator)

Example 33 with Event

use of javax.jcr.observation.Event in project sling by apache.

the class SuperimposingManagerImplTest method prepareNodeRemoveEvent.

private EventIterator prepareNodeRemoveEvent(Resource pResource) throws RepositoryException {
    String resourcePath = pResource.getPath();
    Event nodeEvent = mock(Event.class);
    when(nodeEvent.getType()).thenReturn(Event.NODE_REMOVED);
    when(nodeEvent.getPath()).thenReturn(resourcePath);
    EventIterator eventIterator = mock(EventIterator.class);
    when(eventIterator.hasNext()).thenReturn(true, false);
    when(eventIterator.nextEvent()).thenReturn(nodeEvent);
    return eventIterator;
}
Also used : Event(javax.jcr.observation.Event) EventIterator(javax.jcr.observation.EventIterator)

Example 34 with Event

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

the class CompatibilityIssuesTest method noEventsForTouchedProperties.

/**
 * OAK-948 - JR2 generates propertyChange event for touched properties while Oak does not
 */
@Test
public void noEventsForTouchedProperties() throws RepositoryException, InterruptedException {
    final String testNodeName = "test_touched_node";
    final String testNodePath = '/' + testNodeName;
    // Create the test node
    Session session = getAdminSession();
    Node testNode = session.getRootNode().addNode(testNodeName);
    testNode.setProperty("foo", "bar");
    testNode.setProperty("foo2", "bar0");
    session.save();
    Session observingSession = createAdminSession();
    ObservationManager observationManager = observingSession.getWorkspace().getObservationManager();
    try {
        final List<Event> events = new ArrayList<Event>();
        final CountDownLatch latch = new CountDownLatch(1);
        EventListener listener = new EventListener() {

            @Override
            public void onEvent(EventIterator eventIt) {
                while (eventIt.hasNext()) {
                    events.add(eventIt.nextEvent());
                }
                if (!events.isEmpty()) {
                    latch.countDown();
                }
            }
        };
        observationManager.addEventListener(listener, PROPERTY_CHANGED, testNodePath, true, null, null, false);
        // Now touch foo and modify foo2
        session.getNode(testNodePath).setProperty("foo", "bar");
        session.getNode(testNodePath).setProperty("foo2", "bar2");
        session.save();
        latch.await(60, TimeUnit.SECONDS);
        // Only one event is recorded for foo2 modification
        assertEquals(1, events.size());
    } finally {
        observingSession.logout();
    }
}
Also used : Node(javax.jcr.Node) ArrayList(java.util.ArrayList) Event(javax.jcr.observation.Event) ObservationManager(javax.jcr.observation.ObservationManager) EventListener(javax.jcr.observation.EventListener) CountDownLatch(java.util.concurrent.CountDownLatch) EventIterator(javax.jcr.observation.EventIterator) Session(javax.jcr.Session) Test(org.junit.Test)

Example 35 with Event

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

the class JackrabbitNodeTest method _testRenameEventHandling.

// Ignore("OAK-3658")
public void _testRenameEventHandling() throws RepositoryException, InterruptedException {
    Session s = getHelper().getSuperuserSession();
    ObservationManager mgr = s.getWorkspace().getObservationManager();
    final List<Event> events = newArrayList();
    final CountDownLatch latch1 = new CountDownLatch(1);
    final CountDownLatch latch2 = new CountDownLatch(1);
    try {
        mgr.addEventListener(new EventListener() {

            CountDownLatch latch = latch1;

            @Override
            public void onEvent(EventIterator eventIterator) {
                synchronized (events) {
                    while (eventIterator.hasNext()) {
                        events.add(eventIterator.nextEvent());
                    }
                    latch.countDown();
                    latch = latch2;
                }
            }
        }, Event.PERSIST | Event.NODE_ADDED | Event.NODE_MOVED | Event.NODE_REMOVED, testRootNode.getPath(), true, null, null, false);
        NodeIterator it = testRootNode.getNodes();
        Node n = it.nextNode();
        String name = n.getName();
        JackrabbitNode node = (JackrabbitNode) n;
        node.rename(name + 'X');
        superuser.save();
        StringBuilder diags = new StringBuilder();
        if (!latch1.await(60, SECONDS)) {
            diags.append("latch1 timed out ");
        }
        boolean foundMove = false;
        synchronized (events) {
            for (Event event : events) {
                if (diags.length() != 0) {
                    diags.append(", ");
                }
                diags.append("type " + event.getType() + " " + event.getDate() + "ms " + event.getPath());
                if (Event.NODE_MOVED == event.getType()) {
                    foundMove = true;
                    break;
                }
            }
            if (events.isEmpty()) {
                diags.append("none");
            }
        }
        if (!foundMove) {
            // force another event, wait some more
            testRootNode.addNode(name + "XYZ");
            superuser.save();
            StringBuffer addDiags = new StringBuffer();
            if (!latch2.await(60, SECONDS)) {
                addDiags.append("latch2 timed out ");
            }
            synchronized (events) {
                for (Event event : events) {
                    if (addDiags.length() != 0) {
                        addDiags.append(", ");
                    }
                    addDiags.append("type " + event.getType() + " " + event.getDate() + "ms " + event.getPath());
                }
            }
            if (addDiags.length() > 0) {
                diags.append("; next event after additional addNode/save operation: " + addDiags);
            }
        }
        if (!foundMove) {
            fail("Expected NODE_MOVED event upon renaming a node (received: " + diags + ")");
        }
    } finally {
        s.logout();
    }
}
Also used : NodeIterator(javax.jcr.NodeIterator) JackrabbitNode(org.apache.jackrabbit.api.JackrabbitNode) JackrabbitNode(org.apache.jackrabbit.api.JackrabbitNode) Node(javax.jcr.Node) ObservationManager(javax.jcr.observation.ObservationManager) CountDownLatch(java.util.concurrent.CountDownLatch) Event(javax.jcr.observation.Event) EventListener(javax.jcr.observation.EventListener) EventIterator(javax.jcr.observation.EventIterator) Session(javax.jcr.Session)

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