Search in sources :

Example 1 with MockApplicationContext

use of org.grails.support.MockApplicationContext in project grails-core by grails.

the class GrailsLayoutDecoratorMapperTests method testGetDecoratorHttpServletRequestPage.

/*
     * Test method for 'GrailsLayoutDecoratorMapper.getDecorator(HttpServletRequest, Page)'
     */
public void testGetDecoratorHttpServletRequestPage() 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/test.gsp", "<html><body><g:layoutBody /></body></html>");
    MockHttpServletRequest request = (MockHttpServletRequest) webRequest.getCurrentRequest();
    request.setMethod("GET");
    request.setRequestURI("orders/list");
    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>Test title</title><meta name=\"layout\" content=\"test\"></meta></head><body>here is the body</body></html>";
    Page page = parser.parse(html.toCharArray());
    Decorator d = m.getDecorator(request, page);
    assertNotNull(d);
    assertEquals("/layouts/test.gsp", d.getPage());
    assertEquals("test", 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 2 with MockApplicationContext

use of org.grails.support.MockApplicationContext in project grails-core by grails.

the class GrailsLayoutDecoratorMapperTests method testOverridingDefaultTemplateViaConfig.

public void testOverridingDefaultTemplateViaConfig() throws Exception {
    ConfigObject config = new ConfigSlurper().parse("grails.sitemesh.default.layout='otherApplication'");
    MutablePropertySources propertySources = new MutablePropertySources();
    propertySources.addLast(new MapPropertySource("grails", config));
    GrailsWebRequest webRequest = buildMockRequest(new PropertySourcesConfig(propertySources));
    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>");
    appCtx.registerMockResource("/grails-app/views/layouts/otherApplication.gsp", "<html><body><h1>Other 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/otherApplication.gsp", d.getPage());
    assertEquals("otherApplication", d.getName());
}
Also used : PropertySourcesConfig(org.grails.config.PropertySourcesConfig) 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) MapPropertySource(org.springframework.core.env.MapPropertySource) ServletContext(javax.servlet.ServletContext) MutablePropertySources(org.springframework.core.env.MutablePropertySources) GrailsWebRequest(org.grails.web.servlet.mvc.GrailsWebRequest) ConfigObject(groovy.util.ConfigObject) ConfigSlurper(groovy.util.ConfigSlurper)

Example 3 with MockApplicationContext

use of org.grails.support.MockApplicationContext in project grails-core by grails.

the class GrailsLayoutDecoratorMapperTests method testDecoratedByLayoutPropertyInController.

public void testDecoratedByLayoutPropertyInController() 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/test.gsp", "<html><body><g:layoutBody /></body></html>");
    appCtx.registerMockResource("/grails-app/views/layouts/mylayout.gsp", "<html><body><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 TestController {\n" + "def controllerName = 'test'\n" + "def actionUri = '/test/testAction'\n" + "static layout = 'mylayout'\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>Test 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/mylayout.gsp", d.getPage());
    assertEquals("mylayout", 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 4 with MockApplicationContext

use of org.grails.support.MockApplicationContext in project grails-core by grails.

the class GrailsLayoutDecoratorMapperTests method testDecoratedByControllerConvention.

public void testDecoratedByControllerConvention() 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/test.gsp", "<html><body><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 TestController {\n" + "def controllerName = 'test'\n" + "def actionUri = '/test/testAction'\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>Test 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/test.gsp", d.getPage());
    assertEquals("test", 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 5 with MockApplicationContext

use of org.grails.support.MockApplicationContext in project grails-core by grails.

the class AbstractGrailsMockTests method setUp.

@Override
protected final void setUp() throws Exception {
    ExpandoMetaClass.enableGlobally();
    super.setUp();
    System.out.println("Setting up test");
    ctx = new MockApplicationContext();
    ctx.registerMockBean(GrailsApplication.CLASS_LOADER_BEAN, gcl);
    onSetUp();
    ga = new DefaultGrailsApplication(gcl.getLoadedClasses(), gcl);
    if (ClassUtils.isPresent("Config", gcl)) {
        ConfigObject config = new ConfigSlurper().parse(gcl.loadClass("Config"));
        ga.setConfig(new PropertySourcesConfig(config));
    }
    ga.getMetadata().put(Metadata.APPLICATION_NAME, getClass().getName());
    ga.setApplicationContext(ctx);
    ga.initialise();
    ctx.registerMockBean(GrailsApplication.APPLICATION_ID, ga);
    postSetUp();
}
Also used : PropertySourcesConfig(org.grails.config.PropertySourcesConfig) DefaultGrailsApplication(grails.core.DefaultGrailsApplication) ConfigObject(groovy.util.ConfigObject) MockApplicationContext(org.grails.support.MockApplicationContext) ConfigSlurper(groovy.util.ConfigSlurper)

Aggregations

MockApplicationContext (org.grails.support.MockApplicationContext)11 PropertySourcesConfig (org.grails.config.PropertySourcesConfig)8 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)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 ServletContext (javax.servlet.ServletContext)7 GrailsWebRequest (org.grails.web.servlet.mvc.GrailsWebRequest)7 MockServletConfig (org.springframework.mock.web.MockServletConfig)7 GroovyClassLoader (groovy.lang.GroovyClassLoader)5 GroovyObject (groovy.lang.GroovyObject)5 DefaultGrailsApplication (grails.core.DefaultGrailsApplication)4 ConfigObject (groovy.util.ConfigObject)2 ConfigSlurper (groovy.util.ConfigSlurper)2 GrailsApplication (grails.core.GrailsApplication)1 GrailsClass (grails.core.GrailsClass)1 Map (java.util.Map)1 RequestDispatcher (javax.servlet.RequestDispatcher)1 GroovyPagesTemplateEngine (org.grails.gsp.GroovyPagesTemplateEngine)1