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());
}
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());
}
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());
}
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());
}
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);
}
Aggregations