Skip to content

Commit

Permalink
Add support for JUnit 'vintage' attribute on classpath
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
laeubi committed May 31, 2024
1 parent 3fa8b30 commit 13489c6
Show file tree
Hide file tree
Showing 12 changed files with 286 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" output="test" path="src_test">
<attributes>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src2"/>
<classpathentry kind="src" output="test" path="integration-tests">
<attributes>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5">
<attributes>
<attribute name="vintage" value="false" />
</attributes>
</classpathentry>
<classpathentry kind="output" path="bin"/>
</classpath>
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source.. = src/,src2/
output.. = bin/
bin.includes = META-INF/,\
.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?xml version="1.0" encoding="UTF-8"?>
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>org.eclipse.tycho.tycho-its.surefire</groupId>
<artifactId>bundle.test5</artifactId>
<packaging>eclipse-plugin</packaging>
<version>1.0.0</version>
<properties>
<target-platform>https:////download.eclipse.org/releases/2022-12/</target-platform>
<tycho-version>4.0.0-SNAPSHOT</tycho-version>
</properties>
<repositories>
<repository>
<id>platform</id>
<url>${target-platform}</url>
<layout>p2</layout>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-maven-plugin</artifactId>
<version>${tycho-version}</version>
<extensions>true</extensions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>3.0.0-M5</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>execute-tests</id>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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{

}
Original file line number Diff line number Diff line change
@@ -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);
});
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,24 @@ public Collection<JUnitBundle> 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<JUnitBundle> 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
Expand All @@ -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 <code>true</code> if vintage is enabled (the default)
*/
boolean isVintage();

}

0 comments on commit 13489c6

Please sign in to comment.