Search in sources :

Example 1 with PropertySourcesConfig

use of org.grails.config.PropertySourcesConfig in project grails-core by grails.

the class GrailsLayoutDecoratorMapperTests method testOverridingDefaultTemplateViaConfig.

public void testOverridingDefaultTemplateViaConfig() throws Exception {
    ConfigObject config = new ConfigSlurper().parse("grails.sitemesh.default.layout='otherApplication'");
    MutablePropertySources propertySources = new MutablePropertySources();
    propertySources.addLast(new MapPropertySource("grails", config));
    GrailsWebRequest webRequest = buildMockRequest(new PropertySourcesConfig(propertySources));
    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>");
    appCtx.registerMockResource("/grails-app/views/layouts/otherApplication.gsp", "<html><body><h1>Other 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/otherApplication.gsp", d.getPage());
    assertEquals("otherApplication", d.getName());
}
Also used : PropertySourcesConfig(org.grails.config.PropertySourcesConfig) 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) MapPropertySource(org.springframework.core.env.MapPropertySource) ServletContext(javax.servlet.ServletContext) MutablePropertySources(org.springframework.core.env.MutablePropertySources) GrailsWebRequest(org.grails.web.servlet.mvc.GrailsWebRequest) ConfigObject(groovy.util.ConfigObject) ConfigSlurper(groovy.util.ConfigSlurper)

Example 2 with PropertySourcesConfig

use of org.grails.config.PropertySourcesConfig in project grails-core by grails.

the class DefaultGrailsApplication method getConfig.

public Config getConfig() {
    if (config == null) {
        if (parentContext != null) {
            org.springframework.core.env.Environment environment = parentContext.getEnvironment();
            if (environment instanceof ConfigurableEnvironment) {
                MutablePropertySources propertySources = ((ConfigurableEnvironment) environment).getPropertySources();
                this.config = new PropertySourcesConfig(propertySources);
            } else {
                this.config = new PropertySourcesConfig();
            }
        } else {
            this.config = new PropertySourcesConfig();
        }
        setConfig(this.config);
    }
    return config;
}
Also used : PropertySourcesConfig(org.grails.config.PropertySourcesConfig) ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) MutablePropertySources(org.springframework.core.env.MutablePropertySources)

Example 3 with PropertySourcesConfig

use of org.grails.config.PropertySourcesConfig in project grails-core by grails.

the class AbstractGrailsMockTests method setUp.

@Override
protected final void setUp() throws Exception {
    ExpandoMetaClass.enableGlobally();
    super.setUp();
    System.out.println("Setting up test");
    ctx = new MockApplicationContext();
    ctx.registerMockBean(GrailsApplication.CLASS_LOADER_BEAN, gcl);
    onSetUp();
    ga = new DefaultGrailsApplication(gcl.getLoadedClasses(), gcl);
    if (ClassUtils.isPresent("Config", gcl)) {
        ConfigObject config = new ConfigSlurper().parse(gcl.loadClass("Config"));
        ga.setConfig(new PropertySourcesConfig(config));
    }
    ga.getMetadata().put(Metadata.APPLICATION_NAME, getClass().getName());
    ga.setApplicationContext(ctx);
    ga.initialise();
    ctx.registerMockBean(GrailsApplication.APPLICATION_ID, ga);
    postSetUp();
}
Also used : PropertySourcesConfig(org.grails.config.PropertySourcesConfig) DefaultGrailsApplication(grails.core.DefaultGrailsApplication) ConfigObject(groovy.util.ConfigObject) MockApplicationContext(org.grails.support.MockApplicationContext) ConfigSlurper(groovy.util.ConfigSlurper)

Aggregations

PropertySourcesConfig (org.grails.config.PropertySourcesConfig)3 ConfigObject (groovy.util.ConfigObject)2 ConfigSlurper (groovy.util.ConfigSlurper)2 MockApplicationContext (org.grails.support.MockApplicationContext)2 MutablePropertySources (org.springframework.core.env.MutablePropertySources)2 Decorator (com.opensymphony.module.sitemesh.Decorator)1 Page (com.opensymphony.module.sitemesh.Page)1 HTMLPageParser (com.opensymphony.module.sitemesh.parser.HTMLPageParser)1 Config (grails.config.Config)1 DefaultGrailsApplication (grails.core.DefaultGrailsApplication)1 GroovyClassLoader (groovy.lang.GroovyClassLoader)1 GroovyObject (groovy.lang.GroovyObject)1 ServletContext (javax.servlet.ServletContext)1 GrailsWebRequest (org.grails.web.servlet.mvc.GrailsWebRequest)1 ConfigurableEnvironment (org.springframework.core.env.ConfigurableEnvironment)1 MapPropertySource (org.springframework.core.env.MapPropertySource)1 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)1 MockServletConfig (org.springframework.mock.web.MockServletConfig)1