From 45bca23f7b4c03193e24bb0471057adfb51111d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20L=C3=A4ubrich?= Date: Fri, 31 May 2024 07:12:28 +0200 Subject: [PATCH] [backport] Add support for JUnit 'vintage' attribute on classpath JDT supports a new classpath attribute on the JUnit5 container that disables the vintage engine. This is especially important if one wants to write plain JUnit5 test as it is otherwise easy to mix up Junit4/5. This now adds a testcase that shows that such mixed usage would fail compilation. (cherry picked from commit 13489c62a9464a9c71d4e0a4fefbbc65feb25ae8) --- .../bundle5.no.vintage.test/.classpath | 27 ++++++++++ .../META-INF/MANIFEST.MF | 9 ++++ .../bundle5.no.vintage.test/build.properties | 4 ++ .../bundle5.no.vintage.test/pom.xml | 49 +++++++++++++++++++ .../src/bundle/test/CountDown.java | 38 ++++++++++++++ .../src/bundle/test/Counter.java | 29 +++++++++++ .../src2/bundle/test/RefMe.java | 18 +++++++ .../src_test/bundle/test/AdderTest.java | 35 +++++++++++++ .../src_test/bundle/test/SubtractorTest.java | 40 +++++++++++++++ .../test/surefire/TestsInBundleTest.java | 22 ++++++--- .../model/classpath/ClasspathParser.java | 15 +++++- .../JUnitClasspathContainerEntry.java | 14 ++++++ 12 files changed, 291 insertions(+), 9 deletions(-) create mode 100644 tycho-its/projects/surefire.combinedtests/bundle5.no.vintage.test/.classpath create mode 100644 tycho-its/projects/surefire.combinedtests/bundle5.no.vintage.test/META-INF/MANIFEST.MF create mode 100644 tycho-its/projects/surefire.combinedtests/bundle5.no.vintage.test/build.properties create mode 100644 tycho-its/projects/surefire.combinedtests/bundle5.no.vintage.test/pom.xml create mode 100644 tycho-its/projects/surefire.combinedtests/bundle5.no.vintage.test/src/bundle/test/CountDown.java create mode 100644 tycho-its/projects/surefire.combinedtests/bundle5.no.vintage.test/src/bundle/test/Counter.java create mode 100644 tycho-its/projects/surefire.combinedtests/bundle5.no.vintage.test/src2/bundle/test/RefMe.java create mode 100644 tycho-its/projects/surefire.combinedtests/bundle5.no.vintage.test/src_test/bundle/test/AdderTest.java create mode 100644 tycho-its/projects/surefire.combinedtests/bundle5.no.vintage.test/src_test/bundle/test/SubtractorTest.java diff --git a/tycho-its/projects/surefire.combinedtests/bundle5.no.vintage.test/.classpath b/tycho-its/projects/surefire.combinedtests/bundle5.no.vintage.test/.classpath new file mode 100644 index 0000000000..42e20b6d54 --- /dev/null +++ b/tycho-its/projects/surefire.combinedtests/bundle5.no.vintage.test/.classpath @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tycho-its/projects/surefire.combinedtests/bundle5.no.vintage.test/META-INF/MANIFEST.MF b/tycho-its/projects/surefire.combinedtests/bundle5.no.vintage.test/META-INF/MANIFEST.MF new file mode 100644 index 0000000000..cf690342c7 --- /dev/null +++ b/tycho-its/projects/surefire.combinedtests/bundle5.no.vintage.test/META-INF/MANIFEST.MF @@ -0,0 +1,9 @@ +Manifest-Version: 1.0 +Bundle-ManifestVersion: 2 +Bundle-Name: Test Plug-in +Bundle-SymbolicName: bundle.test5 +Bundle-Version: 1.0.0 +Import-Package: javax.annotation, + org.osgi.framework +Automatic-Module-Name: bundle.test5 +Bundle-RequiredExecutionEnvironment: JavaSE-11 diff --git a/tycho-its/projects/surefire.combinedtests/bundle5.no.vintage.test/build.properties b/tycho-its/projects/surefire.combinedtests/bundle5.no.vintage.test/build.properties new file mode 100644 index 0000000000..92fe2f2b3b --- /dev/null +++ b/tycho-its/projects/surefire.combinedtests/bundle5.no.vintage.test/build.properties @@ -0,0 +1,4 @@ +source.. = src/,src2/ +output.. = bin/ +bin.includes = META-INF/,\ + . diff --git a/tycho-its/projects/surefire.combinedtests/bundle5.no.vintage.test/pom.xml b/tycho-its/projects/surefire.combinedtests/bundle5.no.vintage.test/pom.xml new file mode 100644 index 0000000000..59fc27d97c --- /dev/null +++ b/tycho-its/projects/surefire.combinedtests/bundle5.no.vintage.test/pom.xml @@ -0,0 +1,49 @@ + + + 4.0.0 + org.eclipse.tycho.tycho-its.surefire + bundle.test5 + eclipse-plugin + 1.0.0 + + https:////download.eclipse.org/releases/2022-12/ + 4.0.0-SNAPSHOT + + + + platform + ${target-platform} + p2 + + + + + + org.eclipse.tycho + tycho-maven-plugin + ${tycho-version} + true + + + org.apache.maven.plugins + maven-surefire-plugin + 3.0.0-M5 + + + org.apache.maven.surefire + surefire-junit47 + 3.0.0-M5 + + + + + execute-tests + + test + + + + + + + diff --git a/tycho-its/projects/surefire.combinedtests/bundle5.no.vintage.test/src/bundle/test/CountDown.java b/tycho-its/projects/surefire.combinedtests/bundle5.no.vintage.test/src/bundle/test/CountDown.java new file mode 100644 index 0000000000..ca27d5b7c4 --- /dev/null +++ b/tycho-its/projects/surefire.combinedtests/bundle5.no.vintage.test/src/bundle/test/CountDown.java @@ -0,0 +1,38 @@ +/******************************************************************************* + * Copyright (c) 2021 Christoph Läubrich and others. + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Christoph Läubrich. - initial API and implementation + *******************************************************************************/ +package bundle.test; + +public class CountDown { + + RefMe refFromOtherSourceFolder; + + int count; + + public CountDown(int initalValue) { + count = initalValue; + } + + public void decrement(int x) { + if (x < 0) { + throw new IllegalArgumentException(); + } + if (count-x < 0) { + throw new IllegalStateException(); + } + count -= x; + } + + public int get() { + return count; + } +} diff --git a/tycho-its/projects/surefire.combinedtests/bundle5.no.vintage.test/src/bundle/test/Counter.java b/tycho-its/projects/surefire.combinedtests/bundle5.no.vintage.test/src/bundle/test/Counter.java new file mode 100644 index 0000000000..133e1b749b --- /dev/null +++ b/tycho-its/projects/surefire.combinedtests/bundle5.no.vintage.test/src/bundle/test/Counter.java @@ -0,0 +1,29 @@ +/******************************************************************************* + * Copyright (c) 2021 Christoph Läubrich and others. + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Christoph Läubrich. - initial API and implementation + *******************************************************************************/ +package bundle.test; + +public class Counter { + + int count; + + public void increment(int x) { + if (x < 0) { + throw new IllegalArgumentException(); + } + count += x; + } + + public int get() { + return count; + } +} diff --git a/tycho-its/projects/surefire.combinedtests/bundle5.no.vintage.test/src2/bundle/test/RefMe.java b/tycho-its/projects/surefire.combinedtests/bundle5.no.vintage.test/src2/bundle/test/RefMe.java new file mode 100644 index 0000000000..2b479d82e1 --- /dev/null +++ b/tycho-its/projects/surefire.combinedtests/bundle5.no.vintage.test/src2/bundle/test/RefMe.java @@ -0,0 +1,18 @@ +/******************************************************************************* + * Copyright (c) 2021 Christoph Läubrich and others. + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Christoph Läubrich. - initial API and implementation + *******************************************************************************/ + +package bundle.test; + +public class RefMe extends Counter{ + +} diff --git a/tycho-its/projects/surefire.combinedtests/bundle5.no.vintage.test/src_test/bundle/test/AdderTest.java b/tycho-its/projects/surefire.combinedtests/bundle5.no.vintage.test/src_test/bundle/test/AdderTest.java new file mode 100644 index 0000000000..e91f3fcf5b --- /dev/null +++ b/tycho-its/projects/surefire.combinedtests/bundle5.no.vintage.test/src_test/bundle/test/AdderTest.java @@ -0,0 +1,35 @@ +/******************************************************************************* + * Copyright (c) 2021 Christoph Läubrich and others. + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Christoph Läubrich. - initial API and implementation + *******************************************************************************/ +package bundle.test; + +import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +public class AdderTest { + + @org.junit.jupiter.api.Test + public void incrementTest() { + Counter counter = new Counter(); + counter.increment(1); + counter.increment(3); + assertEquals(4, counter.get()); + } + + @org.junit.jupiter.api.Test + public void decrementTest() { + assertThrows(IllegalArgumentException.class, ()->{ + Counter counter = new Counter(); + counter.increment(-1); + }); + } +} diff --git a/tycho-its/projects/surefire.combinedtests/bundle5.no.vintage.test/src_test/bundle/test/SubtractorTest.java b/tycho-its/projects/surefire.combinedtests/bundle5.no.vintage.test/src_test/bundle/test/SubtractorTest.java new file mode 100644 index 0000000000..93100efa18 --- /dev/null +++ b/tycho-its/projects/surefire.combinedtests/bundle5.no.vintage.test/src_test/bundle/test/SubtractorTest.java @@ -0,0 +1,40 @@ +/******************************************************************************* + * Copyright (c) 2021 Christoph Läubrich and others. + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Christoph Läubrich - initial API and implementation + *******************************************************************************/ +package bundle.test; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +public class SubtractorTest { + + @Test + public void incrementTest() { + CountDown counter = new CountDown(10); + counter.decrement(1); + counter.decrement(3); + assertEquals(6, counter.get()); + } + + @Test(expected = IllegalArgumentException.class) + public void decrementTest() { + CountDown counter = new CountDown(10); + counter.decrement(-1); + } + + @Test(expected = IllegalStateException.class) + public void decrementTest2() { + CountDown counter = new CountDown(1); + counter.decrement(5); + } +} diff --git a/tycho-its/src/test/java/org/eclipse/tycho/test/surefire/TestsInBundleTest.java b/tycho-its/src/test/java/org/eclipse/tycho/test/surefire/TestsInBundleTest.java index febfa5ba90..d21704006e 100644 --- a/tycho-its/src/test/java/org/eclipse/tycho/test/surefire/TestsInBundleTest.java +++ b/tycho-its/src/test/java/org/eclipse/tycho/test/surefire/TestsInBundleTest.java @@ -12,8 +12,8 @@ *******************************************************************************/ package org.eclipse.tycho.test.surefire; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; import java.io.File; import java.util.Arrays; @@ -47,6 +47,15 @@ public void testCompile5() throws Exception { new File(verifier.getBasedir(), "target/test-classes/bundle/test/AdderTest.class").exists()); } + @Test + public void testCompile5WithoutVintage() throws Exception { + Verifier verifier = getVerifier("surefire.combinedtests/bundle5.no.vintage.test"); + assertThrows("Compilation must fail because the usage of junit 4 annotations", VerificationException.class, + () -> verifier.executeGoals(Arrays.asList("clean", "test-compile"))); + verifier.verifyTextInLog("The import org.junit.Assert cannot be resolved"); + verifier.verifyTextInLog("The import org.junit.Test cannot be resolved"); + } + @Test public void testTest() throws Exception { Verifier verifier = getVerifier("surefire.combinedtests/bundle.test"); @@ -70,13 +79,10 @@ public void testIntegrationTest() throws Exception { @Test public void testVerify() throws Exception { Verifier verifier = getVerifier("surefire.combinedtests/bundle.test"); - try { - verifier.executeGoals(Arrays.asList("clean", "verify")); - fail("the build succeed but test-failures are expected!"); - } catch (VerificationException e) { - // thats good indeed... - verifier.verifyTextInLog("There are test failures"); - } + assertThrows("the build succeed but test-failures are expected!", VerificationException.class, + () -> verifier.executeGoals(Arrays.asList("clean", "verify"))); + // thats good indeed... + verifier.verifyTextInLog("There are test failures"); } } diff --git a/tycho-metadata-model/src/main/java/org/eclipse/tycho/model/classpath/ClasspathParser.java b/tycho-metadata-model/src/main/java/org/eclipse/tycho/model/classpath/ClasspathParser.java index 01a2459bcb..65f654596e 100644 --- a/tycho-metadata-model/src/main/java/org/eclipse/tycho/model/classpath/ClasspathParser.java +++ b/tycho-metadata-model/src/main/java/org/eclipse/tycho/model/classpath/ClasspathParser.java @@ -220,11 +220,24 @@ public Collection getArtifacts() { } else if (JUNIT4.equals(junit)) { return JUNIT4_PLUGINS; } else if (JUNIT5.equals(junit)) { - return JUNIT5_PLUGINS; + if (isVintage()) { + return JUNIT5_PLUGINS; + } else { + return JUNIT5_WITHOUT_VINTAGE_PLUGINS; + } } return Collections.emptyList(); } + @Override + public boolean isVintage() { + String vintage = getAttributes().get("vintage"); + if (vintage != null && !vintage.isBlank()) { + return Boolean.parseBoolean(vintage); + } + return true; + } + } private static class JDTContainerClasspathEntry implements ClasspathContainerEntry { diff --git a/tycho-metadata-model/src/main/java/org/eclipse/tycho/model/classpath/JUnitClasspathContainerEntry.java b/tycho-metadata-model/src/main/java/org/eclipse/tycho/model/classpath/JUnitClasspathContainerEntry.java index 38a3abd0e2..30ddf46b19 100644 --- a/tycho-metadata-model/src/main/java/org/eclipse/tycho/model/classpath/JUnitClasspathContainerEntry.java +++ b/tycho-metadata-model/src/main/java/org/eclipse/tycho/model/classpath/JUnitClasspathContainerEntry.java @@ -76,6 +76,12 @@ public interface JUnitClasspathContainerEntry extends ClasspathContainerEntry { JUNIT_PLATFORM_SUITE_API_PLUGIN, JUNIT_VINTAGE_ENGINE_PLUGIN, JUNIT_OPENTEST4J_PLUGIN, JUNIT_APIGUARDIAN_PLUGIN, JUNIT4_PLUGIN, HAMCREST_CORE_PLUGIN); + static final List JUNIT5_WITHOUT_VINTAGE_PLUGINS = List.of(JUNIT_JUPITER_API_PLUGIN, + JUNIT_JUPITER_ENGINE_PLUGIN, JUNIT_JUPITER_MIGRATIONSUPPORT_PLUGIN, JUNIT_JUPITER_PARAMS_PLUGIN, + JUNIT_PLATFORM_COMMONS_PLUGIN, JUNIT_PLATFORM_ENGINE_PLUGIN, JUNIT_PLATFORM_LAUNCHER_PLUGIN, + JUNIT_PLATFORM_RUNNER_PLUGIN, JUNIT_PLATFORM_SUITE_API_PLUGIN, JUNIT_OPENTEST4J_PLUGIN, + JUNIT_APIGUARDIAN_PLUGIN, HAMCREST_CORE_PLUGIN); + /** * * @return the JUnit part of the path @@ -93,4 +99,12 @@ default boolean isTest() { return true; } + /** + * Checks if for JUnit5 the vintage engine has to be included. This is only meaningful if + * {@link #getJUnitSegment()} is equal to {@link #JUNIT5} + * + * @return true if vintage is enabled (the default) + */ + boolean isVintage(); + }