@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);
  }
  private List<View> getCandidateViews(
      String viewName, Locale locale, List<MediaType> requestedMediaTypes) throws Exception {

    List<View> candidateViews = new ArrayList<View>();
    for (ViewResolver viewResolver : this.viewResolvers) {
      View view = viewResolver.resolveViewName(viewName, locale);
      if (view != null) {
        candidateViews.add(view);
      }
      for (MediaType requestedMediaType : requestedMediaTypes) {
        List<String> extensions = getExtensionsForMediaType(requestedMediaType);
        for (String extension : extensions) {
          String viewNameWithExtension = viewName + "." + extension;
          view = viewResolver.resolveViewName(viewNameWithExtension, locale);
          if (view != null) {
            candidateViews.add(view);
          }
        }
      }
    }
    if (!CollectionUtils.isEmpty(this.defaultViews)) {
      candidateViews.addAll(this.defaultViews);
    }
    return candidateViews;
  }
  @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 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);
  }
  @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 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);
  }
  @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);
  }
Example #9
0
 /*
  * 自定义resolve类默认为jsp视图,其余视图用下划线分隔
  */
 @Override
 public View resolveViewName(String viewName, Locale locale) throws Exception {
   // 判断是否为json
   String tmpViewName = viewName.trim().toLowerCase();
   if (JSON_RESOLVER.equals(tmpViewName)) {
     ViewResolver resolver = resolvers.get(JSON_RESOLVER);
     return resolver.resolveViewName(JSON_VIEW_NAME, locale);
   }
   // 若无下划线,默认转向jsp视图
   int n = viewName.lastIndexOf("_");
   if (n == (-1)) {
     ViewResolver resolver = resolvers.get(DEFAULT_RESOLVER);
     return resolver.resolveViewName(viewName, locale);
   }
   // 有的话截取下划线后面的字符串 这里一般是jsp,ftl,vm与配置文件中的<entry key="ftl">的key匹配
   String suffix = viewName.substring(n + 1);
   // 根据下划线后面的字符串去获取托管的视图解析类对象
   ViewResolver resolver = resolvers.get(suffix);
   // 取下划线前面的部分 那时真正的资源名.比如我们要使用hello.jsp 那viewName就应该是hello_jsp
   viewName = viewName.substring(0, n);
   if (resolver != null) {
     return resolver.resolveViewName(viewName, locale);
   }
   return null;
 }
  @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 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);
  }
Example #12
0
  private Map<?, ?> renderView() throws Exception {
    final ModelAndView modelAndView = this.getModelAndViewUponServiceValidationWithSecurePgtUrl();
    final JstlView v =
        (JstlView) resolver.resolveViewName(modelAndView.getViewName(), Locale.getDefault());
    final MockHttpServletRequest req = new MockHttpServletRequest(new MockServletContext());
    v.setServletContext(req.getServletContext());
    req.setAttribute(
        RequestContext.WEB_APPLICATION_CONTEXT_ATTRIBUTE,
        new GenericWebApplicationContext(req.getServletContext()));

    final Cas30ResponseView view = new Cas30ResponseView(v);
    view.setServicesManager(this.servicesManager);
    view.setCasAttributeEncoder(new DefaultCasAttributeEncoder(this.servicesManager));

    final MockHttpServletResponse resp = new MockHttpServletResponse();
    view.render(modelAndView.getModel(), req, resp);
    return (Map<?, ?>)
        req.getAttribute(CasProtocolConstants.VALIDATION_CAS_MODEL_ATTRIBUTE_NAME_ATTRIBUTES);
  }
  @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);
  }