This is an automated email from the ASF dual-hosted git repository. borinquenkid pushed a commit to branch test/datamapping-core-utils-cleanup in repository https://gitbox.apache.org/repos/asf/grails-core.git
commit c87627ecd1ee10aa140b2954c3e8adc45ea1cec9 Author: Walter Duque de Estrada <[email protected]> AuthorDate: Fri Aug 21 10:47:42 2026 -0500 added specs to package --- .../datastore/gorm/utils/ReflectionUtils.java | 6 +- .../IgnoredPackageEntity.groovy} | 23 +------ .../gorm/utils/ClasspathEntityScannerSpec.groovy | 71 +++++++++++++++++-- .../gorm/utils/ReflectionUtilsSpec.groovy | 80 ++++++++++++++++++++++ .../JakartaTestEntity.groovy} | 24 +------ 5 files changed, 152 insertions(+), 52 deletions(-) diff --git a/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/utils/ReflectionUtils.java b/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/utils/ReflectionUtils.java index b07c632ea5..ef1b4499e7 100644 --- a/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/utils/ReflectionUtils.java +++ b/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/utils/ReflectionUtils.java @@ -42,10 +42,8 @@ public class ReflectionUtils { if (superClass != null) { try { - final Method superMethod = superClass.getMethod(method.getName(), method.getParameterTypes()); - if (superMethod != null) { - return true; - } + superClass.getMethod(method.getName(), method.getParameterTypes()); + return true; } catch (NoSuchMethodException e) { // ignore } diff --git a/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/utils/ClasspathEntityScannerSpec.groovy b/grails-datamapping-core/src/test/groovy/com/IgnoredPackageEntity.groovy similarity index 63% copy from grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/utils/ClasspathEntityScannerSpec.groovy copy to grails-datamapping-core/src/test/groovy/com/IgnoredPackageEntity.groovy index 2bfd488527..011489c315 100644 --- a/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/utils/ClasspathEntityScannerSpec.groovy +++ b/grails-datamapping-core/src/test/groovy/com/IgnoredPackageEntity.groovy @@ -16,29 +16,12 @@ * specific language governing permissions and limitations * under the License. */ - -package org.grails.datastore.gorm.utils +package com import grails.gorm.annotation.Entity -import spock.lang.Specification - -/** - * Created by graemerocher on 18/11/16. - */ -class ClasspathEntityScannerSpec extends Specification { - - void "test classpath entity scanner"() { - when:"the classpath is scanned" - def scanner = new ClasspathEntityScanner() - def results = scanner.scan(ClasspathEntityScannerSpec.package) - - then:"The results are correct" - results.size() == 1 - results.first() == TestEntity - } -} +// Lives directly in the top-level "com" package, one of ClasspathEntityScanner's ignoredPackages. @Entity -class TestEntity { +class IgnoredPackageEntity { String name } diff --git a/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/utils/ClasspathEntityScannerSpec.groovy b/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/utils/ClasspathEntityScannerSpec.groovy index 2bfd488527..bbaba99f46 100644 --- a/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/utils/ClasspathEntityScannerSpec.groovy +++ b/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/utils/ClasspathEntityScannerSpec.groovy @@ -16,29 +16,86 @@ * specific language governing permissions and limitations * under the License. */ - package org.grails.datastore.gorm.utils import grails.gorm.annotation.Entity import spock.lang.Specification -/** - * Created by graemerocher on 18/11/16. - */ +import org.grails.datastore.jakartafixture.JakartaTestEntity + class ClasspathEntityScannerSpec extends Specification { - void "test classpath entity scanner"() { - when:"the classpath is scanned" + void "test classpath entity scanner finds a class annotated with grails.gorm.annotation.Entity"() { + when: "the classpath is scanned" def scanner = new ClasspathEntityScanner() def results = scanner.scan(ClasspathEntityScannerSpec.package) - then:"The results are correct" + then: "the results are correct" results.size() == 1 results.first() == TestEntity } + + void "test classpath entity scanner finds a class annotated with jakarta.persistence.Entity"() { + when: "the classpath is scanned" + def scanner = new ClasspathEntityScanner() + def results = scanner.scan(JakartaTestEntity.package) + + then: "the jakarta annotated entity is found" + results.contains(JakartaTestEntity) + } + + void "test classpath entity scanner ignores non-entity classes in the scanned package"() { + when: "the classpath is scanned" + def scanner = new ClasspathEntityScanner() + def results = scanner.scan(ClasspathEntityScannerSpec.package) + + then: "only the annotated entity is returned" + !results.contains(NotAnEntity) + } + + void "test classpath entity scanner returns no results when no packages are given"() { + when: "the scanner is invoked with no packages" + def scanner = new ClasspathEntityScanner() + def results = scanner.scan() + + then: "the result is empty" + results.length == 0 + } + + void "test classpath entity scanner de-duplicates results when the same package is scanned twice"() { + when: "the same package is passed in more than once" + def scanner = new ClasspathEntityScanner() + def results = scanner.scan(ClasspathEntityScannerSpec.package, ClasspathEntityScannerSpec.package) + + then: "the entity is only returned once" + results.count { it == TestEntity } == 1 + } + + void "test classpath entity scanner combines results from multiple packages"() { + when: "two distinct packages are scanned together" + def scanner = new ClasspathEntityScanner() + def results = scanner.scan(ClasspathEntityScannerSpec.package, JakartaTestEntity.package) + + then: "entities from both packages are returned" + results.contains(TestEntity) + results.contains(JakartaTestEntity) + } + + void "test classpath entity scanner does not scan a package that is too generic"() { + when: "a top-level package on the ignore list is scanned" + def scanner = new ClasspathEntityScanner() + def results = scanner.scan(com.IgnoredPackageEntity.package) + + then: "no entities are returned for that package" + results.length == 0 + } } @Entity class TestEntity { String name } + +class NotAnEntity { + String name +} diff --git a/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/utils/ReflectionUtilsSpec.groovy b/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/utils/ReflectionUtilsSpec.groovy new file mode 100644 index 0000000000..326d58670c --- /dev/null +++ b/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/utils/ReflectionUtilsSpec.groovy @@ -0,0 +1,80 @@ +/* + * 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.datastore.gorm.utils + +import groovy.transform.PackageScope +import spock.lang.Specification + +class ReflectionUtilsSpec extends Specification { + + void "test a method overridden from a public parent method is detected as overridden"() { + given: "a method overridden from a public superclass" + def method = ReflectionUtilsChild.getDeclaredMethod('greet') + + expect: "the method is reported as overridden" + ReflectionUtils.isMethodOverriddenFromParent(method) + } + + void "test a method unique to the subclass is not detected as overridden"() { + given: "a method that is only declared on the subclass" + def method = ReflectionUtilsChild.getDeclaredMethod('onlyOnChild') + + expect: "the method is not reported as overridden" + !ReflectionUtils.isMethodOverriddenFromParent(method) + } + + void "test a method whose declaring class has no superclass is not detected as overridden"() { + given: "a method declared on a class with no superclass" + def method = Object.getMethod('toString') + + expect: "the method is not reported as overridden" + !ReflectionUtils.isMethodOverriddenFromParent(method) + } + + void "test a method that only shadows a non-public parent method is not detected as overridden"() { + given: "a package-private method also declared on the superclass" + def method = ReflectionUtilsChild.getDeclaredMethod('hidden') + + expect: "the method is not reported as overridden because the superclass method is not public" + !ReflectionUtils.isMethodOverriddenFromParent(method) + } +} + +class ReflectionUtilsParent { + void greet() { + } + + @PackageScope + void hidden() { + } +} + +class ReflectionUtilsChild extends ReflectionUtilsParent { + @Override + void greet() { + } + + void onlyOnChild() { + } + + @PackageScope + @Override + void hidden() { + } +} diff --git a/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/utils/ClasspathEntityScannerSpec.groovy b/grails-datamapping-core/src/test/groovy/org/grails/datastore/jakartafixture/JakartaTestEntity.groovy similarity index 60% copy from grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/utils/ClasspathEntityScannerSpec.groovy copy to grails-datamapping-core/src/test/groovy/org/grails/datastore/jakartafixture/JakartaTestEntity.groovy index 2bfd488527..0d84ac10b4 100644 --- a/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/utils/ClasspathEntityScannerSpec.groovy +++ b/grails-datamapping-core/src/test/groovy/org/grails/datastore/jakartafixture/JakartaTestEntity.groovy @@ -16,29 +16,11 @@ * specific language governing permissions and limitations * under the License. */ +package org.grails.datastore.jakartafixture -package org.grails.datastore.gorm.utils - -import grails.gorm.annotation.Entity -import spock.lang.Specification - -/** - * Created by graemerocher on 18/11/16. - */ -class ClasspathEntityScannerSpec extends Specification { - - void "test classpath entity scanner"() { - when:"the classpath is scanned" - def scanner = new ClasspathEntityScanner() - def results = scanner.scan(ClasspathEntityScannerSpec.package) - - then:"The results are correct" - results.size() == 1 - results.first() == TestEntity - } -} +import jakarta.persistence.Entity @Entity -class TestEntity { +class JakartaTestEntity { String name }
