public void testDecoratedByApplicationConvention() throws Exception {
    GrailsWebRequest webRequest = buildMockRequest();
    MockApplicationContext appCtx = (MockApplicationContext) webRequest.getApplicationContext();
    appCtx.registerMockResource(
        "WEB-INF/grails-app/views/layouts/application.gsp",
        "<html><body><h1>Default Layout</h1><g:layoutBody /></body></html>");

    MockHttpServletRequest request = new MockHttpServletRequest("GET", "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();
    Config c = new 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("/WEB-INF/grails-app/views/layouts/application.gsp", d.getPage());
    assertEquals("application", d.getName());
  }
 private GrailsWebRequest buildMockRequest() throws Exception {
   MockApplicationContext appCtx = new MockApplicationContext();
   appCtx.registerMockBean(GroovyPagesUriService.BEAN_ID, new DefaultGroovyPagesUriService());
   appCtx
       .getServletContext()
       .setAttribute(GrailsApplicationAttributes.APPLICATION_CONTEXT, appCtx);
   appCtx
       .getServletContext()
       .setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, appCtx);
   return GrailsWebUtil.bindMockWebRequest(appCtx);
 }
  /*
   * Test method for 'org.codehaus.groovy.grails.web.sitemesh.GrailsLayoutDecoratorMapper.getDecorator(HttpServletRequest, Page)'
   */
  public void testGetDecoratorHttpServletRequestPage() throws Exception {
    GrailsWebRequest webRequest = buildMockRequest();
    MockApplicationContext appCtx = (MockApplicationContext) webRequest.getApplicationContext();
    appCtx.registerMockResource(
        "WEB-INF/grails-app/views/layouts/test.gsp", "<html><body><g:layoutBody /></body></html>");

    MockHttpServletRequest request = new MockHttpServletRequest("GET", "orders/list");
    ServletContext context = webRequest.getServletContext();
    GrailsLayoutDecoratorMapper m = new GrailsLayoutDecoratorMapper();
    Config c = new 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("/WEB-INF/grails-app/views/layouts/test.gsp", d.getPage());
    assertEquals("test", d.getName());
  }
  public void testDecoratedByApplicationConventionForViewsNotRenderedByAController()
      throws Exception {
    GrailsWebRequest webRequest = buildMockRequest();
    MockApplicationContext appCtx = (MockApplicationContext) webRequest.getApplicationContext();
    appCtx.registerMockResource(
        "WEB-INF/grails-app/views/layouts/application.gsp",
        "<html><body><h1>Default Layout</h1><g:layoutBody /></body></html>");

    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
    ServletContext context = webRequest.getServletContext();
    GroovyClassLoader gcl = new GroovyClassLoader();

    GrailsLayoutDecoratorMapper m = new GrailsLayoutDecoratorMapper();
    Config c = new 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("/WEB-INF/grails-app/views/layouts/application.gsp", d.getPage());
    assertEquals("application", d.getName());
  }
Example #5
0
 /**
  * Bootstraps Grails with the given GrailsApplication instance.
  *
  * @param application The GrailsApplication instance
  * @return A Grails ApplicationContext
  */
 public static ApplicationContext bootstrapGrailsFromApplication(GrailsApplication application) {
   MockApplicationContext parent = new MockApplicationContext();
   parent.registerMockBean(GrailsApplication.APPLICATION_ID, application);
   return createGrailsApplicationContext(parent, application);
 }