All Downloads are FREE. Search and download functionalities are using the official Maven repository.

grails.gorm.tests.SessionCreationEventSpec.groovy Maven / Gradle / Ivy

There is a newer version: 2023.0.1
Show newest version
package grails.gorm.tests

import org.springframework.context.event.SmartApplicationListener
import org.springframework.context.ApplicationEvent
import org.grails.datastore.mapping.core.SessionCreationEvent
import org.grails.datastore.mapping.core.Session
import spock.lang.Ignore

/**
 * Test case that session creation events are fired.
 */
@Ignore
class SessionCreationEventSpec extends GormDatastoreSpec {

    Listener listener

    def setup() {
        listener = new Listener()
        session.datastore.applicationContext.addApplicationListener(listener)
    }

    void "test event for new session"() {
        when:"Using existing session"
        TestEntity.withSession { s ->
            s.flush()
        }
        then:
        listener.events.empty

        when:"Creating new session"
        def newSession = null
        def isDatastoreSession = false
        TestEntity.withNewSession { s ->
            newSession = s
            isDatastoreSession = s instanceof Session
        }
        then:
        !isDatastoreSession || listener.events.size() == 1
        !isDatastoreSession || listener.events[0].session == newSession
    }


    static class Listener implements SmartApplicationListener {
        List events = []

        @Override
        int getOrder() {
            Integer.MAX_VALUE / 2
        }

        @Override
        void onApplicationEvent(ApplicationEvent event) {
            events << event
        }

        @Override
        boolean supportsSourceType(Class sourceType) {
            return true
        }

        @Override
        boolean supportsEventType(Class eventType) {
            return eventType == SessionCreationEvent
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy