use of grails.core.DefaultGrailsApplication 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 grails.core.DefaultGrailsApplication in project grails-core by grails.
the class ConstraintsEvaluatingPropertyTests method ensureConstraintsPresent.
@SuppressWarnings("rawtypes")
private void ensureConstraintsPresent(String[] classSource, int classIndexToTest, int constraintCount) throws Exception {
// We need to do a real test here to make sure
GroovyClassLoader gcl = new GroovyClassLoader();
Class[] classes = new Class[classSource.length];
for (int i = 0; i < classSource.length; i++) {
classes[i] = gcl.parseClass(classSource[i]);
}
GrailsApplication ga = new DefaultGrailsApplication(classes[classIndexToTest]);
ga.initialise();
new MappingContextBuilder(ga).build(classes[classIndexToTest]);
GrailsDomainClass domainClass = (GrailsDomainClass) ga.getArtefact(DomainClassArtefactHandler.TYPE, classes[classIndexToTest].getName());
Map constraints = domainClass.getConstrainedProperties();
grails.gorm.validation.ConstrainedProperty p = (grails.gorm.validation.ConstrainedProperty) constraints.get("name");
Collection cons = p.getAppliedConstraints();
assertEquals("Incorrect number of constraints extracted: " + constraints, constraintCount, cons.size());
}
use of grails.core.DefaultGrailsApplication 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());
}
use of grails.core.DefaultGrailsApplication in project grails-core by grails.
the class ParseTests method buildMockRequest.
private GrailsWebRequest buildMockRequest(ConfigObject config) throws Exception {
MockApplicationContext appCtx = new MockApplicationContext();
appCtx.registerMockBean(GroovyPagesUriService.BEAN_ID, new DefaultGroovyPagesUriService());
DefaultGrailsApplication grailsApplication = new DefaultGrailsApplication();
grailsApplication.setConfig(config);
appCtx.registerMockBean(GrailsApplication.APPLICATION_ID, grailsApplication);
appCtx.getServletContext().setAttribute(GrailsApplicationAttributes.APPLICATION_CONTEXT, appCtx);
appCtx.getServletContext().setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, appCtx);
return GrailsWebMockUtil.bindMockWebRequest(appCtx);
}
use of grails.core.DefaultGrailsApplication in project grails-core by grails.
the class PluginAwareResourceBundleMessageSource method afterPropertiesSet.
public void afterPropertiesSet() throws Exception {
if (pluginCacheMillis == Long.MIN_VALUE) {
pluginCacheMillis = cacheMillis;
}
if (localResourceLoader == null) {
return;
}
Resource[] resources;
if (Environment.isDevelopmentEnvironmentAvailable()) {
File[] propertiesFiles = new File(BuildSettings.BASE_DIR, GRAILS_APP_I18N_PATH_COMPONENT).listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.endsWith(".properties");
}
});
if (propertiesFiles != null && propertiesFiles.length > 0) {
List<Resource> resourceList = new ArrayList<Resource>(propertiesFiles.length);
for (File propertiesFile : propertiesFiles) {
resourceList.add(new FileSystemResource(propertiesFile));
}
resources = resourceList.toArray(new Resource[resourceList.size()]);
} else {
resources = new Resource[0];
}
} else {
if (searchClasspath) {
resources = resourceResolver.getResources(messageBundleLocationPattern);
} else {
DefaultGrailsApplication defaultGrailsApplication = (DefaultGrailsApplication) application;
if (defaultGrailsApplication != null) {
GrailsApplicationClass applicationClass = defaultGrailsApplication.getApplicationClass();
if (applicationClass != null) {
ResourcePatternResolver resourcePatternResolver = new ClassRelativeResourcePatternResolver(applicationClass.getClass());
resources = resourcePatternResolver.getResources(messageBundleLocationPattern);
} else {
resources = resourceResolver.getResources(messageBundleLocationPattern);
}
} else {
resources = resourceResolver.getResources(messageBundleLocationPattern);
}
}
}
List<String> basenames = new ArrayList<String>();
for (Resource resource : resources) {
String filename = resource.getFilename();
String baseName = GrailsStringUtils.getFileBasename(filename);
int i = baseName.indexOf('_');
if (i > -1) {
baseName = baseName.substring(0, i);
}
if (!basenames.contains(baseName) && !baseName.equals(""))
basenames.add(baseName);
}
setBasenames(basenames.toArray(new String[basenames.size()]));
}
Aggregations