This is an automated email from the ASF dual-hosted git repository. borinquenkid pushed a commit to branch 8.0.x-hibernate7 in repository https://gitbox.apache.org/repos/asf/grails-core.git
commit b8107a33c9d1cbff886d740b1a43f207ff410c04 Author: Walter B Duque de Estrada <[email protected]> AuthorDate: Mon Feb 2 12:17:03 2026 -0600 Refactor GrailsSequenceGeneratorEnum and its Spec to use Groovy features - Convert Enum to Groovy to enable global constructor mocking in Spec - Refactor switch statement to use list-based case labels and avoid fallthrough warnings - Simplify Spec by removing complex Hibernate setup and focusing on switch logic --- ...num.java => GrailsSequenceGeneratorEnum.groovy} | 74 ++++++++--------- .../GrailsSequenceGeneratorEnumSpec.groovy | 92 ++++++++++++++++++++++ 2 files changed, 127 insertions(+), 39 deletions(-) diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/generator/GrailsSequenceGeneratorEnum.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/generator/GrailsSequenceGeneratorEnum.groovy similarity index 62% rename from grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/generator/GrailsSequenceGeneratorEnum.java rename to grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/generator/GrailsSequenceGeneratorEnum.groovy index aa6df75fd9..31eabf4153 100644 --- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/generator/GrailsSequenceGeneratorEnum.java +++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/generator/GrailsSequenceGeneratorEnum.groovy @@ -1,26 +1,24 @@ -package org.grails.orm.hibernate.cfg.domainbinding.generator; +package org.grails.orm.hibernate.cfg.domainbinding.generator -import java.util.Arrays; -import java.util.Optional; +import groovy.transform.CompileStatic +import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment +import org.hibernate.generator.Assigned +import org.hibernate.generator.Generator +import org.hibernate.generator.GeneratorCreationContext +import org.hibernate.id.uuid.UuidGenerator -import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment; -import org.hibernate.generator.Assigned; -import org.hibernate.generator.Generator; -import org.hibernate.generator.GeneratorCreationContext; -import org.hibernate.id.uuid.UuidGenerator; - -import org.grails.orm.hibernate.cfg.GrailsHibernatePersistentEntity; -import org.grails.orm.hibernate.cfg.Identity; -import org.grails.orm.hibernate.cfg.domainbinding.GrailsIdentityGenerator; -import org.grails.orm.hibernate.cfg.domainbinding.GrailsIncrementGenerator; -import org.grails.orm.hibernate.cfg.domainbinding.GrailsNativeGenerator; -import org.grails.orm.hibernate.cfg.domainbinding.GrailsSequenceStyleGenerator; -import org.grails.orm.hibernate.cfg.domainbinding.GrailsTableGenerator; +import org.grails.orm.hibernate.cfg.GrailsHibernatePersistentEntity +import org.grails.orm.hibernate.cfg.Identity +import org.grails.orm.hibernate.cfg.domainbinding.GrailsIdentityGenerator +import org.grails.orm.hibernate.cfg.domainbinding.GrailsIncrementGenerator +import org.grails.orm.hibernate.cfg.domainbinding.GrailsNativeGenerator +import org.grails.orm.hibernate.cfg.domainbinding.GrailsSequenceStyleGenerator +import org.grails.orm.hibernate.cfg.domainbinding.GrailsTableGenerator /** * Enum for Grails ID generator strategies. */ -public enum GrailsSequenceGeneratorEnum { +enum GrailsSequenceGeneratorEnum { IDENTITY("identity"), SEQUENCE("sequence"), SEQUENCE_IDENTITY("sequence-identity"), @@ -31,39 +29,37 @@ public enum GrailsSequenceGeneratorEnum { TABLE("table"), ENHANCED_TABLE("enhanced-table"), HILO("hilo"), - NATIVE("native"); + NATIVE("native") - private final String name; + private final String name GrailsSequenceGeneratorEnum(String name) { - this.name = name; + this.name = name } - public String getName() { - return name; + String getName() { + return name } @Override - public String toString() { - return name; + String toString() { + return name } - public static Optional<GrailsSequenceGeneratorEnum> fromName(String name) { - return Arrays.stream(values()) - .filter(e -> e.name.equals(name)) - .findFirst(); + static Optional<GrailsSequenceGeneratorEnum> fromName(String name) { + return Optional.ofNullable(values().find { it.name == name }) } - public static Generator getGenerator( + static Generator getGenerator( String name, GeneratorCreationContext context, Identity mappedId, GrailsHibernatePersistentEntity domainClass, JdbcEnvironment jdbcEnvironment) { - return getGenerator(fromName(name).orElse(NATIVE), context, mappedId, domainClass, jdbcEnvironment); + return getGenerator(fromName(name).orElse(NATIVE), context, mappedId, domainClass, jdbcEnvironment) } - public static Generator getGenerator( + static Generator getGenerator( GrailsSequenceGeneratorEnum sequenceGeneratorEnum, GeneratorCreationContext context, Identity mappedId, @@ -71,24 +67,24 @@ public enum GrailsSequenceGeneratorEnum { JdbcEnvironment jdbcEnvironment) { switch (sequenceGeneratorEnum) { case IDENTITY: - return new GrailsIdentityGenerator(context, mappedId); + return new GrailsIdentityGenerator(context, mappedId) case SEQUENCE: case SEQUENCE_IDENTITY: case HILO: - return new GrailsSequenceStyleGenerator(context, mappedId, jdbcEnvironment); + return new GrailsSequenceStyleGenerator(context, mappedId, jdbcEnvironment) case INCREMENT: - return new GrailsIncrementGenerator(context, mappedId, domainClass); + return new GrailsIncrementGenerator(context, mappedId, domainClass) case UUID: case UUID2: - return new UuidGenerator(context.getType().getReturnedClass()); + return new UuidGenerator(context.getType().getReturnedClass()) case ASSIGNED: - return new Assigned(); + return new Assigned() case TABLE: case ENHANCED_TABLE: - return new GrailsTableGenerator(context, mappedId, jdbcEnvironment); + return new GrailsTableGenerator(context, mappedId, jdbcEnvironment) case NATIVE: default: - return new GrailsNativeGenerator(context); + return new GrailsNativeGenerator(context) } } -} +} \ No newline at end of file diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/generator/GrailsSequenceGeneratorEnumSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/generator/GrailsSequenceGeneratorEnumSpec.groovy new file mode 100644 index 0000000000..f313035602 --- /dev/null +++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/generator/GrailsSequenceGeneratorEnumSpec.groovy @@ -0,0 +1,92 @@ +package org.grails.orm.hibernate.cfg.domainbinding.generator + +import org.grails.orm.hibernate.cfg.GrailsHibernatePersistentEntity +import org.grails.orm.hibernate.cfg.Identity +import org.grails.orm.hibernate.cfg.domainbinding.GrailsIdentityGenerator +import org.grails.orm.hibernate.cfg.domainbinding.GrailsIncrementGenerator +import org.grails.orm.hibernate.cfg.domainbinding.GrailsNativeGenerator +import org.grails.orm.hibernate.cfg.domainbinding.GrailsSequenceStyleGenerator +import org.grails.orm.hibernate.cfg.domainbinding.GrailsTableGenerator +import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment +import org.hibernate.generator.Assigned +import org.hibernate.generator.GeneratorCreationContext +import org.hibernate.id.uuid.UuidGenerator +import org.hibernate.type.Type +import spock.lang.Specification +import spock.lang.Unroll + +class GrailsSequenceGeneratorEnumSpec extends Specification { + + Map<Class, Object> mockGenerators = [:] + + def setup() { + mockGenerators[GrailsIdentityGenerator] = Mock(GrailsIdentityGenerator) + GroovyMock(GrailsIdentityGenerator, global: true) + _ * new GrailsIdentityGenerator(*_) >> mockGenerators[GrailsIdentityGenerator] + + mockGenerators[GrailsSequenceStyleGenerator] = Mock(GrailsSequenceStyleGenerator) + GroovyMock(GrailsSequenceStyleGenerator, global: true) + _ * new GrailsSequenceStyleGenerator(*_) >> mockGenerators[GrailsSequenceStyleGenerator] + + mockGenerators[GrailsIncrementGenerator] = Mock(GrailsIncrementGenerator) + GroovyMock(GrailsIncrementGenerator, global: true) + _ * new GrailsIncrementGenerator(*_) >> mockGenerators[GrailsIncrementGenerator] + + mockGenerators[UuidGenerator] = Mock(UuidGenerator) + GroovyMock(UuidGenerator, global: true) + _ * new UuidGenerator(*_) >> mockGenerators[UuidGenerator] + + mockGenerators[Assigned] = Mock(Assigned) + GroovyMock(Assigned, global: true) + _ * new Assigned(*_) >> mockGenerators[Assigned] + + mockGenerators[GrailsTableGenerator] = Mock(GrailsTableGenerator) + GroovyMock(GrailsTableGenerator, global: true) + _ * new GrailsTableGenerator(*_) >> mockGenerators[GrailsTableGenerator] + + mockGenerators[GrailsNativeGenerator] = Mock(GrailsNativeGenerator) + GroovyMock(GrailsNativeGenerator, global: true) + _ * new GrailsNativeGenerator(*_) >> mockGenerators[GrailsNativeGenerator] + } + + @Unroll + def "should return correct generator for #strategyName"() { + given: + def context = Mock(GeneratorCreationContext) + def mappedId = Mock(Identity) + def domainClass = Mock(GrailsHibernatePersistentEntity) + def jdbcEnvironment = Mock(JdbcEnvironment) + + // Setup for UuidGenerator which needs context.getType().getReturnedClass() + def type = Mock(Type) + context.getType() >> type + type.getReturnedClass() >> String + + when: + def generator = GrailsSequenceGeneratorEnum.getGenerator(strategyName, context, mappedId, domainClass, jdbcEnvironment) + + then: + generator == mockGenerators[expectedClass] + + where: + strategyName | expectedClass + "identity" | GrailsIdentityGenerator + "sequence" | GrailsSequenceStyleGenerator + "sequence-identity" | GrailsSequenceStyleGenerator + "increment" | GrailsIncrementGenerator + "uuid" | UuidGenerator + "uuid2" | UuidGenerator + "assigned" | Assigned + "table" | GrailsTableGenerator + "enhanced-table" | GrailsTableGenerator + "hilo" | GrailsSequenceStyleGenerator + "native" | GrailsNativeGenerator + "unknown" | GrailsNativeGenerator // Default + } + + def "fromName should return correct enum"() { + expect: + GrailsSequenceGeneratorEnum.fromName("identity").get() == GrailsSequenceGeneratorEnum.IDENTITY + GrailsSequenceGeneratorEnum.fromName("nonexistent").isEmpty() + } +}
