All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.jackrabbit.test.api.observation.AbstractObservationTest Maven / Gradle / Ivy

There is a newer version: 2.23.1-beta
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.jackrabbit.test.api.observation;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.jcr.RepositoryException;
import javax.jcr.UnsupportedRepositoryOperationException;
import javax.jcr.observation.Event;
import javax.jcr.observation.EventListener;
import javax.jcr.observation.EventListenerIterator;
import javax.jcr.observation.ObservationManager;

import org.apache.jackrabbit.test.AbstractJCRTest;
import org.apache.jackrabbit.test.NotExecutableException;

/**
 * This class implements the basic {@link #setUp} and {@link #tearDown()}
 * methods for the observation test cases.
 */
public abstract class AbstractObservationTest extends AbstractJCRTest {

    /**
     * Default wait timeout for events: 5000 ms
     */
    protected static final long DEFAULT_WAIT_TIMEOUT = 5000;


    protected static final int ALL_TYPES = Event.NODE_ADDED | Event.NODE_REMOVED | Event.PROPERTY_ADDED | Event.PROPERTY_CHANGED | Event.PROPERTY_REMOVED | javax.jcr.observation.Event.NODE_MOVED;

    /**
     * The ObservationManager
     */
    protected ObservationManager obsMgr;

    protected void setUp() throws Exception {
        super.setUp();
        try {
            obsMgr = superuser.getWorkspace().getObservationManager();
        }
        catch (UnsupportedRepositoryOperationException ex) {
            throw new NotExecutableException("observation not supported");
        }
    }

    protected void tearDown() throws Exception {
        obsMgr = null;
        super.tearDown();
    }

    /**
     * Registers an EventListener for all events.
     *
     * @param listener the EventListener.
     * @throws RepositoryException if registration fails.
     */
    protected void addEventListener(EventListener listener) throws RepositoryException {
        addEventListener(listener,
                Event.NODE_ADDED | Event.NODE_REMOVED | Event.PROPERTY_ADDED | Event.PROPERTY_CHANGED | Event.PROPERTY_REMOVED);
    }

    /**
     * Registers an EventListener for events of the specified
     * type(s).
     *
     * @param listener  the EventListener.
     * @param eventType the event types
     * @throws RepositoryException if registration fails.
     */
    protected void addEventListener(EventListener listener, int eventType)
            throws RepositoryException {
        if (obsMgr != null) {
            obsMgr.addEventListener(listener,
                    eventType,
                    superuser.getRootNode().getPath(),
                    true,
                    null,
                    null,
                    false);
        } else {
            throw new IllegalStateException("ObservationManager not available.");
        }
    }

    /**
     * Removes the EventListener from the ObservationManager.
     *
     * @param listener the EventListener to unregister.
     * @throws RepositoryException if unregister fails.
     */
    protected void removeEventListener(EventListener listener) throws RepositoryException {
        if (obsMgr != null) {
            obsMgr.removeEventListener(listener);
        } else {
            throw new IllegalStateException("ObservationManager not available.");
        }
    }

    /**
     * Consumes the EventListenerIterator and returns the
     * EventListener as an array.
     * @param it the iterator.
     * @return array of EventListeners.
     */
    protected EventListener[] toArray(EventListenerIterator it) {
        List listeners = new ArrayList();
        while (it.hasNext()) {
            listeners.add(it.nextEventListener());
        }
        return listeners.toArray(new EventListener[listeners.size()]);
    }

    //--------------------< check methods >-------------------------------------

    /**
     * Checks Events for paths. All relPaths are
     * relative to {@link #testRoot}.
     *
     * @param events   the Events.
     * @param requiredRelPaths paths to child nodes added relative to {@link
     *                 #testRoot} (required events).
     * @param optionalRelPaths paths to child nodes added relative to {@link
     *                 #testRoot} (optional events).
     * @throws RepositoryException if an error occurs while retrieving the nodes
     *                             from event instances.
     */
    protected void checkNodeAdded(Event[] events, String[] requiredRelPaths, String[] optionalRelPaths)
            throws RepositoryException {
        checkNodes(events, requiredRelPaths, optionalRelPaths, Event.NODE_ADDED);
    }

    /**
     * Checks Events for paths. All relPaths are
     * relative to {@link #testRoot}.
     *
     * @param events   the Events.
     * @param requiredRelPaths paths to child nodes added relative to {@link
     *                 #testRoot} (required events).
     * @param optionalRelPaths paths to child nodes added relative to {@link
     *                 #testRoot} (optional events).
     * @throws RepositoryException if an error occurs while retrieving the nodes
     *                             from event instances.
     */
    protected void checkNodeRemoved(Event[] events, String[] requiredRelPaths, String[] optionalRelPaths)
            throws RepositoryException {
        checkNodes(events, requiredRelPaths, optionalRelPaths, Event.NODE_REMOVED);
    }

    /**
     * Checks Events for paths. All relPaths are
     * relative to {@link #testRoot}.
     *
     * @param events   the Events.
     * @param relPaths paths to added properties relative to {@link
     *                 #testRoot}.
     * @throws RepositoryException if an error occurs while retrieving the nodes
     *                             from event instances.
     */
    protected void checkPropertyAdded(Event[] events, String[] relPaths)
            throws RepositoryException {
        checkNodes(events, relPaths, null, Event.PROPERTY_ADDED);
    }

    /**
     * Checks Events for paths. All relPaths are
     * relative to {@link #testRoot}.
     *
     * @param events   the Events.
     * @param relPaths paths to changed properties relative to {@link
     *                 #testRoot}.
     * @throws RepositoryException if an error occurs while retrieving the nodes
     *                             from event instances.
     */
    protected void checkPropertyChanged(Event[] events, String[] relPaths)
            throws RepositoryException {
        checkNodes(events, relPaths, null, Event.PROPERTY_CHANGED);
    }

    /**
     * Checks Events for paths. All relPaths are
     * relative to {@link #testRoot}.
     *
     * @param events   the Events.
     * @param relPaths paths to removed properties relative to {@link
     *                 #testRoot}.
     * @throws RepositoryException if an error occurs while retrieving the nodes
     *                             from event instances.
     */
    protected void checkPropertyRemoved(Event[] events, String[] relPaths)
            throws RepositoryException {
        checkNodes(events, relPaths, null, Event.PROPERTY_REMOVED);
    }

    /**
     * Checks Events for paths. All relPaths are
     * relative to {@link #testRoot}.
     *
     * @param events    the Events.
     * @param requiredRelPaths  paths to required item events relative to {@link #testRoot}.
     * @param optionalRelPaths  paths to optional item events relative to {@link #testRoot}.
     * @param eventType the type of event to check.
     * @throws RepositoryException if an error occurs while retrieving the nodes
     *                             from event instances.
     */
    protected void checkNodes(Event[] events, String[] requiredRelPaths, String[] optionalRelPaths, long eventType)
            throws RepositoryException {
        Set paths = new HashSet();
        for (int i = 0; i < events.length; i++) {
            assertEquals("Wrong event type", eventType, events[i].getType());
            String path = events[i].getPath();
            paths.add(path);
        }
        // check all required paths are there
        for (int i = 0; i < requiredRelPaths.length; i++) {
            String expected = testRoot + "/" + requiredRelPaths[i];
            assertTrue("Path " + expected + " not found in events.",
                    paths.contains(expected));
            paths.remove(expected);
        }
        // check what remains in the set is indeed optional
        Set optional = new HashSet();
        if (optionalRelPaths != null) {
            for (int i = 0; i < optionalRelPaths.length; i++) {
                optional.add(testRoot + "/" + optionalRelPaths[i]);
            }
        }
        for (Iterator it = paths.iterator(); it.hasNext(); ) {
            String path = it.next();
            assertTrue("Path " + path + " not expected in events.",
                    optional.contains(path));
        }
    }
    

    /**
     * Registers an event listener for the passed eventTypes and
     * calls the callable.
     *
     * @param call       the callable.
     * @param eventTypes the types of the events to listen for.
     * @return the events that were generated during execution of the callable.
     * @throws RepositoryException if an error occurs.
     */
    protected Event[] getEvents(Callable call, int eventTypes)
            throws RepositoryException {
        EventResult result = new EventResult(log);
        addEventListener(result, eventTypes);
        call.call();
        Event[] events = result.getEvents(DEFAULT_WAIT_TIMEOUT);
        removeEventListener(result);
        return events;
    }

    /**
     * Returns the first event with the given path.
     *
     * @param events the events.
     * @param path   the path.
     * @return the event with the given path or {@link #fail()}s if
     *         no such event exists.
     * @throws RepositoryException if an error occurs while reading from the
     *                             repository.
     */
    protected Event getEventByPath(Event[] events, String path)
            throws RepositoryException {
        for (int i = 0; i < events.length; i++) {
            if (events[i].getPath().equals(path)) {
                return events[i];
            }
        }
        fail("no event with path: " + path + " in " + Arrays.asList(events));
        return null;
    }
    
    /**
     * Helper interface.
     */
    protected interface Callable {
        public void call() throws RepositoryException;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy