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

项目:spring-security-oauth2-boot    文件:ResourceServerPropertiesTests.java   
private void setListableBeanFactory() {
    ListableBeanFactory beanFactory = new StaticWebApplicationContext() {

        @Override
        public String[] getBeanNamesForType(Class<?> type,
                boolean includeNonSingletons, boolean allowEagerInit) {
            if (type.isAssignableFrom(
                    ResourceServerTokenServicesConfiguration.class)) {
                return new String[] { "ResourceServerTokenServicesConfiguration" };
            }
            return new String[0];
        }

    };
    this.properties.setBeanFactory(beanFactory);
}
项目:spring4-understanding    文件:TilesViewTests.java   
@Before
public void setUp() throws Exception {
    MockServletContext servletContext = new MockServletContext();
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.setServletContext(servletContext);
    wac.refresh();

    request = new MockHttpServletRequest();
    request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);

    response = new MockHttpServletResponse();

    renderer = mock(Renderer.class);

    view = new TilesView();
    view.setServletContext(servletContext);
    view.setRenderer(renderer);
    view.setUrl(VIEW_PATH);
    view.afterPropertiesSet();
}
项目:spring4-understanding    文件:ViewResolverTests.java   
@Test
public void testBeanNameViewResolver() throws ServletException {
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.setServletContext(new MockServletContext());
    MutablePropertyValues pvs1 = new MutablePropertyValues();
    pvs1.addPropertyValue(new PropertyValue("url", "/example1.jsp"));
    wac.registerSingleton("example1", InternalResourceView.class, pvs1);
    MutablePropertyValues pvs2 = new MutablePropertyValues();
    pvs2.addPropertyValue(new PropertyValue("url", "/example2.jsp"));
    wac.registerSingleton("example2", JstlView.class, pvs2);
    BeanNameViewResolver vr = new BeanNameViewResolver();
    vr.setApplicationContext(wac);
    wac.refresh();

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

    view = vr.resolveViewName("example2", Locale.getDefault());
    assertEquals("Correct view class", JstlView.class, view.getClass());
    assertEquals("Correct URL", "/example2.jsp", ((JstlView) view).getUrl());
}
项目:spring4-understanding    文件:ViewResolverTests.java   
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());
}
项目:spring4-understanding    文件:ViewResolverTests.java   
@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
    }
}
项目:spring4-understanding    文件:ViewResolverTests.java   
@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");
    }
}
项目:spring4-understanding    文件:ContentNegotiatingViewResolverTests.java   
@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);
}
项目:spring4-understanding    文件:FreeMarkerMacroTests.java   
@Before
public void setUp() throws Exception {
    wac = new StaticWebApplicationContext();
    wac.setServletContext(new MockServletContext());

    // final Template expectedTemplate = new Template();
    fc = new FreeMarkerConfigurer();
    fc.setTemplateLoaderPaths("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();
}
项目:spring4-understanding    文件:VelocityRenderTests.java   
@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();
}
项目:spring4-understanding    文件:VelocityViewResolverTests.java   
@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());
}
项目:spring4-understanding    文件:VelocityMacroTests.java   
@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(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();
}
项目:spring4-understanding    文件:RedirectViewTests.java   
@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");
}
项目:spring4-understanding    文件:ResponseEntityExceptionHandlerTests.java   
@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"));
}
项目:spring4-understanding    文件:RequestMappingInfoHandlerMappingTests.java   
@Test
public void mappedInterceptors() throws Exception {
    String path = "/foo";
    HandlerInterceptor interceptor = new HandlerInterceptorAdapter() {};
    MappedInterceptor mappedInterceptor = new MappedInterceptor(new String[] {path}, interceptor);

    TestRequestMappingInfoHandlerMapping hm = new TestRequestMappingInfoHandlerMapping();
    hm.registerHandler(new TestController());
    hm.setInterceptors(new Object[] { mappedInterceptor });
    hm.setApplicationContext(new StaticWebApplicationContext());

    HandlerExecutionChain chain = hm.getHandler(new MockHttpServletRequest("GET", path));
    assertNotNull(chain);
    assertNotNull(chain.getInterceptors());
    assertSame(interceptor, chain.getInterceptors()[0]);

    chain = hm.getHandler(new MockHttpServletRequest("GET", "/invalid"));
    assertNull(chain);
}
项目:spring4-understanding    文件:HandlerMethodMappingTests.java   
@Test
public void getCorsConfigWithBeanNameHandler() throws Exception {

    String key = "foo";
    String beanName = "handler1";

    StaticWebApplicationContext context = new StaticWebApplicationContext();
    context.registerSingleton(beanName, MyHandler.class);

    this.mapping.setApplicationContext(context);
    this.mapping.registerMapping(key, beanName, this.method1);
    HandlerMethod handlerMethod = this.mapping.getHandlerInternal(new MockHttpServletRequest("GET", key));

    CorsConfiguration config = this.mapping.getMappingRegistry().getCorsConfiguration(handlerMethod);
    assertNotNull(config);
    assertEquals("http://" + beanName.hashCode() + this.method1.getName(), config.getAllowedOrigins().get(0));
}
项目:spring4-understanding    文件:DispatcherServletTests.java   
@Test
public void cleanupAfterIncludeWithRemove() throws ServletException, IOException {
    MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/main.do");
    MockHttpServletResponse response = new MockHttpServletResponse();

    request.setAttribute("test1", "value1");
    request.setAttribute("test2", "value2");
    WebApplicationContext wac = new StaticWebApplicationContext();
    request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);

    request.setAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE, "/form.do");
    simpleDispatcherServlet.service(request, response);

    assertEquals("value1", request.getAttribute("test1"));
    assertEquals("value2", request.getAttribute("test2"));
    assertEquals(wac, request.getAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE));
    assertNull(request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE));
    assertNull(request.getAttribute("command"));
}
项目:spring4-understanding    文件:DispatcherServletTests.java   
@Test
public void cleanupAfterIncludeWithRestore() throws ServletException, IOException {
    MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/main.do");
    MockHttpServletResponse response = new MockHttpServletResponse();

    request.setAttribute("test1", "value1");
    request.setAttribute("test2", "value2");
    WebApplicationContext wac = new StaticWebApplicationContext();
    request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    TestBean command = new TestBean();
    request.setAttribute("command", command);

    request.setAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE, "/form.do");
    simpleDispatcherServlet.service(request, response);

    assertEquals("value1", request.getAttribute("test1"));
    assertEquals("value2", request.getAttribute("test2"));
    assertSame(wac, request.getAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE));
}
项目:spring4-understanding    文件:DispatcherServletTests.java   
@Test
public void noCleanupAfterInclude() throws ServletException, IOException {
    MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/main.do");
    MockHttpServletResponse response = new MockHttpServletResponse();

    request.setAttribute("test1", "value1");
    request.setAttribute("test2", "value2");
    WebApplicationContext wac = new StaticWebApplicationContext();
    request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    TestBean command = new TestBean();
    request.setAttribute("command", command);

    request.setAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE, "/form.do");
    simpleDispatcherServlet.setCleanupAfterInclude(false);
    simpleDispatcherServlet.service(request, response);

    assertEquals("value1", request.getAttribute("test1"));
    assertEquals("value2", request.getAttribute("test2"));
    assertSame(wac, request.getAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE));
}
项目:spring4-understanding    文件:DelegatingFilterProxyTests.java   
@Test
public void testDelegatingFilterProxyWithTargetBeanName() throws ServletException, IOException {
    MockServletContext sc = new MockServletContext();

    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.setServletContext(sc);
    wac.registerSingleton("targetFilter", MockFilter.class);
    wac.refresh();
    sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);

    MockFilter targetFilter = (MockFilter) wac.getBean("targetFilter");

    DelegatingFilterProxy filterProxy = new DelegatingFilterProxy("targetFilter");
    filterProxy.init(new MockFilterConfig(sc));

    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    filterProxy.doFilter(request, response, null);

    assertNull(targetFilter.filterConfig);
    assertEquals(Boolean.TRUE, request.getAttribute("called"));

    filterProxy.destroy();
    assertNull(targetFilter.filterConfig);
}
项目:spring4-understanding    文件:DelegatingFilterProxyTests.java   
@Test
public void testDelegatingFilterProxyWithTargetBeanNameAndNotYetRefreshedApplicationContext() throws ServletException, IOException {
    MockServletContext sc = new MockServletContext();

    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.setServletContext(sc);
    wac.registerSingleton("targetFilter", MockFilter.class);
    // wac.refresh();
    // note that the context is not set as the ROOT attribute in the ServletContext!

    DelegatingFilterProxy filterProxy = new DelegatingFilterProxy("targetFilter", wac);
    filterProxy.init(new MockFilterConfig(sc));

    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    filterProxy.doFilter(request, response, null);

    MockFilter targetFilter = (MockFilter) wac.getBean("targetFilter");

    assertNull(targetFilter.filterConfig);
    assertEquals(Boolean.TRUE, request.getAttribute("called"));

    filterProxy.destroy();
    assertNull(targetFilter.filterConfig);
}
项目:spring4-understanding    文件:DelegatingFilterProxyTests.java   
@Test
public void testDelegatingFilterProxyWithFilterName() throws ServletException, IOException {
    ServletContext sc = new MockServletContext();

    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.setServletContext(sc);
    wac.registerSingleton("targetFilter", MockFilter.class);
    wac.refresh();
    sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);

    MockFilter targetFilter = (MockFilter) wac.getBean("targetFilter");

    MockFilterConfig proxyConfig = new MockFilterConfig(sc, "targetFilter");
    DelegatingFilterProxy filterProxy = new DelegatingFilterProxy();
    filterProxy.init(proxyConfig);

    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    filterProxy.doFilter(request, response, null);

    assertNull(targetFilter.filterConfig);
    assertEquals(Boolean.TRUE, request.getAttribute("called"));

    filterProxy.destroy();
    assertNull(targetFilter.filterConfig);
}
项目:spring4-understanding    文件:DelegatingFilterProxyTests.java   
@Test
public void testDelegatingFilterProxyWithFrameworkServletContext() throws ServletException, IOException {
    ServletContext sc = new MockServletContext();
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.setServletContext(sc);
    wac.registerSingleton("targetFilter", MockFilter.class);
    wac.refresh();
    sc.setAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.dispatcher", wac);

    MockFilter targetFilter = (MockFilter) wac.getBean("targetFilter");

    MockFilterConfig proxyConfig = new MockFilterConfig(sc);
    proxyConfig.addInitParameter("targetBeanName", "targetFilter");
    DelegatingFilterProxy filterProxy = new DelegatingFilterProxy();
    filterProxy.init(proxyConfig);

    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    filterProxy.doFilter(request, response, null);

    assertNull(targetFilter.filterConfig);
    assertEquals(Boolean.TRUE, request.getAttribute("called"));

    filterProxy.destroy();
    assertNull(targetFilter.filterConfig);
}
项目:spring4-understanding    文件:DefaultMockMvcBuilderTests.java   
/**
 * See SPR-12553 and SPR-13075.
 */
@Test
public void rootWacServletContainerAttributeNotPreviouslySetWithContextHierarchy() {
    StaticApplicationContext ear = new StaticApplicationContext();
    StaticWebApplicationContext root = new StaticWebApplicationContext();
    root.setParent(ear);
    root.setServletContext(this.servletContext);
    StaticWebApplicationContext dispatcher = new StaticWebApplicationContext();
    dispatcher.setParent(root);
    dispatcher.setServletContext(this.servletContext);

    DefaultMockMvcBuilder builder = webAppContextSetup(dispatcher);
    WebApplicationContext wac = builder.initWebAppContext();

    assertSame(dispatcher, wac);
    assertSame(root, wac.getParent());
    assertSame(ear, wac.getParent().getParent());
    assertSame(root, WebApplicationContextUtils.getRequiredWebApplicationContext(this.servletContext));
}
项目:https-github.com-g0t4-jenkins2-course-spring-boot    文件:MustacheViewResolverTests.java   
@Test
public void templateResourceInputStreamIsClosed() throws Exception {
    final Resource resource = mock(Resource.class);
    given(resource.exists()).willReturn(true);
    InputStream inputStream = new ByteArrayInputStream(new byte[0]);
    InputStream spyInputStream = spy(inputStream);
    given(resource.getInputStream()).willReturn(spyInputStream);
    this.resolver = new MustacheViewResolver();
    this.resolver.setApplicationContext(new StaticWebApplicationContext() {

        @Override
        public Resource getResource(String location) {
            return resource;
        }

    });
    this.resolver.loadView("foo", null);
    verify(spyInputStream).close();
}
项目:spring-boot-concourse    文件:MustacheViewResolverTests.java   
@Test
public void templateResourceInputStreamIsClosed() throws Exception {
    final Resource resource = mock(Resource.class);
    given(resource.exists()).willReturn(true);
    InputStream inputStream = new ByteArrayInputStream(new byte[0]);
    InputStream spyInputStream = spy(inputStream);
    given(resource.getInputStream()).willReturn(spyInputStream);
    this.resolver = new MustacheViewResolver();
    this.resolver.setApplicationContext(new StaticWebApplicationContext() {

        @Override
        public Resource getResource(String location) {
            return resource;
        }

    });
    this.resolver.loadView("foo", null);
    verify(spyInputStream).close();
}
项目:spring-struts-forwardport    文件:StrutsSupportTests.java   
@Test
@SuppressWarnings("serial")
public void actionSupportWithContextLoaderPlugIn() throws ServletException {
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.addMessage("test", Locale.getDefault(), "testmessage");
    final ServletContext servletContext = new MockServletContext();
    wac.setServletContext(servletContext);
    wac.refresh();
    servletContext.setAttribute(ContextLoaderPlugIn.SERVLET_CONTEXT_PREFIX, wac);

    ActionServlet actionServlet = new ActionServlet() {
        @Override
        public ServletContext getServletContext() {
            return servletContext;
        }
    };
    ActionSupport action = new ActionSupport() {
    };
    action.setServlet(actionServlet);

    assertEquals(wac, action.getWebApplicationContext());
    assertEquals(servletContext, action.getServletContext());
    assertEquals("testmessage", action.getMessageSourceAccessor().getMessage("test"));

    action.setServlet(null);
}
项目:spring-struts-forwardport    文件:StrutsSupportTests.java   
@Test
@SuppressWarnings("serial")
public void actionSupportWithRootContext() throws ServletException {
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.addMessage("test", Locale.getDefault(), "testmessage");
    final ServletContext servletContext = new MockServletContext();
    wac.setServletContext(servletContext);
    wac.refresh();
    servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);

    ActionServlet actionServlet = new ActionServlet() {
        @Override
        public ServletContext getServletContext() {
            return servletContext;
        }
    };
    ActionSupport action = new ActionSupport() {
    };
    action.setServlet(actionServlet);

    assertEquals(wac, action.getWebApplicationContext());
    assertEquals(servletContext, action.getServletContext());
    assertEquals("testmessage", action.getMessageSourceAccessor().getMessage("test"));

    action.setServlet(null);
}
项目:spring-struts-forwardport    文件:StrutsSupportTests.java   
@Test
@SuppressWarnings("serial")
public void dispatchActionSupportWithContextLoaderPlugIn() throws ServletException {
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.addMessage("test", Locale.getDefault(), "testmessage");
    final ServletContext servletContext = new MockServletContext();
    wac.setServletContext(servletContext);
    wac.refresh();
    servletContext.setAttribute(ContextLoaderPlugIn.SERVLET_CONTEXT_PREFIX, wac);

    ActionServlet actionServlet = new ActionServlet() {
        @Override
        public ServletContext getServletContext() {
            return servletContext;
        }
    };
    DispatchActionSupport action = new DispatchActionSupport() {
    };
    action.setServlet(actionServlet);

    assertEquals(wac, action.getWebApplicationContext());
    assertEquals(servletContext, action.getServletContext());
    assertEquals("testmessage", action.getMessageSourceAccessor().getMessage("test"));

    action.setServlet(null);
}
项目:spring-struts-forwardport    文件:StrutsSupportTests.java   
@Test
@SuppressWarnings("serial")
public void dispatchActionSupportWithRootContext() throws ServletException {
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.addMessage("test", Locale.getDefault(), "testmessage");
    final ServletContext servletContext = new MockServletContext();
    wac.setServletContext(servletContext);
    wac.refresh();
    servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);

    ActionServlet actionServlet = new ActionServlet() {
        @Override
        public ServletContext getServletContext() {
            return servletContext;
        }
    };
    DispatchActionSupport action = new DispatchActionSupport() {
    };
    action.setServlet(actionServlet);

    assertEquals(wac, action.getWebApplicationContext());
    assertEquals(servletContext, action.getServletContext());
    assertEquals("testmessage", action.getMessageSourceAccessor().getMessage("test"));

    action.setServlet(null);
}
项目:class-guard    文件:ViewResolverTests.java   
@Test
public void testBeanNameViewResolver() throws ServletException {
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.setServletContext(new MockServletContext());
    MutablePropertyValues pvs1 = new MutablePropertyValues();
    pvs1.addPropertyValue(new PropertyValue("url", "/example1.jsp"));
    wac.registerSingleton("example1", InternalResourceView.class, pvs1);
    MutablePropertyValues pvs2 = new MutablePropertyValues();
    pvs2.addPropertyValue(new PropertyValue("url", "/example2.jsp"));
    wac.registerSingleton("example2", JstlView.class, pvs2);
    BeanNameViewResolver vr = new BeanNameViewResolver();
    vr.setApplicationContext(wac);
    wac.refresh();

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

    view = vr.resolveViewName("example2", Locale.getDefault());
    assertEquals("Correct view class", JstlView.class, view.getClass());
    assertEquals("Correct URL", "/example2.jsp", ((JstlView) view).getUrl());
}
项目:class-guard    文件:ViewResolverTests.java   
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());
}
项目:class-guard    文件:ViewResolverTests.java   
@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
    }
}
项目:class-guard    文件:ViewResolverTests.java   
@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");
    }
}
项目:class-guard    文件:ContentNegotiatingViewResolverTests.java   
@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);
}
项目:class-guard    文件:FreeMarkerMacroTests.java   
@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();
}
项目:class-guard    文件:VelocityRenderTests.java   
@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();
}
项目:class-guard    文件:VelocityViewResolverTests.java   
@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());
}
项目:class-guard    文件:VelocityMacroTests.java   
@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();
}
项目:class-guard    文件:RedirectViewTests.java   
@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");
}
项目:class-guard    文件:ResponseEntityExceptionHandlerTests.java   
@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"));
}