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()); }
/** Retrieve Decorator named in 'name' attribute. Checks the role if specified. */ public Decorator getNamedDecorator(HttpServletRequest request, String name) { Decorator result = null; try { result = configLoader.getDecoratorByName(name); } catch (ServletException e) { e.printStackTrace(); } if (result == null || (result.getRole() != null && !request.isUserInRole(result.getRole()))) { // if the result is null or the user is not in the role return super.getNamedDecorator(request, name); } else { return result; } }
/* * 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()); }