-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
grails/grails-data-mapping#1265 Hibernate5 test to update from the Ab…
…stractPersistentEventListener
- Loading branch information
1 parent
6e87e93
commit 458b737
Showing
1 changed file
with
79 additions
and
0 deletions.
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
...e5/src/test/groovy/grails/gorm/tests/dirtychecking/HibernateUpdateFromListenerSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package grails.gorm.tests.dirtychecking | ||
|
||
import grails.gorm.transactions.Rollback | ||
import org.grails.datastore.gorm.events.ConfigurableApplicationEventPublisher | ||
import org.grails.datastore.mapping.core.Datastore | ||
import org.grails.datastore.mapping.engine.event.AbstractPersistenceEvent | ||
import org.grails.datastore.mapping.engine.event.AbstractPersistenceEventListener | ||
import org.grails.datastore.mapping.engine.event.PreInsertEvent | ||
import org.grails.datastore.mapping.engine.event.PreUpdateEvent | ||
import org.grails.orm.hibernate.HibernateDatastore | ||
import org.springframework.context.ApplicationEvent | ||
import org.springframework.context.ApplicationEventPublisher | ||
import org.springframework.context.ConfigurableApplicationContext | ||
import org.springframework.transaction.PlatformTransactionManager | ||
import spock.lang.AutoCleanup | ||
import spock.lang.Shared | ||
import spock.lang.Specification | ||
import spock.util.concurrent.PollingConditions | ||
|
||
class HibernateUpdateFromListenerSpec extends Specification { | ||
|
||
@Shared | ||
@AutoCleanup | ||
HibernateDatastore datastore = new HibernateDatastore(Person) | ||
@Shared PlatformTransactionManager transactionManager = datastore.transactionManager | ||
|
||
PersonSaveOrUpdatePersistentEventListener listener | ||
|
||
void setup() { | ||
listener = new PersonSaveOrUpdatePersistentEventListener(datastore) | ||
ApplicationEventPublisher publisher = datastore.applicationEventPublisher | ||
if (publisher instanceof ConfigurableApplicationEventPublisher) { | ||
((ConfigurableApplicationEventPublisher) publisher).addApplicationListener(listener) | ||
} else if (publisher instanceof ConfigurableApplicationContext) { | ||
((ConfigurableApplicationContext) publisher).addApplicationListener(listener) | ||
} | ||
} | ||
|
||
@Rollback | ||
void "test the changes made from the listener are saved"() { | ||
when: | ||
Person danny = new Person(name: "Danny", occupation: "manager").save() | ||
|
||
then: | ||
new PollingConditions().eventually {listener.isExecuted && Person.count()} | ||
|
||
when: | ||
datastore.currentSession.flush() | ||
datastore.currentSession.clear() | ||
danny = Person.get(danny.id) | ||
|
||
then: | ||
danny.occupation | ||
danny.occupation.endsWith("listener") | ||
} | ||
|
||
static class PersonSaveOrUpdatePersistentEventListener extends AbstractPersistenceEventListener { | ||
|
||
boolean isExecuted | ||
|
||
protected PersonSaveOrUpdatePersistentEventListener(Datastore datastore) { | ||
super(datastore) | ||
} | ||
|
||
@Override | ||
protected void onPersistenceEvent(AbstractPersistenceEvent event) { | ||
if (event.entityObject instanceof Person) { | ||
Person person = (Person) event.entityObject | ||
person.occupation = person.occupation + " listener" | ||
} | ||
isExecuted = true | ||
} | ||
|
||
@Override | ||
boolean supportsEventType(Class<? extends ApplicationEvent> eventType) { | ||
return eventType == PreUpdateEvent || eventType == PreInsertEvent | ||
} | ||
} | ||
} |