You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@grails.apache.org by bo...@apache.org on 2026/04/12 00:37:18 UTC

(grails-core) 07/17: docs: Update JACOCO_COVERAGE_VIOLATIONS.md and improve HibernateBasicPropertySpec coverage

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

borinquenkid pushed a commit to branch 8.0.x-hibernate7-dev
in repository https://gitbox.apache.org/repos/asf/grails-core.git

commit 993d8ca8da5546d0d2bba831fe8367cb077d3525
Author: Walter Duque de Estrada <wb...@mac.com>
AuthorDate: Sat Apr 11 16:58:53 2026 -0500

    docs: Update JACOCO_COVERAGE_VIOLATIONS.md and improve HibernateBasicPropertySpec coverage
---
 .../hibernate/HibernateBasicPropertySpec.groovy    | 108 +++++++++++++++++++++
 1 file changed, 108 insertions(+)

diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateBasicPropertySpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateBasicPropertySpec.groovy
index 385f2b9e63..c621bf9a9d 100644
--- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateBasicPropertySpec.groovy
+++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateBasicPropertySpec.groovy
@@ -20,6 +20,13 @@ package org.grails.orm.hibernate.cfg.domainbinding.hibernate
 
 import grails.gorm.annotation.Entity
 import grails.gorm.specs.HibernateGormDatastoreSpec
+import org.hibernate.MappingException
+import org.grails.datastore.mapping.model.PersistentEntity
+import org.grails.datastore.mapping.model.PropertyMapping
+import org.grails.datastore.mapping.model.ClassMapping
+import org.grails.datastore.mapping.reflect.EntityReflector
+import org.grails.orm.hibernate.cfg.PropertyConfig
+import java.lang.annotation.RetentionPolicy
 
 class HibernateBasicPropertySpec extends HibernateGormDatastoreSpec {
 
@@ -31,6 +38,7 @@ class HibernateBasicPropertySpec extends HibernateGormDatastoreSpec {
         given:
         def personEntity = (HibernatePersistentEntity) getMappingContext().getPersistentEntity(HBPPerson.name)
         def property = (HibernateBasicProperty) personEntity.getPropertyByName("tags")
+        def original = property.getHibernateCollection()
         property.setHibernateCollection(null)
 
         when:
@@ -39,6 +47,9 @@ class HibernateBasicPropertySpec extends HibernateGormDatastoreSpec {
         then:
         def e = thrown(org.hibernate.MappingException)
         e.message.contains("Hibernate Collection has not been initialized")
+        
+        cleanup:
+        property.setHibernateCollection(original)
     }
 
     def "test setCollection with path configures metadata"() {
@@ -60,6 +71,7 @@ class HibernateBasicPropertySpec extends HibernateGormDatastoreSpec {
         mockCollection.getFetchMode() == property.getFetchMode()
         mockCollection.getBatchSize() == property.getBatchSize()
     }
+
     def "getElementTypeName returns the Hibernate type name for the element type"() {
         given:
         def personEntity = (HibernatePersistentEntity) getMappingContext().getPersistentEntity(HBPPerson.name)
@@ -68,6 +80,102 @@ class HibernateBasicPropertySpec extends HibernateGormDatastoreSpec {
         expect:
         property.getElementTypeName() == 'java.lang.String'
     }
+
+    // ─── Tests using a stub to reach deep logic ──────────────────────────────
+
+    def "isUserButNotCollectionType logic"() {
+        given:
+        def entity = (GrailsHibernatePersistentEntity) getMappingContext().getPersistentEntity(HBPPerson.name)
+        def config = new PropertyConfig()
+        def assoc = new TestHibernateBasicProperty(entity, String, config)
+
+        when:
+        config.type = type
+
+        then:
+        assoc.isUserButNotCollectionType() == result
+
+        where:
+        type | result
+        null | false
+        String | true
+        org.hibernate.usertype.UserCollectionType | false
+    }
+
+    def "isEnumType coverage"() {
+        given:
+        def entity = (GrailsHibernatePersistentEntity) getMappingContext().getPersistentEntity(HBPPerson.name)
+        def assoc = new TestHibernateBasicProperty(entity, type, null)
+
+        expect:
+        assoc.isEnumType() == result
+
+        where:
+        type | result
+        String | false
+        RetentionPolicy | true
+    }
+
+    def "getTypeName priority logic"() {
+        given:
+        def entity = (GrailsHibernatePersistentEntity) getMappingContext().getPersistentEntity(HBPPerson.name)
+        def assoc = new TestHibernateBasicProperty(entity, String, null)
+        
+        def config1 = new PropertyConfig(type: "custom")
+        def mapping1 = Mock(org.grails.orm.hibernate.cfg.Mapping)
+        
+        def config2 = new PropertyConfig(type: null)
+        def mapping2 = Mock(org.grails.orm.hibernate.cfg.Mapping)
+        
+        def config3 = new PropertyConfig(type: null)
+        def mapping3 = Mock(org.grails.orm.hibernate.cfg.Mapping)
+        
+        def config4 = new PropertyConfig(type: null)
+
+        expect: "Config priority"
+        assoc.getTypeName(String, config1, mapping1) == "custom"
+
+        when: "Mapping priority"
+        def res2 = assoc.getTypeName(String, config2, mapping2)
+        
+        then:
+        1 * mapping2.getTypeName(String) >> "mapped"
+        res2 == "mapped"
+
+        when: "Neither have it"
+        def res3 = assoc.getTypeName(String, config3, mapping3)
+        
+        then:
+        1 * mapping3.getTypeName(String) >> null
+        res3 == String.name
+
+        when: "Mapping is null"
+        def res4 = assoc.getTypeName(String, config4, null)
+        
+        then:
+        res4 == String.name
+    }
+
+    static class TestHibernateBasicProperty extends HibernateBasicProperty {
+        Class typeField
+        PropertyConfig mappedFormField
+
+        TestHibernateBasicProperty(GrailsHibernatePersistentEntity entity, Class type, PropertyConfig mappedForm) {
+            super(entity, entity.getMappingContext(), new java.beans.PropertyDescriptor("name", HBPPerson, "getName", null))
+            this.typeField = type
+            this.mappedFormField = mappedForm
+        }
+
+        @Override Class getType() { typeField ?: super.getType() }
+        @Override PropertyConfig getMappedForm() { mappedFormField ?: super.getMappedForm() }
+
+        @Override PropertyMapping<PropertyConfig> getMapping() { 
+            return new PropertyMapping<PropertyConfig>() {
+                @Override ClassMapping getClassMapping() { null }
+                @Override PropertyConfig getMappedForm() { mappedFormField }
+            }
+        }
+    }
 }
 
 @Entity