From b61a5e433b2aa7e5b24dfd2ebf69b52415fe673f Mon Sep 17 00:00:00 2001 From: Eric Milles Date: Sun, 4 Feb 2024 11:08:17 -0600 Subject: [PATCH] Fix for #1542: create a `@Generated` annotation for each delegate method --- .../ast/GroovyCompilationUnitDeclaration.java | 13 +- .../jdt/groovy/core/GroovyPropertyTester.java | 21 +- .../jdt/groovy/core/util/GroovyUtils.java | 3 +- .../TypeInferencingVisitorWithRequestor.java | 6 +- .../groovy/alltests/AllGroovyTests.groovy | 3 +- .../junit/test/JUnit3TestFinderTests.groovy | 106 ++++++--- .../junit/test/JUnit4TestFinderTests.groovy | 216 ++++++++++-------- .../junit/test/JUnit5TestFinderTests.groovy | 171 ++++++++++++++ .../eclipse/junit/test/JUnitTestSuite.groovy | 55 ----- .../junit/test/MainMethodFinderTests.groovy | 57 ++--- .../test/adapters/IsScriptTesterTests.groovy | 4 +- 11 files changed, 421 insertions(+), 234 deletions(-) create mode 100644 ide-test/org.codehaus.groovy.eclipse.junit.test/src/org/codehaus/groovy/eclipse/junit/test/JUnit5TestFinderTests.groovy delete mode 100644 ide-test/org.codehaus.groovy.eclipse.junit.test/src/org/codehaus/groovy/eclipse/junit/test/JUnitTestSuite.groovy diff --git a/base/org.eclipse.jdt.groovy.core/src/org/codehaus/jdt/groovy/internal/compiler/ast/GroovyCompilationUnitDeclaration.java b/base/org.eclipse.jdt.groovy.core/src/org/codehaus/jdt/groovy/internal/compiler/ast/GroovyCompilationUnitDeclaration.java index eb78732d24..de6407f29a 100644 --- a/base/org.eclipse.jdt.groovy.core/src/org/codehaus/jdt/groovy/internal/compiler/ast/GroovyCompilationUnitDeclaration.java +++ b/base/org.eclipse.jdt.groovy.core/src/org/codehaus/jdt/groovy/internal/compiler/ast/GroovyCompilationUnitDeclaration.java @@ -1,5 +1,5 @@ /* - * Copyright 2009-2023 the original author or authors. + * Copyright 2009-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,7 +15,9 @@ */ package org.codehaus.jdt.groovy.internal.compiler.ast; +import static org.apache.groovy.ast.tools.AnnotatedNodeUtils.isGenerated; import static org.apache.groovy.ast.tools.AnnotatedNodeUtils.markAsGenerated; +import static org.codehaus.groovy.runtime.DefaultGroovyMethods.plus; import static org.codehaus.groovy.runtime.StringGroovyMethods.find; import java.io.PrintWriter; @@ -1523,10 +1525,10 @@ private void createConstructorDeclarations(ClassNode classNode, List cn.getDeclaredMethods("main").stream()).anyMatch(JAVA_MAIN); - case "isScript": - return !node.getStatementBlock().isEmpty() || (!node.getClasses().isEmpty() && - node.getClasses().get(0).getNameEnd() < 1 /* un-named */ && getGroovyVersion().getMajor() >= 5 && - node.getClasses().get(0).getDeclaredMethods("main").stream().anyMatch(JEP_445_MAIN.and(JAVA_MAIN.negate()))); - } + ModuleNode node = Adapters.adapt(receiver, ModuleNode.class); + if (node != null && !node.encounteredUnrecoverableError()) { + switch (property) { + case "hasMain": + return node.getClasses().stream().flatMap(cn -> cn.getDeclaredMethods("main").stream()).anyMatch(JAVA_MAIN); + case "isScript": + return !node.getStatementBlock().isEmpty() || (!node.getClasses().isEmpty() && + node.getClasses().get(0).getNameEnd() < 1 /* un-named */ && getGroovyVersion().getMajor() >= 5 && + node.getClasses().get(0).getDeclaredMethods("main").stream().anyMatch(JEP_445_MAIN.and(JAVA_MAIN.negate()))); } } return false; diff --git a/base/org.eclipse.jdt.groovy.core/src/org/eclipse/jdt/groovy/core/util/GroovyUtils.java b/base/org.eclipse.jdt.groovy.core/src/org/eclipse/jdt/groovy/core/util/GroovyUtils.java index 023ff1665e..267d2d1ce6 100644 --- a/base/org.eclipse.jdt.groovy.core/src/org/eclipse/jdt/groovy/core/util/GroovyUtils.java +++ b/base/org.eclipse.jdt.groovy.core/src/org/eclipse/jdt/groovy/core/util/GroovyUtils.java @@ -46,6 +46,7 @@ import org.codehaus.groovy.ast.Parameter; import org.codehaus.groovy.ast.PropertyNode; import org.codehaus.groovy.ast.Variable; +import org.codehaus.groovy.ast.expr.ConstantExpression; import org.codehaus.groovy.ast.expr.Expression; import org.codehaus.groovy.ast.expr.MethodCallExpression; import org.codehaus.groovy.ast.expr.TernaryExpression; @@ -427,7 +428,7 @@ public static Expression getTraitFieldExpression(MethodCallExpression call) { } else if (objType.equals(ClassHelper.CLASS_Type) && asBoolean(objType.getGenericsTypes())) { objType = objType.getGenericsTypes()[0].getType(); // look for $static$self.T__name$get() } - if (Traits.isTrait(objType)) { + if (Traits.isTrait(objType) && call.getMethod() instanceof ConstantExpression) { Matcher m = Pattern.compile(".+__(\\p{javaJavaIdentifierPart}+)\\$[gs]et").matcher(call.getMethodAsString()); if (m.matches()) { String fieldName = m.group(1); diff --git a/base/org.eclipse.jdt.groovy.core/src/org/eclipse/jdt/groovy/search/TypeInferencingVisitorWithRequestor.java b/base/org.eclipse.jdt.groovy.core/src/org/eclipse/jdt/groovy/search/TypeInferencingVisitorWithRequestor.java index 3b91925596..e449e987b8 100644 --- a/base/org.eclipse.jdt.groovy.core/src/org/eclipse/jdt/groovy/search/TypeInferencingVisitorWithRequestor.java +++ b/base/org.eclipse.jdt.groovy.core/src/org/eclipse/jdt/groovy/search/TypeInferencingVisitorWithRequestor.java @@ -2071,14 +2071,14 @@ private boolean handleSimpleExpression(final Expression node) { primaryType = null; // implicit-this calls are handled like free variables isStatic = scope.isStatic(); } else { - isStatic = mce.getObjectExpression() instanceof ClassExpression || primaryType.equals(VariableScope.CLASS_CLASS_NODE); + isStatic = mce.getObjectExpression() instanceof ClassExpression || VariableScope.CLASS_CLASS_NODE.equals(primaryType); } } else if (enclosingNode instanceof PropertyExpression) { PropertyExpression pe = (PropertyExpression) enclosingNode; - isStatic = pe.getObjectExpression() instanceof ClassExpression || primaryType.equals(VariableScope.CLASS_CLASS_NODE); + isStatic = pe.getObjectExpression() instanceof ClassExpression || VariableScope.CLASS_CLASS_NODE.equals(primaryType); } else if (enclosingNode instanceof MethodPointerExpression) { MethodPointerExpression mpe = (MethodPointerExpression) enclosingNode; - isStatic = mpe.getExpression() instanceof ClassExpression || primaryType.equals(VariableScope.CLASS_CLASS_NODE); + isStatic = mpe.getExpression() instanceof ClassExpression || VariableScope.CLASS_CLASS_NODE.equals(primaryType); } else /*if (enclosingNode instanceof ImportNode)*/ { isStatic = true; } diff --git a/ide-test/org.codehaus.groovy.alltests/src/org/codehaus/groovy/alltests/AllGroovyTests.groovy b/ide-test/org.codehaus.groovy.alltests/src/org/codehaus/groovy/alltests/AllGroovyTests.groovy index 60b0cc5acf..1911246eb4 100644 --- a/ide-test/org.codehaus.groovy.alltests/src/org/codehaus/groovy/alltests/AllGroovyTests.groovy +++ b/ide-test/org.codehaus.groovy.alltests/src/org/codehaus/groovy/alltests/AllGroovyTests.groovy @@ -1,5 +1,5 @@ /* - * Copyright 2009-2023 the original author or authors. + * Copyright 2009-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -94,6 +94,7 @@ import org.junit.runners.Suite // org.codehaus.groovy.eclipse.junit.tests org.codehaus.groovy.eclipse.junit.test.JUnit3TestFinderTests, org.codehaus.groovy.eclipse.junit.test.JUnit4TestFinderTests, + org.codehaus.groovy.eclipse.junit.test.JUnit5TestFinderTests, org.codehaus.groovy.eclipse.junit.test.MainMethodFinderTests, // org.codehaus.groovy.eclipse.quickfix.tests diff --git a/ide-test/org.codehaus.groovy.eclipse.junit.test/src/org/codehaus/groovy/eclipse/junit/test/JUnit3TestFinderTests.groovy b/ide-test/org.codehaus.groovy.eclipse.junit.test/src/org/codehaus/groovy/eclipse/junit/test/JUnit3TestFinderTests.groovy index 978a1caa8c..c4b9144aa0 100644 --- a/ide-test/org.codehaus.groovy.eclipse.junit.test/src/org/codehaus/groovy/eclipse/junit/test/JUnit3TestFinderTests.groovy +++ b/ide-test/org.codehaus.groovy.eclipse.junit.test/src/org/codehaus/groovy/eclipse/junit/test/JUnit3TestFinderTests.groovy @@ -1,5 +1,5 @@ /* - * Copyright 2009-2020 the original author or authors. + * Copyright 2009-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,87 +15,119 @@ */ package org.codehaus.groovy.eclipse.junit.test +import org.codehaus.groovy.eclipse.test.GroovyEclipseTestSuite +import org.codehaus.groovy.eclipse.test.SynchronizationUtils import org.eclipse.jdt.core.ICompilationUnit import org.eclipse.jdt.core.IType import org.eclipse.jdt.internal.junit.launcher.JUnit3TestFinder +import org.junit.Before import org.junit.Test -final class JUnit3TestFinderTests extends JUnitTestSuite { +final class JUnit3TestFinderTests extends GroovyEclipseTestSuite { - private void assertTypeIsTest(boolean expected, ICompilationUnit unit, String typeName, String reasonText = '') { + @Before + void setUp() { + addJUnit(3) + } + + private Set getAllTests() { + Set testTypes = [] + SynchronizationUtils.waitForIndexingToComplete() + new JUnit3TestFinder().findTestsInContainer(packageFragmentRoot, testTypes, null) + return testTypes + } + + private boolean isTest(ICompilationUnit unit, String typeName = unit.types[0].elementName) { def type = unit.getType(typeName) assert type.exists() : "Groovy type $typeName should exist" - assert new JUnit3TestFinder().isTest(type) == expected : "Groovy type $typeName should${expected ? '' : 'n\'t'} be a JUnit 3 test $reasonText" + return new JUnit3TestFinder().isTest(type) } + //-------------------------------------------------------------------------- + @Test - void testFinderWithSuite() { - def test = addGroovySource ''' - class A { - static junit.framework.Test suite() throws Exception {} + void testIsTest0() { + def unit = addGroovySource ''' + class C { + void test() { } } - ''' + ''', 'C', 'p' + assert !isTest(unit) + } - assertTypeIsTest(true, test, 'A') + @Test + void testIsTest1() { + def unit = addGroovySource ''' + class C extends junit.framework.TestCase { + void test() { } + } + ''', 'C', 'p' + assert isTest(unit) } @Test - void testFinderOfSubclass() { - def base = addGroovySource ''' - abstract class TestBase extends junit.framework.TestCase { + void testIsTest2() { + def unit = addGroovySource ''' + class C { + static junit.framework.Test suite() { } } - ''' + ''', 'C', 'p' + assert isTest(unit) + } - def test = addGroovySource ''' - class B extends TestBase { + @Test + void testIsTest3() { + def unit = addGroovySource ''' + abstract class TestBase extends junit.framework.TestCase { } - ''' + ''', 'TestBase', 'p' + assert !isTest(unit) - assertTypeIsTest(false, base, 'TestBase', '(it is abstract)') - assertTypeIsTest(true, test, 'B') + unit = addGroovySource ''' + class C extends TestBase { + } + ''', 'C', 'p' + assert isTest(unit) } @Test - void testFinderOfNonPublicSubclass() { - def base = addGroovySource ''' + void testIsTest4() { + def unit = addGroovySource ''' abstract class TestBase extends junit.framework.TestCase { } - ''' + ''', 'TestBase', 'p' + assert !isTest(unit) - def test = addGroovySource ''' + unit = addGroovySource ''' @groovy.transform.PackageScope class C extends TestBase { } - ''' - - assertTypeIsTest(false, base, 'TestBase', '(it is abstract)') - assertTypeIsTest(true, test, 'C') + ''', 'C', 'p' + assert isTest(unit) } + // + @Test - void testFindAllTestSuites() { + void testFindTests() { addGroovySource ''' abstract class TestBase extends junit.framework.TestCase { } - ''' - + ''', 'TestBase', 'p' addGroovySource ''' class X extends TestBase { } - ''' - + ''', 'X', 'p' addGroovySource ''' class Y extends junit.framework.TestCase { } - ''' - + ''', 'Y', 'p' addGroovySource ''' class Z { static junit.framework.Test suite() throws Exception {} } - ''' + ''', 'Z', 'p' - Set testTypes = [] - new JUnit3TestFinder().findTestsInContainer(packageFragmentRoot, testTypes, null) + Set testTypes = allTests assert testTypes.any { it.elementName == 'X' } : 'X should be a test type' assert testTypes.any { it.elementName == 'Y' } : 'Y should be a test type' diff --git a/ide-test/org.codehaus.groovy.eclipse.junit.test/src/org/codehaus/groovy/eclipse/junit/test/JUnit4TestFinderTests.groovy b/ide-test/org.codehaus.groovy.eclipse.junit.test/src/org/codehaus/groovy/eclipse/junit/test/JUnit4TestFinderTests.groovy index 26c8699bc6..a165f67f98 100644 --- a/ide-test/org.codehaus.groovy.eclipse.junit.test/src/org/codehaus/groovy/eclipse/junit/test/JUnit4TestFinderTests.groovy +++ b/ide-test/org.codehaus.groovy.eclipse.junit.test/src/org/codehaus/groovy/eclipse/junit/test/JUnit4TestFinderTests.groovy @@ -1,5 +1,5 @@ /* - * Copyright 2009-2022 the original author or authors. + * Copyright 2009-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,158 +17,196 @@ package org.codehaus.groovy.eclipse.junit.test import static org.eclipse.jdt.internal.compiler.impl.CompilerOptions.OPTIONG_GroovyCompilerConfigScript +import org.codehaus.groovy.eclipse.test.GroovyEclipseTestSuite +import org.codehaus.groovy.eclipse.test.SynchronizationUtils import org.eclipse.jdt.core.ICompilationUnit import org.eclipse.jdt.core.IType -import org.eclipse.jdt.internal.junit.JUnitPropertyTester import org.eclipse.jdt.internal.junit.launcher.JUnit4TestFinder +import org.junit.Before import org.junit.Test -final class JUnit4TestFinderTests extends JUnitTestSuite { +final class JUnit4TestFinderTests extends GroovyEclipseTestSuite { - private void assertTypeIsTest(boolean expected, ICompilationUnit unit, String typeName, String reasonText = '') { + @Before + void setUp() { + addJUnit(4) + } + + private Set getAllTests() { + Set testTypes = [] + SynchronizationUtils.waitForIndexingToComplete() + new JUnit4TestFinder().findTestsInContainer(packageFragmentRoot, testTypes, null) + return testTypes + } + + private boolean isTest(ICompilationUnit unit, String typeName = unit.types[0].elementName) { def type = unit.getType(typeName) assert type.exists() : "Groovy type $typeName should exist" - assert new JUnit4TestFinder().isTest(type) == expected : "Groovy type $typeName should${expected ? '' : 'n\'t'} be a JUnit 4 test $reasonText" + return new JUnit4TestFinder().isTest(type) } + //-------------------------------------------------------------------------- + @Test - void testFinderWithSuite() { - def test = addGroovySource ''' - class A { - static junit.framework.Test suite() throws Exception {} + void testIsTest0() { + def unit = addGroovySource ''' + class C { + void test() { } } - ''' - - assertTypeIsTest(true, test, 'A') + ''', 'C', 'p' + assert !isTest(unit) } @Test - void testFinderOfSubclass() { - def base = addGroovySource ''' - abstract class TestBase extends junit.framework.TestCase { + void testIsTest1() { + def unit = addGroovySource ''' + class C { + @org.junit.Test + void test() { } } - ''' + ''', 'C', 'p' + assert isTest(unit) + } - def test = addGroovySource ''' - class B extends TestBase { + @Test + void testIsTest2() { + def unit = addGroovySource ''' + class C { + static junit.framework.Test suite() { } } - ''' - - assertTypeIsTest(false, base, 'TestBase', '(it is abstract)') - assertTypeIsTest(true, test, 'B') + ''', 'C', 'p' + assert isTest(unit) } @Test - void testFinderOfNonPublicSubclass() { - def base = addGroovySource ''' + void testIsTest3() { + def unit = addGroovySource ''' abstract class TestBase extends junit.framework.TestCase { } - ''' + ''', 'TestBase', 'p' + assert !isTest(unit) - def test = addGroovySource ''' - @groovy.transform.PackageScope class C extends TestBase { + unit = addGroovySource ''' + class C extends TestBase { } - ''' - - assertTypeIsTest(false, base, 'TestBase', '(it is abstract)') - assertTypeIsTest(true, test, 'C') + ''', 'C', 'p' + assert isTest(unit) } @Test - void testUsingTestAnnotation() { - def test = addGroovySource ''' - class D { - @org.junit.Test - void method() {} + void testIsTest4() { + def unit = addGroovySource ''' + abstract class TestBase extends junit.framework.TestCase { } - ''' + ''', 'TestBase', 'p' + assert !isTest(unit) - assertTypeIsTest(true, test, 'D') + unit = addGroovySource ''' + @groovy.transform.PackageScope class C extends TestBase { + } + ''', 'C', 'p' + assert isTest(unit) } @Test - void testUsingRunWithAnnotation() { - def test = addGroovySource ''' + void testIsTest5() { + def unit = addGroovySource ''' @org.junit.runner.RunWith(org.junit.runners.Suite) - class E { - void method() {} + class C { + void test() { } } - ''' - - assertTypeIsTest(true, test, 'E') + ''', 'C', 'p' + assert isTest(unit) } - @Test // GRECLIPSE-569: @Test(expected=RuntimeException) not being found - void testFindTestWithExpectedException() { - def test = addGroovySource ''' - class F { + @Test // GRECLIPSE-569 + void testIsTest6() { + def unit = addGroovySource ''' + class C { @org.junit.Test(expected=RuntimeException) - void method() {} + void test() { } } - ''' + ''', 'C', 'p' + assert isTest(unit) + } + + @Test // https://github.com/groovy/groovy-eclipse/issues/1542 + void testIsTest7() { + addGroovySource '''import static java.lang.annotation.ElementType.* + @java.lang.annotation.Target([METHOD,CONSTRUCTOR]) + @interface A { } + ''', 'A', 'p' - boolean found = new JUnitPropertyTester().test(test, 'canLaunchAsJUnit', new Object[0], null) - assert found : "F should be a test type for $test.elementName" + def unit = addGroovySource ''' + class C { + @A m(x, y='foo', z='bar') { } + @org.junit.Test + void test() { } + } + ''', 'C', 'p' + assert isTest(unit) + + unit = addGroovySource ''' + class C { + @A C(x, y='foo', z='bar') { } + } + ''', 'C', 'p' + assert !isTest(unit) } + // + @Test - void testFindAllTestSuites() { + void testFindTests1() { addGroovySource ''' abstract class TestBase extends junit.framework.TestCase { } - ''' - + ''', 'C', 'p' addGroovySource ''' - class X3 extends TestBase { + class X1 extends TestBase { } - ''' - + ''', 'X1', 'p' addGroovySource ''' - class Y3 extends junit.framework.TestCase { + class Y1 extends junit.framework.TestCase { } - ''' - + ''', 'Y1', 'p' addGroovySource ''' - class Z3 { + class Z1 { static junit.framework.Test suite() throws Exception {} } - ''' - + ''', 'Z1', 'p' addGroovySource ''' - class X4 { + class X2 { @org.junit.Test void method() {} } - ''' - + ''', 'X2', 'p' addGroovySource ''' - class Y4 { + class Y2 { @org.junit.Test(expected = IllegalStateException) void method() {} } - ''' - + ''', 'Y2', 'p' addGroovySource ''' @org.junit.runner.RunWith(org.junit.runners.Suite) - class Z4 { + class Z2 { void method() {} } - ''' + ''', 'Z2', 'p' - Set testTypes = [] - new JUnit4TestFinder().findTestsInContainer(packageFragmentRoot, testTypes, null) + Set testTypes = allTests - assert testTypes.any { it.elementName == 'X3' } : 'X3 should be a test type' - assert testTypes.any { it.elementName == 'Y3' } : 'Y3 should be a test type' - assert testTypes.any { it.elementName == 'Z3' } : 'Z3 should be a test type' - assert testTypes.any { it.elementName == 'X4' } : 'X4 should be a test type' - assert testTypes.any { it.elementName == 'Y4' } : 'Y4 should be a test type' - assert testTypes.any { it.elementName == 'Z4' } : 'Z4 should be a test type' + assert testTypes.any { it.elementName == 'X1' } : 'X1 should be a test type' + assert testTypes.any { it.elementName == 'Y1' } : 'Y1 should be a test type' + assert testTypes.any { it.elementName == 'Z1' } : 'Z1 should be a test type' + assert testTypes.any { it.elementName == 'X2' } : 'X2 should be a test type' + assert testTypes.any { it.elementName == 'Y2' } : 'Y2 should be a test type' + assert testTypes.any { it.elementName == 'Z2' } : 'Z2 should be a test type' assert testTypes.size() == 6 } @Test - void testFindAllTestSuites2() { + void testFindTests2() { try { setJavaPreference(OPTIONG_GroovyCompilerConfigScript, 'config.groovy') addPlainText ''' @@ -180,28 +218,26 @@ final class JUnit4TestFinderTests extends JUnitTestSuite { normal 'org.junit.Test' } } - }''', '../config.groovy' + } + ''', '../config.groovy' addGroovySource ''' abstract class TestBase extends TestCase { } - ''' - + ''', 'TestBase', 'p' addGroovySource ''' class X3 extends TestBase { } - ''' - + ''', 'X3', 'p' addGroovySource ''' class X4 { @Test // unresolved if GroovyCompilationUnit skips config script void m() { } } - ''' + ''', 'X4', 'p' - Set testTypes = [] - new JUnit4TestFinder().findTestsInContainer(packageFragmentRoot, testTypes, null) + Set testTypes = allTests assert testTypes.any { it.elementName == 'X3' } : 'X3 should be a test type' assert testTypes.any { it.elementName == 'X4' } : 'X4 should be a test type' diff --git a/ide-test/org.codehaus.groovy.eclipse.junit.test/src/org/codehaus/groovy/eclipse/junit/test/JUnit5TestFinderTests.groovy b/ide-test/org.codehaus.groovy.eclipse.junit.test/src/org/codehaus/groovy/eclipse/junit/test/JUnit5TestFinderTests.groovy new file mode 100644 index 0000000000..7bd3d4c00e --- /dev/null +++ b/ide-test/org.codehaus.groovy.eclipse.junit.test/src/org/codehaus/groovy/eclipse/junit/test/JUnit5TestFinderTests.groovy @@ -0,0 +1,171 @@ +/* + * Copyright 2009-2024 the original author or authors. + * + * Licensed 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.codehaus.groovy.eclipse.junit.test + +import org.codehaus.groovy.eclipse.test.GroovyEclipseTestSuite +import org.eclipse.jdt.core.ICompilationUnit +import org.eclipse.jdt.internal.junit.launcher.JUnit5TestFinder +import org.junit.Before +import org.junit.Test + +final class JUnit5TestFinderTests extends GroovyEclipseTestSuite { + + @Before + void setUp() { + addJUnit(5) + } + + private boolean isTest(ICompilationUnit unit, String typeName = unit.types[0].elementName) { + def type = unit.getType(typeName) + assert type.exists() : "Groovy type $typeName should exist" + return new JUnit5TestFinder().isTest(type) + } + + //-------------------------------------------------------------------------- + + @Test + void testIsTest0() { + def unit = addGroovySource ''' + class C { + void test() { } + } + ''', 'C', 'p' + assert !isTest(unit) + } + + @Test + void testIsTest1() { + def unit = addGroovySource ''' + class C { + @org.junit.Test + void test() { } + } + ''', 'C', 'p' + assert isTest(unit) + } + + @Test + void testIsTest2() { + def unit = addGroovySource ''' + class C { + @org.junit.jupiter.api.Test + void test() { } + } + ''', 'C', 'p' + assert isTest(unit) + } + + @Test + void testIsTest3() { + def unit = addGroovySource ''' + class C { + @org.junit.jupiter.api.RepeatedTest(1) + void test() { } + } + ''', 'C', 'p' + assert isTest(unit) + } + + @Test + void testIsTest4() { + def unit = addGroovySource ''' + class C { + @org.junit.jupiter.params.ParameterizedTest + @org.junit.jupiter.params.provider.ValueSource(strings=['fizz','buzz']) + void test(String string) { } + } + ''', 'C', 'p' + assert isTest(unit) + } + + @Test + void testIsTest5() { + def unit = addGroovySource ''' + class C { + @org.junit.jupiter.params.ParameterizedTest + @org.junit.jupiter.params.provider.NullSource + @org.junit.jupiter.params.provider.EmptySource + void test(String string) { } + } + ''', 'C', 'p' + assert isTest(unit) + } + + @Test + void testIsTest6() { + def unit = addGroovySource '''import org.junit.jupiter.api.* + class C { + @TestFactory + DynamicTest test() { + DynamicTest.dynamicTest('test name') { -> assert true } + } + } + ''', 'C', 'p' + assert isTest(unit) + } + + @Test + void testIsTest7() { + def unit = addGroovySource ''' + class C { + @org.junit.jupiter.api.Nested + class D { + @org.junit.jupiter.api.Test + void test() { } + } + } + ''', 'C', 'p' + assert isTest(unit) + } + + @Test + void testIsTest8() { + def unit = addGroovySource ''' + @org.junit.jupiter.api.Disabled + class C { + @org.junit.jupiter.api.Test + void test() { } + } + ''', 'C', 'p' + assert isTest(unit) + } + + @Test + void testIsTest9() { + def unit = addGroovySource ''' + @org.junit.jupiter.api.Test + def @interface A { + } + class C { + @A + void test() { } + } + ''', 'C', 'p' + assert isTest(unit, 'C') + } + + @Test + void testIsTest10() { + def unit = addGroovySource ''' + class C { + def m(x='foo', y='bar') { } + @org.junit.jupiter.api.Test + void test() { } + } + ''', 'C', 'p' + assert isTest(unit) + } +} diff --git a/ide-test/org.codehaus.groovy.eclipse.junit.test/src/org/codehaus/groovy/eclipse/junit/test/JUnitTestSuite.groovy b/ide-test/org.codehaus.groovy.eclipse.junit.test/src/org/codehaus/groovy/eclipse/junit/test/JUnitTestSuite.groovy deleted file mode 100644 index 0558fcafc2..0000000000 --- a/ide-test/org.codehaus.groovy.eclipse.junit.test/src/org/codehaus/groovy/eclipse/junit/test/JUnitTestSuite.groovy +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright 2009-2021 the original author or authors. - * - * Licensed 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. - */ -/* - * Copyright 2009-2017 the original author or authors. - * - * Licensed 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 org.codehaus.groovy.eclipse.junit.test - -import groovy.transform.AutoFinal -import groovy.transform.CompileStatic - -import org.codehaus.groovy.eclipse.test.GroovyEclipseTestSuite -import org.codehaus.groovy.eclipse.test.SynchronizationUtils -import org.codehaus.jdt.groovy.model.GroovyCompilationUnit -import org.junit.Before - -@AutoFinal @CompileStatic -abstract class JUnitTestSuite extends GroovyEclipseTestSuite { - - @Before - final void setUpJUnitTestCase() { - addJUnit(4) - } - - @Override - protected GroovyCompilationUnit addGroovySource(CharSequence contents, String name = nextUnitName(), String pack = 'p') { - GroovyCompilationUnit unit = super.addGroovySource(contents, name, pack) - SynchronizationUtils.waitForIndexingToComplete() - return unit - } -} diff --git a/ide-test/org.codehaus.groovy.eclipse.junit.test/src/org/codehaus/groovy/eclipse/junit/test/MainMethodFinderTests.groovy b/ide-test/org.codehaus.groovy.eclipse.junit.test/src/org/codehaus/groovy/eclipse/junit/test/MainMethodFinderTests.groovy index f5f6c8bb70..ff7eb985f9 100644 --- a/ide-test/org.codehaus.groovy.eclipse.junit.test/src/org/codehaus/groovy/eclipse/junit/test/MainMethodFinderTests.groovy +++ b/ide-test/org.codehaus.groovy.eclipse.junit.test/src/org/codehaus/groovy/eclipse/junit/test/MainMethodFinderTests.groovy @@ -1,5 +1,5 @@ /* - * Copyright 2009-2020 the original author or authors. + * Copyright 2009-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,6 +15,7 @@ */ package org.codehaus.groovy.eclipse.junit.test +import org.codehaus.groovy.eclipse.test.GroovyEclipseTestSuite import org.eclipse.core.runtime.NullProgressMonitor import org.eclipse.jdt.internal.core.search.JavaWorkspaceScope import org.eclipse.jdt.internal.ui.util.MainMethodSearchEngine @@ -24,7 +25,7 @@ import org.junit.Test /** * Tests for {@link org.eclipse.jdt.internal.ui.util.MainMethodSearchEngine}. */ -final class MainMethodFinderTests extends JUnitTestSuite { +final class MainMethodFinderTests extends GroovyEclipseTestSuite { /** * @param expected fully-qualified type names @@ -42,21 +43,21 @@ final class MainMethodFinderTests extends JUnitTestSuite { @Test void testMainMethodFinder1() { addGroovySource ''' - class A { - static def main(args) {} + class C { + static main(args) { } } - ''' + ''' - expectTypesWithMain 'p.A' + expectTypesWithMain('C') } @Test void testMainMethodFinder2() { addGroovySource ''' - class B { - static def main(String... args) {} + class C { + static main(String... args) { } } - ''' + ''' expectTypesWithMain() } @@ -65,9 +66,9 @@ final class MainMethodFinderTests extends JUnitTestSuite { void testMainMethodFinder3() { addGroovySource ''' class C { - static def main(String[] args) {} + static main(String[] args) { } } - ''' + ''' expectTypesWithMain() } @@ -75,10 +76,10 @@ final class MainMethodFinderTests extends JUnitTestSuite { @Test void testMainMethodFinder4() { addGroovySource ''' - class D { - private static def main(String[] args) {} + class C { + private static main(String[] args) { } } - ''' + ''' expectTypesWithMain() } @@ -86,10 +87,10 @@ final class MainMethodFinderTests extends JUnitTestSuite { @Test void testMainMethodFinder5() { addGroovySource ''' - class E { - def main(String[] args) {} + class C { + def main(String[] args) { } } - ''' + ''' expectTypesWithMain() } @@ -98,7 +99,7 @@ final class MainMethodFinderTests extends JUnitTestSuite { void testMainMethodFinder6() { addGroovySource 'print "hello"', 'script1' - expectTypesWithMain 'p.script1' + expectTypesWithMain('script1') } @Test @@ -106,12 +107,12 @@ final class MainMethodFinderTests extends JUnitTestSuite { addGroovySource ''' print 'Hello' - class F { - static def main(args) {} + class C { + static def main(args) { } } - ''', 'script2' + ''', 'script2' - expectTypesWithMain 'p.script2', 'p.F' + expectTypesWithMain('script2', 'C') } @Test @@ -119,14 +120,14 @@ final class MainMethodFinderTests extends JUnitTestSuite { addGroovySource ''' print 'Hello' - class G { - static def main(args) {} + class A { + static main(args) { } } - class H { - static def main(args) {} + class B { + static main(args) { } } - ''', 'script3' + ''', 'script3' - expectTypesWithMain 'p.script3', 'p.G', 'p.H' + expectTypesWithMain('script3', 'A', 'B') } } diff --git a/ide-test/org.codehaus.groovy.eclipse.tests/src/org/codehaus/groovy/eclipse/test/adapters/IsScriptTesterTests.groovy b/ide-test/org.codehaus.groovy.eclipse.tests/src/org/codehaus/groovy/eclipse/test/adapters/IsScriptTesterTests.groovy index bc462b1bb9..195aba297a 100644 --- a/ide-test/org.codehaus.groovy.eclipse.tests/src/org/codehaus/groovy/eclipse/test/adapters/IsScriptTesterTests.groovy +++ b/ide-test/org.codehaus.groovy.eclipse.tests/src/org/codehaus/groovy/eclipse/test/adapters/IsScriptTesterTests.groovy @@ -23,8 +23,8 @@ import org.junit.Test final class IsScriptTesterTests extends GroovyEclipseTestSuite { - private void doTest(String name = 'Main', String text, boolean expected) { - boolean isScript = new GroovyPropertyTester().test(addGroovySource(text, name), 'isScript', null, null) + private void doTest(String text, boolean expected) { + boolean isScript = new GroovyPropertyTester().test(addGroovySource(text, nextUnitName()), 'isScript', null, null) assert (isScript == expected) : "Should have ${expected ? '' : '*not*'} found a script class in:\n$text" }