Search in sources :

Example 36 with ApplicationEvent

use of org.springframework.context.ApplicationEvent in project spring-integration by spring-projects.

the class DeserializationTests method doDeserialize.

private TcpDeserializationExceptionEvent doDeserialize(AbstractByteArraySerializer deser, String expectedMessage, byte[] data, int mms) {
    final AtomicReference<TcpDeserializationExceptionEvent> event = new AtomicReference<TcpDeserializationExceptionEvent>();
    class Publisher implements ApplicationEventPublisher {

        @Override
        public void publishEvent(ApplicationEvent anEvent) {
            event.set((TcpDeserializationExceptionEvent) anEvent);
        }

        @Override
        public void publishEvent(Object event) {
        }
    }
    Publisher publisher = new Publisher();
    ByteArrayInputStream bais = new ByteArrayInputStream(data);
    deser.setApplicationEventPublisher(publisher);
    deser.setMaxMessageSize(mms);
    try {
        deser.deserialize(bais);
        fail("expected exception");
    } catch (Exception e) {
        assertNotNull(event.get());
        assertSame(e, event.get().getCause());
        assertThat(e.getMessage(), containsString(expectedMessage));
    }
    return event.get();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) ApplicationEvent(org.springframework.context.ApplicationEvent) AtomicReference(java.util.concurrent.atomic.AtomicReference) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) IOException(java.io.IOException)

Example 37 with ApplicationEvent

use of org.springframework.context.ApplicationEvent in project spring-framework by spring-projects.

the class ApplicationContextEventTests method lambdaAsListenerWithJava9StyleClassCastMessage.

@Test
public void lambdaAsListenerWithJava9StyleClassCastMessage() {
    StaticApplicationContext context = new StaticApplicationContext();
    ApplicationListener<ApplicationEvent> listener = event -> {
        throw new ClassCastException("spring.context/" + event.getClass().getName());
    };
    context.addApplicationListener(listener);
    context.refresh();
    context.publishEvent(new MyEvent(context));
    context.close();
}
Also used : RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Ordered(org.springframework.core.Ordered) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) InitializingBean(org.springframework.beans.factory.InitializingBean) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) MethodInvocation(org.aopalliance.intercept.MethodInvocation) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) BDDMockito.given(org.mockito.BDDMockito.given) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) TestBean(org.springframework.beans.testfixture.beans.TestBean) BeanThatBroadcasts(org.springframework.context.testfixture.beans.BeanThatBroadcasts) ResolvableType(org.springframework.core.ResolvableType) PayloadApplicationEvent(org.springframework.context.PayloadApplicationEvent) ArgumentMatchers.isA(org.mockito.ArgumentMatchers.isA) ApplicationEventPublisherAware(org.springframework.context.ApplicationEventPublisherAware) Order(org.springframework.core.annotation.Order) BDDMockito.willThrow(org.mockito.BDDMockito.willThrow) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference) Set(java.util.Set) BeansException(org.springframework.beans.BeansException) Mockito.times(org.mockito.Mockito.times) ApplicationListener(org.springframework.context.ApplicationListener) ApplicationContext(org.springframework.context.ApplicationContext) BeanThatListens(org.springframework.context.testfixture.beans.BeanThatListens) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) StaticMessageSource(org.springframework.context.support.StaticMessageSource) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) BeanPostProcessor(org.springframework.beans.factory.config.BeanPostProcessor) ApplicationEvent(org.springframework.context.ApplicationEvent) List(java.util.List) AbstractApplicationContext(org.springframework.context.support.AbstractApplicationContext) ReflectionUtils(org.springframework.util.ReflectionUtils) ProxyFactory(org.springframework.aop.framework.ProxyFactory) TaskUtils(org.springframework.scheduling.support.TaskUtils) ApplicationContextAware(org.springframework.context.ApplicationContextAware) Mockito.mock(org.mockito.Mockito.mock) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) PayloadApplicationEvent(org.springframework.context.PayloadApplicationEvent) ApplicationEvent(org.springframework.context.ApplicationEvent) Test(org.junit.jupiter.api.Test)

Example 38 with ApplicationEvent

use of org.springframework.context.ApplicationEvent in project spring-framework by spring-projects.

the class ApplicationContextEventTests method simpleApplicationEventMulticasterWithErrorHandler.

@Test
public void simpleApplicationEventMulticasterWithErrorHandler() {
    @SuppressWarnings("unchecked") ApplicationListener<ApplicationEvent> listener = mock(ApplicationListener.class);
    ApplicationEvent evt = new ContextClosedEvent(new StaticApplicationContext());
    SimpleApplicationEventMulticaster smc = new SimpleApplicationEventMulticaster();
    smc.setErrorHandler(TaskUtils.LOG_AND_SUPPRESS_ERROR_HANDLER);
    smc.addApplicationListener(listener);
    willThrow(new RuntimeException()).given(listener).onApplicationEvent(evt);
    smc.multicastEvent(evt);
}
Also used : StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) PayloadApplicationEvent(org.springframework.context.PayloadApplicationEvent) ApplicationEvent(org.springframework.context.ApplicationEvent) Test(org.junit.jupiter.api.Test)

Example 39 with ApplicationEvent

use of org.springframework.context.ApplicationEvent in project spring-framework by spring-projects.

the class EventPublishingTestExecutionListenerTests method assertEvent.

private void assertEvent(Class<? extends TestContextEvent> eventClass, Consumer<TestContext> callback) {
    callback.accept(testContext);
    // The listener attempted to publish the event...
    verify(testContext, times(1)).publishEvent(eventFactory.capture());
    // The listener successfully published the event...
    verify(applicationContext, times(1)).publishEvent(any());
    // Verify the type of event that was published.
    ApplicationEvent event = eventFactory.getValue().apply(testContext);
    assertThat(event).isInstanceOf(eventClass);
    assertThat(event.getSource()).isEqualTo(testContext);
}
Also used : ApplicationEvent(org.springframework.context.ApplicationEvent)

Example 40 with ApplicationEvent

use of org.springframework.context.ApplicationEvent in project spring-security by spring-projects.

the class DefaultSessionAuthenticationStrategyTests method onlySavedRequestAttributeIsMigratedIfMigrateAttributesIsFalseWithEventPublisher.

// SEC-2002
@Test
public void onlySavedRequestAttributeIsMigratedIfMigrateAttributesIsFalseWithEventPublisher() {
    SessionFixationProtectionStrategy strategy = new SessionFixationProtectionStrategy();
    strategy.setMigrateSessionAttributes(false);
    HttpServletRequest request = new MockHttpServletRequest();
    HttpSession session = request.getSession();
    session.setAttribute("blah", "blah");
    session.setAttribute("SPRING_SECURITY_SAVED_REQUEST_KEY", "DefaultSavedRequest");
    String oldSessionId = session.getId();
    ApplicationEventPublisher eventPublisher = mock(ApplicationEventPublisher.class);
    strategy.setApplicationEventPublisher(eventPublisher);
    Authentication mockAuthentication = mock(Authentication.class);
    strategy.onAuthentication(mockAuthentication, request, new MockHttpServletResponse());
    ArgumentCaptor<ApplicationEvent> eventArgumentCaptor = ArgumentCaptor.forClass(ApplicationEvent.class);
    verify(eventPublisher).publishEvent(eventArgumentCaptor.capture());
    assertThat(request.getSession().getAttribute("blah")).isNull();
    assertThat(request.getSession().getAttribute("SPRING_SECURITY_SAVED_REQUEST_KEY")).isNotNull();
    assertThat(eventArgumentCaptor.getValue()).isNotNull();
    assertThat(eventArgumentCaptor.getValue() instanceof SessionFixationProtectionEvent).isTrue();
    SessionFixationProtectionEvent event = (SessionFixationProtectionEvent) eventArgumentCaptor.getValue();
    assertThat(event.getOldSessionId()).isEqualTo(oldSessionId);
    assertThat(event.getNewSessionId()).isEqualTo(request.getSession().getId());
    assertThat(event.getAuthentication()).isSameAs(mockAuthentication);
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) HttpSession(jakarta.servlet.http.HttpSession) Authentication(org.springframework.security.core.Authentication) SessionFixationProtectionEvent(org.springframework.security.web.authentication.session.SessionFixationProtectionEvent) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) ApplicationEvent(org.springframework.context.ApplicationEvent) SessionFixationProtectionStrategy(org.springframework.security.web.authentication.session.SessionFixationProtectionStrategy) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Aggregations

ApplicationEvent (org.springframework.context.ApplicationEvent)44 ApplicationEventPublisher (org.springframework.context.ApplicationEventPublisher)16 Test (org.junit.Test)14 Test (org.junit.jupiter.api.Test)11 ApplicationListener (org.springframework.context.ApplicationListener)9 AtomicReference (java.util.concurrent.atomic.AtomicReference)8 ArrayList (java.util.ArrayList)7 PayloadApplicationEvent (org.springframework.context.PayloadApplicationEvent)7 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)6 ApplicationContext (org.springframework.context.ApplicationContext)6 StaticApplicationContext (org.springframework.context.support.StaticApplicationContext)6 CountDownLatch (java.util.concurrent.CountDownLatch)5 Matchers.containsString (org.hamcrest.Matchers.containsString)5 Socket (java.net.Socket)4 List (java.util.List)4 AbstractApplicationContext (org.springframework.context.support.AbstractApplicationContext)4 IOException (java.io.IOException)3 InOrder (org.mockito.InOrder)3 ApplicationReadyEvent (org.springframework.boot.context.event.ApplicationReadyEvent)3 ApplicationStartedEvent (org.springframework.boot.context.event.ApplicationStartedEvent)3