Converting tests to junit5 (#4699)

* started converting tests to junit5

* replaced TempFolder with JUnit TempDir

* migrated some test classes to JUnit5

* migrated a parameterized testclass to junit5

* Update core/pom.xml

Co-authored-by: Mark Waite <mark.earl.waite@gmail.com>

* replaced junit4 with junit5-vintage engine

Co-authored-by: Mark Waite <mark.earl.waite@gmail.com>
This commit is contained in:
StefanSpieker 2020-07-20 23:14:21 +02:00 committed by GitHub
parent 8cc32e956d
commit dcc9a924d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 155 additions and 135 deletions

View File

@ -40,6 +40,7 @@ THE SOFTWARE.
<properties>
<staplerFork>true</staplerFork>
<hamcrest.version>2.2</hamcrest.version>
<junit.jupiter.version>5.6.2</junit.jupiter.version>
</properties>
<dependencyManagement>
@ -428,8 +429,27 @@ THE SOFTWARE.
<artifactId>spring-aop</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>

View File

@ -25,12 +25,14 @@ package hudson;
import hudson.util.VersionNumber;
import jenkins.plugins.DetachedPluginsUtil;
import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import java.util.List;
import static jenkins.plugins.DetachedPluginsUtil.DetachedPlugin;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* @author <a href="mailto:tom.fennelly@gmail.com">tom.fennelly@gmail.com</a>
@ -41,21 +43,21 @@ public class ClassicPluginStrategyTest {
public void test_getDetachedPlugins() {
List<DetachedPlugin> list = DetachedPluginsUtil.getDetachedPlugins(new VersionNumber("1.296"));
Assert.assertTrue(list.size() >= 14); // There were 14 at the time of writing this test
Assert.assertNotNull(findPlugin("maven-plugin", list));
Assert.assertNotNull(findPlugin("subversion", list));
assertTrue(list.size() >= 14); // There were 14 at the time of writing this test
assertNotNull(findPlugin("maven-plugin", list));
assertNotNull(findPlugin("subversion", list));
// Narrow the list to since "1.310" (the subversion detach version).
list = DetachedPluginsUtil.getDetachedPlugins(new VersionNumber("1.310"));
// Maven should no longer be in the list, but subversion should.
Assert.assertNull(findPlugin("maven-plugin", list));
Assert.assertNotNull(findPlugin("subversion", list));
assertNull(findPlugin("maven-plugin", list));
assertNotNull(findPlugin("subversion", list));
// Narrow the list to since "1.311" (after the subversion detach version).
list = DetachedPluginsUtil.getDetachedPlugins(new VersionNumber("1.311"));
// Neither Maven or subversion should be in the list.
Assert.assertNull(findPlugin("maven-plugin", list));
Assert.assertNull(findPlugin("subversion", list));
assertNull(findPlugin("maven-plugin", list));
assertNull(findPlugin("subversion", list));
}
private DetachedPlugin findPlugin(String shortName, List<DetachedPlugin> list) {

View File

@ -25,15 +25,20 @@ package hudson;
import hudson.EnvVars.OverrideOrderCalculator;
import java.util.*;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.TreeMap;
import com.google.common.collect.Sets;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
/**
* @author Kohsuke Kawaguchi
*/
@ -118,7 +123,7 @@ public class EnvVarsTest {
OverrideOrderCalculator calc = new OverrideOrderCalculator(env, overrides);
List<String> order = calc.getOrderedVariableNames();
assertEquals(Arrays.asList("PATH"), order);
assertEquals(Collections.singletonList("PATH"), order);
}
@Test

View File

@ -23,14 +23,14 @@
*/
package hudson;
import static org.junit.Assert.assertEquals;
import hudson.MarkupText.SubText;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import java.util.List;
import java.util.regex.Pattern;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* @author Kohsuke Kawaguchi
*/
@ -60,8 +60,8 @@ public class MarkupTextTest {
public void findTokensOnSubText() {
MarkupText t = new MarkupText("Fixed 2 issues in this commit, fixing issue 155, 145");
List<SubText> tokens = t.findTokens(Pattern.compile("issue .*"));
assertEquals("Expected one token", 1, tokens.size());
assertEquals("Expected single token was incorrect", "issue 155, 145", tokens.get(0).group(0));
assertEquals(1, tokens.size(), "Expected one token");
assertEquals("issue 155, 145", tokens.get(0).group(0), "Expected single token was incorrect");
for (SubText st : tokens.get(0).findTokens(Pattern.compile("([0-9]+)")))
st.surroundWith("<$1>","<$1>");

View File

@ -28,35 +28,40 @@ import java.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import org.apache.tools.ant.filters.StringInputStream;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.xml.sax.SAXException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Path;
import java.util.jar.Attributes;
import java.util.jar.Manifest;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import org.apache.commons.io.FileUtils;
import org.jvnet.hudson.test.Issue;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsInstanceOf.instanceOf;
import static org.hamcrest.core.StringContains.containsString;
import static org.junit.Assert.assertEquals;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.fail;
import org.junit.Rule;
import org.junit.rules.TemporaryFolder;
import org.jvnet.hudson.test.Issue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
/**
* Tests of {@link PluginManager}.
*/
public class PluginManagerTest {
@Rule public TemporaryFolder tmp = new TemporaryFolder();
@TempDir Path tmp;
@Test public void parseRequestedPlugins() throws Exception {
assertEquals("{other=2.0, stuff=1.2}", new LocalPluginManager(tmp.getRoot())
@Test
public void parseRequestedPlugins() throws Exception {
Path output = Files.createFile(
tmp.resolve("output.txt")
);
assertEquals("{other=2.0, stuff=1.2}", new LocalPluginManager(output.toFile())
.parseRequestedPlugins(new StringInputStream("<root><stuff plugin='stuff@1.0'><more plugin='other@2.0'><things plugin='stuff@1.2'/></more></stuff></root>")).toString());
}
@ -145,12 +150,11 @@ public class PluginManagerTest {
"Plugin-Developers: ";
private File createHpiWithManifest() throws IOException {
File newFolder = tmp.newFolder("myJar");
String manifestPath = "META-INF/MANIFEST.MF";
new File("META-INF").mkdir();
FileUtils.write(new File(newFolder, manifestPath), SAMPLE_MANIFEST_FILE, StandardCharsets.UTF_8);
FileUtils.write(new File(tmp.toFile(), manifestPath), SAMPLE_MANIFEST_FILE, StandardCharsets.UTF_8);
final File f = new File(tmp.getRoot(), "my.hpi");
final File f = new File(tmp.toFile(), "my.hpi");
try(ZipOutputStream out = new ZipOutputStream(Files.newOutputStream(f.toPath()))) {
ZipEntry e = new ZipEntry(manifestPath);
out.putNextEntry(e);

View File

@ -10,36 +10,37 @@ import java.util.jar.Attributes;
import java.util.jar.Manifest;
import jenkins.model.Jenkins;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import org.jvnet.hudson.test.Issue;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class PluginWrapperTest {
private Locale loc;
private static Locale loc;
@Before
public void before() throws Exception {
@BeforeAll
public static void before() {
Jenkins.VERSION = "2.0"; // Some value needed - tests will overwrite if necessary
loc = Locale.getDefault();
Locale.setDefault(new Locale("en", "GB"));
}
@After
public void after() {
@AfterAll
public static void after() {
Locale.setDefault(loc);
}
@ -62,7 +63,7 @@ public class PluginWrapperTest {
}
@Test
public void jenkinsCoreTooOld() throws Exception {
public void jenkinsCoreTooOld() {
PluginWrapper pw = pluginWrapper("fake").requiredCoreVersion("3.0").buildLoaded();
try {
pw.resolvePluginDependencies();
@ -73,7 +74,7 @@ public class PluginWrapperTest {
}
@Test
public void dependencyNotInstalled() throws Exception {
public void dependencyNotInstalled() {
PluginWrapper pw = pluginWrapper("dependee").deps("dependency:42").buildLoaded();
try {
pw.resolvePluginDependencies();
@ -84,7 +85,7 @@ public class PluginWrapperTest {
}
@Test
public void dependencyOutdated() throws Exception {
public void dependencyOutdated() {
pluginWrapper("dependency").version("3").buildLoaded();
PluginWrapper pw = pluginWrapper("dependee").deps("dependency:5").buildLoaded();
try {
@ -96,7 +97,7 @@ public class PluginWrapperTest {
}
@Test
public void dependencyFailedToLoad() throws Exception {
public void dependencyFailedToLoad() {
pluginWrapper("dependency").version("5").buildFailed();
PluginWrapper pw = pluginWrapper("dependee").deps("dependency:3").buildLoaded();
try {

View File

@ -23,12 +23,12 @@
*/
package hudson;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import java.net.Proxy;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class ProxyConfigurationTest {
@Test

View File

@ -23,13 +23,13 @@
*/
package hudson.console;
import static org.junit.Assert.assertEquals;
import hudson.MarkupText;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.jvnet.hudson.test.Issue;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* @author Alan Harder
*/

View File

@ -31,31 +31,32 @@ import java.io.PrintStream;
import java.io.StringReader;
import org.apache.commons.io.output.NullOutputStream;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.jvnet.hudson.test.Issue;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class ComputerLauncherTest {
@Test(expected=IOException.class) public void jdk7() throws IOException {
assertChecked("java version \"1.7.0_05\"\nJava(TM) SE Runtime Environment (build 1.7.0_05-b05)\nJava HotSpot(TM) Server VM (build 23.1-b03, mixed mode)\n", "1.7.0");
@Test public void jdk7() {
assertThrows(IOException.class, () -> assertChecked("java version \"1.7.0_05\"\nJava(TM) SE Runtime Environment (build 1.7.0_05-b05)\nJava HotSpot(TM) Server VM (build 23.1-b03, mixed mode)\n", "1.7.0"));
}
@Test(expected=IOException.class) public void openJDK7() throws IOException {
assertChecked("openjdk version \"1.7.0-internal\"\nOpenJDK Runtime Environment (build 1.7.0-internal-pkgsrc_2010_01_03_06_54-b00)\nOpenJDK 64-Bit Server VM (build 17.0-b04, mixed mode)\n", "1.7.0");
@Test public void openJDK7() {
assertThrows(IOException.class, () -> assertChecked("openjdk version \"1.7.0-internal\"\nOpenJDK Runtime Environment (build 1.7.0-internal-pkgsrc_2010_01_03_06_54-b00)\nOpenJDK 64-Bit Server VM (build 17.0-b04, mixed mode)\n", "1.7.0"));
}
@Test(expected=IOException.class) public void jdk6() throws IOException {
assertChecked("java version \"1.6.0_33\"\nJava(TM) SE Runtime Environment (build 1.6.0_33-b03)\nJava HotSpot(TM) Server VM (build 20.8-b03, mixed mode)\n", "1.6.0");
@Test public void jdk6() {
assertThrows(IOException.class, () -> assertChecked("java version \"1.6.0_33\"\nJava(TM) SE Runtime Environment (build 1.6.0_33-b03)\nJava HotSpot(TM) Server VM (build 20.8-b03, mixed mode)\n", "1.6.0"));
}
@Test(expected=IOException.class) public void jdk5() throws IOException {
ComputerLauncher.checkJavaVersion(new PrintStream(new NullOutputStream()), "-", new BufferedReader(new StringReader("java version \"1.5.0_22\"\nJava(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_22-b03)\nJava HotSpot(TM) Server VM (build 1.5.0_22-b03, mixed mode)\n")));
@Test public void jdk5() {
assertThrows(IOException.class, () -> ComputerLauncher.checkJavaVersion(new PrintStream(new NullOutputStream()), "-", new BufferedReader(new StringReader("java version \"1.5.0_22\"\nJava(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_22-b03)\nJava HotSpot(TM) Server VM (build 1.5.0_22-b03, mixed mode)\n"))));
}
@Test(expected=IOException.class) public void j2sdk4() throws IOException {
ComputerLauncher.checkJavaVersion(new PrintStream(new NullOutputStream()), "-", new BufferedReader(new StringReader("java version \"1.4.2_19\"\nJava(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_19-b04)\nJava HotSpot(TM) Client VM (build 1.4.2_19-b04, mixed mode)\n")));
@Test public void j2sdk4() {
assertThrows(IOException.class, () -> ComputerLauncher.checkJavaVersion(new PrintStream(new NullOutputStream()), "-", new BufferedReader(new StringReader("java version \"1.4.2_19\"\nJava(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_19-b04)\nJava HotSpot(TM) Client VM (build 1.4.2_19-b04, mixed mode)\n"))));
}
@Test public void jdk8() throws IOException {
@ -102,7 +103,6 @@ public class ComputerLauncherTest {
ByteArrayOutputStream os = new ByteArrayOutputStream();
ComputerLauncher.checkJavaVersion(new PrintStream(os), "bin/java", new BufferedReader(new StringReader(text)));
String logged = os.toString();
assertTrue(logged, logged.contains(Messages.ComputerLauncher_JavaVersionResult("bin/java", spec)));
assertTrue(logged.contains(Messages.ComputerLauncher_JavaVersionResult("bin/java", spec)), logged);
}
}

View File

@ -23,8 +23,8 @@
*/
package hudson.slaves;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.*;
import jenkins.model.Jenkins;
@ -35,7 +35,7 @@ import java.io.File;
import java.nio.charset.Charset;
import org.apache.commons.io.FileUtils;
import org.junit.Test;
import org.junit.jupiter.api.Test;
/**
* @author Kohsuke Kawaguchi

View File

@ -23,7 +23,7 @@
*/
package hudson.triggers;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.jvnet.hudson.test.Issue;
/**

View File

@ -24,63 +24,47 @@
package jenkins.model.labels;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.assertEquals;
/**
*
* @author dty
*/
@RunWith(Parameterized.class)
public class LabelAutoCompleteSeederTest {
public static class TestData {
private final String seed;
private final List<String> expected;
public TestData(String seed, String... expected) {
this.seed = seed;
this.expected = Arrays.asList(expected);
}
}
@Parameterized.Parameters
public static Collection<Object[]> data() {
return Arrays.asList( new Object[][] {
{ new TestData("", "") },
{ new TestData("\"", "") },
{ new TestData("\"\"", "") },
{ new TestData("freebsd", "freebsd") },
{ new TestData(" freebsd", "freebsd") },
{ new TestData("freebsd ", "") },
{ new TestData("freebsd 6", "6") },
{ new TestData("\"freebsd", "freebsd") },
{ new TestData("\"freebsd ", "freebsd ") },
{ new TestData("\"freebsd\"", "") },
{ new TestData("\"freebsd\" ", "") },
{ new TestData("\"freebsd 6", "freebsd 6") },
{ new TestData("\"freebsd 6\"", "") },
});
static Stream<Arguments> localParameters()
{
return Stream.of(
Arguments.of("", Collections.singletonList("")),
Arguments.of("\"", Collections.singletonList("")),
Arguments.of("\"\"", Collections.singletonList("")),
Arguments.of("freebsd", Collections.singletonList("freebsd")),
Arguments.of(" freebsd", Collections.singletonList("freebsd")),
Arguments.of("freebsd ", Collections.singletonList("")),
Arguments.of("freebsd 6", Collections.singletonList("6")),
Arguments.of("\"freebsd", Collections.singletonList("freebsd")),
Arguments.of("\"freebsd ", Collections.singletonList("freebsd ")),
Arguments.of("\"freebsd\"", Collections.singletonList("")),
Arguments.of("\"freebsd\" ", Collections.singletonList("")),
Arguments.of("\"freebsd 6", Collections.singletonList("freebsd 6")),
Arguments.of("\"freebsd 6\"", Collections.singletonList(""))
);
}
private final String seed;
private final List<String> expected;
public LabelAutoCompleteSeederTest(TestData dataSet) {
this.seed = dataSet.seed;
this.expected = dataSet.expected;
}
@Test
public void testAutoCompleteSeeds() throws Exception {
LabelAutoCompleteSeeder seeder = new LabelAutoCompleteSeeder(seed);
@ParameterizedTest( name = "{index}" )
@MethodSource( "localParameters" )
public void testAutoCompleteSeeds(String underTest, List<String> expected) {
LabelAutoCompleteSeeder seeder = new LabelAutoCompleteSeeder(underTest);
assertEquals(expected, seeder.getSeeds());
}
}
}

View File

@ -1,13 +1,16 @@
package jenkins.util;
import hudson.util.OneShotEvent;
import org.junit.Test;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicInteger;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import org.junit.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
public class AtmostOneTaskExecutorTest {

View File

@ -1,11 +1,8 @@
package jenkins.util;
import static org.junit.Assert.fail;
import groovy.lang.GroovyClassLoader;
import hudson.triggers.SafeTimerTask;
import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.jvnet.hudson.test.Issue;
import java.util.concurrent.CountDownLatch;
@ -13,6 +10,9 @@ import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
public class TimerTest {
/**
@ -35,7 +35,7 @@ public class TimerTest {
SafeTimerTask task2 = new SafeTimerTask() {
@Override
protected void doRun() throws Exception {
protected void doRun() {
stopLatch.countDown();
}
};
@ -60,7 +60,7 @@ public class TimerTest {
final CountDownLatch startLatch = new CountDownLatch(threadCount);
final ClassLoader[] contextClassloaders = new ClassLoader[threadCount];
ScheduledFuture[] futures = new ScheduledFuture[threadCount];
ScheduledFuture<?>[] futures = new ScheduledFuture[threadCount];
final ClassLoader bogusClassloader = new GroovyClassLoader();
Runnable timerTest = new Runnable() {
@ -88,12 +88,12 @@ public class TimerTest {
};
Thread t = new Thread(timerTest);
t.run();
t.start();
t.join(1000L);
for (int i=0; i<threadCount; i++) {
futures[i].get();
Assert.assertEquals(Timer.class.getClassLoader(), contextClassloaders[i]);
assertEquals(Timer.class.getClassLoader(), contextClassloaders[i]);
}
}
}

View File

@ -1,10 +1,11 @@
package jenkins.util;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.jvnet.hudson.test.Issue;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class UrlHelperTest {