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();
}
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();
}
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);
}
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);
}
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);
}
Aggregations