@Test
  public void resolveViewNameAcceptHeaderDefaultView() throws Exception {
    request.addHeader("Accept", "application/json");

    ViewResolver viewResolverMock1 = createMock(ViewResolver.class);
    ViewResolver viewResolverMock2 = createMock(ViewResolver.class);
    viewResolver.setViewResolvers(Arrays.asList(viewResolverMock1, viewResolverMock2));

    View viewMock1 = createMock("application_xml", View.class);
    View viewMock2 = createMock("text_html", View.class);
    View viewMock3 = createMock("application_json", View.class);

    List<View> defaultViews = new ArrayList<View>();
    defaultViews.add(viewMock3);
    viewResolver.setDefaultViews(defaultViews);

    String viewName = "view";
    Locale locale = Locale.ENGLISH;

    expect(viewResolverMock1.resolveViewName(viewName, locale)).andReturn(viewMock1);
    expect(viewResolverMock2.resolveViewName(viewName, locale)).andReturn(viewMock2);
    expect(viewMock1.getContentType()).andReturn("application/xml").anyTimes();
    expect(viewMock2.getContentType()).andReturn("text/html;charset=ISO-8859-1").anyTimes();
    expect(viewMock3.getContentType()).andReturn("application/json").anyTimes();

    replay(viewResolverMock1, viewResolverMock2, viewMock1, viewMock2, viewMock3);

    View result = viewResolver.resolveViewName(viewName, locale);
    assertSame("Invalid view", viewMock3, result);

    verify(viewResolverMock1, viewResolverMock2, viewMock1, viewMock2, viewMock3);
  }
  @Test
  public void resolveViewNoMatchUseUnacceptableStatus() throws Exception {
    viewResolver.setUseNotAcceptableStatusCode(true);
    request.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9");

    ViewResolver viewResolverMock = createMock(ViewResolver.class);
    viewResolver.setViewResolvers(Collections.singletonList(viewResolverMock));

    View viewMock = createMock("application_xml", View.class);

    String viewName = "view";
    Locale locale = Locale.ENGLISH;

    expect(viewResolverMock.resolveViewName(viewName, locale)).andReturn(viewMock);
    expect(viewMock.getContentType()).andReturn("application/pdf").anyTimes();

    replay(viewResolverMock, viewMock);

    View result = viewResolver.resolveViewName(viewName, locale);
    assertNotNull("Invalid view", result);
    MockHttpServletResponse response = new MockHttpServletResponse();
    result.render(null, request, response);
    assertEquals("Invalid status code set", 406, response.getStatus());

    verify(viewResolverMock, viewMock);
  }
  @Test
  public void resolveViewNameRedirectView() throws Exception {
    request.addHeader("Accept", "application/json");
    request.setRequestURI("/test");

    StaticWebApplicationContext webAppContext = new StaticWebApplicationContext();
    webAppContext.setServletContext(new MockServletContext());
    webAppContext.refresh();

    UrlBasedViewResolver urlViewResolver = new InternalResourceViewResolver();
    urlViewResolver.setApplicationContext(webAppContext);
    ViewResolver xmlViewResolver = createMock(ViewResolver.class);
    viewResolver.setViewResolvers(Arrays.<ViewResolver>asList(xmlViewResolver, urlViewResolver));

    View xmlView = createMock("application_xml", View.class);
    View jsonView = createMock("application_json", View.class);
    viewResolver.setDefaultViews(Arrays.asList(jsonView));

    String viewName = "redirect:anotherTest";
    Locale locale = Locale.ENGLISH;

    expect(xmlViewResolver.resolveViewName(viewName, locale)).andReturn(xmlView);
    expect(jsonView.getContentType()).andReturn("application/json").anyTimes();

    replay(xmlViewResolver, xmlView, jsonView);

    View actualView = viewResolver.resolveViewName(viewName, locale);
    assertEquals("Invalid view", RedirectView.class, actualView.getClass());

    verify(xmlViewResolver, xmlView, jsonView);
  }
  @Test
  public void resolveViewNameFilename() throws Exception {
    request.setRequestURI("/test.html");

    ViewResolver viewResolverMock1 = createMock("viewResolver1", ViewResolver.class);
    ViewResolver viewResolverMock2 = createMock("viewResolver2", ViewResolver.class);
    viewResolver.setViewResolvers(Arrays.asList(viewResolverMock1, viewResolverMock2));

    View viewMock1 = createMock("application_xml", View.class);
    View viewMock2 = createMock("text_html", View.class);

    String viewName = "view";
    Locale locale = Locale.ENGLISH;

    expect(viewResolverMock1.resolveViewName(viewName, locale)).andReturn(viewMock1);
    expect(viewResolverMock1.resolveViewName(viewName + ".html", locale)).andReturn(null);
    expect(viewResolverMock2.resolveViewName(viewName, locale)).andReturn(null);
    expect(viewResolverMock2.resolveViewName(viewName + ".html", locale)).andReturn(viewMock2);
    expect(viewMock1.getContentType()).andReturn("application/xml").anyTimes();
    expect(viewMock2.getContentType()).andReturn("text/html;charset=ISO-8859-1").anyTimes();

    replay(viewResolverMock1, viewResolverMock2, viewMock1, viewMock2);

    View result = viewResolver.resolveViewName(viewName, locale);
    assertSame("Invalid view", viewMock2, result);

    verify(viewResolverMock1, viewResolverMock2, viewMock1, viewMock2);
  }
  @Test
  public void resolveViewNameWithDefaultContentType() throws Exception {
    request.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");

    viewResolver.setIgnoreAcceptHeader(true);
    viewResolver.setDefaultContentType(new MediaType("application", "xml"));

    ViewResolver viewResolverMock1 = createMock("viewResolver1", ViewResolver.class);
    ViewResolver viewResolverMock2 = createMock("viewResolver2", ViewResolver.class);
    viewResolver.setViewResolvers(Arrays.asList(viewResolverMock1, viewResolverMock2));

    View viewMock1 = createMock("application_xml", View.class);
    View viewMock2 = createMock("text_html", View.class);

    String viewName = "view";
    Locale locale = Locale.ENGLISH;

    expect(viewResolverMock1.resolveViewName(viewName, locale)).andReturn(viewMock1);
    expect(viewResolverMock2.resolveViewName(viewName, locale)).andReturn(viewMock2);
    expect(viewMock1.getContentType()).andReturn("application/xml").anyTimes();
    expect(viewMock2.getContentType()).andReturn("text/html;charset=ISO-8859-1").anyTimes();

    replay(viewResolverMock1, viewResolverMock2, viewMock1, viewMock2);

    View result = viewResolver.resolveViewName(viewName, locale);
    assertSame("Invalid view", viewMock1, result);

    verify(viewResolverMock1, viewResolverMock2, viewMock1, viewMock2);
  }
  @Test
  public void resolveViewNameAcceptHeaderSortByQuality() throws Exception {
    request.addHeader("Accept", "text/plain;q=0.5, application/json");

    ViewResolver htmlViewResolver = createMock(ViewResolver.class);
    ViewResolver jsonViewResolver = createMock(ViewResolver.class);
    viewResolver.setViewResolvers(Arrays.asList(htmlViewResolver, jsonViewResolver));

    View htmlView = createMock("text_html", View.class);
    View jsonViewMock = createMock("application_json", View.class);

    String viewName = "view";
    Locale locale = Locale.ENGLISH;

    expect(htmlViewResolver.resolveViewName(viewName, locale)).andReturn(htmlView);
    expect(jsonViewResolver.resolveViewName(viewName, locale)).andReturn(jsonViewMock);
    expect(htmlView.getContentType()).andReturn("text/html").anyTimes();
    expect(jsonViewMock.getContentType()).andReturn("application/json").anyTimes();
    replay(htmlViewResolver, jsonViewResolver, htmlView, jsonViewMock);

    viewResolver.setFavorPathExtension(false);
    View result = viewResolver.resolveViewName(viewName, locale);
    assertSame("Invalid view", jsonViewMock, result);

    verify(htmlViewResolver, jsonViewResolver, htmlView, jsonViewMock);
  }
예제 #7
0
 @Bean
 public ContentNegotiatingViewResolver contentNegotiatingViewResolver() {
   ContentNegotiatingViewResolver resolver = new ContentNegotiatingViewResolver();
   resolver.setViewResolvers(Arrays.asList(urlBasedViewResolver()));
   resolver.setDefaultViews(Arrays.asList(mappingJacksonJsonView()));
   return resolver;
 }
  @Test
  public void resolveViewNameWithRequestParameter() throws Exception {
    request.addParameter("format", "xls");

    viewResolver.setFavorParameter(true);
    viewResolver.setMediaTypes(Collections.singletonMap("xls", "application/vnd.ms-excel"));

    ViewResolver viewResolverMock = createMock(ViewResolver.class);
    viewResolver.setViewResolvers(Collections.singletonList(viewResolverMock));

    View viewMock = createMock("application_xls", View.class);

    String viewName = "view";
    Locale locale = Locale.ENGLISH;

    expect(viewResolverMock.resolveViewName(viewName, locale)).andReturn(null);
    expect(viewResolverMock.resolveViewName(viewName + ".xls", locale)).andReturn(viewMock);
    expect(viewMock.getContentType()).andReturn("application/vnd.ms-excel").anyTimes();

    replay(viewResolverMock, viewMock);

    View result = viewResolver.resolveViewName(viewName, locale);
    assertSame("Invalid view", viewMock, result);

    verify(viewResolverMock, viewMock);
  }
예제 #9
0
  @Bean
  public ViewResolver contentNegotiatingViewResolver(ContentNegotiationManager manager) {
    List<ViewResolver> resolvers = new ArrayList<ViewResolver>();
    resolvers.add(getJspViewResolver());
    resolvers.add(getJsonViewResolver());

    ContentNegotiatingViewResolver resolver = new ContentNegotiatingViewResolver();
    resolver.setContentNegotiationManager(manager);
    resolver.setViewResolvers(resolvers);
    return resolver;
  }
예제 #10
0
  /*
   * @Bean public ThymeleafViewResolver thymeleafViewResolver() {
   * ThymeleafViewResolver thymeleafViewResolver = new
   * ThymeleafViewResolver(); thymeleafViewResolver .setViewNames(new String[]
   * { "WEEB-IBF/views/thymeleaf/*" }); thymeleafViewResolver.setOrder(2);
   * thymeleafViewResolver.setTemplateEngine(springTemplateEngine()); return
   * thymeleafViewResolver; }
   */
  @Bean
  public ContentNegotiatingViewResolver contentNegotiatingViewResolver(
      final ContentNegotiationManager contentNegotiationManager) {
    ContentNegotiatingViewResolver contentNegotiatingViewResolver =
        new ContentNegotiatingViewResolver();
    contentNegotiatingViewResolver.setContentNegotiationManager(contentNegotiationManager);

    List<ViewResolver> viewResolvers = new ArrayList<ViewResolver>();
    viewResolvers.add(beanNameViewResolver());
    viewResolvers.add(internalResourceViewResolver());
    contentNegotiatingViewResolver.setViewResolvers(viewResolvers);
    return contentNegotiatingViewResolver;
  }
  @Test
  public void nestedViewResolverIsNotSpringBean() throws Exception {
    StaticWebApplicationContext webAppContext = new StaticWebApplicationContext();
    webAppContext.setServletContext(new MockServletContext());
    webAppContext.refresh();

    InternalResourceViewResolver nestedResolver = new InternalResourceViewResolver();
    nestedResolver.setApplicationContext(webAppContext);
    nestedResolver.setViewClass(InternalResourceView.class);
    viewResolver.setViewResolvers(new ArrayList<ViewResolver>(Arrays.asList(nestedResolver)));
    viewResolver.setDefaultContentType(MediaType.TEXT_HTML);

    String viewName = "view";
    Locale locale = Locale.ENGLISH;

    View result = viewResolver.resolveViewName(viewName, locale);
    assertNotNull("Invalid view", result);
  }
예제 #12
0
  @Bean
  public ContentNegotiatingViewResolver contentViewResolver() throws Exception {
    ContentNegotiationManagerFactoryBean contentNegotiationManager =
        new ContentNegotiationManagerFactoryBean();
    contentNegotiationManager.addMediaType("json", MediaType.APPLICATION_JSON);

    InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
    viewResolver.setPrefix("/WEB-INF/views/");
    viewResolver.setSuffix(".jsp");

    MappingJackson2JsonView defaultView = new MappingJackson2JsonView();
    defaultView.setExtractValueFromSingleKeyModel(true);

    ContentNegotiatingViewResolver contentViewResolver = new ContentNegotiatingViewResolver();
    contentViewResolver.setContentNegotiationManager(contentNegotiationManager.getObject());
    contentViewResolver.setViewResolvers(Arrays.<ViewResolver>asList(viewResolver));
    contentViewResolver.setDefaultViews(Arrays.<View>asList(defaultView));
    return contentViewResolver;
  }
  @Bean
  ContentNegotiatingViewResolver contentNegotiatingViewResolver() {
    if (null == viewResolver) {
      viewResolver = new ContentNegotiatingViewResolver();

      Map<String, String> mediaTypes =
          new HashMap<String, String>() {
            {
              put("json", "application/json");
              put("urilist", "text/uri-list");
            }
          };
      viewResolver.setMediaTypes(mediaTypes);

      RepositoryRestViewResolver jsonvr = new RepositoryRestViewResolver(jsonView());
      RepositoryRestViewResolver urilistvr = new RepositoryRestViewResolver(urilistView());
      viewResolver.setViewResolvers(Arrays.<ViewResolver>asList(jsonvr, urilistvr));
    }
    return viewResolver;
  }
  @Test
  public void resolveViewNoMatch() throws Exception {
    request.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9");

    ViewResolver viewResolverMock = createMock(ViewResolver.class);
    viewResolver.setViewResolvers(Collections.singletonList(viewResolverMock));

    View viewMock = createMock("application_xml", View.class);

    String viewName = "view";
    Locale locale = Locale.ENGLISH;

    expect(viewResolverMock.resolveViewName(viewName, locale)).andReturn(viewMock);
    expect(viewMock.getContentType()).andReturn("application/pdf").anyTimes();

    replay(viewResolverMock, viewMock);

    View result = viewResolver.resolveViewName(viewName, locale);
    assertNull("Invalid view", result);

    verify(viewResolverMock, viewMock);
  }
  @Test
  public void resolveViewNameFilenameDefaultView() throws Exception {
    request.setRequestURI("/test.json");

    Map<String, String> mediaTypes = new HashMap<String, String>();
    mediaTypes.put("json", "application/json");
    viewResolver.setMediaTypes(mediaTypes);

    ViewResolver viewResolverMock1 = createMock(ViewResolver.class);
    ViewResolver viewResolverMock2 = createMock(ViewResolver.class);
    viewResolver.setViewResolvers(Arrays.asList(viewResolverMock1, viewResolverMock2));

    View viewMock1 = createMock("application_xml", View.class);
    View viewMock2 = createMock("text_html", View.class);
    View viewMock3 = createMock("application_json", View.class);

    List<View> defaultViews = new ArrayList<View>();
    defaultViews.add(viewMock3);
    viewResolver.setDefaultViews(defaultViews);

    String viewName = "view";
    Locale locale = Locale.ENGLISH;

    expect(viewResolverMock1.resolveViewName(viewName, locale)).andReturn(viewMock1);
    expect(viewResolverMock1.resolveViewName(viewName + ".json", locale)).andReturn(null);
    expect(viewResolverMock2.resolveViewName(viewName, locale)).andReturn(viewMock2);
    expect(viewResolverMock2.resolveViewName(viewName + ".json", locale)).andReturn(null);
    expect(viewMock1.getContentType()).andReturn("application/xml").anyTimes();
    expect(viewMock2.getContentType()).andReturn("text/html;charset=ISO-8859-1").anyTimes();
    expect(viewMock3.getContentType()).andReturn("application/json").anyTimes();

    replay(viewResolverMock1, viewResolverMock2, viewMock1, viewMock2, viewMock3);

    View result = viewResolver.resolveViewName(viewName, locale);
    assertSame("Invalid view", viewMock3, result);

    verify(viewResolverMock1, viewResolverMock2, viewMock1, viewMock2, viewMock3);
  }
  @Test
  public void resolveViewNameWithPathExtension() throws Exception {
    request.setRequestURI("/test.xls");

    ViewResolver viewResolverMock = createMock(ViewResolver.class);
    viewResolver.setViewResolvers(Collections.singletonList(viewResolverMock));

    View viewMock = createMock("application_xls", View.class);

    String viewName = "view";
    Locale locale = Locale.ENGLISH;

    expect(viewResolverMock.resolveViewName(viewName, locale)).andReturn(null);
    expect(viewResolverMock.resolveViewName(viewName + ".xls", locale)).andReturn(viewMock);
    expect(viewMock.getContentType()).andReturn("application/vnd.ms-excel").anyTimes();

    replay(viewResolverMock, viewMock);

    View result = viewResolver.resolveViewName(viewName, locale);
    assertSame("Invalid view", viewMock, result);

    verify(viewResolverMock, viewMock);
  }