|
17 | 17 | import junit.framework.TestCase; |
18 | 18 |
|
19 | 19 | import java.util.Date; |
| 20 | +import java.util.Map; |
20 | 21 | import java.net.URL; |
21 | 22 | import java.net.URI; |
| 23 | +import org.codehaus.groovy.grails.orm.hibernate.cfg.DefaultGrailsDomainConfiguration; |
| 24 | +import org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsDomainBinder; |
| 25 | +import org.hibernate.cfg.ImprovedNamingStrategy; |
| 26 | +import org.codehaus.groovy.grails.validation.ConstrainedProperty; |
| 27 | +import org.codehaus.groovy.grails.validation.NullableConstraint; |
| 28 | +import groovy.lang.ExpandoMetaClass; |
| 29 | +import groovy.lang.GroovyClassLoader; |
22 | 30 |
|
23 | 31 | /** |
24 | 32 | * Tests for the GrailsDomainConfigurationUtil class |
|
32 | 40 | */ |
33 | 41 | public class GrailsDomainConfigurationUtilTests extends TestCase { |
34 | 42 |
|
| 43 | + @Override |
| 44 | + protected void setUp() throws Exception { |
| 45 | + super.setUp(); |
| 46 | + ExpandoMetaClass.enableGlobally(); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + protected void tearDown() throws Exception { |
| 51 | + super.tearDown(); |
| 52 | + GrailsDomainBinder.namingStrategy = ImprovedNamingStrategy.INSTANCE; |
| 53 | + } |
| 54 | + |
35 | 55 | public void testIsBasicType() { |
36 | 56 | assertTrue(GrailsDomainConfigurationUtil.isBasicType(boolean.class)); |
37 | 57 | assertTrue(GrailsDomainConfigurationUtil.isBasicType(long.class)); |
@@ -67,6 +87,42 @@ public void testIsBasicType() { |
67 | 87 | assertTrue(GrailsDomainConfigurationUtil.isBasicType(Character[].class)); |
68 | 88 | assertTrue(GrailsDomainConfigurationUtil.isBasicType(Double[].class)); |
69 | 89 | assertTrue(GrailsDomainConfigurationUtil.isBasicType(Float[].class)); |
70 | | - assertTrue(GrailsDomainConfigurationUtil.isBasicType(Byte[].class)); |
| 90 | + assertTrue(GrailsDomainConfigurationUtil.isBasicType(Byte[].class)); |
| 91 | + } |
| 92 | + |
| 93 | + public void testEvaluateConstraintsInsertableShouldBeNullableByDefault() { |
| 94 | + GroovyClassLoader cl = new GroovyClassLoader(); |
| 95 | + GrailsDomainClass domainClass = new DefaultGrailsDomainClass( |
| 96 | + cl.parseClass( |
| 97 | + "class TestInsertableUpdateableDomain {\n" + |
| 98 | + " Long id \n" + |
| 99 | + " Long version \n" + |
| 100 | + " String testString1 \n" + |
| 101 | + " String testString2 \n"+ |
| 102 | + "\n" + |
| 103 | + " static mapping = {\n" + |
| 104 | + " testString1 insertable:false \n" + |
| 105 | + " testString2 max:50 \n" + |
| 106 | + " }\n" + |
| 107 | + "}") |
| 108 | + ); |
| 109 | + |
| 110 | + DefaultGrailsDomainConfiguration config = getDomainConfig(cl, |
| 111 | + new Class[] { domainClass.getClazz() }); |
| 112 | + Map<String, ConstrainedProperty> mapping = GrailsDomainConfigurationUtil.evaluateConstraints(domainClass.getClazz(),domainClass.getProperties(),null); |
| 113 | + ConstrainedProperty property1 = mapping.get("testString1"); |
| 114 | + assertTrue("constraint was not nullable and should be", ((NullableConstraint)property1.getAppliedConstraint(ConstrainedProperty.NULLABLE_CONSTRAINT)).isNullable()); |
| 115 | + ConstrainedProperty property2 = mapping.get("testString2"); |
| 116 | + assertFalse("constraint was nullable and shouldn't be", ((NullableConstraint)property2.getAppliedConstraint(ConstrainedProperty.NULLABLE_CONSTRAINT)).isNullable()); |
| 117 | + |
| 118 | + } |
| 119 | + |
| 120 | + private DefaultGrailsDomainConfiguration getDomainConfig(GroovyClassLoader cl, Class<?>[] classes) { |
| 121 | + GrailsApplication grailsApplication = new DefaultGrailsApplication(classes, cl); |
| 122 | + grailsApplication.initialise(); |
| 123 | + DefaultGrailsDomainConfiguration config = new DefaultGrailsDomainConfiguration(); |
| 124 | + config.setGrailsApplication(grailsApplication); |
| 125 | + config.buildMappings(); |
| 126 | + return config; |
71 | 127 | } |
72 | 128 | } |
0 commit comments