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

emilles pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/master by this push:
     new 8ce1f51ea9 add tests for interface constants
8ce1f51ea9 is described below

commit 8ce1f51ea99878a079fea6761fc6e7ff09edd438
Author: Eric Milles <[email protected]>
AuthorDate: Thu Feb 26 13:23:14 2026 -0600

    add tests for interface constants
---
 src/main/java/groovy/lang/MetaClassImpl.java |   2 +-
 src/test/groovy/bugs/Groovy5272Bug.groovy    | 136 ---------------------
 src/test/groovy/groovy/InterfaceTest.groovy  | 174 +++++++++++++++++++++++++++
 3 files changed, 175 insertions(+), 137 deletions(-)

diff --git a/src/main/java/groovy/lang/MetaClassImpl.java 
b/src/main/java/groovy/lang/MetaClassImpl.java
index 126509ff46..a2f026ba31 100644
--- a/src/main/java/groovy/lang/MetaClassImpl.java
+++ b/src/main/java/groovy/lang/MetaClassImpl.java
@@ -2395,7 +2395,7 @@ public class MetaClassImpl implements MetaClass, 
MutableMetaClass {
             // sort interfaces so that we may ensure a deterministic behaviour 
in case of
             // ambiguous fields -- class implementing two interfaces using the 
same field
             if (superInterfaces.size() > 1) {
-                superInterfaces.sort(CACHED_CLASS_NAME_COMPARATOR);
+                superInterfaces.sort(CACHED_CLASS_NAME_COMPARATOR); // 
GROOVY-5272
             }
 
             if (theCachedClass.isArray) { // add the special read-only 
"length" property
diff --git a/src/test/groovy/bugs/Groovy5272Bug.groovy 
b/src/test/groovy/bugs/Groovy5272Bug.groovy
deleted file mode 100644
index 74c9a2f1a8..0000000000
--- a/src/test/groovy/bugs/Groovy5272Bug.groovy
+++ /dev/null
@@ -1,136 +0,0 @@
-/*
- *  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
- *
- *    http://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 bugs
-
-import org.junit.jupiter.api.Test
-
-import static groovy.test.GroovyAssert.assertScript
-
-
-class Groovy5272Bug {
-    /**
-     * In Groovy-5272, there are chances that the following test fails.
-     */
-    @Test
-    void testShouldNeverFail() {
-        10.times {
-            assertScript '''
-            public interface InterfaceA {
-                String FOO="Foo A";
-            }
-            public interface InterfaceB extends InterfaceA {
-                String FOO="Foo B";
-            }
-
-            // Fails randomly
-            assert InterfaceA.FOO!=InterfaceB.FOO
-            '''
-        }
-    }
-
-    @Test
-    void testShouldNeverFail2() {
-        10.times {
-            assertScript '''
-            public interface InterfaceA {
-                String FOO="Foo A";
-            }
-            public interface AnotherInterface extends InterfaceA {
-                String FOO="Foo B";
-            }
-
-            // Fails randomly
-            assert InterfaceA.FOO!=AnotherInterface.FOO
-            '''
-        }
-    }
-
-    @Test
-    void testResolvingAmbiguousStaticFieldShouldAlwaysReturnTheSameValue() {
-        10.times {
-        assertScript '''
-            public interface InterfaceA {
-                String FOO="Foo A";
-            }
-            public interface InterfaceB extends InterfaceA {
-                String FOO="Foo B";
-            }
-            public interface InterfaceC extends InterfaceA {
-                String FOO="Foo C";
-            }
-
-            class A implements InterfaceB, InterfaceC {
-            }
-
-            assert A.FOO == "Foo C"
-            '''
-        }
-    }
-
-    @Test
-    void testResolveConstantInSuperInterfaceWithExpando() {
-        assertScript '''
-            ExpandoMetaClass.enableGlobally()
-            interface Foo {
-                String FOO = 'FOO'
-            }
-            interface Bar extends Foo { }
-            assert Bar.FOO == 'FOO'
-            ExpandoMetaClass.disableGlobally()
-        '''
-    }
-
-    @Test
-    void testResolveConstantInSuperInterfaceWithoutExpando() {
-        assertScript '''
-            interface Foo {
-                String FOO = 'FOO'
-            }
-            interface Bar extends Foo { }
-            assert Bar.FOO == 'FOO'
-        '''
-    }
-
-    @Test
-    void testResolveConstantInClassWithSuperInterfaceWithoutExpando() {
-        assertScript '''
-            interface Foo {
-                String FOO = 'FOO'
-            }
-            interface Bar extends Foo { }
-            class Baz implements Bar {}
-            assert Baz.FOO == 'FOO'
-        '''
-    }
-
-    @Test
-    void testResolveConstantInClassWithSuperInterfaceWithExpando() {
-        assertScript '''
-            ExpandoMetaClass.enableGlobally()
-            interface Foo {
-                String FOO = 'FOO'
-            }
-            interface Bar extends Foo { }
-            class Baz implements Bar {}
-            assert Baz.FOO == 'FOO'
-            ExpandoMetaClass.disableGlobally()
-        '''
-    }
-
-}
diff --git a/src/test/groovy/groovy/InterfaceTest.groovy 
b/src/test/groovy/groovy/InterfaceTest.groovy
index 2dd67f284a..316d27ebdc 100644
--- a/src/test/groovy/groovy/InterfaceTest.groovy
+++ b/src/test/groovy/groovy/InterfaceTest.groovy
@@ -18,6 +18,7 @@
  */
 package groovy
 
+import org.junit.jupiter.api.RepeatedTest
 import org.junit.jupiter.api.Test
 import org.junit.jupiter.params.ParameterizedTest
 import org.junit.jupiter.params.provider.ValueSource
@@ -218,6 +219,179 @@ final class InterfaceTest {
         '''
     }
 
+    @Test
+    void testPublicStaticInterfaceConstant1() {
+        String interfaces = '''
+            interface A {
+                String FOO = 'A'
+            }
+            interface B {
+                String FOO = 'B'
+            }
+        '''
+        assertScript interfaces + '''
+            class C implements A, B {
+                def m() {
+                    FOO
+                }
+            }
+            assert new C().m() == 'B'
+        '''
+        assertScript interfaces + '''
+            class C implements B, A {
+                def m() {
+                    FOO
+                }
+            }
+            assert new C().m() == 'B'
+        '''
+    }
+
+    @Test
+    void testPublicStaticInterfaceConstant2() {
+        String interfaces = '''
+            interface A {
+                String FOO = 'A'
+            }
+            interface B {
+                String FOO = 'B'
+            }
+            interface X extends B { }
+        '''
+        assertScript interfaces + '''
+            class C implements A, X {
+                def m() {
+                    FOO
+                }
+            }
+            assert new C().m() == 'B'
+        '''
+        assertScript interfaces + '''
+            class C implements X, A {
+                def m() {
+                    FOO
+                }
+            }
+            assert new C().m() == 'B'
+        '''
+    }
+
+    // GROOVY-5272
+    @RepeatedTest(10)
+    void testPublicStaticInterfaceConstant3() {
+        assertScript '''
+            interface A {
+                String FOO = 'A'
+            }
+            interface B extends A {
+                String FOO = 'B'
+            }
+
+            assert A.FOO != B.FOO // was non-deterministic
+        '''
+    }
+
+    // GROOVY-5272
+    @RepeatedTest(10)
+    void testPublicStaticInterfaceConstant4() {
+        assertScript '''
+            interface B {
+                String FOO = 'B'
+            }
+            interface A extends B {
+                String FOO = 'A'
+            }
+
+            assert A.FOO != B.FOO // was non-deterministic
+        '''
+    }
+
+    // GROOVY-5272
+    @RepeatedTest(10)
+    void testPublicStaticInterfaceConstant5() {
+        assertScript '''
+            interface A {
+                String FOO = 'A'
+            }
+            interface B extends A {
+                String FOO = 'B'
+            }
+            interface C extends A {
+                String FOO = 'C'
+            }
+            class X implements B, C {
+            }
+
+            assert X.FOO == 'C'
+        '''
+    }
+
+    @Test
+    void testConstantInSuperInterfaceNoExpando() {
+        assertScript '''
+            interface Foo {
+                String FOO = 'FOO'
+            }
+            interface Bar extends Foo {
+            }
+
+            assert Bar.FOO == 'FOO'
+        '''
+    }
+
+    @Test
+    void testConstantInSuperInterfaceYesExpando() {
+        assertScript '''
+            interface Foo {
+                String FOO = 'FOO'
+            }
+            interface Bar extends Foo {
+            }
+
+            ExpandoMetaClass.enableGlobally()
+            try {
+                assert Bar.FOO == 'FOO'
+            } finally {
+                ExpandoMetaClass.disableGlobally()
+            }
+        '''
+    }
+
+    @Test
+    void testConstantInSuperSuperInterfaceNoExpando() {
+        assertScript '''
+            interface Foo {
+                String FOO = 'FOO'
+            }
+            interface Bar extends Foo {
+            }
+            class Baz implements Bar {
+            }
+
+            assert Baz.FOO == 'FOO'
+        '''
+    }
+
+    @Test
+    void testConstantInSuperSuperInterfaceYesExpando() {
+        assertScript '''
+            interface Foo {
+                String FOO = 'FOO'
+            }
+            interface Bar extends Foo {
+            }
+            class Baz implements Bar {
+            }
+
+            ExpandoMetaClass.enableGlobally()
+            try {
+                assert Baz.FOO == 'FOO'
+            } finally {
+                ExpandoMetaClass.disableGlobally()
+            }
+        '''
+    }
+
     // GROOVY-11758, GROOVY-11830
     @ParameterizedTest
     @ValueSource(strings=['protected 
final','protected','@PackageScope','private'])

Reply via email to