use of groovy.util.ConfigSlurper 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());
}
use of groovy.util.ConfigSlurper in project grails-core by grails.
the class ParseTests method testParseWithUTF8.
public void testParseWithUTF8() throws Exception {
// This is some unicode Chinese (who knows what it says!)
String src = "Chinese text: 㐡㐷㐿㑃㐐㐅㢳㢚㥞㥇㫛㭚㭧";
// Sanity check the string loaded OK as unicode - it won't look right if you output it, default stdout is not UTF-8
// on many OSes
assertEquals(src.indexOf('?'), -1);
ConfigObject config = new ConfigSlurper().parse("grails.views.gsp.encoding = \"UTF-8\"");
buildMockRequest(config);
ParsedResult output = null;
try {
output = parseCode("myTest4", src);
} finally {
RequestContextHolder.resetRequestAttributes();
}
String expected = makeImports() + "\n" + "class myTest4 extends org.grails.gsp.GroovyPage {\n" + "public String getGroovyPageFileName() { \"myTest4\" }\n" + "public Object run() {\n" + "Writer out = getOut()\n" + "Writer expressionOut = getExpressionOut()\n" + "registerSitemeshPreprocessMode()\n" + "printHtmlPart(0)\n" + "}\n" + GSP_FOOTER;
assertEquals(trimAndRemoveCR(expected), trimAndRemoveCR(output.generatedGsp));
assertEquals(src, output.htmlParts[0]);
}
use of groovy.util.ConfigSlurper in project cas by apereo.
the class GroovyConfigurationPropertiesLoader method load.
@Override
public PropertySource load() {
val properties = new LinkedHashMap<>();
val slurper = new ConfigSlurper();
applicationProfiles.forEach(Unchecked.consumer(profile -> {
slurper.setEnvironment(profile);
slurper.registerConditionalBlock("profiles", profile);
val bindings = CollectionUtils.wrap("profile", profile, "logger", LOGGER);
slurper.setBinding(bindings);
val groovyConfig = slurper.parse(getResource().getURL());
val pp = groovyConfig.toProperties();
LOGGER.debug("Found settings [{}] in Groovy file [{}]", pp.keySet(), getResource());
properties.putAll(pp);
}));
return finalizeProperties(decryptProperties(properties));
}
use of groovy.util.ConfigSlurper 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.setApplicationContext(ctx);
ga.initialise();
ctx.registerMockBean(GrailsApplication.APPLICATION_ID, ga);
postSetUp();
}
use of groovy.util.ConfigSlurper in project grails-core by grails.
the class AbstractGrailsPluginManager method informOfClassChange.
public void informOfClassChange(File file, @SuppressWarnings("rawtypes") Class cls) {
if (file.getName().equals(CONFIG_FILE)) {
ConfigSlurper configSlurper = getConfigSlurper(application);
ConfigObject c;
try {
c = configSlurper.parse(file.toURI().toURL());
application.getConfig().merge(c);
final Map flat = c.flatten();
application.getConfig().merge(flat);
application.configChanged();
informPluginsOfConfigChange();
} catch (Exception e) {
// ignore
LOG.debug("Error in changing Config", e);
}
} else {
if (cls != null) {
MetaClassRegistry registry = GroovySystem.getMetaClassRegistry();
registry.removeMetaClass(cls);
ExpandoMetaClass newMc = new ExpandoMetaClass(cls, true, true);
newMc.initialize();
registry.setMetaClass(cls, newMc);
Enhanced en = AnnotationUtils.findAnnotation(cls, Enhanced.class);
if (en != null) {
Class<?>[] mixinClasses = en.mixins();
if (mixinClasses != null) {
DefaultGroovyMethods.mixin(newMc, mixinClasses);
}
}
}
for (GrailsPlugin grailsPlugin : pluginList) {
if (grailsPlugin.hasInterestInChange(file.getAbsolutePath())) {
try {
if (cls == null) {
grailsPlugin.notifyOfEvent(GrailsPlugin.EVENT_ON_CHANGE, new FileSystemResource(file));
} else {
grailsPlugin.notifyOfEvent(GrailsPlugin.EVENT_ON_CHANGE, cls);
}
Environment.setCurrentReloadError(null);
} catch (Exception e) {
LOG.error("Plugin " + grailsPlugin + " could not reload changes to file [" + file + "]: " + e.getMessage(), e);
Environment.setCurrentReloadError(e);
}
}
}
}
}
Aggregations