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/07 18:10:15 UTC

(grails-core) 18/33: test: expand MappingSpec and add HibernateMappingKeywordSpec for 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 25395c920c8f64fef7f5eadf28e58390f55a0c05
Author: Walter Duque de Estrada <wb...@mac.com>
AuthorDate: Tue Apr 7 09:01:20 2026 -0500

    test: expand MappingSpec and add HibernateMappingKeywordSpec for coverage
    
    - Fix id(Map)/id(Closure) assertions to use .column not .name
    - Fix isJoinedSubclass test: setTablePerConcreteClass resets tablePerHierarchy via setter
    - Fix @Unroll title referencing undefined #name variable
    - All tests pass
    
    Co-authored-by: Copilot <22...@users.noreply.github.com>
---
 .../grails/orm/hibernate/cfg/MappingSpec.groovy    | 337 +++++++++++++++++++++
 .../hibernate/HibernateMappingKeywordSpec.groovy   | 110 +++++++
 2 files changed, 447 insertions(+)

diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/MappingSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/MappingSpec.groovy
index 2a5fc2d3f7..3c3813a5ed 100644
--- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/MappingSpec.groovy
+++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/MappingSpec.groovy
@@ -221,6 +221,343 @@ class MappingSpec extends HibernateGormDatastoreSpec {
         thrown(RuntimeException)
     }
 
+    // --- table() DSL ---
+
+    void "table(String) sets the table name on the mapping"() {
+        given:
+        Mapping mapping = new Mapping()
+
+        when:
+        mapping.table('my_table')
+
+        then:
+        mapping.tableName == 'my_table'
+    }
+
+    void "table(Closure) configures the Table via closure"() {
+        given:
+        Mapping mapping = new Mapping()
+
+        when:
+        mapping.table { name = 'closure_table' }
+
+        then:
+        mapping.tableName == 'closure_table'
+    }
+
+    void "table(Map) configures the Table via map"() {
+        given:
+        Mapping mapping = new Mapping()
+
+        when:
+        mapping.table(name: 'map_table')
+
+        then:
+        mapping.tableName == 'map_table'
+    }
+
+    void "setTableName delegates to table.name"() {
+        given:
+        Mapping mapping = new Mapping()
+
+        when:
+        mapping.tableName = 'direct_name'
+
+        then:
+        mapping.table.name == 'direct_name'
+    }
+
+    // --- id() DSL ---
+
+    void "id(Map) configures the identity from a map"() {
+        given:
+        Mapping mapping = new Mapping()
+
+        when:
+        mapping.id(column: 'my_id_col')
+
+        then:
+        (mapping.identity as HibernateSimpleIdentity).column == 'my_id_col'
+    }
+
+    void "id(Closure) configures the identity from a closure"() {
+        given:
+        Mapping mapping = new Mapping()
+
+        when:
+        mapping.id { column = 'closure_id' }
+
+        then:
+        (mapping.identity as HibernateSimpleIdentity).column == 'closure_id'
+    }
+
+    void "id(HibernateCompositeIdentity) replaces the identity"() {
+        given:
+        Mapping mapping = new Mapping()
+        HibernateCompositeIdentity composite = new HibernateCompositeIdentity(propertyNames: ['a', 'b'])
+
+        when:
+        mapping.id(composite)
+
+        then:
+        mapping.identity.is(composite)
+    }
+
+    // --- cache() DSL ---
+
+    void "cache(Closure) initialises CacheConfig and applies the closure"() {
+        given:
+        Mapping mapping = new Mapping()
+        assert mapping.cache == null
+
+        when:
+        mapping.cache { usage = CacheConfig.Usage.READ_ONLY }
+
+        then:
+        mapping.cache != null
+        mapping.cache.usage == CacheConfig.Usage.READ_ONLY
+    }
+
+    void "cache(Map) initialises CacheConfig and applies the map"() {
+        given:
+        Mapping mapping = new Mapping()
+
+        when:
+        mapping.cache(usage: 'read-only')
+
+        then:
+        mapping.cache != null
+    }
+
+    void "cache(String) sets cache usage and enables caching"() {
+        given:
+        Mapping mapping = new Mapping()
+
+        when:
+        mapping.cache('read-only')
+
+        then:
+        mapping.cache != null
+        mapping.cache.enabled
+        mapping.cache.usage == CacheConfig.Usage.READ_ONLY
+    }
+
+    // --- sort() DSL ---
+
+    void "sort(String, String) sets name and direction"() {
+        given:
+        Mapping mapping = new Mapping()
+
+        when:
+        mapping.sort('name', 'asc')
+
+        then:
+        mapping.sort.name == 'name'
+        mapping.sort.direction == 'asc'
+    }
+
+    void "sort(Map) sets namesAndDirections"() {
+        given:
+        Mapping mapping = new Mapping()
+
+        when:
+        mapping.sort(name: 'asc', age: 'desc')
+
+        then:
+        mapping.sort.namesAndDirections == [name: 'asc', age: 'desc']
+    }
+
+    // --- discriminator() DSL ---
+
+    void "discriminator(String) sets the discriminator value"() {
+        given:
+        Mapping mapping = new Mapping()
+
+        when:
+        mapping.discriminator('DOG')
+
+        then:
+        mapping.discriminator.value == 'DOG'
+    }
+
+    void "discriminator(Closure) applies closure to DiscriminatorConfig"() {
+        given:
+        Mapping mapping = new Mapping()
+
+        when:
+        mapping.discriminator { value = 'CAT' }
+
+        then:
+        mapping.discriminator.value == 'CAT'
+    }
+
+    void "discriminator(Map) sets value and column"() {
+        given:
+        Mapping mapping = new Mapping()
+
+        when:
+        mapping.discriminator(value: 'BIRD', column: 'disc_col')
+
+        then:
+        mapping.discriminator.value == 'BIRD'
+        mapping.discriminator.column.name == 'disc_col'
+    }
+
+    // --- composite() ---
+
+    void "composite(String...) creates a HibernateCompositeIdentity"() {
+        given:
+        Mapping mapping = new Mapping()
+
+        when:
+        HibernateCompositeIdentity id = mapping.composite('title', 'author')
+
+        then:
+        id instanceof HibernateCompositeIdentity
+        mapping.identity.is(id)
+    }
+
+    // --- version() ---
+
+    void "version(false) disables versioning"() {
+        given:
+        Mapping mapping = new Mapping()
+        assert mapping.versioned
+
+        when:
+        mapping.version(false)
+
+        then:
+        !mapping.versioned
+    }
+
+    void "version(Map) configures the version column"() {
+        given:
+        Mapping mapping = new Mapping()
+
+        when:
+        mapping.version(column: 'ver_col')
+
+        then:
+        mapping.columns['version'] != null
+        mapping.columns['version'].column == 'ver_col'
+    }
+
+    // --- isJoinedSubclass / setTablePerConcreteClass ---
+
+    void "isJoinedSubclass returns true when tablePerHierarchy=false and tablePerConcreteClass=false"() {
+        given:
+        Mapping mapping = new Mapping()
+        mapping.tablePerHierarchy = false
+
+        expect:
+        mapping.isJoinedSubclass()
+    }
+
+    void "isJoinedSubclass returns false when tablePerHierarchy is true"() {
+        given:
+        Mapping mapping = new Mapping()
+        mapping.tablePerHierarchy = true
+
+        expect:
+        !mapping.isJoinedSubclass()
+    }
+
+    void "setTablePerConcreteClass(true) also sets tablePerHierarchy to false"() {
+        given:
+        Mapping mapping = new Mapping()
+        assert mapping.tablePerHierarchy
+
+        when:
+        mapping.tablePerConcreteClass = true
+
+        then:
+        !mapping.tablePerHierarchy
+        mapping.tablePerConcreteClass
+    }
+
+    // --- getTypeName ---
+
+    void "getTypeName returns null for unknown class"() {
+        given:
+        Mapping mapping = new Mapping()
+
+        expect:
+        mapping.getTypeName(String) == null
+    }
+
+    void "getTypeName returns class name when mapped to a Class"() {
+        given:
+        Mapping mapping = new Mapping()
+        mapping.userTypes[String] = Integer
+
+        expect:
+        mapping.getTypeName(String) == Integer.name
+    }
+
+    void "getTypeName returns string value when mapped to a string"() {
+        given:
+        Mapping mapping = new Mapping()
+        mapping.userTypes[String] = 'my.custom.Type'
+
+        expect:
+        mapping.getTypeName(String) == 'my.custom.Type'
+    }
+
+    // --- hasCompositeIdentifier ---
+
+    void "hasCompositeIdentifier returns false for default simple identity"() {
+        given:
+        Mapping mapping = new Mapping()
+
+        expect:
+        !mapping.hasCompositeIdentifier()
+    }
+
+    void "hasCompositeIdentifier returns true after composite() is called"() {
+        given:
+        Mapping mapping = new Mapping()
+        mapping.composite('a', 'b')
+
+        expect:
+        mapping.hasCompositeIdentifier()
+    }
+
+    // --- configureNew / configureExisting ---
+
+    void "configureNew(Closure) returns a new Mapping with closure applied"() {
+        when:
+        Mapping m = Mapping.configureNew { table 'books' }
+
+        then:
+        m != null
+        m.tableName == 'books'
+    }
+
+    void "configureExisting(Mapping, Map) applies map values to existing mapping"() {
+        given:
+        Mapping existing = new Mapping()
+
+        when:
+        Mapping result = Mapping.configureExisting(existing, [tablePerHierarchy: false])
+
+        then:
+        result.is(existing)
+        !result.tablePerHierarchy
+    }
+
+    void "configureExisting(Mapping, Closure) applies closure to existing mapping"() {
+        given:
+        Mapping existing = new Mapping()
+
+        when:
+        Mapping result = Mapping.configureExisting(existing, { table 'my_table' })
+
+        then:
+        result.is(existing)
+        result.tableName == 'my_table'
+    }
+
 }
 
 // --- Test Domain Classes ---
diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateMappingKeywordSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateMappingKeywordSpec.groovy
new file mode 100644
index 0000000000..716c0fa988
--- /dev/null
+++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateMappingKeywordSpec.groovy
@@ -0,0 +1,110 @@
+/*
+ *  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 org.grails.orm.hibernate.cfg.domainbinding.hibernate
+
+import spock.lang.Specification
+import spock.lang.Unroll
+
+class HibernateMappingKeywordSpec extends Specification {
+
+    @Unroll
+    def "getKeyword returns '#expected' for #name"() {
+        expect:
+        HibernateMappingKeyword.valueOf(name).getKeyword() == expected
+
+        where:
+        name                      | expected
+        'INCLUDES'                | 'includes'
+        'TABLE'                   | 'table'
+        'DISCRIMINATOR'           | 'discriminator'
+        'AUTO_IMPORT'             | 'autoImport'
+        'SORT'                    | 'sort'
+        'CACHE'                   | 'cache'
+        'ID'                      | 'id'
+        'VERSION'                 | 'version'
+        'TENANT_ID'               | 'tenantId'
+        'BATCH_SIZE'              | 'batchSize'
+        'COMMENT'                 | 'comment'
+        'DATASOURCE'              | 'datasource'
+        'DATASOURCES'             | 'datasources'
+    }
+
+    @Unroll
+    def "toString returns '#expected' for #name"() {
+        expect:
+        HibernateMappingKeyword.valueOf(name).toString() == expected
+
+        where:
+        name            | expected
+        'TABLE'         | 'table'
+        'ID'            | 'id'
+        'CACHE'         | 'cache'
+    }
+
+    @Unroll
+    def "fromString('#keyword') returns expected enum"() {
+        expect:
+        HibernateMappingKeyword.fromString(keyword) == expected
+
+        where:
+        keyword                  | expected
+        'table'                  | HibernateMappingKeyword.TABLE
+        'id'                     | HibernateMappingKeyword.ID
+        'cache'                  | HibernateMappingKeyword.CACHE
+        'includes'               | HibernateMappingKeyword.INCLUDES
+        'discriminator'          | HibernateMappingKeyword.DISCRIMINATOR
+        'autoImport'             | HibernateMappingKeyword.AUTO_IMPORT
+        'hibernateCustomUserType'| HibernateMappingKeyword.HIBERNATE_CUSTOM_USER_TYPE
+        'sort'                   | HibernateMappingKeyword.SORT
+        'autowire'               | HibernateMappingKeyword.AUTOWIRE
+        'dynamicUpdate'          | HibernateMappingKeyword.DYNAMIC_UPDATE
+        'dynamicInsert'          | HibernateMappingKeyword.DYNAMIC_INSERT
+        'batchSize'              | HibernateMappingKeyword.BATCH_SIZE
+        'order'                  | HibernateMappingKeyword.ORDER
+        'autoTimestamp'          | HibernateMappingKeyword.AUTO_TIMESTAMP
+        'version'                | HibernateMappingKeyword.VERSION
+        'tenantId'               | HibernateMappingKeyword.TENANT_ID
+        'tablePerHierarchy'      | HibernateMappingKeyword.TABLE_PER_HIERARCHY
+        'tablePerSubclass'       | HibernateMappingKeyword.TABLE_PER_SUBCLASS
+        'tablePerConcreteClass'  | HibernateMappingKeyword.TABLE_PER_CONCRETE_CLASS
+        'property'               | HibernateMappingKeyword.PROPERTY
+        'columns'                | HibernateMappingKeyword.COLUMNS
+        'datasource'             | HibernateMappingKeyword.DATASOURCE
+        'datasources'            | HibernateMappingKeyword.DATASOURCES
+        'comment'                | HibernateMappingKeyword.COMMENT
+        'user-type'              | HibernateMappingKeyword.USER_TYPE
+        'importFrom'             | HibernateMappingKeyword.IMPORT_FROM
+        'unknown'                | null
+    }
+
+    def "all enum constants have non-null keywords"() {
+        expect:
+        HibernateMappingKeyword.values().every { it.keyword != null }
+    }
+
+    def "fromString returns null for unknown keyword"() {
+        expect:
+        HibernateMappingKeyword.fromString('nonExistentKeyword') == null
+    }
+
+    def "fromString returns null for empty string"() {
+        expect:
+        HibernateMappingKeyword.fromString('') == null
+    }
+}