Search in sources :

Example 76 with DirectFieldAccessor

use of org.springframework.beans.DirectFieldAccessor in project spring-framework by spring-projects.

the class SubProtocolWebSocketHandlerTests method checkSession.

@Test
@SuppressWarnings("unchecked")
public void checkSession() throws Exception {
    TestWebSocketSession session1 = new TestWebSocketSession("id1");
    TestWebSocketSession session2 = new TestWebSocketSession("id2");
    session1.setOpen(true);
    session2.setOpen(true);
    session1.setAcceptedProtocol("v12.stomp");
    session2.setAcceptedProtocol("v12.stomp");
    this.webSocketHandler.setProtocolHandlers(Arrays.asList(this.stompHandler));
    this.webSocketHandler.afterConnectionEstablished(session1);
    this.webSocketHandler.afterConnectionEstablished(session2);
    DirectFieldAccessor handlerAccessor = new DirectFieldAccessor(this.webSocketHandler);
    Map<String, ?> map = (Map<String, ?>) handlerAccessor.getPropertyValue("sessions");
    DirectFieldAccessor session1Accessor = new DirectFieldAccessor(map.get("id1"));
    DirectFieldAccessor session2Accessor = new DirectFieldAccessor(map.get("id2"));
    long sixtyOneSecondsAgo = System.currentTimeMillis() - 61 * 1000;
    handlerAccessor.setPropertyValue("lastSessionCheckTime", sixtyOneSecondsAgo);
    session1Accessor.setPropertyValue("createTime", sixtyOneSecondsAgo);
    session2Accessor.setPropertyValue("createTime", sixtyOneSecondsAgo);
    this.webSocketHandler.start();
    this.webSocketHandler.handleMessage(session1, new TextMessage("foo"));
    assertTrue(session1.isOpen());
    assertNull(session1.getCloseStatus());
    assertFalse(session2.isOpen());
    assertEquals(CloseStatus.SESSION_NOT_RELIABLE, session2.getCloseStatus());
    assertNotEquals("lastSessionCheckTime not updated", sixtyOneSecondsAgo, handlerAccessor.getPropertyValue("lastSessionCheckTime"));
}
Also used : TestWebSocketSession(org.springframework.web.socket.handler.TestWebSocketSession) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) Map(java.util.Map) TextMessage(org.springframework.web.socket.TextMessage) Test(org.junit.Test)

Example 77 with DirectFieldAccessor

use of org.springframework.beans.DirectFieldAccessor in project spring-framework by spring-projects.

the class TestValidator method annotationMethodMessageHandler.

@Test
public void annotationMethodMessageHandler() {
    loadBeanDefinitions("websocket-config-broker-simple.xml");
    SimpAnnotationMethodMessageHandler annotationMethodMessageHandler = this.appContext.getBean(SimpAnnotationMethodMessageHandler.class);
    assertNotNull(annotationMethodMessageHandler);
    MessageConverter messageConverter = annotationMethodMessageHandler.getMessageConverter();
    assertNotNull(messageConverter);
    assertTrue(messageConverter instanceof CompositeMessageConverter);
    String name = MessageBrokerBeanDefinitionParser.MESSAGE_CONVERTER_BEAN_NAME;
    CompositeMessageConverter compositeMessageConverter = this.appContext.getBean(name, CompositeMessageConverter.class);
    assertNotNull(compositeMessageConverter);
    name = MessageBrokerBeanDefinitionParser.MESSAGING_TEMPLATE_BEAN_NAME;
    SimpMessagingTemplate simpMessagingTemplate = this.appContext.getBean(name, SimpMessagingTemplate.class);
    assertNotNull(simpMessagingTemplate);
    assertEquals("/personal/", simpMessagingTemplate.getUserDestinationPrefix());
    List<MessageConverter> converters = compositeMessageConverter.getConverters();
    assertThat(converters.size(), Matchers.is(3));
    assertThat(converters.get(0), Matchers.instanceOf(StringMessageConverter.class));
    assertThat(converters.get(1), Matchers.instanceOf(ByteArrayMessageConverter.class));
    assertThat(converters.get(2), Matchers.instanceOf(MappingJackson2MessageConverter.class));
    ContentTypeResolver resolver = ((MappingJackson2MessageConverter) converters.get(2)).getContentTypeResolver();
    assertEquals(MimeTypeUtils.APPLICATION_JSON, ((DefaultContentTypeResolver) resolver).getDefaultMimeType());
    DirectFieldAccessor handlerAccessor = new DirectFieldAccessor(annotationMethodMessageHandler);
    String pathSeparator = (String) new DirectFieldAccessor(handlerAccessor.getPropertyValue("pathMatcher")).getPropertyValue("pathSeparator");
    assertEquals(".", pathSeparator);
}
Also used : StringMessageConverter(org.springframework.messaging.converter.StringMessageConverter) SimpMessagingTemplate(org.springframework.messaging.simp.SimpMessagingTemplate) ByteArrayMessageConverter(org.springframework.messaging.converter.ByteArrayMessageConverter) MappingJackson2MessageConverter(org.springframework.messaging.converter.MappingJackson2MessageConverter) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) ContentTypeResolver(org.springframework.messaging.converter.ContentTypeResolver) DefaultContentTypeResolver(org.springframework.messaging.converter.DefaultContentTypeResolver) SimpAnnotationMethodMessageHandler(org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler) ByteArrayMessageConverter(org.springframework.messaging.converter.ByteArrayMessageConverter) CompositeMessageConverter(org.springframework.messaging.converter.CompositeMessageConverter) MessageConverter(org.springframework.messaging.converter.MessageConverter) MappingJackson2MessageConverter(org.springframework.messaging.converter.MappingJackson2MessageConverter) StringMessageConverter(org.springframework.messaging.converter.StringMessageConverter) CompositeMessageConverter(org.springframework.messaging.converter.CompositeMessageConverter) Test(org.junit.Test)

Example 78 with DirectFieldAccessor

use of org.springframework.beans.DirectFieldAccessor in project spring-framework by spring-projects.

the class TestPathHelper method testArgumentResolvers.

private void testArgumentResolvers(Object bean) {
    assertNotNull(bean);
    Object value = new DirectFieldAccessor(bean).getPropertyValue("customArgumentResolvers");
    assertNotNull(value);
    assertTrue(value instanceof List);
    @SuppressWarnings("unchecked") List<HandlerMethodArgumentResolver> resolvers = (List<HandlerMethodArgumentResolver>) value;
    assertEquals(3, resolvers.size());
    assertTrue(resolvers.get(0) instanceof ServletWebArgumentResolverAdapter);
    assertTrue(resolvers.get(1) instanceof TestHandlerMethodArgumentResolver);
    assertTrue(resolvers.get(2) instanceof TestHandlerMethodArgumentResolver);
    assertNotSame(resolvers.get(1), resolvers.get(2));
}
Also used : DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) List(java.util.List) ServletWebArgumentResolverAdapter(org.springframework.web.servlet.mvc.method.annotation.ServletWebArgumentResolverAdapter) HandlerMethodArgumentResolver(org.springframework.web.method.support.HandlerMethodArgumentResolver)

Example 79 with DirectFieldAccessor

use of org.springframework.beans.DirectFieldAccessor in project spring-framework by spring-projects.

the class TestPathHelper method verifyResponseBodyAdvice.

@SuppressWarnings("unchecked")
private void verifyResponseBodyAdvice(Object bean) {
    assertNotNull(bean);
    Object value = new DirectFieldAccessor(bean).getPropertyValue("responseBodyAdvice");
    assertNotNull(value);
    assertTrue(value instanceof List);
    List<ResponseBodyAdvice> converters = (List<ResponseBodyAdvice>) value;
    assertTrue(converters.get(0) instanceof JsonViewResponseBodyAdvice);
}
Also used : JsonViewResponseBodyAdvice(org.springframework.web.servlet.mvc.method.annotation.JsonViewResponseBodyAdvice) ResponseBodyAdvice(org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) JsonViewResponseBodyAdvice(org.springframework.web.servlet.mvc.method.annotation.JsonViewResponseBodyAdvice) List(java.util.List)

Example 80 with DirectFieldAccessor

use of org.springframework.beans.DirectFieldAccessor in project spring-framework by spring-projects.

the class TestPathHelper method verifyRequestResponseBodyAdvice.

@SuppressWarnings("unchecked")
private void verifyRequestResponseBodyAdvice(Object bean) {
    assertNotNull(bean);
    Object value = new DirectFieldAccessor(bean).getPropertyValue("requestResponseBodyAdvice");
    assertNotNull(value);
    assertTrue(value instanceof List);
    List<ResponseBodyAdvice> converters = (List<ResponseBodyAdvice>) value;
    assertTrue(converters.get(0) instanceof JsonViewRequestBodyAdvice);
    assertTrue(converters.get(1) instanceof JsonViewResponseBodyAdvice);
}
Also used : JsonViewResponseBodyAdvice(org.springframework.web.servlet.mvc.method.annotation.JsonViewResponseBodyAdvice) ResponseBodyAdvice(org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice) JsonViewRequestBodyAdvice(org.springframework.web.servlet.mvc.method.annotation.JsonViewRequestBodyAdvice) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) JsonViewResponseBodyAdvice(org.springframework.web.servlet.mvc.method.annotation.JsonViewResponseBodyAdvice) List(java.util.List)

Aggregations

DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)95 Test (org.junit.Test)79 List (java.util.List)25 Method (java.lang.reflect.Method)16 ScheduledMethodRunnable (org.springframework.scheduling.support.ScheduledMethodRunnable)16 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)15 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)15 ScheduledTaskRegistrar (org.springframework.scheduling.config.ScheduledTaskRegistrar)15 IntervalTask (org.springframework.scheduling.config.IntervalTask)8 CronTask (org.springframework.scheduling.config.CronTask)7 DefaultMessageListenerContainer (org.springframework.jms.listener.DefaultMessageListenerContainer)5 CachingConnectionFactory (org.springframework.amqp.rabbit.connection.CachingConnectionFactory)4 DataSourceTransactionManagerAutoConfiguration (org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration)4 EmbeddedDataSourceConfiguration (org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration)4 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)4 JdbcOperations (org.springframework.jdbc.core.JdbcOperations)4 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)4 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)4 JdbcOperationsSessionRepository (org.springframework.session.jdbc.JdbcOperationsSessionRepository)4 ViewResolver (org.springframework.web.servlet.ViewResolver)4