Search in sources :

Example 1 with EventListener

use of org.xwiki.observation.EventListener in project xwiki-platform by xwiki.

the class DefaultVelocityManager method initialize.

@Override
public void initialize() throws InitializationException {
    this.observation.addListener(new EventListener() {

        @Override
        public void onEvent(Event event, Object source, Object data) {
            if (event instanceof TemplateEvent) {
                TemplateEvent templateEvent = (TemplateEvent) event;
                DefaultVelocityManager.this.velocityFactory.removeVelocityEngine(templateEvent.getId());
            }
        }

        @Override
        public String getName() {
            return DefaultVelocityManager.class.getName();
        }

        @Override
        public List<Event> getEvents() {
            return EVENTS;
        }
    });
    // Set reserved bindings
    // "context" is a reserved binding in JSR223 world
    this.reservedBindings.add("context");
    // Macros directive
    this.reservedBindings.add("macro");
    // Foreach directive
    this.reservedBindings.add("foreach");
    this.reservedBindings.add(this.velocityConfiguration.getProperties().getProperty(RuntimeConstants.COUNTER_NAME, RuntimeSingleton.getString(RuntimeConstants.COUNTER_NAME)));
    this.reservedBindings.add(this.velocityConfiguration.getProperties().getProperty(RuntimeConstants.HAS_NEXT_NAME, RuntimeSingleton.getString(RuntimeConstants.HAS_NEXT_NAME)));
    // Evaluate directive
    this.reservedBindings.add("evaluate");
    // TryCatch directive
    this.reservedBindings.add("exception");
    this.reservedBindings.add("try");
    // Default directive
    this.reservedBindings.add("define");
    // The name of the context variable used for the template-level scope
    this.reservedBindings.add("template");
}
Also used : TemplateEvent(org.xwiki.template.event.TemplateEvent) TemplateDeletedEvent(org.xwiki.template.event.TemplateDeletedEvent) TemplateUpdatedEvent(org.xwiki.template.event.TemplateUpdatedEvent) TemplateEvent(org.xwiki.template.event.TemplateEvent) Event(org.xwiki.observation.event.Event) List(java.util.List) EventListener(org.xwiki.observation.EventListener)

Example 2 with EventListener

use of org.xwiki.observation.EventListener in project xwiki-platform by xwiki.

the class IntegrationTests method initialize.

@RenderingTestSuite.Initialized
public void initialize(MockingComponentManager cm) throws Exception {
    Mockery mockery = new JUnit4Mockery();
    new ScriptMockSetup(mockery, cm);
    // fake nested script validator never fails
    final EventListener nestedValidator = cm.registerMockComponent(mockery, EventListener.class, "nestedscriptmacrovalidator");
    mockery.checking(new Expectations() {

        {
            atLeast(1).of(nestedValidator).onEvent(with(any(Event.class)), with(any(MacroTransformationContext.class)), with(any(ScriptMacroParameters.class)));
            allowing(nestedValidator).getName();
            will(returnValue("nestedscriptmacrovalidator"));
            allowing(nestedValidator).getEvents();
            will(returnValue(Collections.singletonList((Event) new ScriptEvaluatingEvent())));
        }
    });
}
Also used : Expectations(org.jmock.Expectations) JUnit4Mockery(org.jmock.integration.junit4.JUnit4Mockery) ScriptEvaluatingEvent(org.xwiki.script.event.ScriptEvaluatingEvent) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) ScriptEvaluatingEvent(org.xwiki.script.event.ScriptEvaluatingEvent) Event(org.xwiki.observation.event.Event) EventListener(org.xwiki.observation.EventListener) JUnit4Mockery(org.jmock.integration.junit4.JUnit4Mockery) Mockery(org.jmock.Mockery)

Example 3 with EventListener

use of org.xwiki.observation.EventListener in project xwiki-platform by xwiki.

the class TCPROMTest method testSerializableEvent.

/**
 * Validate sharing a simple Serializable event between two instances of {@link RemoteObservationManager}.
 */
@Test
public void testSerializableEvent() throws InterruptedException {
    final EventListener localListener = this.mockery.mock(EventListener.class, "local");
    final EventListener remoteListener = this.mockery.mock(EventListener.class, "remote");
    final TestEvent event = new TestEvent();
    final Unserializable unserializable = new Unserializable();
    this.mockery.checking(new Expectations() {

        {
            allowing(localListener).getName();
            will(returnValue("mylistener"));
            allowing(remoteListener).getName();
            will(returnValue("mylistener"));
            allowing(localListener).getEvents();
            will(returnValue(Arrays.asList(event)));
            allowing(remoteListener).getEvents();
            will(returnValue(Arrays.asList(event)));
            oneOf(localListener).onEvent(with(same(event)), with(equal("some source")), with(equal("some data")));
            oneOf(localListener).onEvent(with(same(event)), with(same(unserializable)), with(same(unserializable)));
            oneOf(remoteListener).onEvent(with(equal(event)), with(equal("some source")), with(equal("some data")));
        }
    });
    getObservationManager1().addListener(localListener);
    getObservationManager2().addListener(remoteListener);
    getObservationManager1().notify(event, "some source", "some data");
    getObservationManager1().notify(event, unserializable, unserializable);
    getObservationManager1().notify(new LogEvent(), "some source", "some data");
    // Make sure JGroups has enough time to send the message
    Thread.sleep(1000);
}
Also used : Expectations(org.jmock.Expectations) TestEvent(org.xwiki.observation.remote.test.TestEvent) LogEvent(org.xwiki.logging.event.LogEvent) EventListener(org.xwiki.observation.EventListener) Test(org.junit.Test)

Example 4 with EventListener

use of org.xwiki.observation.EventListener in project xwiki-platform by xwiki.

the class XWikiHibernateStoreTest method testLocksAreReleasedOnLogout.

@Test
public void testLocksAreReleasedOnLogout() throws Exception {
    // Capture the event listener.
    ObservationManager observationManager = getMocker().getInstance(ObservationManager.class);
    ArgumentCaptor<EventListener> eventListenerCaptor = ArgumentCaptor.forClass(EventListener.class);
    verify(observationManager).addListener(eventListenerCaptor.capture());
    assertEquals("deleteLocksOnLogoutListener", eventListenerCaptor.getValue().getName());
    Query query = mock(Query.class);
    when(session.createQuery("delete from XWikiLock as lock where lock.userName=:userName")).thenReturn(query);
    when(xcontext.getUserReference()).thenReturn(new DocumentReference("xwiki", "XWiki", "LoggerOutter"));
    when(xcontext.getUser()).thenReturn("XWiki.LoggerOutter");
    // Fire the logout event.
    eventListenerCaptor.getValue().onEvent(new ActionExecutingEvent("logout"), null, xcontext);
    verify(query).setString("userName", "XWiki.LoggerOutter");
    verify(query).executeUpdate();
    verify(this.hibernateStore).beginTransaction();
    verify(this.hibernateStore).endTransaction(true);
}
Also used : SQLQuery(org.hibernate.SQLQuery) Query(org.hibernate.Query) ObservationManager(org.xwiki.observation.ObservationManager) ActionExecutingEvent(org.xwiki.bridge.event.ActionExecutingEvent) EventListener(org.xwiki.observation.EventListener) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Aggregations

EventListener (org.xwiki.observation.EventListener)4 Expectations (org.jmock.Expectations)2 Test (org.junit.Test)2 Event (org.xwiki.observation.event.Event)2 List (java.util.List)1 Query (org.hibernate.Query)1 SQLQuery (org.hibernate.SQLQuery)1 Mockery (org.jmock.Mockery)1 JUnit4Mockery (org.jmock.integration.junit4.JUnit4Mockery)1 ActionExecutingEvent (org.xwiki.bridge.event.ActionExecutingEvent)1 LogEvent (org.xwiki.logging.event.LogEvent)1 DocumentReference (org.xwiki.model.reference.DocumentReference)1 ObservationManager (org.xwiki.observation.ObservationManager)1 TestEvent (org.xwiki.observation.remote.test.TestEvent)1 MacroTransformationContext (org.xwiki.rendering.transformation.MacroTransformationContext)1 ScriptEvaluatingEvent (org.xwiki.script.event.ScriptEvaluatingEvent)1 TemplateDeletedEvent (org.xwiki.template.event.TemplateDeletedEvent)1 TemplateEvent (org.xwiki.template.event.TemplateEvent)1 TemplateUpdatedEvent (org.xwiki.template.event.TemplateUpdatedEvent)1