コード例 #1
0
  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());
  }
コード例 #2
0
  /*
   * 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());
  }
コード例 #3
0
  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());
  }