This is an automated email from the ASF dual-hosted git repository. borinquenkid pushed a commit to branch test/abstract-datastore-initializer in repository https://gitbox.apache.org/repos/asf/grails-core.git
commit 6236d0cbff50dde5f05d500c0d174138152a65ab Author: Walter Duque de Estrada <[email protected]> AuthorDate: Thu Aug 13 14:56:58 2026 -0500 Fix H5 HibernateDatastoreSpringInitializer warnings and add PropertyResolver constructor coverage Fixes a real unchecked-cast issue in getPersistenceInterceptorClass() (it was missing the cast that the Hibernate7 sibling already has), and removes two genuinely dead members: the never-invoked getTestDbUrl() method and the defaultSessionFactoryBeanName property, whose value was never actually read by the sessionFactory bean registration it appeared to configure. Rather than silence the "unused constructor" warnings on the PropertyResolver-based constructors, adds direct tests for all three - they are exercised in production only through a dynamically-typed call site in HibernateGrailsPlugin, which static analysis can't resolve to a specific overload. Also swaps a couple of ad-hoc H2 URLs (introduced in the prior commit, one already merged for hibernate7) for the class's own TEST_DB_URL constant. Co-Authored-By: Claude Sonnet 5 <[email protected]> --- .../HibernateDatastoreSpringInitializer.groovy | 7 +-- .../HibernateDatastoreSpringInitializerSpec.groovy | 52 +++++++++++++++++++++- .../HibernateDatastoreSpringInitializerSpec.groovy | 2 +- 3 files changed, 53 insertions(+), 8 deletions(-) diff --git a/grails-data-hibernate5/grails-plugin/src/main/groovy/grails/orm/bootstrap/HibernateDatastoreSpringInitializer.groovy b/grails-data-hibernate5/grails-plugin/src/main/groovy/grails/orm/bootstrap/HibernateDatastoreSpringInitializer.groovy index 62727e13dc..b5ee5ab4f2 100644 --- a/grails-data-hibernate5/grails-plugin/src/main/groovy/grails/orm/bootstrap/HibernateDatastoreSpringInitializer.groovy +++ b/grails-data-hibernate5/grails-plugin/src/main/groovy/grails/orm/bootstrap/HibernateDatastoreSpringInitializer.groovy @@ -53,7 +53,6 @@ class HibernateDatastoreSpringInitializer extends AbstractDatastoreInitializer { public static final String TEST_DB_URL = 'jdbc:h2:mem:grailsDb;LOCK_TIMEOUT=10000;DB_CLOSE_DELAY=-1' String defaultDataSourceBeanName = ConnectionSource.DEFAULT - String defaultSessionFactoryBeanName = SESSION_FACTORY_BEAN_NAME Set<String> dataSources = [defaultDataSourceBeanName] as Set<String> boolean enableReload = false boolean grailsPlugin = false @@ -107,7 +106,7 @@ class HibernateDatastoreSpringInitializer extends AbstractDatastoreInitializer { @Override protected Class<AbstractDatastorePersistenceContextInterceptor> getPersistenceInterceptorClass() { - getClass().classLoader.loadClass('org.grails.plugin.hibernate.support.HibernatePersistenceContextInterceptor') + getClass().classLoader.loadClass('org.grails.plugin.hibernate.support.HibernatePersistenceContextInterceptor') as Class<AbstractDatastorePersistenceContextInterceptor> } /** @@ -121,10 +120,6 @@ class HibernateDatastoreSpringInitializer extends AbstractDatastoreInitializer { return applicationContext } - protected String getTestDbUrl() { - TEST_DB_URL - } - @CompileStatic ApplicationContext configureForDataSource(DataSource dataSource) { GenericApplicationContext applicationContext = createApplicationContext() diff --git a/grails-data-hibernate5/grails-plugin/src/test/groovy/grails/orm/bootstrap/HibernateDatastoreSpringInitializerSpec.groovy b/grails-data-hibernate5/grails-plugin/src/test/groovy/grails/orm/bootstrap/HibernateDatastoreSpringInitializerSpec.groovy index f2e542589b..bd37fd7fe6 100644 --- a/grails-data-hibernate5/grails-plugin/src/test/groovy/grails/orm/bootstrap/HibernateDatastoreSpringInitializerSpec.groovy +++ b/grails-data-hibernate5/grails-plugin/src/test/groovy/grails/orm/bootstrap/HibernateDatastoreSpringInitializerSpec.groovy @@ -19,6 +19,7 @@ package grails.orm.bootstrap import grails.gorm.annotation.Entity +import org.grails.datastore.mapping.core.DatastoreUtils import org.grails.orm.hibernate.HibernateDatastore import org.hibernate.Session import org.hibernate.SessionFactory @@ -116,9 +117,58 @@ class HibernateDatastoreSpringInitializerSpec extends Specification{ Person.withNewSession { Person.count() == 0 } } + void "Test the PropertyResolver/Collection<Class> constructor bootstraps GORM"() { + given: "An initializer built from a PropertyResolver and a Collection of persistent classes" + def resolver = DatastoreUtils.createPropertyResolver([ + 'dataSource.url' : 'jdbc:h2:mem:propertyResolverCollectionCtor;LOCK_TIMEOUT=10000', + 'hibernate.hbm2ddl.auto': 'create' + ]) + def datastoreInitializer = new HibernateDatastoreSpringInitializer(resolver, [Person] as Collection<Class>) + + when: "the application is configured" + applicationContext = (ConfigurableApplicationContext) datastoreInitializer.configure() + + then: "GORM is bootstrapped with the given entity" + applicationContext.getBean(HibernateDatastore).mappingContext.getPersistentEntity(Person.name) != null + Person.withNewSession { Person.count() == 0 } + } + + void "Test the PropertyResolver/Class... constructor bootstraps GORM"() { + given: "An initializer built from a PropertyResolver and an array of persistent classes" + def resolver = DatastoreUtils.createPropertyResolver([ + 'dataSource.url' : 'jdbc:h2:mem:propertyResolverClassCtor;LOCK_TIMEOUT=10000', + 'hibernate.hbm2ddl.auto': 'create' + ]) + def datastoreInitializer = new HibernateDatastoreSpringInitializer(resolver, Person) + + when: "the application is configured" + applicationContext = (ConfigurableApplicationContext) datastoreInitializer.configure() + + then: "GORM is bootstrapped with the given entity" + applicationContext.getBean(HibernateDatastore).mappingContext.getPersistentEntity(Person.name) != null + Person.withNewSession { Person.count() == 0 } + } + + void "Test the PropertyResolver/String... packages constructor discovers entities via classpath scan"() { + given: "an initializer configured with a package name rather than explicit classes; the scan also picks up Book and Author, which require the 'books'/'moreBooks' datasources" + def resolver = DatastoreUtils.createPropertyResolver([ + 'dataSource.url' : 'jdbc:h2:mem:propertyResolverPackageCtor;LOCK_TIMEOUT=10000', + 'hibernate.hbm2ddl.auto': 'create', + 'dataSources.books.url' : 'jdbc:h2:mem:propertyResolverPackageCtorBooks;LOCK_TIMEOUT=10000', + 'dataSources.moreBooks.url': 'jdbc:h2:mem:propertyResolverPackageCtorMoreBooks;LOCK_TIMEOUT=10000' + ]) + def datastoreInitializer = new HibernateDatastoreSpringInitializer(resolver, Person.package.name) + + when: "the application is configured" + applicationContext = (ConfigurableApplicationContext) datastoreInitializer.configure() + + then: "the Person entity declared in the scanned package was discovered and mapped" + applicationContext.getBean(HibernateDatastore).mappingContext.getPersistentEntity(Person.name) != null + } + void "Test configureForDataSource bootstraps GORM around a pre-existing DataSource"() { given: "a DataSource created ahead of time" - def dataSource = new DriverManagerDataSource('jdbc:h2:mem:configureForDataSource;LOCK_TIMEOUT=10000', 'sa', '') + def dataSource = new DriverManagerDataSource(HibernateDatastoreSpringInitializer.TEST_DB_URL, 'sa', '') dataSource.driverClassName = 'org.h2.Driver' def datastoreInitializer = new HibernateDatastoreSpringInitializer(['hibernate.hbm2ddl.auto': 'create'], Person) diff --git a/grails-data-hibernate7/grails-plugin/src/test/groovy/grails/orm/bootstrap/HibernateDatastoreSpringInitializerSpec.groovy b/grails-data-hibernate7/grails-plugin/src/test/groovy/grails/orm/bootstrap/HibernateDatastoreSpringInitializerSpec.groovy index afa580120f..8fbeeb238a 100644 --- a/grails-data-hibernate7/grails-plugin/src/test/groovy/grails/orm/bootstrap/HibernateDatastoreSpringInitializerSpec.groovy +++ b/grails-data-hibernate7/grails-plugin/src/test/groovy/grails/orm/bootstrap/HibernateDatastoreSpringInitializerSpec.groovy @@ -116,7 +116,7 @@ class HibernateDatastoreSpringInitializerSpec extends Specification{ void "Test configureForDataSource bootstraps GORM around a pre-existing DataSource"() { given: "a DataSource created ahead of time" - def dataSource = new DriverManagerDataSource('jdbc:h2:mem:configureForDataSource;LOCK_TIMEOUT=10000', 'sa', '') + def dataSource = new DriverManagerDataSource(HibernateDatastoreSpringInitializer.TEST_DB_URL, 'sa', '') dataSource.driverClassName = 'org.h2.Driver' def datastoreInitializer = new HibernateDatastoreSpringInitializer(['hibernate.hbm2ddl.auto': 'create'], Person)
