Skip to content

Migrate tests to JUnit5 #223

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,46 @@

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());
rule.jenkins.getPluginManager().dynamicLoad(plugin);

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);
}


}
62 changes: 35 additions & 27 deletions src/test/java/org/jenkinsci/plugins/structs/SymbolLookupTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)));
Expand All @@ -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<String> netSet = Collections.singleton("net");
Set<String> fooSet = Collections.singleton("foo");

Expand All @@ -79,9 +83,9 @@ public void symbolValueFromObject() {
assertEquals(netSet, SymbolLookup.getSymbolValue(fishingNetDescriptor));
assertEquals(fooSet, SymbolLookup.getSymbolValue(foo));
}

@Test
public void symbolValueFromClass() {
void symbolValueFromClass() {
Set<String> netSet = Collections.singleton("net");
Set<String> fooSet = Collections.singleton("foo");

Expand All @@ -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)));
}
Expand All @@ -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<Stuff> {}

public abstract static class Stuff extends AbstractDescribableImpl<Stuff> {}
public static final class StuffA extends Stuff {
@Symbol("a")
@TestExtension("descriptorSansExtension") public static final class DescriptorImpl extends Descriptor<Stuff> {}
Expand Down
Loading