This is an automated email from the ASF dual-hosted git repository.

borinquenkid pushed a commit to branch pr-15934-mongo-osiv-coverage
in repository https://gitbox.apache.org/repos/asf/grails-core.git

commit d054328fe19ec119af57d9af7e46e3ecde9f671e
Author: Walter Duque de Estrada <[email protected]>
AuthorDate: Thu Jul 9 16:42:04 2026 -0500

    test(grails-data-mongodb): add unit-testable Mongo OSIV coverage
    
    The Mongo OSIV regression coverage added in e737052 lives in
    grails-test-examples/mongodb/base, which requires a real Mongo already
    listening on localhost:27017 and can't be verified without that
    external dependency. grails-data-mongodb/core already has
    Testcontainers wired up via AutoStartedMongoSpec (auto-starts Mongo,
    needs only Docker), so add MongoOpenSessionInViewSpec there, exercising
    MongoDbDataStoreSpringInitializer.isWebApplicationRegistry() directly
    against a GenericWebApplicationContext with no dispatcherServlet bean
    definition present yet — the exact ordering the retimed plugin
    lifecycle now requires — plus a non-web control case.
    
    Co-Authored-By: Claude Sonnet 5 <[email protected]>
---
 grails-data-mongodb/core/build.gradle              |  6 ++
 .../bootstrap/MongoOpenSessionInViewSpec.groovy    | 82 ++++++++++++++++++++++
 2 files changed, 88 insertions(+)

diff --git a/grails-data-mongodb/core/build.gradle 
b/grails-data-mongodb/core/build.gradle
index af14d921b7..37099e2eff 100644
--- a/grails-data-mongodb/core/build.gradle
+++ b/grails-data-mongodb/core/build.gradle
@@ -132,6 +132,12 @@ dependencies {
     testImplementation 'org.apache.grails.testing:grails-testing-support-core'
     testImplementation 'org.spockframework:spock-core'
     testImplementation project(':grails-testing-support-mongodb')
+    testImplementation project(':grails-datastore-web'), {
+        // test: OpenSessionInViewInterceptor, spring-web's 
GenericWebApplicationContext
+    }
+    testImplementation 'jakarta.servlet:jakarta.servlet-api', {
+        // test: GenericWebApplicationContext requires ServletContext on the 
classpath
+    }
 
     testImplementation 'org.testcontainers:testcontainers'
     testImplementation 'org.testcontainers:testcontainers-mongodb'
diff --git 
a/grails-data-mongodb/core/src/test/groovy/grails/mongodb/bootstrap/MongoOpenSessionInViewSpec.groovy
 
b/grails-data-mongodb/core/src/test/groovy/grails/mongodb/bootstrap/MongoOpenSessionInViewSpec.groovy
new file mode 100644
index 0000000000..0c991037f3
--- /dev/null
+++ 
b/grails-data-mongodb/core/src/test/groovy/grails/mongodb/bootstrap/MongoOpenSessionInViewSpec.groovy
@@ -0,0 +1,82 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    https://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package grails.mongodb.bootstrap
+
+import org.apache.grails.testing.mongo.AutoStartedMongoSpec
+import org.grails.datastore.mapping.mongo.MongoDatastore
+import org.grails.datastore.mapping.mongo.config.MongoSettings
+import org.springframework.context.support.GenericApplicationContext
+import org.springframework.web.context.support.GenericWebApplicationContext
+
+/**
+ * Verifies the MongoDB open-session-in-view interceptor registers whenever 
the target registry is a
+ * {@code WebApplicationContext}, independent of whether a {@code 
dispatcherServlet} bean definition has
+ * been registered yet. This is the registration-order-independent branch of
+ * {@code AbstractDatastoreInitializer#isWebApplicationRegistry} that lets the 
interceptor keep registering
+ * once plugin beans drain before Spring Boot auto-configuration (which is 
what registers
+ * {@code dispatcherServlet}).
+ */
+class MongoOpenSessionInViewSpec extends AutoStartedMongoSpec {
+
+    void "the mongo open session in view interceptor registers in a web 
application context"() {
+        given: "the initializer targeting a WebApplicationContext with no 
dispatcherServlet bean definition"
+        def initializer = makeInitializer()
+        def applicationContext = new GenericWebApplicationContext()
+
+        when: "the context is configured and refreshed"
+        initializer.configureForBeanDefinitionRegistry(applicationContext)
+        applicationContext.refresh()
+
+        then: "the OSIV interceptor is registered"
+        applicationContext.containsBean('mongoOpenSessionInViewInterceptor')
+
+        cleanup:
+        applicationContext.getBean(MongoDatastore).destroy()
+        applicationContext.close()
+    }
+
+    void "the mongo open session in view interceptor is not registered outside 
a web application context (control)"() {
+        given: "the same initializer targeting a plain, non-web 
ApplicationContext"
+        def initializer = makeInitializer()
+        def applicationContext = new GenericApplicationContext()
+
+        when: "the context is configured and refreshed"
+        initializer.configureForBeanDefinitionRegistry(applicationContext)
+        applicationContext.refresh()
+
+        then: "the OSIV interceptor is not registered"
+        !applicationContext.containsBean('mongoOpenSessionInViewInterceptor')
+
+        cleanup:
+        applicationContext.getBean(MongoDatastore).destroy()
+        applicationContext.close()
+    }
+
+    private MongoDbDataStoreSpringInitializer makeInitializer() {
+        new MongoDbDataStoreSpringInitializer([
+                (MongoSettings.SETTING_HOST): mongoHost,
+                (MongoSettings.SETTING_PORT): mongoPort,
+        ]) {
+            @Override
+            protected Map<String, Class<?>> loadDataServices(String 
secondaryDatastore = null) {
+                [:]
+            }
+        }
+    }
+}

Reply via email to