@Test
 public void testMessageConverters() {
   loadBeanDefinitions("mvc-config-message-converters.xml");
   verifyMessageConverters(appContext.getBean(RequestMappingHandlerAdapter.class), true);
   verifyMessageConverters(appContext.getBean(ExceptionHandlerExceptionResolver.class), true);
   verifyResponseBodyAdvice(appContext.getBean(RequestMappingHandlerAdapter.class));
   verifyResponseBodyAdvice(appContext.getBean(ExceptionHandlerExceptionResolver.class));
 }
 @Test
 public void beanNameUrlHandlerMapping() {
   loadBeanDefinitions("mvc-config.xml");
   BeanNameUrlHandlerMapping mapping = appContext.getBean(BeanNameUrlHandlerMapping.class);
   assertNotNull(mapping);
   assertEquals(2, mapping.getOrder());
 }
  @Test
  public void sockJsAttributesSupport() {
    loadBeanDefinitions("websocket-config-handlers-sockjs-attributes.xml");
    SimpleUrlHandlerMapping handlerMapping = appContext.getBean(SimpleUrlHandlerMapping.class);
    assertNotNull(handlerMapping);
    SockJsHttpRequestHandler handler =
        (SockJsHttpRequestHandler) handlerMapping.getUrlMap().get("/test/**");
    assertNotNull(handler);
    checkDelegateHandlerType(handler.getWebSocketHandler(), TestWebSocketHandler.class);
    SockJsService sockJsService = handler.getSockJsService();
    assertNotNull(sockJsService);
    assertThat(sockJsService, Matchers.instanceOf(TransportHandlingSockJsService.class));
    TransportHandlingSockJsService defaultSockJsService =
        (TransportHandlingSockJsService) sockJsService;
    assertThat(
        defaultSockJsService.getTaskScheduler(), Matchers.instanceOf(TestTaskScheduler.class));
    assertThat(
        defaultSockJsService.getTransportHandlers().values(),
        Matchers.containsInAnyOrder(
            Matchers.instanceOf(XhrPollingTransportHandler.class),
            Matchers.instanceOf(XhrStreamingTransportHandler.class)));

    assertEquals("testSockJsService", defaultSockJsService.getName());
    assertFalse(defaultSockJsService.isWebSocketEnabled());
    assertFalse(defaultSockJsService.isSessionCookieNeeded());
    assertEquals(2048, defaultSockJsService.getStreamBytesLimit());
    assertEquals(256, defaultSockJsService.getDisconnectDelay());
    assertEquals(1024, defaultSockJsService.getHttpMessageCacheSize());
    assertEquals(20, defaultSockJsService.getHeartbeatTime());
  }
 @SuppressWarnings("unchecked")
 @Test
 public void testReturnValueHandlers() {
   loadBeanDefinitions("mvc-config-return-value-handlers.xml");
   RequestMappingHandlerAdapter adapter = appContext.getBean(RequestMappingHandlerAdapter.class);
   assertNotNull(adapter);
   Object value = new DirectFieldAccessor(adapter).getPropertyValue("customReturnValueHandlers");
   assertNotNull(value);
   assertTrue(value instanceof List);
   List<HandlerMethodReturnValueHandler> handlers = (List<HandlerMethodReturnValueHandler>) value;
   assertEquals(1, handlers.size());
   assertEquals(TestHandlerMethodReturnValueHandler.class, handlers.get(0).getClass());
 }
 @Test
 public void testPathMatchingConfiguration() {
   loadBeanDefinitions("mvc-config-path-matching.xml");
   RequestMappingHandlerMapping hm = appContext.getBean(RequestMappingHandlerMapping.class);
   assertNotNull(hm);
   assertTrue(hm.useSuffixPatternMatch());
   assertFalse(hm.useTrailingSlashMatch());
   assertTrue(hm.useRegisteredSuffixPatternMatch());
   assertThat(hm.getUrlPathHelper(), Matchers.instanceOf(TestPathHelper.class));
   assertThat(hm.getPathMatcher(), Matchers.instanceOf(TestPathMatcher.class));
   List<String> fileExtensions = hm.getContentNegotiationManager().getAllFileExtensions();
   assertThat(fileExtensions, Matchers.contains("xml"));
   assertThat(fileExtensions, Matchers.hasSize(1));
 }
 @Test
 public void testMessageCodesResolver() {
   loadBeanDefinitions("mvc-config-message-codes-resolver.xml");
   RequestMappingHandlerAdapter adapter = appContext.getBean(RequestMappingHandlerAdapter.class);
   assertNotNull(adapter);
   Object initializer = adapter.getWebBindingInitializer();
   assertNotNull(initializer);
   MessageCodesResolver resolver =
       ((ConfigurableWebBindingInitializer) initializer).getMessageCodesResolver();
   assertNotNull(resolver);
   assertEquals(TestMessageCodesResolver.class, resolver.getClass());
   assertEquals(
       false, new DirectFieldAccessor(adapter).getPropertyValue("ignoreDefaultModelOnRedirect"));
 }
 @SuppressWarnings("unchecked")
 @Test
 public void testArgumentResolvers() {
   loadBeanDefinitions("mvc-config-argument-resolvers.xml");
   RequestMappingHandlerAdapter adapter = appContext.getBean(RequestMappingHandlerAdapter.class);
   assertNotNull(adapter);
   Object value = new DirectFieldAccessor(adapter).getPropertyValue("customArgumentResolvers");
   assertNotNull(value);
   assertTrue(value instanceof List);
   List<HandlerMethodArgumentResolver> resolvers = (List<HandlerMethodArgumentResolver>) value;
   assertEquals(2, resolvers.size());
   assertTrue(resolvers.get(0) instanceof ServletWebArgumentResolverAdapter);
   assertTrue(resolvers.get(1) instanceof TestHandlerMethodArgumentResolver);
 }
  private Object getProxyBean(Object handler) {
    GenericWebApplicationContext wac = new GenericWebApplicationContext();
    wac.registerBeanDefinition("controller", new RootBeanDefinition(handler.getClass()));

    DefaultAdvisorAutoProxyCreator autoProxyCreator = new DefaultAdvisorAutoProxyCreator();
    autoProxyCreator.setBeanFactory(wac.getBeanFactory());
    wac.getBeanFactory().addBeanPostProcessor(autoProxyCreator);
    wac.getBeanFactory()
        .registerSingleton("advsr", new DefaultPointcutAdvisor(new SimpleTraceInterceptor()));

    wac.refresh();

    return wac.getBean("controller");
  }
  @Test
  @SuppressWarnings("unchecked")
  public void websocketHandlersAttributes() {
    loadBeanDefinitions("websocket-config-handlers-attributes.xml");
    HandlerMapping handlerMapping = appContext.getBean(HandlerMapping.class);
    assertNotNull(handlerMapping);
    assertTrue(handlerMapping instanceof SimpleUrlHandlerMapping);

    SimpleUrlHandlerMapping urlHandlerMapping = (SimpleUrlHandlerMapping) handlerMapping;
    assertEquals(2, urlHandlerMapping.getOrder());

    WebSocketHttpRequestHandler handler =
        (WebSocketHttpRequestHandler) urlHandlerMapping.getUrlMap().get("/foo");
    assertNotNull(handler);
    checkDelegateHandlerType(handler.getWebSocketHandler(), FooWebSocketHandler.class);
    HandshakeHandler handshakeHandler =
        (HandshakeHandler) new DirectFieldAccessor(handler).getPropertyValue("handshakeHandler");
    assertNotNull(handshakeHandler);
    assertTrue(handshakeHandler instanceof TestHandshakeHandler);
    List<HandshakeInterceptor> handshakeInterceptorList =
        (List<HandshakeInterceptor>)
            new DirectFieldAccessor(handler).getPropertyValue("interceptors");
    assertNotNull(handshakeInterceptorList);
    assertThat(
        handshakeInterceptorList,
        Matchers.contains(
            Matchers.instanceOf(FooTestInterceptor.class),
            Matchers.instanceOf(BarTestInterceptor.class)));

    handler = (WebSocketHttpRequestHandler) urlHandlerMapping.getUrlMap().get("/test");
    assertNotNull(handler);
    checkDelegateHandlerType(handler.getWebSocketHandler(), TestWebSocketHandler.class);
    handshakeHandler =
        (HandshakeHandler) new DirectFieldAccessor(handler).getPropertyValue("handshakeHandler");
    assertNotNull(handshakeHandler);
    assertTrue(handshakeHandler instanceof TestHandshakeHandler);
    handshakeInterceptorList =
        (List<HandshakeInterceptor>)
            new DirectFieldAccessor(handler).getPropertyValue("interceptors");
    assertNotNull(handshakeInterceptorList);
    assertThat(
        handshakeInterceptorList,
        Matchers.contains(
            Matchers.instanceOf(FooTestInterceptor.class),
            Matchers.instanceOf(BarTestInterceptor.class)));
  }
  @Test
  public void sockJsSupport() {
    loadBeanDefinitions("websocket-config-handlers-sockjs.xml");
    SimpleUrlHandlerMapping handlerMapping = appContext.getBean(SimpleUrlHandlerMapping.class);
    assertNotNull(handlerMapping);
    SockJsHttpRequestHandler testHandler =
        (SockJsHttpRequestHandler) handlerMapping.getUrlMap().get("/test/**");
    assertNotNull(testHandler);
    checkDelegateHandlerType(testHandler.getWebSocketHandler(), TestWebSocketHandler.class);
    SockJsService testSockJsService = testHandler.getSockJsService();
    SockJsHttpRequestHandler fooHandler =
        (SockJsHttpRequestHandler) handlerMapping.getUrlMap().get("/foo/**");
    assertNotNull(fooHandler);
    checkDelegateHandlerType(fooHandler.getWebSocketHandler(), FooWebSocketHandler.class);

    SockJsService sockJsService = fooHandler.getSockJsService();
    assertNotNull(sockJsService);
    assertEquals(testSockJsService, sockJsService);

    assertThat(sockJsService, Matchers.instanceOf(DefaultSockJsService.class));
    DefaultSockJsService defaultSockJsService = (DefaultSockJsService) sockJsService;
    assertThat(
        defaultSockJsService.getTaskScheduler(),
        Matchers.instanceOf(ThreadPoolTaskScheduler.class));
    assertThat(
        defaultSockJsService.getTransportHandlers().values(),
        Matchers.containsInAnyOrder(
            Matchers.instanceOf(XhrPollingTransportHandler.class),
            Matchers.instanceOf(XhrReceivingTransportHandler.class),
            Matchers.instanceOf(JsonpPollingTransportHandler.class),
            Matchers.instanceOf(JsonpReceivingTransportHandler.class),
            Matchers.instanceOf(XhrStreamingTransportHandler.class),
            Matchers.instanceOf(EventSourceTransportHandler.class),
            Matchers.instanceOf(HtmlFileTransportHandler.class),
            Matchers.instanceOf(WebSocketTransportHandler.class)));
  }
 @Test
 public void testMessageConvertersWithoutDefaultRegistrations() {
   loadBeanDefinitions("mvc-config-message-converters-defaults-off.xml");
   verifyMessageConverters(appContext.getBean(RequestMappingHandlerAdapter.class), false);
   verifyMessageConverters(appContext.getBean(ExceptionHandlerExceptionResolver.class), false);
 }