Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch '3.0.x'

* 3.0.x:
  Fixes #676 by adding datastore event listeners for tests
  Test case for #676 (GRAILS-11855). The fix is in grails-data-mapping.
commit 23461301a310956df6c8f7c49f12d189b906f55e 2 parents 1e56da5 + 6aea5bb
@bobbywarner bobbywarner authored
View
8 grails-plugin-testing/src/main/groovy/grails/test/mixin/domain/DomainClassUnitTestMixin.groovy
@@ -28,11 +28,14 @@ import org.grails.plugins.web.controllers.ControllersGrailsPlugin
import org.grails.validation.ConstraintEvalUtils
import org.grails.datastore.gorm.GormEnhancer
import org.grails.datastore.gorm.GormStaticApi
+import org.grails.datastore.gorm.events.AutoTimestampEventListener
+import org.grails.datastore.gorm.events.DomainEventListener
import org.grails.datastore.gorm.finders.FinderMethod
import org.grails.datastore.mapping.core.Datastore
import org.grails.datastore.mapping.model.PersistentEntity
import org.grails.datastore.mapping.query.api.BuildableCriteria
import org.grails.datastore.mapping.simple.SimpleMapDatastore
+import org.springframework.context.ConfigurableApplicationContext
import org.springframework.transaction.PlatformTransactionManager
import org.springframework.validation.Validator
@@ -73,7 +76,10 @@ class DomainClassUnitTestMixin extends GrailsUnitTestMixin {
}
SimpleMapDatastore getSimpleDatastore() {
- grailsApplication.mainContext.getBean(SimpleMapDatastore)
+ SimpleMapDatastore datastore = grailsApplication.mainContext.getBean(SimpleMapDatastore)
+ ((ConfigurableApplicationContext) applicationContext).addApplicationListener new DomainEventListener(datastore)
+ ((ConfigurableApplicationContext) applicationContext).addApplicationListener new AutoTimestampEventListener(datastore)
+ datastore
}
PlatformTransactionManager getTransactionManager() {
View
36 grails-test-suite-persistence/src/test/groovy/grails/test/mixin/domain/SaveDomainSpec.groovy
@@ -0,0 +1,36 @@
+package grails.test.mixin.domain
+
+import grails.persistence.Entity
+import grails.test.mixin.TestFor
+import spock.lang.Specification
+
+@TestFor(Person)
+class SaveDomainSpec extends Specification {
+
+ void 'test dateCreated and lastUpdated populated'() {
+ given:
+ Person person = new Person(name: 'Bobby')
+
+ when:
+ person.save(flush: true)
+
+ then:
+ person.dateCreated != null
+ person.lastUpdated != null
+ person.dateCreated == person.lastUpdated
+
+ when:
+ person.name = 'Bobby Updated'
+ person.save(flush: true)
+
+ then:
+ person.lastUpdated > person.dateCreated
+ }
+}
+
+@Entity
+class Person {
+ String name
+ Date dateCreated
+ Date lastUpdated
+}
Please sign in to comment.
Something went wrong with that request. Please try again.