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);
}
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());
}
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());
}
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());
}
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());
}
Aggregations