java类org.springframework.web.context.support.StaticWebApplicationContext的实例源码

ViewResolverTests.java 文件源码 项目:class-guard 阅读 22 收藏 0 点赞 0 评论 0
private void doTestUrlBasedViewResolverWithPrefixes(UrlBasedViewResolver vr) throws Exception {
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.setServletContext(new MockServletContext());
    wac.refresh();
    vr.setPrefix("/WEB-INF/");
    vr.setSuffix(".jsp");
    vr.setApplicationContext(wac);

    View view = vr.resolveViewName("example1", Locale.getDefault());
    assertEquals("Correct view class", JstlView.class, view.getClass());
    assertEquals("Correct URL", "/WEB-INF/example1.jsp", ((InternalResourceView) view).getUrl());

    view = vr.resolveViewName("example2", Locale.getDefault());
    assertEquals("Correct view class", JstlView.class, view.getClass());
    assertEquals("Correct URL", "/WEB-INF/example2.jsp", ((InternalResourceView) view).getUrl());

    view = vr.resolveViewName("redirect:myUrl", Locale.getDefault());
    assertEquals("Correct view class", RedirectView.class, view.getClass());
    assertEquals("Correct URL", "myUrl", ((RedirectView) view).getUrl());

    view = vr.resolveViewName("forward:myUrl", Locale.getDefault());
    assertEquals("Correct view class", InternalResourceView.class, view.getClass());
    assertEquals("Correct URL", "myUrl", ((InternalResourceView) view).getUrl());
}
ViewResolverTests.java 文件源码 项目:class-guard 阅读 21 收藏 0 点赞 0 评论 0
@Test
public void testXmlViewResolverDefaultLocation() {
    StaticWebApplicationContext wac = new StaticWebApplicationContext() {
        @Override
        protected Resource getResourceByPath(String path) {
            assertTrue("Correct default location", XmlViewResolver.DEFAULT_LOCATION.equals(path));
            return super.getResourceByPath(path);
        }
    };
    wac.setServletContext(new MockServletContext());
    wac.refresh();
    XmlViewResolver vr = new XmlViewResolver();
    try {
        vr.setApplicationContext(wac);
        vr.afterPropertiesSet();
        fail("Should have thrown BeanDefinitionStoreException");
    }
    catch (BeanDefinitionStoreException ex) {
        // expected
    }
}
ViewResolverTests.java 文件源码 项目:class-guard 阅读 28 收藏 0 点赞 0 评论 0
@Test
public void testCacheRemoval() throws Exception {
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.setServletContext(new MockServletContext());
    wac.refresh();
    InternalResourceViewResolver vr = new InternalResourceViewResolver();
    vr.setViewClass(JstlView.class);
    vr.setApplicationContext(wac);

    View view = vr.resolveViewName("example1", Locale.getDefault());
    View cached = vr.resolveViewName("example1", Locale.getDefault());
    if (view != cached) {
        fail("Caching doesn't work");
    }

    vr.removeFromCache("example1", Locale.getDefault());
    cached = vr.resolveViewName("example1", Locale.getDefault());
    if (view == cached) {
        // the chance of having the same reference (hashCode) twice if negligible).
        fail("View wasn't removed from cache");
    }
}
ContentNegotiatingViewResolverTests.java 文件源码 项目:class-guard 阅读 21 收藏 0 点赞 0 评论 0
@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)));

    FixedContentNegotiationStrategy fixedStrategy = new FixedContentNegotiationStrategy(MediaType.TEXT_HTML);
    viewResolver.setContentNegotiationManager(new ContentNegotiationManager(fixedStrategy));

    viewResolver.afterPropertiesSet();

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

    View result = viewResolver.resolveViewName(viewName, locale);
    assertNotNull("Invalid view", result);
}
FreeMarkerMacroTests.java 文件源码 项目:class-guard 阅读 26 收藏 0 点赞 0 评论 0
@Before
public void setUp() throws Exception {
    wac = new StaticWebApplicationContext();
    wac.setServletContext(new MockServletContext());

    // final Template expectedTemplate = new Template();
    fc = new FreeMarkerConfigurer();
    fc.setTemplateLoaderPaths(new String[] { "classpath:/", "file://" + System.getProperty("java.io.tmpdir") });
    fc.afterPropertiesSet();

    wac.getDefaultListableBeanFactory().registerSingleton("freeMarkerConfigurer", fc);
    wac.refresh();

    request = new MockHttpServletRequest();
    request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
    request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, new FixedThemeResolver());
    response = new MockHttpServletResponse();
}
VelocityRenderTests.java 文件源码 项目:class-guard 阅读 19 收藏 0 点赞 0 评论 0
@Before
public void setUp() throws Exception {
    wac = new StaticWebApplicationContext();
    wac.setServletContext(new MockServletContext());

    final Template expectedTemplate = new Template();
    VelocityConfig vc = new VelocityConfig() {
        @Override
        public VelocityEngine getVelocityEngine() {
            return new TestVelocityEngine("test.vm", expectedTemplate);
        }
    };
    wac.getDefaultListableBeanFactory().registerSingleton("velocityConfigurer", vc);
    wac.refresh();

    request = new MockHttpServletRequest();
    request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
    request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, new FixedThemeResolver());
    response = new MockHttpServletResponse();
}
VelocityViewResolverTests.java 文件源码 项目:class-guard 阅读 17 收藏 0 点赞 0 评论 0
@Test
public void testVelocityViewResolverWithToolbox() throws Exception {
    VelocityConfig vc = new VelocityConfig() {
        @Override
        public VelocityEngine getVelocityEngine() {
            return new TestVelocityEngine("prefix_test_suffix", new Template());
        }
    };

    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.getBeanFactory().registerSingleton("configurer", vc);
    wac.refresh();

    String toolbox = "org/springframework/web/servlet/view/velocity/toolbox.xml";

    VelocityViewResolver vr = new VelocityViewResolver();
    vr.setPrefix("prefix_");
    vr.setSuffix("_suffix");
    vr.setToolboxConfigLocation(toolbox);
    vr.setApplicationContext(wac);

    View view = vr.resolveViewName("test", Locale.CANADA);
    assertEquals("Correct view class", VelocityToolboxView.class, view.getClass());
    assertEquals("Correct URL", "prefix_test_suffix", ((VelocityView) view).getUrl());
    assertEquals("Correct toolbox", toolbox, ((VelocityToolboxView) view).getToolboxConfigLocation());
}
VelocityMacroTests.java 文件源码 项目:class-guard 阅读 18 收藏 0 点赞 0 评论 0
@Override
public void setUp() throws Exception {
    wac = new StaticWebApplicationContext();
    wac.setServletContext(new MockServletContext());

    final Template expectedTemplate = new Template();
    VelocityConfig vc = new VelocityConfig() {
        @Override
        public VelocityEngine getVelocityEngine() {
            return new TestVelocityEngine(TEMPLATE_FILE, expectedTemplate);
        }
    };
    wac.getDefaultListableBeanFactory().registerSingleton("velocityConfigurer", vc);
    wac.refresh();

    request = new MockHttpServletRequest();
    request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
    request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, new FixedThemeResolver());
    response = new MockHttpServletResponse();
}
RedirectViewTests.java 文件源码 项目:class-guard 阅读 21 收藏 0 点赞 0 评论 0
@Test
public void updateTargetUrl() throws Exception {
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.registerSingleton("requestDataValueProcessor", RequestDataValueProcessorWrapper.class);
    wac.setServletContext(new MockServletContext());
    wac.refresh();

    RequestDataValueProcessor mockProcessor = mock(RequestDataValueProcessor.class);
    wac.getBean(RequestDataValueProcessorWrapper.class).setRequestDataValueProcessor(mockProcessor);

    RedirectView rv = new RedirectView();
    rv.setApplicationContext(wac);  // Init RedirectView with WebAppCxt
    rv.setUrl("/path");

    MockHttpServletRequest request = createRequest();
    request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    HttpServletResponse response = new MockHttpServletResponse();

    given(mockProcessor.processUrl(request, "/path")).willReturn("/path?key=123");

    rv.render(new ModelMap(), request, response);

    verify(mockProcessor).processUrl(request, "/path");
}
ResponseEntityExceptionHandlerTests.java 文件源码 项目:class-guard 阅读 25 收藏 0 点赞 0 评论 0
@Test
public void controllerAdvice() throws Exception {
    StaticWebApplicationContext cxt = new StaticWebApplicationContext();
    cxt.registerSingleton("exceptionHandler", ApplicationExceptionHandler.class);
    cxt.refresh();

    ExceptionHandlerExceptionResolver resolver = new ExceptionHandlerExceptionResolver();
    resolver.setApplicationContext(cxt);
    resolver.afterPropertiesSet();

    ServletRequestBindingException ex = new ServletRequestBindingException("message");
    resolver.resolveException(this.servletRequest, this.servletResponse, null, ex);

    assertEquals(400, this.servletResponse.getStatus());
    assertEquals("error content", this.servletResponse.getContentAsString());
    assertEquals("someHeaderValue", this.servletResponse.getHeader("someHeader"));
}


问题


面经


文章

微信
公众号

扫码关注公众号