Permalink
Browse files

simplify test of domain class setProperties and getProperties

  • Loading branch information...
1 parent a0afc78 commit 993f9df27fc6ddcc39caef85c6405b59e4518753 @jeffbrown jeffbrown committed Jul 17, 2014
@@ -1,117 +0,0 @@
-package org.codehaus.groovy.grails.compiler.web
-
-import grails.spring.WebBeanBuilder
-import grails.util.GrailsWebUtil
-
-import grails.core.DefaultGrailsApplication
-import org.grails.compiler.web.ControllerDomainTransformer
-import org.grails.core.artefact.DomainClassArtefactHandler
-import grails.core.GrailsApplication
-import org.grails.core.metaclass.MetaClassEnhancer
-import grails.compiler.ast.ClassInjector
-import org.grails.compiler.injection.GrailsAwareClassLoader
-import org.grails.plugins.web.controllers.api.ControllersDomainBindingApi
-import org.grails.web.servlet.mvc.GrailsWebRequest
-import org.springframework.web.context.WebApplicationContext
-import org.springframework.web.context.request.RequestContextHolder
-
-import spock.lang.Specification
-
-/**
- * Tests for ControllerDomainTransformer
- */
-class ControllerDomainTransformerSpec extends Specification {
-
- void setup() {
- GrailsWebUtil.bindMockWebRequest(applicationContext)
- }
-
- void cleanup() {
- RequestContextHolder.setRequestAttributes(null)
- }
-
- void "Test binding constructor adding via AST"() {
- given:
- def cls = getTestClass()
-
- when:
- def test = cls.newInstance(age:"10")
-
- then:
- test.age == 10
- }
-
- void "Test setProperties method added via AST"() {
- given:
- def cls = getTestClass()
-
- when:
- def test = cls.newInstance()
- test.properties = [age:"10"]
-
- then:
- test.age == 10
- }
-
- void "Test getProperties method added via AST"() {
- given:
- def cls = getTestClass()
-
- when:
- def test = cls.newInstance()
- test.properties['age', 'name'] = [age:"10"]
-
- then:
- test.age == 10
- }
-
- void "Test transforming a @grails.persistence.Entity marked class doesn't generate duplication methods"() {
- when:
- def cls = classLoader.parseClass('''
-@grails.persistence.Entity
-class TestEntity {
- Long id
-}
- ''')
-
- then:
- cls
- }
-
- Class getTestClass() {
- def cls = classLoader.parseClass('''
- class Test {
- Long id
- Long version
- Integer age
- }
- ''')
- GrailsApplication application = GrailsWebRequest.lookup().applicationContext.grailsApplication
- application.initialise()
- application.addArtefact(DomainClassArtefactHandler.TYPE,cls)
-
- MetaClassEnhancer enhancer = new MetaClassEnhancer()
- enhancer.addApi(new ControllersDomainBindingApi())
- enhancer.enhance(cls.metaClass)
- return cls
- }
-
- GroovyClassLoader getClassLoader() {
- def gcl = new GrailsAwareClassLoader()
- def transformer = new ControllerDomainTransformer() {
- @Override
- boolean shouldInject(URL url) { true }
- }
- gcl.classInjectors = [transformer] as ClassInjector[]
-
- return gcl
- }
-
- WebApplicationContext getApplicationContext() {
- def bb = new WebBeanBuilder()
- bb.beans {
- grailsApplication(DefaultGrailsApplication)
- }
- bb.createApplicationContext()
- }
-}
@@ -0,0 +1,43 @@
+package org.grails.domain.compiler
+
+import grails.persistence.Entity
+
+import org.grails.plugins.web.controllers.api.ControllersDomainBindingApi
+
+import spock.lang.Specification
+
+
+class DomainPropertiesAccessorSpec extends Specification {
+
+ void "Test binding constructor adding via AST"() {
+ when:
+ def test = new TestDomain(age:"10")
+
+ then:
+ test.age == 10
+ }
+
+ void "Test setProperties method added via AST"() {
+ when:
+ def test = new TestDomain()
+ test.properties = [age:"10"]
+
+ then:
+ test.age == 10
+ }
+
+ void "Test getProperties method added via AST"() {
+ when:
+ def test = new TestDomain()
+ test.properties['age', 'name'] = [age:"10"]
+
+ then:
+ test.age == 10
+ }
+}
+
+@Entity
+class TestDomain {
+ Integer age
+}
+

0 comments on commit 993f9df

Please sign in to comment.