Search in sources :

Example 6 with MockApplicationContext

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

the class GrailsApplicationAttributesTests method getAttributesForClasses.

private GrailsApplicationAttributes getAttributesForClasses(Class<?>[] classes, GroovyClassLoader gcl) {
    MockApplicationContext context = new MockApplicationContext();
    MockServletContext servletContext = new MockServletContext();
    servletContext.setAttribute(GrailsApplicationAttributes.APPLICATION_CONTEXT, context);
    GrailsApplication app = new DefaultGrailsApplication(classes, gcl);
    app.initialise();
    context.registerMockBean(GrailsApplication.APPLICATION_ID, app);
    GrailsClass[] controllers = app.getArtefacts(ControllerArtefactHandler.TYPE);
    for (int i = 0; i < controllers.length; i++) {
        context.registerMockBean(controllers[i].getFullName(), controllers[i].newInstance());
    }
    GrailsClass[] taglibs = app.getArtefacts(TagLibArtefactHandler.TYPE);
    for (int i = 0; i < taglibs.length; i++) {
        context.registerMockBean(taglibs[i].getFullName(), taglibs[i].newInstance());
    }
    return new DefaultGrailsApplicationAttributes(servletContext);
}
Also used : GrailsClass(grails.core.GrailsClass) GrailsApplication(grails.core.GrailsApplication) DefaultGrailsApplication(grails.core.DefaultGrailsApplication) DefaultGrailsApplication(grails.core.DefaultGrailsApplication) MockServletContext(org.springframework.mock.web.MockServletContext) MockApplicationContext(org.grails.support.MockApplicationContext)

Example 7 with MockApplicationContext

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

the class GrailsLayoutDecoratorMapperTests method testDecoratedByActionConvention.

public void testDecoratedByActionConvention() 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/test2/testAction.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 Test2Controller {\n" + "def controllerName = 'test2'\n" + "def actionUri = '/test2/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/test2/testAction.gsp", d.getPage());
    assertEquals("test2/testAction", 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 8 with MockApplicationContext

use of org.grails.support.MockApplicationContext 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 9 with MockApplicationContext

use of org.grails.support.MockApplicationContext 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 10 with MockApplicationContext

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

the class GrailsLayoutDecoratorMapperTests method buildMockRequest.

private GrailsWebRequest buildMockRequest(Config config) throws Exception {
    MockApplicationContext appCtx = new MockApplicationContext();
    appCtx.registerMockBean(GroovyPagesUriService.BEAN_ID, new DefaultGroovyPagesUriService());
    DefaultGrailsApplication grailsApplication = new DefaultGrailsApplication();
    grailsApplication.setConfig(config);
    Holders.setConfig(config);
    appCtx.registerMockBean(GrailsApplication.APPLICATION_ID, grailsApplication);
    GrailsConventionGroovyPageLocator pageLocator = new GrailsConventionGroovyPageLocator();
    pageLocator.setApplicationContext(appCtx);
    GroovyPagesTemplateEngine gpte = new GroovyPagesTemplateEngine();
    gpte.setResourceLoader(appCtx);
    gpte.setApplicationContext(appCtx);
    gpte.afterPropertiesSet();
    GroovyPageViewResolver grailsViewResolver = new GroovyPageViewResolver();
    grailsViewResolver.setApplicationContext(appCtx);
    grailsViewResolver.setGroovyPageLocator(pageLocator);
    grailsViewResolver.setTemplateEngine(gpte);
    GroovyPageLayoutFinder layoutFinder = new GroovyPageLayoutFinder();
    layoutFinder.setViewResolver(grailsViewResolver);
    @SuppressWarnings("rawtypes") Map flat = config != null ? config.flatten() : Collections.emptyMap();
    layoutFinder.setDefaultDecoratorName(flat.get("grails.sitemesh.default.layout") != null ? flat.get("grails.sitemesh.default.layout").toString() : "application");
    appCtx.registerMockBean("groovyPageLocator", pageLocator);
    appCtx.registerMockBean("groovyPageLayoutFinder", layoutFinder);
    DefaultCodecLookup codecLookup = new DefaultCodecLookup();
    codecLookup.setGrailsApplication(grailsApplication);
    codecLookup.afterPropertiesSet();
    appCtx.registerMockBean("codecLookup", codecLookup);
    appCtx.getServletContext().setAttribute(GrailsApplicationAttributes.APPLICATION_CONTEXT, appCtx);
    appCtx.getServletContext().setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, appCtx);
    grailsApplication.setMainContext(appCtx);
    return GrailsWebMockUtil.bindMockWebRequest(appCtx, new MockHttpServletRequest(appCtx.getServletContext()) {

        @Override
        public RequestDispatcher getRequestDispatcher(String path) {
            return null;
        }
    }, new MockHttpServletResponse());
}
Also used : GrailsConventionGroovyPageLocator(org.grails.web.gsp.io.GrailsConventionGroovyPageLocator) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) DefaultGroovyPagesUriService(org.grails.web.pages.DefaultGroovyPagesUriService) DefaultGrailsApplication(grails.core.DefaultGrailsApplication) GroovyPagesTemplateEngine(org.grails.gsp.GroovyPagesTemplateEngine) RequestDispatcher(javax.servlet.RequestDispatcher) MockApplicationContext(org.grails.support.MockApplicationContext) DefaultCodecLookup(org.grails.plugins.codecs.DefaultCodecLookup) GroovyPageViewResolver(org.grails.web.servlet.view.GroovyPageViewResolver) Map(java.util.Map) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse)

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