Search in sources :

Example 6 with DefaultGrailsApplication

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();
}
Also used : PropertySourcesConfig(org.grails.config.PropertySourcesConfig) DefaultGrailsApplication(grails.core.DefaultGrailsApplication) ConfigObject(groovy.util.ConfigObject) MockApplicationContext(org.grails.support.MockApplicationContext) ConfigSlurper(groovy.util.ConfigSlurper)

Example 7 with DefaultGrailsApplication

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());
}
Also used : GrailsDomainClass(grails.core.GrailsDomainClass) DefaultGrailsApplication(grails.core.DefaultGrailsApplication) GroovyClassLoader(groovy.lang.GroovyClassLoader) GrailsApplication(grails.core.GrailsApplication) DefaultGrailsApplication(grails.core.DefaultGrailsApplication) Collection(java.util.Collection) GrailsDomainClass(grails.core.GrailsDomainClass) Map(java.util.Map) grails.gorm.validation(grails.gorm.validation)

Example 8 with DefaultGrailsApplication

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());
}
Also used : GrailsConventionGroovyPageLocator(org.grails.web.gsp.io.GrailsConventionGroovyPageLocator) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) DefaultGroovyPagesUriService(org.grails.web.pages.DefaultGroovyPagesUriService) DefaultGrailsApplication(grails.core.DefaultGrailsApplication) GroovyPagesTemplateEngine(org.grails.gsp.GroovyPagesTemplateEngine) RequestDispatcher(javax.servlet.RequestDispatcher) MockApplicationContext(org.grails.support.MockApplicationContext) DefaultCodecLookup(org.grails.plugins.codecs.DefaultCodecLookup) GroovyPageViewResolver(org.grails.web.servlet.view.GroovyPageViewResolver) Map(java.util.Map) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse)

Example 9 with DefaultGrailsApplication

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);
}
Also used : DefaultGrailsApplication(grails.core.DefaultGrailsApplication) MockApplicationContext(org.grails.support.MockApplicationContext)

Example 10 with DefaultGrailsApplication

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()]));
}
Also used : PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) CachingPathMatchingResourcePatternResolver(org.grails.core.io.CachingPathMatchingResourcePatternResolver) ResourcePatternResolver(org.springframework.core.io.support.ResourcePatternResolver) ClassRelativeResourcePatternResolver(org.grails.core.support.internal.tools.ClassRelativeResourcePatternResolver) GrailsApplicationClass(grails.core.GrailsApplicationClass) Resource(org.springframework.core.io.Resource) FileSystemResource(org.springframework.core.io.FileSystemResource) ArrayList(java.util.ArrayList) FileSystemResource(org.springframework.core.io.FileSystemResource) DefaultGrailsApplication(grails.core.DefaultGrailsApplication) FilenameFilter(java.io.FilenameFilter) ClassRelativeResourcePatternResolver(org.grails.core.support.internal.tools.ClassRelativeResourcePatternResolver) File(java.io.File)

Aggregations

DefaultGrailsApplication (grails.core.DefaultGrailsApplication)13 GrailsApplication (grails.core.GrailsApplication)9 GroovyClassLoader (groovy.lang.GroovyClassLoader)8 GrailsDomainClass (grails.core.GrailsDomainClass)5 Map (java.util.Map)5 MockApplicationContext (org.grails.support.MockApplicationContext)4 ArrayList (java.util.ArrayList)3 IncludingPluginFilter (org.grails.plugins.IncludingPluginFilter)3 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)3 Collection (java.util.Collection)2 List (java.util.List)2 Test (org.junit.jupiter.api.Test)2 GrailsApplicationClass (grails.core.GrailsApplicationClass)1 GrailsClass (grails.core.GrailsClass)1 grails.gorm.validation (grails.gorm.validation)1 Constrained (grails.gorm.validation.Constrained)1 GroovyObject (groovy.lang.GroovyObject)1 ConfigObject (groovy.util.ConfigObject)1 ConfigSlurper (groovy.util.ConfigSlurper)1 File (java.io.File)1