From e1baba9fd4144f04931652dee20db4c06a68d466 Mon Sep 17 00:00:00 2001 From: strangelookingnerd <49242855+strangelookingnerd@users.noreply.github.com> Date: Mon, 14 Apr 2025 16:10:34 +0200 Subject: [PATCH] Migrate tests to JUnit5 * Migrate annotations and imports * Migrate assertions * Remove public visibility for test classes and methods * Minor code cleanup --- .../structs/SymbolLookupAnticacheTest.java | 33 ++- .../plugins/structs/SymbolLookupTest.java | 62 +++-- .../describable/DescribableModelTest.java | 263 ++++++++++-------- .../structs/describable/Security3371Test.java | 32 +-- .../UninstantiatedDescribableTest.java | 17 +- 5 files changed, 220 insertions(+), 187 deletions(-) diff --git a/src/test/java/org/jenkinsci/plugins/structs/SymbolLookupAnticacheTest.java b/src/test/java/org/jenkinsci/plugins/structs/SymbolLookupAnticacheTest.java index 60367e8..ecaeb32 100644 --- a/src/test/java/org/jenkinsci/plugins/structs/SymbolLookupAnticacheTest.java +++ b/src/test/java/org/jenkinsci/plugins/structs/SymbolLookupAnticacheTest.java @@ -2,32 +2,32 @@ import hudson.model.Descriptor; import jenkins.tasks.SimpleBuildStep; -import org.junit.Assert; -import org.junit.Rule; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.jvnet.hudson.test.JenkinsRule; -import org.jvnet.hudson.test.Url; +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; import java.io.File; -import java.net.URI; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; /** * @author Sam Van Oort */ -public class SymbolLookupAnticacheTest { - @Rule - public JenkinsRule rule = new JenkinsRule(); +@WithJenkins +class SymbolLookupAnticacheTest { /** Verify that if we install a new plugin with a Symbol use, that symbol is found. * Without the plugin we hit the "anticache" i.e. {@link SymbolLookup#noHitCache} that the symbol does not exist. * Once we add the plugin it should hit the cache. */ @Test - public void testAnticache() throws Exception{ + void testAnticache(JenkinsRule rule) throws Exception{ Descriptor d = SymbolLookup.get().findDescriptor(SimpleBuildStep.class, "trivialBuilder"); Descriptor stepDescriptor = SymbolLookup.get().find(Descriptor.class, "trivialBuilder"); - assert d == null; - assert stepDescriptor == null; + assertNull(d); + assertNull(stepDescriptor); // See the test-plugin folder to rebuild this File plugin = new File(SymbolLookupAnticacheTest.class.getResource("/structs-test-plugin.hpi").toURI()); @@ -35,14 +35,13 @@ public void testAnticache() throws Exception{ d = SymbolLookup.get().findDescriptor(SimpleBuildStep.class, "trivialBuilder"); stepDescriptor = SymbolLookup.get().find(Descriptor.class, "trivialBuilder"); - assert d != null; - assert stepDescriptor != null; - Assert.assertEquals("simpleBuild", d.getDisplayName()); - Assert.assertEquals("org.jenkinsci.plugins.structstest.TrivialBuilder", stepDescriptor.clazz.getName()); + assertNotNull(d); + assertNotNull(stepDescriptor); + assertEquals("simpleBuild", d.getDisplayName()); + assertEquals("org.jenkinsci.plugins.structstest.TrivialBuilder", stepDescriptor.clazz.getName()); // Once more just to confirm there's no wackiness when hitting the normal cache too - Assert.assertEquals(SymbolLookup.get().findDescriptor(SimpleBuildStep.class, "trivialBuilder"), d); + assertEquals(SymbolLookup.get().findDescriptor(SimpleBuildStep.class, "trivialBuilder"), d); } - } diff --git a/src/test/java/org/jenkinsci/plugins/structs/SymbolLookupTest.java b/src/test/java/org/jenkinsci/plugins/structs/SymbolLookupTest.java index 9c117a9..671f4fd 100644 --- a/src/test/java/org/jenkinsci/plugins/structs/SymbolLookupTest.java +++ b/src/test/java/org/jenkinsci/plugins/structs/SymbolLookupTest.java @@ -8,27 +8,30 @@ import jakarta.inject.Inject; import jenkins.model.GlobalConfiguration; import static org.hamcrest.CoreMatchers.*; +import static org.hamcrest.MatcherAssert.assertThat; + import org.jenkinsci.Symbol; -import static org.junit.Assert.*; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ErrorCollector; + +import static org.junit.jupiter.api.Assertions.assertAll; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.jvnet.hudson.test.Issue; import org.jvnet.hudson.test.JenkinsRule; import org.jvnet.hudson.test.TestExtension; +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; -public class SymbolLookupTest { +@WithJenkins +class SymbolLookupTest { @TestExtension @Symbol("foo") public static class Foo {} @TestExtension @Symbol("bar") public static class Bar {} - @Rule - public JenkinsRule rule = new JenkinsRule(); - - @Rule public ErrorCollector errors = new ErrorCollector(); + private JenkinsRule rule; @Inject SymbolLookup lookup; @@ -45,13 +48,14 @@ public static class Bar {} @Inject Internet.DescriptorImpl internetDescriptor; - @Before - public void setUp() { + @BeforeEach + void setUp(JenkinsRule r) { + rule = r; rule.jenkins.getInjector().injectMembers(this); } @Test - public void test() { + void test() { assertNull(lookup.find(Object.class, "zoo")); assertThat((Foo) lookup.find(Object.class, "foo"), is(sameInstance(this.foo))); assertThat((Bar) lookup.find(Object.class, "bar"), is(sameInstance(this.bar))); @@ -61,13 +65,13 @@ public void test() { } @Test - public void descriptorLookup() { + void descriptorLookup() { assertThat(lookup.findDescriptor(Fishing.class, "net"), is(sameInstance((Descriptor)fishingNetDescriptor))); assertThat(lookup.findDescriptor(Tech.class, "net"), is(sameInstance((Descriptor)internetDescriptor))); } @Test - public void symbolValueFromObject() { + void symbolValueFromObject() { Set netSet = Collections.singleton("net"); Set fooSet = Collections.singleton("foo"); @@ -79,9 +83,9 @@ public void symbolValueFromObject() { assertEquals(netSet, SymbolLookup.getSymbolValue(fishingNetDescriptor)); assertEquals(fooSet, SymbolLookup.getSymbolValue(foo)); } - + @Test - public void symbolValueFromClass() { + void symbolValueFromClass() { Set netSet = Collections.singleton("net"); Set fooSet = Collections.singleton("foo"); @@ -93,14 +97,14 @@ public void symbolValueFromClass() { @Issue("JENKINS-26093") @Test - public void parameters() { + void parameters() { assertEquals(Collections.singleton("booleanParam"), SymbolLookup.getSymbolValue(BooleanParameterValue.class)); assertEquals(Collections.singleton("booleanParam"), SymbolLookup.getSymbolValue(new BooleanParameterValue("flag", true))); } @Issue("JENKINS-37820") @Test - public void descriptorIsDescribable() { + void descriptorIsDescribable() { assertEquals(Collections.singleton("whatever"), SymbolLookup.getSymbolValue(SomeConfiguration.class)); assertEquals(Collections.singleton("whatever"), SymbolLookup.getSymbolValue(rule.jenkins.getDescriptorByType(SomeConfiguration.class))); } @@ -109,16 +113,20 @@ public void descriptorIsDescribable() { public static class SomeConfiguration extends GlobalConfiguration {} @Issue("JENKINS-57218") - @Test public void descriptorSansExtension() throws Exception { + @Test + void descriptorSansExtension() { SymbolLookup sl = rule.jenkins.getExtensionList(SymbolLookup.class).get(0); - errors.checkThat("A is registered", sl.findDescriptor(Stuff.class, "a"), is(instanceOf(StuffA.DescriptorImpl.class))); - errors.checkThat("B is not", sl.findDescriptor(Stuff.class, "b"), nullValue()); - errors.checkThat("C is, but the registration is broken", sl.findDescriptor(Stuff.class, "c"), nullValue()); - errors.checkThat("A (cached)", sl.findDescriptor(Stuff.class, "a"), is(instanceOf(StuffA.DescriptorImpl.class))); - errors.checkThat("B (cached)", sl.findDescriptor(Stuff.class, "b"), nullValue()); - errors.checkThat("C (cached)", sl.findDescriptor(Stuff.class, "c"), nullValue()); + assertAll( + () -> assertThat("A is registered", sl.findDescriptor(Stuff.class, "a"), is(instanceOf(StuffA.DescriptorImpl.class))), + () -> assertThat("B is not", sl.findDescriptor(Stuff.class, "b"), nullValue()), + () -> assertThat("C is, but the registration is broken", sl.findDescriptor(Stuff.class, "c"), nullValue()), + () -> assertThat("A (cached)", sl.findDescriptor(Stuff.class, "a"), is(instanceOf(StuffA.DescriptorImpl.class))), + () -> assertThat("B (cached)", sl.findDescriptor(Stuff.class, "b"), nullValue()), + () -> assertThat("C (cached)", sl.findDescriptor(Stuff.class, "c"), nullValue()) + ); } - public static abstract class Stuff extends AbstractDescribableImpl {} + + public abstract static class Stuff extends AbstractDescribableImpl {} public static final class StuffA extends Stuff { @Symbol("a") @TestExtension("descriptorSansExtension") public static final class DescriptorImpl extends Descriptor {} diff --git a/src/test/java/org/jenkinsci/plugins/structs/describable/DescribableModelTest.java b/src/test/java/org/jenkinsci/plugins/structs/describable/DescribableModelTest.java index 1bb6649..547ecb0 100644 --- a/src/test/java/org/jenkinsci/plugins/structs/describable/DescribableModelTest.java +++ b/src/test/java/org/jenkinsci/plugins/structs/describable/DescribableModelTest.java @@ -27,7 +27,6 @@ import hudson.Extension; import hudson.model.AbstractDescribableImpl; import hudson.model.BooleanParameterValue; -import hudson.model.Describable; import hudson.model.Descriptor; import hudson.model.ParameterValue; import hudson.model.ParametersDefinitionProperty; @@ -46,12 +45,12 @@ import org.jenkinsci.plugins.structs.Internet; import org.jenkinsci.plugins.structs.Tech; import org.jenkinsci.plugins.structs.describable.first.SharedName; -import org.junit.Assert; -import org.junit.ClassRule; -import org.junit.Test; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; import org.jvnet.hudson.test.Issue; import org.jvnet.hudson.test.JenkinsRule; import org.jvnet.hudson.test.LoggerRule; +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; import org.kohsuke.stapler.DataBoundConstructor; import org.kohsuke.stapler.DataBoundSetter; @@ -66,21 +65,27 @@ import java.util.logging.Level; import static org.apache.commons.lang3.SerializationUtils.roundtrip; +import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.is; import static org.jenkinsci.plugins.structs.describable.DescribableModel.*; import static org.jenkinsci.plugins.structs.describable.UninstantiatedDescribable.ANONYMOUS_KEY; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.*; @SuppressWarnings("unchecked") // generic array construction -public class DescribableModelTest { - @ClassRule - public static JenkinsRule rule = new JenkinsRule(); - @ClassRule - public static LoggerRule logging = new LoggerRule().record(DescribableModel.class, Level.ALL); +@WithJenkins +class DescribableModelTest { + + private static JenkinsRule rule; + private static final LoggerRule logging = new LoggerRule().record(DescribableModel.class, Level.ALL); + + @BeforeAll + static void setUp(JenkinsRule r) { + rule = r; + } @Test - public void instantiate() throws Exception { + void instantiate() { Map args = map("text", "hello", "flag", true); assertEquals("C:hello/true", instantiate(C.class, args).toString()); args.put("value", "main"); @@ -89,23 +94,21 @@ public void instantiate() throws Exception { } @Test - public void erroneousParameters() { - try { - Map args = map("text", "hello", "flag", true, "garbage", "!", "junk", "splat"); - instantiate(C.class, args); - Assert.fail("Instantiation should have failed due to unnecessary arguments"); - } catch (Exception e) { - assertThat(e, Matchers.instanceOf(IllegalArgumentException.class)); - assertThat(e.getMessage(), is("WARNING: Unknown parameter(s) found for class type '" + - C.class.getName() + "': garbage,junk")); - } + void erroneousParameters() { + Map args = map("text", "hello", "flag", true, "garbage", "!", "junk", "splat"); + IllegalArgumentException e = assertThrows(IllegalArgumentException.class, + () -> instantiate(C.class, args), + "Instantiation should have failed due to unnecessary arguments"); + assertThat(e.getMessage(), is("WARNING: Unknown parameter(s) found for class type '" + + C.class.getName() + "': garbage,junk")); } - private T instantiate(Class type, Map args) throws Exception { - return new DescribableModel(type).instantiate(args); + private T instantiate(Class type, Map args) { + return new DescribableModel<>(type).instantiate(args); } - @Test public void uninstantiate() throws Exception { + @Test + void uninstantiate() throws Exception { assertEquals("{flag=true, text=stuff}", DescribableModel.uninstantiate_(new C("stuff", true)).toString()); I i = new I("stuff"); i.setFlag(true); @@ -116,30 +119,30 @@ private T instantiate(Class type, Map args) throws Except UninstantiatedDescribable ud = UninstantiatedDescribable.from(net); assertEquals("net",ud.getSymbol()); assertTrue(ud.getArguments().isEmpty()); - assertTrue(ud.instantiate(Tech.class) instanceof Internet); + assertInstanceOf(Internet.class, ud.instantiate(Tech.class)); } - @Test public void mismatchedTypes() throws Exception { - try { - instantiate(I.class, map("value", 99)); - fail(); - } catch (Exception x) { - String message = x.getMessage(); - assertTrue(message, message.contains(I.class.getName())); - assertTrue(message, message.contains("value")); - assertTrue(message, message.contains("java.lang.String")); - assertTrue(message, message.contains("java.lang.Integer")); - } + @Test + void mismatchedTypes() { + IllegalArgumentException x = assertThrows(IllegalArgumentException.class, + () -> instantiate(I.class, map("value", 99))); + + String message = x.getMessage(); + assertTrue(message.contains(I.class.getName()), message); + assertTrue(message.contains("value"), message); + assertTrue(message.contains("java.lang.String"), message); + assertTrue(message.contains("java.lang.Integer"), message); } - @Test public void schemaFor() throws Exception { + @Test + void schemaFor() throws Exception { schema(C.class, "C(text: String, flag: boolean, shorty?: short, toBeRemoved?(deprecated): String)"); schema(I.class, "I(value: String, flag?: boolean, text?: String)"); DescribableModel schema = new DescribableModel(Impl1.class); assertEquals("Implementation #1", schema.getDisplayName()); assertEquals("
Overall help.
", schema.getHelp()); assertEquals("
The text to display.
", schema.getParameter("text").getHelp()); - schema = new DescribableModel(C.class); + schema = new DescribableModel<>(C.class); assertEquals("C", schema.getDisplayName()); assertNull(schema.getHelp()); assertNull(schema.getParameter("text").getHelp()); @@ -200,22 +203,26 @@ public boolean isFlag() { } } - @Test public void findSubtypes() throws Exception { + @Test + void findSubtypes() { assertEquals(new HashSet>(Arrays.asList(Impl1.class, Impl2.class, Impl3.class, Impl4.class)), DescribableModel.findSubtypes(Base.class)); assertEquals(Collections.singleton(Impl1.class), DescribableModel.findSubtypes(Marker.class)); } - @Test public void bindMapsFQN() throws Exception { + @Test + void bindMapsFQN() { assertEquals("UsesBase[Impl1[hello]]", instantiate(UsesBase.class, map("base", map(CLAZZ, Impl1.class.getName(), "text", "hello"))).toString()); } // TODO also check case that a FQN is needed - @Test public void gstring() throws Exception { + @Test + void gstring() { assertEquals("UsesBase[Impl1[hello world]]", instantiate(UsesBase.class, map("base", map(CLAZZ, "Impl1", "text", new GStringImpl(new Object[]{"hello", "world"}, new String[]{"", " "})))).toString()); } - @Test public void nestedStructs() throws Exception { + @Test + void nestedStructs() { roundTrip(UsesBase.class, map("base", map(CLAZZ, "Impl1", "text", "hello"))); roundTrip(UsesBase.class, map("base", map(CLAZZ, "Impl2", "flag", true))); roundTrip(UsesImpl2.class, map("impl2", map())); @@ -245,7 +252,7 @@ public static class UsesImpl2 { } } - public static abstract class Base extends AbstractDescribableImpl {} + public abstract static class Base extends AbstractDescribableImpl {} public interface Marker {} @@ -323,12 +330,12 @@ public Base[] getBases() { } } - public static abstract class UnimplementedExtensionPoint extends AbstractDescribableImpl {} + public abstract static class UnimplementedExtensionPoint extends AbstractDescribableImpl {} public static final class UsesUnimplementedExtensionPoint { @DataBoundConstructor public UsesUnimplementedExtensionPoint(UnimplementedExtensionPoint delegate) {} } - public static abstract class SomeImplsBroken extends AbstractDescribableImpl {} + public abstract static class SomeImplsBroken extends AbstractDescribableImpl {} public static class BrokenImpl extends SomeImplsBroken { @Extension public static class DescriptorImpl extends Descriptor { @Override public String getDisplayName() { @@ -348,12 +355,14 @@ public static class UsesSomeImplsBroken { @DataBoundConstructor public UsesSomeImplsBroken(SomeImplsBroken delegate) {} } - @Test public void enums() throws Exception { + @Test + void enums() { roundTrip(UsesEnum.class, map("e", "ZERO")); schema(UsesEnum.class, "UsesEnum(e: E[ZERO])"); } - @Test public void enumsWithGString() throws Exception { + @Test + void enumsWithGString() { assertEquals("UsesEnum[ZERO]", instantiate(UsesEnum.class, map("e", new GStringImpl(new Object[0], new String[]{"ZERO"}))).toString()); } @@ -374,12 +383,14 @@ public enum E { public abstract int v(); } - @Test public void urls() throws Exception { + @Test + void urls() { roundTrip(UsesURL.class, map("u", "http://nowhere.net/")); schema(UsesURL.class, "UsesURL(u?: String)"); } - @Test public void urlsWithGString() throws Exception { + @Test + void urlsWithGString() { assertEquals("UsesURL[http://nowhere.net/]", instantiate(UsesURL.class, map("u", new GStringImpl(new Object[0], new String[]{"http://nowhere.net/"}))).toString()); } @@ -391,7 +402,8 @@ public static final class UsesURL { } } - @Test public void result() throws Exception { + @Test + void result() { roundTrip(UsesResult.class, map("r", "SUCCESS")); schema(UsesResult.class, "UsesResult(r?: String)"); } @@ -404,12 +416,14 @@ public static final class UsesResult { } } - @Test public void chars() throws Exception { + @Test + void chars() { roundTrip(UsesCharacter.class, map("c", "!")); schema(UsesCharacter.class, "UsesCharacter(c?: char)"); } - @Test public void charsWithGString() throws Exception { + @Test + void charsWithGString() { assertEquals("UsesCharacter[x]", instantiate(UsesCharacter.class, map("c", new GStringImpl(new Object[0], new String[]{"x"}))).toString()); } @@ -421,12 +435,14 @@ public static final class UsesCharacter { } } - @Test public void stringArray() throws Exception { + @Test + void stringArray() { roundTrip(UsesStringArray.class, map("strings", Arrays.asList("one", "two"))); schema(UsesStringArray.class, "UsesStringArray(strings: String[])"); } - @Test public void stringList() throws Exception { + @Test + void stringList() { roundTrip(UsesStringList.class, map("strings", Arrays.asList("one", "two"))); schema(UsesStringList.class, "UsesStringList(strings: String[])"); } @@ -451,7 +467,8 @@ public List getStrings() { } } - @Test public void structArrayHomo() throws Exception { + @Test + void structArrayHomo() { roundTrip(UsesStructArrayHomo.class, map("impls", Arrays.asList(map(), map("flag", true))), "UsesStructArrayHomo[Impl2[false], Impl2[true]]"); schema(UsesStructArrayHomo.class, "UsesStructArrayHomo(impls: Impl2(flag?: boolean)[])"); } @@ -469,7 +486,8 @@ public Impl2[] getImpls() { } } - @Test public void structListHomo() throws Exception { + @Test + void structListHomo() { roundTrip(UsesStructListHomo.class, map("impls", Arrays.asList(map(), map("flag", true))), "UsesStructListHomo[Impl2[false], Impl2[true]]"); schema(UsesStructListHomo.class, "UsesStructListHomo(impls: Impl2(flag?: boolean)[])"); } @@ -487,7 +505,8 @@ public List getImpls() { } } - @Test public void structCollectionHomo() throws Exception { + @Test + void structCollectionHomo() { roundTrip(UsesStructCollectionHomo.class, map("impls", Arrays.asList(map(), map("flag", true))), "UsesStructCollectionHomo[Impl2[false], Impl2[true]]"); schema(UsesStructCollectionHomo.class, "UsesStructCollectionHomo(impls: Impl2(flag?: boolean)[])"); } @@ -505,7 +524,8 @@ public Collection getImpls() { } } - @Test public void structArrayHetero() throws Exception { + @Test + void structArrayHetero() { roundTrip(UsesStructArrayHetero.class, map("bases", Arrays.asList(map(CLAZZ, "Impl1", "text", "hello"), map(CLAZZ, "Impl2", "flag", true))), "UsesStructArrayHetero[Impl1[hello], Impl2[true]]"); schema(UsesStructArrayHetero.class, "UsesStructArrayHetero(bases: Base{Impl1(text: String) | Impl2(flag?: boolean) | Impl3(base: Base…) | Impl4(bases: Base…[])}[])"); } @@ -523,7 +543,8 @@ public Base[] getBases() { } } - @Test public void structListHetero() throws Exception { + @Test + void structListHetero() { roundTrip(UsesStructListHetero.class, map("bases", Arrays.asList(map(CLAZZ, "Impl1", "text", "hello"), map(CLAZZ, "Impl2", "flag", true))), "UsesStructListHetero[Impl1[hello], Impl2[true]]"); schema(UsesStructListHetero.class, "UsesStructListHetero(bases: Base{Impl1(text: String) | Impl2(flag?: boolean) | Impl3(base: Base…) | Impl4(bases: Base…[])}[])"); } @@ -541,7 +562,8 @@ public List getBases() { } } - @Test public void structCollectionHetero() throws Exception { + @Test + void structCollectionHetero() { roundTrip(UsesStructCollectionHetero.class, map("bases", Arrays.asList(map(CLAZZ, "Impl1", "text", "hello"), map(CLAZZ, "Impl2", "flag", true))), "UsesStructCollectionHetero[Impl1[hello], Impl2[true]]"); schema(UsesStructCollectionHetero.class, "UsesStructCollectionHetero(bases: Base{Impl1(text: String) | Impl2(flag?: boolean) | Impl3(base: Base…) | Impl4(bases: Base…[])}[])"); } @@ -559,48 +581,56 @@ public Collection getBases() { } } - @Test public void defaultValuesStructCollectionCommon() throws Exception { + @Test + void defaultValuesStructCollectionCommon() { roundTrip(DefaultStructCollection.class, map("bases", Arrays.asList(map(CLAZZ, "Impl1", "text", "special"))), "DefaultStructCollection[Impl1[special]]"); } - @Test public void defaultValuesStructCollectionEmpty() throws Exception { + @Test + void defaultValuesStructCollectionEmpty() { roundTrip(DefaultStructCollection.class, map("bases", Collections.emptyList()), "DefaultStructCollection[]"); } @Issue("JENKINS-25779") - @Test public void defaultValuesStructCollection() throws Exception { + @Test + void defaultValuesStructCollection() { roundTrip(DefaultStructCollection.class, map(), "DefaultStructCollection[Impl1[default]]"); } @Issue("JENKINS-25779") - @Test public void defaultValuesNestedStruct() throws Exception { + @Test + void defaultValuesNestedStruct() { roundTrip(DefaultStructCollection.class, map("bases", Arrays.asList(map(CLAZZ, "Impl2"), map(CLAZZ, "Impl2", "flag", true))), "DefaultStructCollection[Impl2[false], Impl2[true]]"); } @Issue("JENKINS-25779") - @Test public void defaultValuesNullSetter() throws Exception { + @Test + void defaultValuesNullSetter() { roundTrip(DefaultStructCollection.class, map("bases", null), "DefaultStructCollectionnull"); } public static final class DefaultStructCollection { - private Collection bases = Arrays.asList(new Impl1("default")); + private Collection bases = Arrays.asList(new Impl1("default")); @DataBoundConstructor public DefaultStructCollection() {} public Collection getBases() {return bases;} @DataBoundSetter public void setBases(Collection bases) {this.bases = bases;} @Override public String toString() {return "DefaultStructCollection" + bases;} } - @Test public void defaultValuesStructArrayCommon() throws Exception { + @Test + void defaultValuesStructArrayCommon() { roundTrip(DefaultStructArray.class, map("bases", Arrays.asList(map(CLAZZ, "Impl1", "text", "special")), "stuff", "val"), "DefaultStructArray[Impl1[special]];stuff=val"); } @Issue("JENKINS-25779") - @Test public void defaultValuesStructArray() throws Exception { + @Test + void defaultValuesStructArray() { roundTrip(DefaultStructArray.class, map("stuff", "val"), "DefaultStructArray[Impl1[default], Impl2[true]];stuff=val"); } @Issue("JENKINS-25779") - @Test public void defaultValuesNullConstructorParameter() throws Exception { + @Test + void defaultValuesNullConstructorParameter() { roundTrip(DefaultStructArray.class, map(), "DefaultStructArray[Impl1[default], Impl2[true]];stuff=null"); } @@ -620,7 +650,8 @@ public static final class DefaultStructArray { } @Issue("JENKINS-26093") - @Test public void parameterValues() throws Exception { + @Test + void parameterValues() { assertTrue(DescribableModel.findSubtypes(ParameterValue.class).contains(BooleanParameterValue.class)); // do not want to enumerate ListSubversionTagsParameterValue etc. // Omitting RunParameterValue since it is not friendly for a unit test. // JobParameterDefinition is not registered as an extension, so not supporting FileParameterValue. @@ -633,7 +664,7 @@ public static final class DefaultStructArray { map(CLAZZ, "StringParameterValue", "name", "n", "value", "stuff"), map(CLAZZ, "TextParameterValue", "name", "text", "value", "here\nthere"))), "TakesParams;BooleanParameterValue:flag=true;StringParameterValue:n=stuff;TextParameterValue:text=here\nthere"); - assertEquals("(parameters=[@booleanParam$BooleanParameterValue(name=flag,value=true)])", DescribableModel.uninstantiate2_(new TakesParams(Collections.singletonList(new BooleanParameterValue("flag", true)))).toString()); + assertEquals("(parameters=[@booleanParam$BooleanParameterValue(name=flag,value=true)])", DescribableModel.uninstantiate2_(new TakesParams(Collections.singletonList(new BooleanParameterValue("flag", true)))).toString()); } public static final class TakesParams { public final List parameters; @@ -649,7 +680,8 @@ public static final class TakesParams { } } - @Test public void parametersDefinitionProperty() throws Exception { + @Test + void parametersDefinitionProperty() { if (Jenkins.getVersion().isNewerThanOrEqualTo(new VersionNumber("2.281"))) { roundTrip(ParametersDefinitionProperty.class, map("parameterDefinitions", Arrays.asList( map(CLAZZ, "BooleanParameterDefinition", "name", "flag"), @@ -662,7 +694,8 @@ public static final class TakesParams { } @Issue("JENKINS-26619") - @Test public void getterDescribableList() throws Exception { + @Test + void getterDescribableList() { roundTrip(GitSCM.class, map( "extensions", Arrays.asList(map(CLAZZ, CleanBeforeCheckout.class.getSimpleName())), // Default values for these things do not work because GitSCM fails to use @DataBoundSetter: @@ -671,13 +704,14 @@ public static final class TakesParams { } @Issue("JENKINS-34070") - @Test public void userMergeOptions() throws Exception { + @Test + void userMergeOptions() { roundTrip(UserMergeOptions.class, map("mergeRemote", "x", "mergeTarget", "y", "mergeStrategy", "OCTOPUS", "fastForwardMode", "FF_ONLY"), "UserMergeOptions{mergeRemote='x', mergeTarget='y', mergeStrategy='OCTOPUS', fastForwardMode='FF_ONLY'}"); } @Issue("JENKINS-32925") // but Base3/Base4 usages are the more realistic case @Test - public void recursion() throws Exception { + void recursion() { schema(Recursion.class, "Recursion(foo?: Recursion…)"); } @@ -692,7 +726,7 @@ public void setFoo(Recursion r) {} * Makes sure resolveClass can do both symbol & class name lookup */ @Test - public void resolveClass() throws Exception { + void resolveClass() throws Exception { assertEquals(FishingNet.class, DescribableModel.resolveClass(Fishing.class, null, "net")); assertEquals(FishingNet.class, DescribableModel.resolveClass(Fishing.class, "FishingNet", null)); assertEquals(Internet.class, DescribableModel.resolveClass(Tech.class, null, "net")); @@ -701,17 +735,15 @@ public void resolveClass() throws Exception { @Issue("JENKINS-46122") @Test - public void resolveSymbolOnWrongBaseClass() throws Exception { - try { - DescribableModel.resolveClass(Tech.class, null, "rod"); - fail("No symbol for Tech should exist."); - } catch (UnsupportedOperationException e) { - assertEquals("no known implementation of " + Tech.class + " is using symbol ‘rod’", e.getMessage()); - } + void resolveSymbolOnWrongBaseClass() { + UnsupportedOperationException e = assertThrows(UnsupportedOperationException.class, + () -> DescribableModel.resolveClass(Tech.class, null, "rod"), + "No symbol for Tech should exist."); + assertEquals("no known implementation of " + Tech.class + " is using symbol ‘rod’", e.getMessage()); } @Test - public void singleRequiredParameter() throws Exception { + void singleRequiredParameter() { // positive case DescribableModel dc = new DescribableModel(LoneStar.class); assertTrue(dc.hasSingleRequiredParameter()); @@ -729,11 +761,11 @@ public void singleRequiredParameter() throws Exception { LoneStar star = new LoneStar("foo"); star.setCapital("Should be Dallas"); UninstantiatedDescribable y = dc.uninstantiate2(star); - assertTrue(!y.hasSoleRequiredArgument()); + assertFalse(y.hasSoleRequiredArgument()); } @Test - public void anonymousKey() throws Exception { + void anonymousKey() throws Exception { DescribableModel dc = new DescribableModel(LoneStar.class); UninstantiatedDescribable u = dc.uninstantiate2(new LoneStar("texas")); assertFalse(u.getArguments().containsKey(ANONYMOUS_KEY)); // shouldn't show up as a key from uninstantiate @@ -745,25 +777,21 @@ public void anonymousKey() throws Exception { assertEquals("alamo",ls.star); // it cannot be used when multiple parameters are given - try { - new UninstantiatedDescribable( - "texas",null, ImmutableMap.of(ANONYMOUS_KEY,"alamo","capital","Austin")).instantiate(); - fail(); - } catch (IllegalArgumentException e) { - // expected - } + assertThrows(IllegalArgumentException.class, + () -> new UninstantiatedDescribable( + "texas",null, ImmutableMap.of(ANONYMOUS_KEY,"alamo","capital","Austin")).instantiate()); } @Test - public void coerceNumbersAndBoolean() throws Exception { + void coerceNumbersAndBoolean() throws Exception { IntAndBool intAndBool = (IntAndBool) new UninstantiatedDescribable("intAndBool", null, ImmutableMap.of("i", "5", "b", "true")).instantiate(); assertEquals(5, intAndBool.i); - assertEquals(true, intAndBool.b); + assertTrue(intAndBool.b); } @Test - public void serialization() { + void serialization() { LoneStar s = new LoneStar("texas"); DescribableModel m = DescribableModel.of(LoneStar.class); UninstantiatedDescribable d = m.uninstantiate2(s); @@ -774,7 +802,7 @@ public void serialization() { } @Test - public void deprecated() throws Exception { + void deprecated() { assertTrue(new DescribableModel(ToBeRemoved.class).isDeprecated()); assertFalse(new DescribableModel(Impl1.class).isDeprecated()); } @@ -788,21 +816,21 @@ public ToBeRemoved() { @Issue("JENKINS-43337") @Test - public void ambiguousSimpleName() throws Exception { + void ambiguousSimpleName() { AmbiguousContainer container = new AmbiguousContainer(new FirstAmbiguous.CommonName("first"), new UnambiguousClassName("second")); UninstantiatedDescribable ud = DescribableModel.uninstantiate2_(container); Object o = ud.toMap().get("ambiguous"); - assertTrue(o instanceof Map); + assertInstanceOf(Map.class, o); Map m = (Map)o; // Make sure the ambiguous class is fully qualified. assertEquals(FirstAmbiguous.CommonName.class.getName(), m.get("$class")); Object o2 = ud.toMap().get("unambiguous"); - assertTrue(o2 instanceof Map); + assertInstanceOf(Map.class, o2); Map m2 = (Map)o2; // Make sure the unambiguous class just uses the simple name. @@ -811,21 +839,21 @@ public void ambiguousSimpleName() throws Exception { @Issue("JENKINS-45130") // @Test - public void ambiguousTopLevelSimpleName() throws Exception { + void ambiguousTopLevelSimpleName() { AmbiguousContainer container = new AmbiguousContainer(new SharedName("first"), new UnambiguousClassName("second")); UninstantiatedDescribable ud = DescribableModel.uninstantiate2_(container); Object o = ud.toMap().get("ambiguous"); - assertTrue(o instanceof Map); + assertInstanceOf(Map.class, o); Map m = (Map)o; // Make sure the ambiguous class is fully qualified. assertEquals(SharedName.class.getName(), m.get("$class")); Object o2 = ud.toMap().get("unambiguous"); - assertTrue(o2 instanceof Map); + assertInstanceOf(Map.class, o2); Map m2 = (Map)o2; // Make sure the unambiguous class just uses the simple name. @@ -834,16 +862,16 @@ public void ambiguousTopLevelSimpleName() throws Exception { @Issue("JENKINS-45130") @Test - public void ambiguousTopLevelSimpleNameInList() throws Exception { + void ambiguousTopLevelSimpleNameInList() throws Exception { SharedName first = new SharedName("first"); first.setTwo("something"); - AmbiguousListContainer container = new AmbiguousListContainer(Arrays.>asList(first, + AmbiguousListContainer container = new AmbiguousListContainer(Arrays.asList(first, new UnambiguousClassName("second"))); UninstantiatedDescribable ud = DescribableModel.uninstantiate2_(container); Object o = ud.toMap().get("list"); - assertTrue(o instanceof List); + assertInstanceOf(List.class, o); List> l = (List>) o; Map m = l.get(0); @@ -856,7 +884,7 @@ public void ambiguousTopLevelSimpleNameInList() throws Exception { // Make sure the unambiguous class just uses the simple name. assertEquals(UnambiguousClassName.class.getSimpleName(), m2.get("$class")); - System.out.println(ud.toString()); + System.out.println(ud); AmbiguousListContainer roundtrip = (AmbiguousListContainer) ud.instantiate(); assertThat(roundtrip.list.get(0), instanceOf(SharedName.class)); @@ -865,7 +893,7 @@ public void ambiguousTopLevelSimpleNameInList() throws Exception { @Issue("JENKINS-45130") @Test - public void ambiguousTopLevelSimpleNameInArray() throws Exception { + void ambiguousTopLevelSimpleNameInArray() throws Exception { SharedName first = new SharedName("first"); first.setTwo("something"); AmbiguousArrayContainer container = new AmbiguousArrayContainer(first, @@ -874,7 +902,7 @@ public void ambiguousTopLevelSimpleNameInArray() throws Exception { UninstantiatedDescribable ud = DescribableModel.uninstantiate2_(container); Object o = ud.toMap().get("array"); - assertTrue(o instanceof List); + assertInstanceOf(List.class, o); List> l = (List>) o; Map m = l.get(0); @@ -887,7 +915,7 @@ public void ambiguousTopLevelSimpleNameInArray() throws Exception { // Make sure the unambiguous class just uses the simple name. assertEquals(UnambiguousClassName.class.getSimpleName(), m2.get("$class")); - System.out.println(ud.toString()); + System.out.println(ud); AmbiguousArrayContainer roundtrip = (AmbiguousArrayContainer) ud.instantiate(); assertThat(roundtrip.getArray()[0], instanceOf(SharedName.class)); @@ -898,18 +926,18 @@ private static Map map(Object... keysAndValues) { if (keysAndValues.length % 2 != 0) { throw new IllegalArgumentException(); } - Map m = new TreeMap(); + Map m = new TreeMap<>(); for (int i = 0; i < keysAndValues.length; i += 2) { m.put((String) keysAndValues[i], keysAndValues[i + 1]); } return m; } - private void roundTrip(Class c, Map m) throws Exception { + private void roundTrip(Class c, Map m) { roundTrip(c, m, null); } - private void roundTrip(Class c, Map m, String toString) throws Exception { + private void roundTrip(Class c, Map m, String toString) { Object o = instantiate(c, m); if (toString != null) { assertEquals(toString, o.toString()); @@ -918,7 +946,7 @@ private void roundTrip(Class c, Map m, String toString) throws assertEquals(m, m2); } - private static void schema(Class c, String schema) throws Exception { + private static void schema(Class c, String schema) { assertEquals(schema, new DescribableModel(c).toString()); } @@ -983,7 +1011,7 @@ public String toString() { @Issue("JENKINS-31967") @Test - public void testJavaStandardTypes() throws Exception { + void testJavaStandardTypes() { // check instantiate with not default values roundTrip(AllJavaStandardTypesClass.class, map( "booleanValue1", Boolean.TRUE, @@ -1008,9 +1036,9 @@ public void testJavaStandardTypes() throws Exception { @Test @Issue("JENKINS-44864") - public void given_model_when_fieldRenamed_then_uselessSettersIgnored() throws Exception { + void given_model_when_fieldRenamed_then_uselessSettersIgnored() { EvolvedClass instance; - Map expected = new HashMap(); + Map expected = new HashMap<>(); instance = new EvolvedClass(false); expected.clear(); @@ -1067,7 +1095,6 @@ public void given_model_when_fieldRenamed_then_uselessSettersIgnored() throws Ex expected.put("legacyMode", 53); assertThat("A deprecated setter that produces a different object instance is retained", DescribableModel.uninstantiate2_(instance).toMap(), Matchers.is(expected)); - } public static class EvolvedClass { diff --git a/src/test/java/org/jenkinsci/plugins/structs/describable/Security3371Test.java b/src/test/java/org/jenkinsci/plugins/structs/describable/Security3371Test.java index a96a038..873b3cd 100644 --- a/src/test/java/org/jenkinsci/plugins/structs/describable/Security3371Test.java +++ b/src/test/java/org/jenkinsci/plugins/structs/describable/Security3371Test.java @@ -3,6 +3,7 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.not; +import static org.junit.jupiter.api.Assertions.assertThrows; import hudson.Extension; import hudson.model.AbstractDescribableImpl; @@ -11,34 +12,31 @@ import java.io.Serializable; import java.util.Map; import java.util.logging.Level; -import org.junit.Rule; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.jvnet.hudson.test.JenkinsRule; -import org.jvnet.hudson.test.LoggerRule; +import org.jvnet.hudson.test.LogRecorder; +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; import org.kohsuke.stapler.DataBoundConstructor; import org.kohsuke.stapler.DataBoundSetter; -public class Security3371Test { +@WithJenkins +class Security3371Test { - @Rule - public JenkinsRule r = new JenkinsRule(); - - @Rule - public LoggerRule warningLogger = new LoggerRule().record(DescribableModel.class, Level.WARNING).capture(10); + private final LogRecorder warningLogger = new LogRecorder().record(DescribableModel.class, Level.WARNING).capture(10); @Test - public void secretsAreNotLoggedWhenInstantiationFails() { + void secretsAreNotLoggedWhenInstantiationFails(JenkinsRule r) { String password = "password"; - try { - new DescribableModel<>(Cred.class).instantiate(Map.of("username", "username", "pwd", Secret.fromString(password)), null); - } catch (Exception e) { - assertThat(e.getMessage(), containsString("Secrets are involved, so details are available on more verbose logging levels.")); - assertThat(e.getMessage(), not(containsString("pwd=" + password))); - } + + IllegalArgumentException e = assertThrows(IllegalArgumentException.class, + () -> new DescribableModel<>(Cred.class).instantiate(Map.of("username", "username", "pwd", Secret.fromString(password)), null)); + + assertThat(e.getMessage(), containsString("Secrets are involved, so details are available on more verbose logging levels.")); + assertThat(e.getMessage(), not(containsString("pwd=" + password))); } @Test - public void secretsAreNotLoggedInUninstantiatedDescribable() { + void secretsAreNotLoggedInUninstantiatedDescribable(JenkinsRule r) { String password = "password"; ServerModel serverModel = new ServerModel("http://localhost", new Cred("username", password)); new DescribableModel<>(ServerModel.class).uninstantiate2(serverModel); diff --git a/src/test/java/org/jenkinsci/plugins/structs/describable/UninstantiatedDescribableTest.java b/src/test/java/org/jenkinsci/plugins/structs/describable/UninstantiatedDescribableTest.java index 6a90d29..6aec1d8 100644 --- a/src/test/java/org/jenkinsci/plugins/structs/describable/UninstantiatedDescribableTest.java +++ b/src/test/java/org/jenkinsci/plugins/structs/describable/UninstantiatedDescribableTest.java @@ -1,31 +1,32 @@ package org.jenkinsci.plugins.structs.describable; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.util.Map; import java.util.TreeMap; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.assertEquals; /** * @author Kohsuke Kawaguchi */ -public class UninstantiatedDescribableTest { +class UninstantiatedDescribableTest { + @Test - public void _toString() { + void _toString() { assertEquals("@symbol$class(x=4,y=hello)", make().toString()); } @Test - public void equals() { + void equals() { assertEquals(make(),make()); assertEquals(make().hashCode(),make().hashCode()); } private Object make() { - Map args = new TreeMap(); - args.put("x",4); - args.put("y","hello"); + Map args = new TreeMap<>(); + args.put("x", 4); + args.put("y", "hello"); return new UninstantiatedDescribable("symbol", "class", args); } } \ No newline at end of file