Skip to content

Commit 934711a

Browse files
Migrate tests to JUnit5
* second batch of green tests
1 parent 0d14dd6 commit 934711a

24 files changed

+1928
-1278
lines changed

test/src/test/java/hudson/ClassicPluginStrategyTest.java

Lines changed: 123 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -25,82 +25,67 @@
2525

2626
package hudson;
2727

28-
import static org.junit.Assert.assertNotNull;
29-
import static org.junit.Assert.assertTrue;
30-
import static org.junit.Assert.fail;
28+
import static org.junit.jupiter.api.Assertions.assertNotNull;
29+
import static org.junit.jupiter.api.Assertions.assertTrue;
30+
import static org.junit.jupiter.api.Assertions.fail;
3131

3232
import hudson.model.Hudson;
3333
import java.io.File;
34+
import java.lang.reflect.Method;
3435
import java.net.URL;
3536
import java.util.Collection;
3637
import java.util.Enumeration;
3738
import java.util.LinkedHashSet;
3839
import java.util.Set;
39-
import org.junit.Rule;
40-
import org.junit.Test;
41-
import org.junit.experimental.categories.Category;
40+
import org.junit.jupiter.api.Tag;
41+
import org.junit.jupiter.api.Test;
42+
import org.junit.jupiter.api.extension.ExtensionContext;
43+
import org.junit.jupiter.api.extension.RegisterExtension;
44+
import org.junit.runner.Description;
45+
import org.junit.runners.model.Statement;
4246
import org.jvnet.hudson.test.Issue;
4347
import org.jvnet.hudson.test.JenkinsRecipe;
4448
import org.jvnet.hudson.test.JenkinsRule;
45-
import org.jvnet.hudson.test.SmokeTest;
49+
import org.jvnet.hudson.test.junit.jupiter.JenkinsSessionExtension;
4650
import org.jvnet.hudson.test.recipes.LocalData;
4751

4852
/**
4953
* @author Alan Harder
5054
*/
51-
@Category(SmokeTest.class)
52-
public class ClassicPluginStrategyTest {
55+
@Tag("SmokeTest")
56+
class ClassicPluginStrategyTest {
5357

54-
@Rule
55-
public JenkinsRule j = new JenkinsRule() {
56-
@Override
57-
protected Hudson newHudson() throws Exception {
58-
File home = homeLoader.allocate();
59-
60-
for (JenkinsRecipe.Runner r : recipes) {
61-
r.decorateHome(this, home);
62-
}
63-
LocalPluginManager pluginManager = new LocalPluginManager(home) {
64-
@Override
65-
protected Collection<String> loadBundledPlugins() {
66-
// Overriding so we can force loading of the detached plugins for testing
67-
Set<String> names = new LinkedHashSet<>();
68-
names.addAll(loadPluginsFromWar("/WEB-INF/plugins"));
69-
names.addAll(loadPluginsFromWar("/WEB-INF/detached-plugins"));
70-
return names;
71-
}
72-
};
73-
setPluginManager(pluginManager);
74-
return new Hudson(home, createWebServer2(), pluginManager);
75-
}
76-
};
58+
@RegisterExtension
59+
private final JenkinsSessionExtension session = new CustomPluginManagerExtension();
7760

7861
/**
7962
* Test finding resources via DependencyClassLoader.
8063
*/
8164
@LocalData
8265
@Test
83-
public void testDependencyClassLoader() throws Exception {
84-
// Test data has: foo3 depends on foo2,foo1; foo2 depends on foo1
85-
// (thus findResources from foo3 can find foo1 resources via 2 dependency paths)
86-
PluginWrapper p = j.jenkins.getPluginManager().getPlugin("foo3");
87-
String res;
88-
89-
// In the current impl, the dependencies are the parent ClassLoader so resources
90-
// are found there before checking the plugin itself. Adjust the expected results
91-
// below if this is ever changed to check the plugin first.
92-
Enumeration<URL> en = p.classLoader.getResources("test-resource");
93-
for (int i = 0; en.hasMoreElements(); i++) {
94-
res = en.nextElement().toString();
95-
if (i < 2)
96-
assertTrue("In current impl, " + res + "should be foo1 or foo2",
97-
res.contains("/foo1/") || res.contains("/foo2/"));
98-
else
99-
assertTrue("In current impl, " + res + "should be foo3", res.contains("/foo3/"));
100-
}
101-
res = p.classLoader.getResource("test-resource").toString();
102-
assertTrue("In current impl, " + res + " should be foo1 or foo2",
103-
res.contains("/foo1/") || res.contains("/foo2/"));
66+
void testDependencyClassLoader() throws Throwable {
67+
session.then(j -> {
68+
// Test data has: foo3 depends on foo2,foo1; foo2 depends on foo1
69+
// (thus findResources from foo3 can find foo1 resources via 2 dependency paths)
70+
PluginWrapper p = j.jenkins.getPluginManager().getPlugin("foo3");
71+
String res;
72+
73+
// In the current impl, the dependencies are the parent ClassLoader so resources
74+
// are found there before checking the plugin itself. Adjust the expected results
75+
// below if this is ever changed to check the plugin first.
76+
Enumeration<URL> en = p.classLoader.getResources("test-resource");
77+
for (int i = 0; en.hasMoreElements(); i++) {
78+
res = en.nextElement().toString();
79+
if (i < 2)
80+
assertTrue(res.contains("/foo1/") || res.contains("/foo2/"),
81+
"In current impl, " + res + "should be foo1 or foo2");
82+
else
83+
assertTrue(res.contains("/foo3/"), "In current impl, " + res + "should be foo3");
84+
}
85+
res = p.classLoader.getResource("test-resource").toString();
86+
assertTrue(res.contains("/foo1/") || res.contains("/foo2/"),
87+
"In current impl, " + res + " should be foo1 or foo2");
88+
});
10489
}
10590

10691
/**
@@ -110,17 +95,19 @@ public void testDependencyClassLoader() throws Exception {
11095
@LocalData
11196
@Issue("JENKINS-18654")
11297
@Test
113-
public void testDisabledDependencyClassLoader() throws Exception {
114-
PluginWrapper p = j.jenkins.getPluginManager().getPlugin("foo4");
115-
116-
Enumeration<URL> en = p.classLoader.getResources("test-resource");
117-
for (int i = 0; en.hasMoreElements(); i++) {
118-
String res = en.nextElement().toString();
119-
if (i == 0)
120-
assertTrue("expected foo4, found " + res, res.contains("/foo4/"));
121-
else
122-
fail("disabled dependency should not be included");
123-
}
98+
void testDisabledDependencyClassLoader() throws Throwable {
99+
session.then(j -> {
100+
PluginWrapper p = j.jenkins.getPluginManager().getPlugin("foo4");
101+
102+
Enumeration<URL> en = p.classLoader.getResources("test-resource");
103+
for (int i = 0; en.hasMoreElements(); i++) {
104+
String res = en.nextElement().toString();
105+
if (i == 0)
106+
assertTrue(res.contains("/foo4/"), "expected foo4, found " + res);
107+
else
108+
fail("disabled dependency should not be included");
109+
}
110+
});
124111
}
125112

126113
/**
@@ -130,12 +117,77 @@ public void testDisabledDependencyClassLoader() throws Exception {
130117
@LocalData
131118
@Issue("JENKINS-27289")
132119
@Test
133-
public void testMaskResourceClassLoader() throws Exception {
134-
PluginWrapper pw = j.jenkins.getPluginManager().getPlugin("foo1");
135-
Class<?> clazz = pw.classLoader.loadClass("org.apache.http.impl.io.SocketInputBuffer");
136-
ClassLoader cl = clazz.getClassLoader();
137-
URL url = cl.getResource("org/apache/http/impl/io/SocketInputBuffer.class");
138-
assertNotNull(url);
139-
assertTrue("expected to find the class from foo1 plugin", url.toString().contains("plugins/foo1"));
120+
void testMaskResourceClassLoader() throws Throwable {
121+
session.then(j -> {
122+
PluginWrapper pw = j.jenkins.getPluginManager().getPlugin("foo1");
123+
Class<?> clazz = pw.classLoader.loadClass("org.apache.http.impl.io.SocketInputBuffer");
124+
ClassLoader cl = clazz.getClassLoader();
125+
URL url = cl.getResource("org/apache/http/impl/io/SocketInputBuffer.class");
126+
assertNotNull(url);
127+
assertTrue(url.toString().contains("plugins/foo1"), "expected to find the class from foo1 plugin");
128+
});
129+
}
130+
131+
private static final class CustomPluginManagerExtension extends JenkinsSessionExtension {
132+
133+
private int port;
134+
private Description description;
135+
136+
@Override
137+
public void beforeEach(ExtensionContext context) {
138+
super.beforeEach(context);
139+
description = Description.createTestDescription(
140+
context.getTestClass().map(Class::getName).orElse(null),
141+
context.getTestMethod().map(Method::getName).orElse(null),
142+
context.getTestMethod().map(Method::getAnnotations).orElse(null));
143+
}
144+
145+
@Override
146+
public void then(Step s) throws Throwable {
147+
CustomJenkinsRule r = new CustomJenkinsRule(getHome(), port);
148+
r.apply(
149+
new Statement() {
150+
@Override
151+
public void evaluate() throws Throwable {
152+
port = r.getPort();
153+
s.run(r);
154+
}
155+
},
156+
description
157+
).evaluate();
158+
}
159+
160+
private static final class CustomJenkinsRule extends JenkinsRule {
161+
162+
CustomJenkinsRule(File home, int port) {
163+
with(() -> home);
164+
localPort = port;
165+
}
166+
167+
int getPort() {
168+
return localPort;
169+
}
170+
171+
@Override
172+
protected Hudson newHudson() throws Exception {
173+
File home = homeLoader.allocate();
174+
175+
for (JenkinsRecipe.Runner r : recipes) {
176+
r.decorateHome(this, home);
177+
}
178+
LocalPluginManager pluginManager = new LocalPluginManager(home) {
179+
@Override
180+
protected Collection<String> loadBundledPlugins() {
181+
// Overriding so we can force loading of the detached plugins for testing
182+
Set<String> names = new LinkedHashSet<>();
183+
names.addAll(loadPluginsFromWar("/WEB-INF/plugins"));
184+
names.addAll(loadPluginsFromWar("/WEB-INF/detached-plugins"));
185+
return names;
186+
}
187+
};
188+
setPluginManager(pluginManager);
189+
return new Hudson(home, createWebServer2(), pluginManager);
190+
}
191+
}
140192
}
141193
}

test/src/test/java/hudson/CustomPluginManagerTest.java

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,26 +29,29 @@
2929
import static org.hamcrest.MatcherAssert.assertThat;
3030
import static org.hamcrest.Matchers.instanceOf;
3131
import static org.hamcrest.Matchers.not;
32-
import static org.junit.Assert.assertNotNull;
33-
import static org.junit.Assert.assertTrue;
32+
import static org.junit.jupiter.api.Assertions.assertNotNull;
33+
import static org.junit.jupiter.api.Assertions.assertTrue;
3434

3535
import jakarta.servlet.ServletContext;
3636
import java.io.File;
3737
import java.lang.annotation.Retention;
3838
import java.lang.annotation.Target;
3939
import jenkins.model.Jenkins;
40-
import org.junit.Rule;
41-
import org.junit.Test;
40+
import org.junit.jupiter.api.Test;
41+
import org.junit.jupiter.api.extension.RegisterExtension;
4242
import org.jvnet.hudson.test.Issue;
4343
import org.jvnet.hudson.test.JenkinsRecipe;
4444
import org.jvnet.hudson.test.JenkinsRule;
45+
import org.jvnet.hudson.test.junit.jupiter.JenkinsSessionExtension;
4546
import org.jvnet.hudson.test.recipes.WithPlugin;
4647

4748
/**
4849
* Tests for the use of a custom plugin manager in custom wars.
4950
*/
50-
public class CustomPluginManagerTest {
51-
@Rule public final JenkinsRule r = new JenkinsRule();
51+
class CustomPluginManagerTest {
52+
53+
@RegisterExtension
54+
private final JenkinsSessionExtension session = new JenkinsSessionExtension();
5255

5356
// TODO: Move to jenkins-test-harness
5457
@JenkinsRecipe(WithCustomLocalPluginManager.RuleRunnerImpl.class)
@@ -65,7 +68,6 @@ public void setup(JenkinsRule jenkinsRule, WithCustomLocalPluginManager recipe)
6568
jenkinsRule.useLocalPluginManager = true;
6669
oldValue = System.getProperty(PluginManager.CUSTOM_PLUGIN_MANAGER);
6770
System.setProperty(PluginManager.CUSTOM_PLUGIN_MANAGER, recipe.value().getName());
68-
6971
}
7072

7173
@Override
@@ -79,9 +81,11 @@ public void tearDown(JenkinsRule jenkinsRule, WithCustomLocalPluginManager recip
7981
}
8082
}
8183

82-
private void check(Class<? extends CustomPluginManager> klass) {
83-
assertTrue("Correct plugin manager installed", klass.isAssignableFrom(r.getPluginManager().getClass()));
84-
assertNotNull("Plugin 'htmlpublisher' installed", r.jenkins.getPlugin("htmlpublisher"));
84+
private void check(Class<? extends CustomPluginManager> klass) throws Throwable {
85+
session.then(r -> {
86+
assertTrue(klass.isAssignableFrom(r.getPluginManager().getClass()), "Correct plugin manager installed");
87+
assertNotNull(r.jenkins.getPlugin("htmlpublisher"), "Plugin 'htmlpublisher' installed");
88+
});
8589
}
8690

8791
// An interface not to override every constructor.
@@ -91,11 +95,13 @@ interface CustomPluginManager {
9195
@Issue("JENKINS-34681")
9296
@WithPlugin("htmlpublisher.jpi")
9397
@WithCustomLocalPluginManager(CustomPluginManager1.class)
94-
@Test public void customPluginManager1() {
98+
@Test
99+
void customPluginManager1() throws Throwable {
95100
check(CustomPluginManager1.class);
96101
}
97102

98103
public static class CustomPluginManager1 extends LocalPluginManager implements CustomPluginManager {
104+
@SuppressWarnings("checkstyle:redundantmodifier")
99105
public CustomPluginManager1(Jenkins jenkins) {
100106
super(jenkins);
101107
}
@@ -104,11 +110,13 @@ public CustomPluginManager1(Jenkins jenkins) {
104110
@Issue("JENKINS-34681")
105111
@WithPlugin("htmlpublisher.jpi")
106112
@WithCustomLocalPluginManager(CustomPluginManager2.class)
107-
@Test public void customPluginManager2() {
113+
@Test
114+
void customPluginManager2() throws Throwable {
108115
check(CustomPluginManager2.class);
109116
}
110117

111118
public static class CustomPluginManager2 extends LocalPluginManager implements CustomPluginManager {
119+
@SuppressWarnings("checkstyle:redundantmodifier")
112120
public CustomPluginManager2(ServletContext ctx, File root) {
113121
super(ctx, root);
114122
}
@@ -117,11 +125,13 @@ public CustomPluginManager2(ServletContext ctx, File root) {
117125
@Issue("JENKINS-34681")
118126
@WithPlugin("htmlpublisher.jpi")
119127
@WithCustomLocalPluginManager(CustomPluginManager3.class)
120-
@Test public void customPluginManager3() {
128+
@Test
129+
void customPluginManager3() throws Throwable {
121130
check(CustomPluginManager3.class);
122131
}
123132

124133
public static class CustomPluginManager3 extends LocalPluginManager implements CustomPluginManager {
134+
@SuppressWarnings("checkstyle:redundantmodifier")
125135
public CustomPluginManager3(File root) {
126136
super(root);
127137
}
@@ -130,11 +140,15 @@ public CustomPluginManager3(File root) {
130140
@Issue("JENKINS-34681")
131141
@WithPlugin("htmlpublisher.jpi")
132142
@WithCustomLocalPluginManager(BadCustomPluginManager.class)
133-
@Test public void badCustomPluginManager() {
134-
assertThat("Custom plugin manager not installed", r.getPluginManager(), not(instanceOf(CustomPluginManager.class)));
143+
@Test
144+
void badCustomPluginManager() throws Throwable {
145+
session.then(r ->
146+
assertThat("Custom plugin manager not installed", r.getPluginManager(), not(instanceOf(CustomPluginManager.class)))
147+
);
135148
}
136149

137150
public static class BadCustomPluginManager extends LocalPluginManager implements CustomPluginManager {
151+
@SuppressWarnings("checkstyle:redundantmodifier")
138152
public BadCustomPluginManager(File root, ServletContext ctx) {
139153
super(ctx, root);
140154
}

0 commit comments

Comments
 (0)