Search in sources :

Example 31 with MockServletConfig

use of org.springframework.mock.web.MockServletConfig in project grails-core by grails.

the class GrailsLayoutDecoratorMapperTests method testDecoratedByApplicationConventionForViewsNotRenderedByAController.

public void testDecoratedByApplicationConventionForViewsNotRenderedByAController() throws Exception {
    GrailsWebRequest webRequest = buildMockRequest(null);
    webRequest.setAttribute(GrailsLayoutDecoratorMapper.RENDERING_VIEW, Boolean.TRUE, RequestAttributes.SCOPE_REQUEST);
    MockApplicationContext appCtx = (MockApplicationContext) webRequest.getApplicationContext();
    appCtx.registerMockResource("/grails-app/views/layouts/application.gsp", "<html><body><h1>Default Layout</h1><g:layoutBody /></body></html>");
    MockHttpServletRequest request = (MockHttpServletRequest) webRequest.getCurrentRequest();
    request.setMethod("GET");
    request.setRequestURI("/");
    ServletContext context = webRequest.getServletContext();
    GrailsLayoutDecoratorMapper m = new GrailsLayoutDecoratorMapper();
    com.opensymphony.module.sitemesh.Config c = new com.opensymphony.module.sitemesh.Config(new MockServletConfig(context));
    m.init(c, null, null);
    HTMLPageParser parser = new HTMLPageParser();
    String html = "<html><head><title>Foo title</title></head><body>here is the body</body></html>";
    Page page = parser.parse(html.toCharArray());
    Decorator d = m.getDecorator(request, page);
    assertNotNull(d);
    assertEquals("/layouts/application.gsp", d.getPage());
    assertEquals("application", d.getName());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Config(grails.config.Config) MockServletConfig(org.springframework.mock.web.MockServletConfig) PropertySourcesConfig(org.grails.config.PropertySourcesConfig) MockServletConfig(org.springframework.mock.web.MockServletConfig) Page(com.opensymphony.module.sitemesh.Page) HTMLPageParser(com.opensymphony.module.sitemesh.parser.HTMLPageParser) MockApplicationContext(org.grails.support.MockApplicationContext) Decorator(com.opensymphony.module.sitemesh.Decorator) ServletContext(javax.servlet.ServletContext) GrailsWebRequest(org.grails.web.servlet.mvc.GrailsWebRequest)

Example 32 with MockServletConfig

use of org.springframework.mock.web.MockServletConfig in project grails-core by grails.

the class GrailsLayoutDecoratorMapperTests method testDecoratedByApplicationConvention.

public void testDecoratedByApplicationConvention() throws Exception {
    GrailsWebRequest webRequest = buildMockRequest(null);
    webRequest.setAttribute(GrailsLayoutDecoratorMapper.RENDERING_VIEW, Boolean.TRUE, RequestAttributes.SCOPE_REQUEST);
    MockApplicationContext appCtx = (MockApplicationContext) webRequest.getApplicationContext();
    appCtx.registerMockResource("/grails-app/views/layouts/application.gsp", "<html><body><h1>Default Layout</h1><g:layoutBody /></body></html>");
    MockHttpServletRequest request = (MockHttpServletRequest) webRequest.getCurrentRequest();
    request.setMethod("GET");
    request.setRequestURI("orders/list");
    ServletContext context = webRequest.getServletContext();
    GroovyClassLoader gcl = new GroovyClassLoader();
    // create mock controller
    GroovyObject controller = (GroovyObject) gcl.parseClass("class FooController {\n" + "def controllerName = 'foo'\n" + "def actionUri = '/foo/fooAction'\n" + "}").newInstance();
    request.setAttribute(GrailsApplicationAttributes.CONTROLLER, controller);
    GrailsLayoutDecoratorMapper m = new GrailsLayoutDecoratorMapper();
    com.opensymphony.module.sitemesh.Config c = new com.opensymphony.module.sitemesh.Config(new MockServletConfig(context));
    m.init(c, null, null);
    HTMLPageParser parser = new HTMLPageParser();
    String html = "<html><head><title>Foo title</title></head><body>here is the body</body></html>";
    Page page = parser.parse(html.toCharArray());
    Decorator d = m.getDecorator(request, page);
    assertNotNull(d);
    assertEquals("/layouts/application.gsp", d.getPage());
    assertEquals("application", d.getName());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Config(grails.config.Config) MockServletConfig(org.springframework.mock.web.MockServletConfig) PropertySourcesConfig(org.grails.config.PropertySourcesConfig) MockServletConfig(org.springframework.mock.web.MockServletConfig) Page(com.opensymphony.module.sitemesh.Page) HTMLPageParser(com.opensymphony.module.sitemesh.parser.HTMLPageParser) MockApplicationContext(org.grails.support.MockApplicationContext) GroovyObject(groovy.lang.GroovyObject) GroovyClassLoader(groovy.lang.GroovyClassLoader) Decorator(com.opensymphony.module.sitemesh.Decorator) ServletContext(javax.servlet.ServletContext) GrailsWebRequest(org.grails.web.servlet.mvc.GrailsWebRequest)

Example 33 with MockServletConfig

use of org.springframework.mock.web.MockServletConfig in project opennms by OpenNMS.

the class HttpRemotingContextIT method testLoadContext.

public void testLoadContext() throws Throwable {
    DaoTestConfigBean bean = new DaoTestConfigBean();
    bean.afterPropertiesSet();
    MockDatabase db = new MockDatabase(true);
    DataSourceFactory.setInstance(db);
    servletContext = new MockServletContext("file:src/main/webapp");
    servletContext.addInitParameter("contextConfigLocation", "classpath:/META-INF/opennms/applicationContext-commonConfigs.xml " + "classpath:/META-INF/opennms/applicationContext-soa.xml " + "classpath:/META-INF/opennms/applicationContext-mockDao.xml " + "classpath*:/META-INF/opennms/component-service.xml " + "classpath*:/META-INF/opennms/component-dao.xml " + // Contexts within this project
    "/WEB-INF/applicationContext-common.xml " + "/WEB-INF/applicationContext-serviceRegistryRemoting.xml " + "/WEB-INF/applicationContext-spring-security.xml " + "/WEB-INF/applicationContext-svclayer.xml ");
    servletContext.addInitParameter("parentContextKey", "daoContext");
    ServletContextEvent e = new ServletContextEvent(servletContext);
    contextListener = new ContextLoaderListener();
    contextListener.contextInitialized(e);
    servletContext.setContextPath(contextPath);
    servletConfig = new MockServletConfig(servletContext, "dispatcher");
    servletConfig.addInitParameter("com.sun.jersey.config.property.resourceConfigClass", "com.sun.jersey.api.core.PackagesResourceConfig");
    servletConfig.addInitParameter("com.sun.jersey.config.property.packages", "org.opennms.web.rest");
    try {
        MockFilterConfig filterConfig = new MockFilterConfig(servletContext, "openSessionInViewFilter");
        filter = new OpenSessionInViewFilter();
        filter.init(filterConfig);
    } catch (ServletException se) {
        throw se.getRootCause();
    }
}
Also used : DaoTestConfigBean(org.opennms.test.DaoTestConfigBean) OpenSessionInViewFilter(org.springframework.orm.hibernate3.support.OpenSessionInViewFilter) ServletException(javax.servlet.ServletException) MockDatabase(org.opennms.core.test.db.MockDatabase) MockServletConfig(org.springframework.mock.web.MockServletConfig) ContextLoaderListener(org.springframework.web.context.ContextLoaderListener) MockServletContext(org.springframework.mock.web.MockServletContext) ServletContextEvent(javax.servlet.ServletContextEvent) MockFilterConfig(org.springframework.mock.web.MockFilterConfig)

Example 34 with MockServletConfig

use of org.springframework.mock.web.MockServletConfig in project freemarker by apache.

the class FreemarkerServletTest method assertResponseContentTypeEquals.

private void assertResponseContentTypeEquals(String exptectContentType, String ctInitParam, String overrideCTInitParam, String templateName, String responseCT) throws ServletException, IOException {
    MockHttpServletRequest request = createMockHttpServletRequest(servletContext, templateName, null);
    MockHttpServletResponse response = new MockHttpServletResponse();
    if (responseCT != null) {
        response.setContentType(responseCT);
        assertEquals(responseCT, response.getContentType());
    } else {
        assertNull(response.getContentType());
    }
    MockServletConfig servletConfig = new MockServletConfig(servletContext);
    servletConfig.addInitParameter(Configuration.DEFAULT_ENCODING_KEY, "UTF-8");
    if (ctInitParam != null) {
        servletConfig.addInitParameter(INIT_PARAM_CONTENT_TYPE, ctInitParam);
    }
    if (overrideCTInitParam != null) {
        servletConfig.addInitParameter(INIT_PARAM_OVERRIDE_RESPONSE_CONTENT_TYPE, overrideCTInitParam);
    }
    TestFreemarkerServlet freemarkerServlet = new TestFreemarkerServlet();
    try {
        freemarkerServlet.init(servletConfig);
        freemarkerServlet.doGet(request, response);
        assertEquals(HttpServletResponse.SC_OK, response.getStatus());
        assertEquals(exptectContentType, response.getContentType());
    } finally {
        freemarkerServlet.destroy();
    }
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockServletConfig(org.springframework.mock.web.MockServletConfig) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse)

Example 35 with MockServletConfig

use of org.springframework.mock.web.MockServletConfig in project archiva by apache.

the class RssFeedServletTest method setUp.

@Before
@Override
public void setUp() throws Exception {
    final MockServletContext mockServletContext = new MockServletContext();
    WebApplicationContext webApplicationContext = new TestWebapplicationContext(applicationContext, mockServletContext);
    mockServletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, webApplicationContext);
    MockServletConfig mockServletConfig = new MockServletConfig() {

        @Override
        public ServletContext getServletContext() {
            return mockServletContext;
        }
    };
    rssFeedServlet.init(mockServletConfig);
}
Also used : MockServletConfig(org.springframework.mock.web.MockServletConfig) MockServletContext(org.springframework.mock.web.MockServletContext) WebApplicationContext(org.springframework.web.context.WebApplicationContext) Before(org.junit.Before)

Aggregations

MockServletConfig (org.springframework.mock.web.MockServletConfig)45 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)18 MockServletContext (org.springframework.mock.web.MockServletContext)12 StatViewServlet (com.alibaba.druid.support.http.StatViewServlet)10 ServletContext (javax.servlet.ServletContext)8 Before (org.junit.Before)8 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)8 Decorator (com.opensymphony.module.sitemesh.Decorator)7 Page (com.opensymphony.module.sitemesh.Page)7 HTMLPageParser (com.opensymphony.module.sitemesh.parser.HTMLPageParser)7 Config (grails.config.Config)7 PropertySourcesConfig (org.grails.config.PropertySourcesConfig)7 MockApplicationContext (org.grails.support.MockApplicationContext)7 GrailsWebRequest (org.grails.web.servlet.mvc.GrailsWebRequest)7 GroovyClassLoader (groovy.lang.GroovyClassLoader)5 GroovyObject (groovy.lang.GroovyObject)5 Test (org.junit.Test)4 PluginTestVerifier (com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier)3 Method (java.lang.reflect.Method)3 WebApplicationContext (org.springframework.web.context.WebApplicationContext)3