From ab5d044dcc31e4283b3be9e6c053ac18d0c3bbcb Mon Sep 17 00:00:00 2001 From: Tim Jacomb <21194782+timja@users.noreply.github.com> Date: Mon, 18 Jan 2021 16:04:12 +0000 Subject: [PATCH] Fix tests on newer java versions (#5182) --- core/src/test/java/hudson/FunctionsTest.java | 10 ++++++--- .../plugins/DetachedPluginsUtilTest.java | 21 ++++++++++++------- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/core/src/test/java/hudson/FunctionsTest.java b/core/src/test/java/hudson/FunctionsTest.java index c8365a1f78..9b4efeaadb 100644 --- a/core/src/test/java/hudson/FunctionsTest.java +++ b/core/src/test/java/hudson/FunctionsTest.java @@ -43,6 +43,9 @@ import java.util.regex.Pattern; import hudson.util.VersionNumber; import jenkins.model.Jenkins; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; @@ -517,7 +520,8 @@ public class FunctionsTest { stack1.cause(stack2); stack2.cause(stack1); //Format changed in 11.0.9 / 8.0.272 (JDK-8226809 / JDK-8252444 / JDK-8252489) - if ((getVersion().getDigitAt(0) == 11 && getVersion().isNewerThanOrEqualTo(new VersionNumber("11.0.9"))) || + + if ((getVersion().isNewerThanOrEqualTo(new VersionNumber("11.0.9"))) || (getVersion().getDigitAt(0) == 8 && getVersion().isNewerThanOrEqualTo(new VersionNumber("8.0.272")))) { assertPrintThrowable(stack1, "p.Exc1\n" + @@ -554,10 +558,10 @@ public class FunctionsTest { private static void assertPrintThrowable(Throwable t, String traditional, String custom) { StringWriter sw = new StringWriter(); t.printStackTrace(new PrintWriter(sw)); - assertEquals(sw.toString().replace(System.lineSeparator(), "\n"), traditional); + assertThat(sw.toString().replace(System.lineSeparator(), "\n"), is(traditional)); String actual = Functions.printThrowable(t); System.out.println(actual); - assertEquals(actual.replace(System.lineSeparator(), "\n"), custom); + assertThat(actual.replace(System.lineSeparator(), "\n"), is(custom)); } private static final class Stack extends Throwable { private static final Pattern LINE = Pattern.compile("(.+)[.](.+)[.](.+):(\\d+)"); diff --git a/core/src/test/java/jenkins/plugins/DetachedPluginsUtilTest.java b/core/src/test/java/jenkins/plugins/DetachedPluginsUtilTest.java index 97c136efba..a18f26c4cb 100644 --- a/core/src/test/java/jenkins/plugins/DetachedPluginsUtilTest.java +++ b/core/src/test/java/jenkins/plugins/DetachedPluginsUtilTest.java @@ -10,6 +10,10 @@ import java.nio.charset.StandardCharsets; import java.util.List; import java.util.stream.Collectors; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.contains; +import static org.hamcrest.Matchers.hasItem; +import static org.hamcrest.Matchers.not; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; @@ -27,22 +31,23 @@ public class DetachedPluginsUtilTest { assertEquals(new VersionNumber("11"), jaxb.getMinimumJavaVersion()); - final List detachedPlugins = DetachedPluginsUtil.getDetachedPlugins(); + final List detachedPlugins = mapToPluginShortName(DetachedPluginsUtil.getDetachedPlugins()); if (JavaUtils.isRunningWithJava8OrBelow()) { - assertEquals(0, detachedPlugins.stream() - .filter(plugin -> plugin.getShortName().equals("jaxb")).count()); - } else if (JavaUtils.getCurrentJavaRuntimeVersionNumber().isNewerThanOrEqualTo(new VersionNumber("11.0.2"))) { - assertEquals(1, detachedPlugins.stream() - .filter(plugin -> plugin.getShortName().equals("jaxb")).count()); + assertThat(detachedPlugins, not(hasItem("jaxb"))); + } else { + assertThat(detachedPlugins, hasItem("jaxb")); final List detachedPluginsSince2_161 = DetachedPluginsUtil.getDetachedPlugins(new VersionNumber("2.161")); - assertEquals(1, detachedPluginsSince2_161.size()); - assertEquals("jaxb", detachedPluginsSince2_161.get(0).getShortName()); + assertThat(mapToPluginShortName(detachedPluginsSince2_161), contains("jaxb", "trilead-api")); } } + private List mapToPluginShortName(List detachedPlugins) { + return detachedPlugins.stream().map(DetachedPluginsUtil.DetachedPlugin::getShortName).collect(Collectors.toList()); + } + /** * Checks the format of the {@code /jenkins/split-plugins.txt} file has maximum 4 columns. */