Skip to content

Commit 0233324

Browse files
committed
Fix for #1542: create a @Generated annotation for each delegate method
1 parent 9f3799e commit 0233324

File tree

7 files changed

+405
-216
lines changed

7 files changed

+405
-216
lines changed

base/org.eclipse.jdt.groovy.core/src/org/codehaus/jdt/groovy/internal/compiler/ast/GroovyCompilationUnitDeclaration.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2009-2023 the original author or authors.
2+
* Copyright 2009-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,7 +15,9 @@
1515
*/
1616
package org.codehaus.jdt.groovy.internal.compiler.ast;
1717

18+
import static org.apache.groovy.ast.tools.AnnotatedNodeUtils.isGenerated;
1819
import static org.apache.groovy.ast.tools.AnnotatedNodeUtils.markAsGenerated;
20+
import static org.codehaus.groovy.runtime.DefaultGroovyMethods.plus;
1921
import static org.codehaus.groovy.runtime.StringGroovyMethods.find;
2022

2123
import java.io.PrintWriter;
@@ -1523,10 +1525,10 @@ private void createConstructorDeclarations(ClassNode classNode, List<AbstractMet
15231525
if (!variables.isEmpty()) constructorDecl.statements = createStatements(variables.values());
15241526
}
15251527
if (constructorNode.hasDefaultValue()) {
1526-
Annotation[] generated = createAnnotations(ClassHelper.make(groovy.transform.Generated.class));
15271528
for (Argument[] variantArgs : getVariantsAllowingForDefaulting(constructorNode.getParameters(), constructorDecl.arguments)) {
15281529
ConstructorDeclaration variantDecl = new ConstructorDeclaration(unitDeclaration.compilationResult);
1529-
variantDecl.annotations = constructorDecl.annotations == null ? generated : ArrayUtils.concat(constructorDecl.annotations, generated);
1530+
variantDecl.annotations = createAnnotations(isGenerated(constructorNode) ? constructorNode.getAnnotations()
1531+
: plus(constructorNode.getAnnotations(), new AnnotationNode(ClassHelper.make(groovy.transform.Generated.class))));
15301532
variantDecl.arguments = variantArgs;
15311533
variantDecl.bits = constructorDecl.bits;
15321534
variantDecl.javadoc = constructorDecl.javadoc;
@@ -1601,10 +1603,11 @@ private void createMethodDeclarations(ClassNode classNode, GroovyTypeDeclaration
16011603
}
16021604

16031605
if (methodNode.hasDefaultValue()) {
1604-
Annotation[] generated = createAnnotations(ClassHelper.make(groovy.transform.Generated.class));
16051606
for (Argument[] variantArgs : getVariantsAllowingForDefaulting(methodNode.getParameters(), methodDecl.arguments)) {
16061607
AbstractMethodDeclaration variantDecl = createMethodDeclaration(classNode, methodNode);
1607-
variantDecl.annotations = variantDecl.annotations == null ? generated : ArrayUtils.concat(variantDecl.annotations, generated);
1608+
variantDecl.annotations = ArrayUtils.concat(
1609+
variantDecl.annotations != null ? variantDecl.annotations : new Annotation[0],
1610+
createAnnotations(ClassHelper.make(groovy.transform.Generated.class)));
16081611
variantDecl.arguments = variantArgs;
16091612

16101613
variantDecl.declarationSourceStart = 0;

ide-test/org.codehaus.groovy.alltests/src/org/codehaus/groovy/alltests/AllGroovyTests.groovy

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2009-2023 the original author or authors.
2+
* Copyright 2009-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -94,6 +94,7 @@ import org.junit.runners.Suite
9494
// org.codehaus.groovy.eclipse.junit.tests
9595
org.codehaus.groovy.eclipse.junit.test.JUnit3TestFinderTests,
9696
org.codehaus.groovy.eclipse.junit.test.JUnit4TestFinderTests,
97+
org.codehaus.groovy.eclipse.junit.test.JUnit5TestFinderTests,
9798
org.codehaus.groovy.eclipse.junit.test.MainMethodFinderTests,
9899

99100
// org.codehaus.groovy.eclipse.quickfix.tests

ide-test/org.codehaus.groovy.eclipse.junit.test/src/org/codehaus/groovy/eclipse/junit/test/JUnit3TestFinderTests.groovy

Lines changed: 69 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2009-2020 the original author or authors.
2+
* Copyright 2009-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,87 +15,119 @@
1515
*/
1616
package org.codehaus.groovy.eclipse.junit.test
1717

18+
import org.codehaus.groovy.eclipse.test.GroovyEclipseTestSuite
19+
import org.codehaus.groovy.eclipse.test.SynchronizationUtils
1820
import org.eclipse.jdt.core.ICompilationUnit
1921
import org.eclipse.jdt.core.IType
2022
import org.eclipse.jdt.internal.junit.launcher.JUnit3TestFinder
23+
import org.junit.Before
2124
import org.junit.Test
2225

23-
final class JUnit3TestFinderTests extends JUnitTestSuite {
26+
final class JUnit3TestFinderTests extends GroovyEclipseTestSuite {
2427

25-
private void assertTypeIsTest(boolean expected, ICompilationUnit unit, String typeName, String reasonText = '') {
28+
@Before
29+
void setUp() {
30+
addJUnit(3)
31+
}
32+
33+
private Set<IType> getAllTests() {
34+
Set<IType> testTypes = []
35+
SynchronizationUtils.waitForIndexingToComplete()
36+
new JUnit3TestFinder().findTestsInContainer(packageFragmentRoot, testTypes, null)
37+
return testTypes
38+
}
39+
40+
private boolean isTest(ICompilationUnit unit, String typeName = unit.types[0].elementName) {
2641
def type = unit.getType(typeName)
2742
assert type.exists() : "Groovy type $typeName should exist"
28-
assert new JUnit3TestFinder().isTest(type) == expected : "Groovy type $typeName should${expected ? '' : 'n\'t'} be a JUnit 3 test $reasonText"
43+
return new JUnit3TestFinder().isTest(type)
2944
}
3045

46+
//--------------------------------------------------------------------------
47+
3148
@Test
32-
void testFinderWithSuite() {
33-
def test = addGroovySource '''
34-
class A {
35-
static junit.framework.Test suite() throws Exception {}
49+
void testIsTest0() {
50+
def unit = addGroovySource '''
51+
class C {
52+
void test() { }
3653
}
37-
'''
54+
''', 'C', 'p'
55+
assert !isTest(unit)
56+
}
3857

39-
assertTypeIsTest(true, test, 'A')
58+
@Test
59+
void testIsTest1() {
60+
def unit = addGroovySource '''
61+
class C extends junit.framework.TestCase {
62+
void test() { }
63+
}
64+
''', 'C', 'p'
65+
assert isTest(unit)
4066
}
4167

4268
@Test
43-
void testFinderOfSubclass() {
44-
def base = addGroovySource '''
45-
abstract class TestBase extends junit.framework.TestCase {
69+
void testIsTest2() {
70+
def unit = addGroovySource '''
71+
class C {
72+
static junit.framework.Test suite() { }
4673
}
47-
'''
74+
''', 'C', 'p'
75+
assert isTest(unit)
76+
}
4877

49-
def test = addGroovySource '''
50-
class B extends TestBase {
78+
@Test
79+
void testIsTest3() {
80+
def unit = addGroovySource '''
81+
abstract class TestBase extends junit.framework.TestCase {
5182
}
52-
'''
83+
''', 'TestBase', 'p'
84+
assert !isTest(unit)
5385

54-
assertTypeIsTest(false, base, 'TestBase', '(it is abstract)')
55-
assertTypeIsTest(true, test, 'B')
86+
unit = addGroovySource '''
87+
class C extends TestBase {
88+
}
89+
''', 'C', 'p'
90+
assert isTest(unit)
5691
}
5792

5893
@Test
59-
void testFinderOfNonPublicSubclass() {
60-
def base = addGroovySource '''
94+
void testIsTest4() {
95+
def unit = addGroovySource '''
6196
abstract class TestBase extends junit.framework.TestCase {
6297
}
63-
'''
98+
''', 'TestBase', 'p'
99+
assert !isTest(unit)
64100

65-
def test = addGroovySource '''
101+
unit = addGroovySource '''
66102
@groovy.transform.PackageScope class C extends TestBase {
67103
}
68-
'''
69-
70-
assertTypeIsTest(false, base, 'TestBase', '(it is abstract)')
71-
assertTypeIsTest(true, test, 'C')
104+
''', 'C', 'p'
105+
assert isTest(unit)
72106
}
73107

108+
//
109+
74110
@Test
75-
void testFindAllTestSuites() {
111+
void testFindTests() {
76112
addGroovySource '''
77113
abstract class TestBase extends junit.framework.TestCase {
78114
}
79-
'''
80-
115+
''', 'TestBase', 'p'
81116
addGroovySource '''
82117
class X extends TestBase {
83118
}
84-
'''
85-
119+
''', 'X', 'p'
86120
addGroovySource '''
87121
class Y extends junit.framework.TestCase {
88122
}
89-
'''
90-
123+
''', 'Y', 'p'
91124
addGroovySource '''
92125
class Z {
93126
static junit.framework.Test suite() throws Exception {}
94127
}
95-
'''
128+
''', 'Z', 'p'
96129

97-
Set<IType> testTypes = []
98-
new JUnit3TestFinder().findTestsInContainer(packageFragmentRoot, testTypes, null)
130+
Set<IType> testTypes = allTests
99131

100132
assert testTypes.any { it.elementName == 'X' } : 'X should be a test type'
101133
assert testTypes.any { it.elementName == 'Y' } : 'Y should be a test type'

0 commit comments

Comments
 (0)