Search in sources :

Example 1 with DefaultGrailsApplication

use of grails.core.DefaultGrailsApplication in project grails-core by grails.

the class ConstrainedPropertyTests method testConstraintBuilder.

@SuppressWarnings("rawtypes")
public void testConstraintBuilder() throws Exception {
    GroovyClassLoader gcl = new GroovyClassLoader();
    Class groovyClass = gcl.parseClass("class TestClass {\n" + "Long id\n" + "Long version\n" + "String login\n" + "String other\n" + "String email\n" + "static constraints = {\n" + "login(size:5..15,nullable:false,blank:false)\n" + "other(blank:false,size:5..15,nullable:false)\n" + "email(email:true)\n" + "}\n" + "}");
    GrailsApplication ga = new DefaultGrailsApplication(groovyClass);
    ga.initialise();
    new MappingContextBuilder(ga).build(groovyClass);
    GrailsDomainClass domainClass = (GrailsDomainClass) ga.getArtefact(DomainClassArtefactHandler.TYPE, "TestClass");
    Map constrainedProperties = domainClass.getConstrainedProperties();
    assert constrainedProperties.size() == 3;
    grails.gorm.validation.ConstrainedProperty loginConstraint = (grails.gorm.validation.ConstrainedProperty) constrainedProperties.get("login");
    Collection appliedConstraints = loginConstraint.getAppliedConstraints();
    assertTrue(appliedConstraints.size() == 3);
    // Check the order of the constraints for the 'login' property...
    int index = 0;
    String[] constraintNames = new String[] { "size", "nullable", "blank" };
    for (Iterator iter = appliedConstraints.iterator(); iter.hasNext(); ) {
        Constraint c = (Constraint) iter.next();
        assertEquals(constraintNames[index], c.getName());
        index++;
    }
    // ...and for the 'other' property.
    appliedConstraints = ((grails.gorm.validation.ConstrainedProperty) constrainedProperties.get("other")).getAppliedConstraints();
    index = 0;
    constraintNames = new String[] { "blank", "size", "nullable" };
    for (Iterator iter = appliedConstraints.iterator(); iter.hasNext(); ) {
        Constraint c = (Constraint) iter.next();
        assertEquals(constraintNames[index], c.getName());
        index++;
    }
    grails.gorm.validation.ConstrainedProperty emailConstraint = (grails.gorm.validation.ConstrainedProperty) constrainedProperties.get("email");
    assertEquals(2, emailConstraint.getAppliedConstraints().size());
    GroovyObject go = (GroovyObject) groovyClass.newInstance();
    go.setProperty("email", "rubbish_email");
    Errors errors = new BindException(go, "TestClass");
    emailConstraint.validate(go, go.getProperty("email"), errors);
    assertTrue(errors.hasErrors());
    go.setProperty("email", "valid@email.com");
    errors = new BindException(go, "TestClass");
    emailConstraint.validate(go, go.getProperty("email"), errors);
    assertFalse(errors.hasErrors());
}
Also used : GrailsDomainClass(grails.core.GrailsDomainClass) BindException(org.springframework.validation.BindException) DefaultGrailsApplication(grails.core.DefaultGrailsApplication) GroovyObject(groovy.lang.GroovyObject) GroovyClassLoader(groovy.lang.GroovyClassLoader) Errors(org.springframework.validation.Errors) DefaultGrailsApplication(grails.core.DefaultGrailsApplication) GrailsApplication(grails.core.GrailsApplication) Iterator(java.util.Iterator) Collection(java.util.Collection) GrailsDomainClass(grails.core.GrailsDomainClass) Map(java.util.Map)

Example 2 with DefaultGrailsApplication

use of grails.core.DefaultGrailsApplication in project grails-core by grails.

the class ConstraintsEvaluatingPropertyTests method testNullableConstraint.

/**
     * Test that static constraints work
     */
@SuppressWarnings("rawtypes")
public void testNullableConstraint() throws Exception {
    GroovyClassLoader gcl = new GroovyClassLoader();
    gcl.parseClass("package org.grails.validation\n" + "@grails.persistence.Entity class Book {\n" + "   Long id\n" + "   Long version\n" + "   String title\n" + "   String description\n" + "   Author author\n" + "   Author assistent\n" + "   Set chapters\n" + "   Map remarks\n" + "   static hasMany = [chapters:Chapter]\n" + "   static constraints = {\n" + "      description(nullable: true)\n" + "      assistent(nullable: true)\n" + "   }\n" + "}\n" + "@grails.persistence.Entity class Author {\n" + "   Long id\n" + "   Long version\n" + "   String name\n" + "}\n" + "@grails.persistence.Entity class Chapter {\n" + "   Long id\n" + "   Long version\n" + "   String text\n" + "}");
    GrailsApplication ga = new DefaultGrailsApplication(gcl.getLoadedClasses());
    ga.initialise();
    new MappingContextBuilder(ga).build(gcl.getLoadedClasses());
    GrailsDomainClass bookClass = (GrailsDomainClass) ga.getArtefact(DomainClassArtefactHandler.TYPE, "org.grails.validation.Book");
    Map constraints = bookClass.getConstrainedProperties();
    Constrained p = (Constrained) constraints.get("title");
    assertFalse("Title property should be required", p.isNullable());
    p = (Constrained) constraints.get("description");
    assertTrue("Description property should be optional", p.isNullable());
    p = (Constrained) constraints.get("author");
    assertFalse("Author property should be required", p.isNullable());
    p = (Constrained) constraints.get("assistent");
    assertTrue("Assistent property should be optional", p.isNullable());
    // Test that Collections and Maps are nullable by default
    p = (Constrained) constraints.get("chapters");
    assertTrue("Chapters property should be optional", p.isNullable());
    p = (Constrained) constraints.get("remarks");
    assertTrue("Remarks property should be optional", p.isNullable());
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader) Constrained(grails.gorm.validation.Constrained) GrailsApplication(grails.core.GrailsApplication) DefaultGrailsApplication(grails.core.DefaultGrailsApplication) GrailsDomainClass(grails.core.GrailsDomainClass) DefaultGrailsApplication(grails.core.DefaultGrailsApplication) Map(java.util.Map)

Example 3 with DefaultGrailsApplication

use of grails.core.DefaultGrailsApplication in project grails-core by grails.

the class ConstraintsEvaluatingPropertyTests method testGet.

/*
     * Test method for 'ConstraintsDynamicProperty.get(Object)'
     */
@SuppressWarnings("rawtypes")
public void testGet() throws Exception {
    GroovyClassLoader gcl = new GroovyClassLoader();
    Class<?> groovyClass = gcl.parseClass("package org.grails.validation\n" + "class Test {\n" + // WE NEED this even though GORM 2 doesn't, as we're not a "domain" class within grails-app
    "   Long id\n" + // WE NEED this even though GORM 2 doesn't, as we're not a "domain" class within grails-app
    "   Long version\n" + " String name\n" + "}");
    GrailsApplication ga = new DefaultGrailsApplication(groovyClass);
    ga.initialise();
    new MappingContextBuilder(ga).build(groovyClass);
    GrailsDomainClass domainClass = (GrailsDomainClass) ga.getArtefact(DomainClassArtefactHandler.TYPE, groovyClass.getName());
    Map constraints = domainClass.getConstrainedProperties();
    assertNotNull(constraints);
    assertFalse(constraints.isEmpty());
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader) GrailsApplication(grails.core.GrailsApplication) DefaultGrailsApplication(grails.core.DefaultGrailsApplication) GrailsDomainClass(grails.core.GrailsDomainClass) DefaultGrailsApplication(grails.core.DefaultGrailsApplication) Map(java.util.Map)

Example 4 with DefaultGrailsApplication

use of grails.core.DefaultGrailsApplication in project grails-core by grails.

the class HeirarchyDomainClassTests method testClassHeirarchy.

public void testClassHeirarchy() throws Exception {
    GroovyClassLoader gcl = new GroovyClassLoader();
    gcl.parseClass("@grails.persistence.Entity class Super { Long id;Long version;}\n" + "class Sub extends Super { }\n" + "class Sub2 extends Sub { }");
    GrailsApplication ga = new DefaultGrailsApplication(gcl.getLoadedClasses(), gcl);
    ga.initialise();
    new MappingContextBuilder(ga).build(gcl.getLoadedClasses());
    GrailsDomainClass gdc1 = (GrailsDomainClass) ga.getArtefact(DomainClassArtefactHandler.TYPE, "Super");
    assertNotNull(gdc1);
    assertTrue(gdc1.isRoot());
    assertEquals(2, gdc1.getSubClasses().size());
    assertFalse(gdc1.getPropertyByName("id").isInherited());
    GrailsDomainClass gdc2 = (GrailsDomainClass) ga.getArtefact(DomainClassArtefactHandler.TYPE, "Sub");
    assertFalse(gdc2.isRoot());
    assertEquals(1, gdc2.getSubClasses().size());
    assertTrue(gdc2.getPropertyByName("id").isInherited());
    GrailsDomainClass gdc3 = (GrailsDomainClass) ga.getArtefact(DomainClassArtefactHandler.TYPE, "Sub2");
    assertFalse(gdc3.isRoot());
    assertEquals(0, gdc3.getSubClasses().size());
    assertTrue(gdc3.getPropertyByName("id").isInherited());
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader) GrailsApplication(grails.core.GrailsApplication) DefaultGrailsApplication(grails.core.DefaultGrailsApplication) GrailsDomainClass(grails.core.GrailsDomainClass) DefaultGrailsApplication(grails.core.DefaultGrailsApplication) MappingContextBuilder(org.grails.core.support.MappingContextBuilder)

Example 5 with DefaultGrailsApplication

use of grails.core.DefaultGrailsApplication in project grails-core by grails.

the class DefaultGrailsPluginManagerTests method testDependenciesWithDelayedLoadingWithVersionRangeStrings.

/**
 * Test the known 1.0.2 failure where:
 *
 * mail 0.3 = has no deps
 * quartz 0.3-SNAPSHOT: loadAfter = ['core', 'hibernate']
 * emailconfirmation 0.4: dependsOn = [quartz:'0.3 > *', mail: '0.2 > *']
 *
 * ...and emailconfirmation is NOT loaded first.
 */
@Test
@SuppressWarnings("rawtypes")
public void testDependenciesWithDelayedLoadingWithVersionRangeStrings() {
    GroovyClassLoader gcl = new GroovyClassLoader();
    // These are defined in a specific order so that the one with the range dependencies
    // is the first in the list, and its dependencies load after
    first = gcl.parseClass("class FirstGrailsPlugin {\n" + "def version = \"0.4\"\n" + "def dependsOn = [second:'0.3 > *', third:'0.2 > *']\n" + "}");
    second = gcl.parseClass("class SecondGrailsPlugin {\n" + "def version = \"0.3\"\n" + "def dependsOn = [:]\n" + "}");
    third = gcl.parseClass("class ThirdGrailsPlugin {\n" + "def version = \"0.3-SNAPSHOT\"\n" + "def loadAfter = ['core', 'hibernate']\n" + "}");
    GrailsApplication app = new DefaultGrailsApplication(new Class[] {}, gcl);
    GenericApplicationContext parent = new GenericApplicationContext();
    parent.getDefaultListableBeanFactory().registerSingleton(GrailsApplication.APPLICATION_ID, app);
    DefaultGrailsPluginManager manager = new DefaultGrailsPluginManager(new Class[] { first, second, third }, app);
    manager.setParentApplicationContext(parent);
    manager.setPluginFilter(new IncludingPluginFilter("dataSource", "first", "second", "third"));
    manager.loadPlugins();
    List pluginList = manager.getPluginList();
    assertNotNull(manager.getGrailsPlugin("first"));
    assertNotNull(manager.getGrailsPlugin("second"));
    // dataSource depends on core
    assertNotNull(manager.getGrailsPlugin("core"));
    // third depends on i18n
    assertNotNull(manager.getGrailsPlugin("third"));
    assertEquals(5, pluginList.size(), "Expected plugins not loaded. Expected " + 5 + " but got " + pluginList);
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) IncludingPluginFilter(org.grails.plugins.IncludingPluginFilter) GrailsApplication(grails.core.GrailsApplication) DefaultGrailsApplication(grails.core.DefaultGrailsApplication) List(java.util.List) ArrayList(java.util.ArrayList) DefaultGrailsApplication(grails.core.DefaultGrailsApplication) Test(org.junit.jupiter.api.Test)

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