Skip to content

Commit 9d38e13

Browse files
Migrate tests to JUnit5 (test / hudson / 2)
* Migrate annotations and imports * Migrate assertions * Remove public visibility for test classes and methods * Minor code cleanup
1 parent 5d3c96b commit 9d38e13

File tree

84 files changed

+1785
-1259
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+1785
-1259
lines changed

src/checkstyle/checkstyle-configuration.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<property name="max" value="240"/>
1111
<property name="ignorePattern" value="^package.*|^import.*|a href|href|http://|https://|ftp://"/>
1212
</module>
13+
<module name="SuppressWarningsFilter" />
1314
<module name="TreeWalker">
1415
<!--
1516
Annotations: https://checkstyle.sourceforge.io/config_annotation.html
@@ -38,6 +39,7 @@
3839
<module name="StringLiteralEquality"/>
3940
<module name="SuperClone"/>
4041
<module name="SuperFinalize"/>
42+
<module name="SuppressWarningsHolder" />
4143
<module name="UnnecessarySemicolonAfterOuterTypeDeclaration"/>
4244
<module name="UnnecessarySemicolonAfterTypeMemberDeclaration"/>
4345
<module name="UnnecessarySemicolonInEnumeration"/>

test/src/test/java/hudson/AboutJenkinsTest.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,28 +26,33 @@
2626

2727
import static org.hamcrest.MatcherAssert.assertThat;
2828
import static org.hamcrest.Matchers.containsString;
29-
import static org.junit.Assert.assertEquals;
29+
import static org.junit.jupiter.api.Assertions.assertEquals;
3030

3131
import java.net.HttpURLConnection;
3232
import jenkins.model.Jenkins;
3333
import org.htmlunit.html.HtmlPage;
34-
import org.junit.Rule;
35-
import org.junit.Test;
36-
import org.junit.experimental.categories.Category;
34+
import org.junit.jupiter.api.BeforeEach;
35+
import org.junit.jupiter.api.Tag;
36+
import org.junit.jupiter.api.Test;
3737
import org.jvnet.hudson.test.Issue;
3838
import org.jvnet.hudson.test.JenkinsRule;
3939
import org.jvnet.hudson.test.MockAuthorizationStrategy;
40-
import org.jvnet.hudson.test.SmokeTest;
40+
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
4141

42-
@Category(SmokeTest.class)
43-
public class AboutJenkinsTest {
42+
@Tag("SmokeTest")
43+
@WithJenkins
44+
class AboutJenkinsTest {
4445

45-
@Rule
46-
public JenkinsRule j = new JenkinsRule();
46+
private JenkinsRule j;
47+
48+
@BeforeEach
49+
void setUp(JenkinsRule rule) {
50+
j = rule;
51+
}
4752

4853
@Test
4954
@Issue("SECURITY-771")
50-
public void onlyAdminOrManageOrSystemReadCanReadAbout() throws Exception {
55+
void onlyAdminOrManageOrSystemReadCanReadAbout() throws Exception {
5156
final String ADMIN = "admin";
5257
final String USER = "user";
5358
final String MANAGER = "manager";

test/src/test/java/hudson/AbstractItemSecurity1114Test.java

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package hudson;
22

3-
import static org.junit.Assert.assertThrows;
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
45

56
import edu.umd.cs.findbugs.annotations.CheckForNull;
67
import hudson.model.AbstractItem;
@@ -10,23 +11,29 @@
1011
import hudson.security.ACLContext;
1112
import jenkins.model.Jenkins;
1213
import org.htmlunit.FailingHttpStatusCodeException;
13-
import org.junit.Assert;
14-
import org.junit.Rule;
15-
import org.junit.Test;
14+
import org.junit.jupiter.api.BeforeEach;
15+
import org.junit.jupiter.api.Test;
1616
import org.jvnet.hudson.test.For;
1717
import org.jvnet.hudson.test.Issue;
1818
import org.jvnet.hudson.test.JenkinsRule;
1919
import org.jvnet.hudson.test.MockAuthorizationStrategy;
2020
import org.jvnet.hudson.test.TestExtension;
21+
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
2122

22-
public class AbstractItemSecurity1114Test {
23-
@Rule
24-
public JenkinsRule j = new JenkinsRule();
23+
@WithJenkins
24+
class AbstractItemSecurity1114Test {
25+
26+
private JenkinsRule j;
27+
28+
@BeforeEach
29+
void setUp(JenkinsRule rule) {
30+
j = rule;
31+
}
2532

2633
@Test
2734
@Issue("SECURITY-1114")
2835
@For(AbstractItem.class)
29-
public void testAccess() throws Exception {
36+
void testAccess() throws Exception {
3037
j.jenkins.setSecurityRealm(j.createDummySecurityRealm());
3138
MockAuthorizationStrategy authorizationStrategy = new MockAuthorizationStrategy();
3239
authorizationStrategy.grant(Jenkins.READ).onRoot().toEveryone();
@@ -39,7 +46,7 @@ public void testAccess() throws Exception {
3946
// alice can discover project
4047
JenkinsRule.WebClient alice = j.createWebClient().login("alice");
4148
FailingHttpStatusCodeException e = assertThrows(FailingHttpStatusCodeException.class, () -> alice.goTo("bypass/myproject"));
42-
Assert.assertEquals("alice can discover", 403, e.getStatusCode());
49+
assertEquals(403, e.getStatusCode(), "alice can discover");
4350

4451
// bob can read project
4552
JenkinsRule.WebClient bob = j.createWebClient().login("bob");
@@ -49,9 +56,9 @@ public void testAccess() throws Exception {
4956
// carol has no permissions
5057
JenkinsRule.WebClient carol = j.createWebClient().login("carol");
5158
e = assertThrows(FailingHttpStatusCodeException.class, () -> carol.goTo("bypass/nonexisting"));
52-
Assert.assertEquals("carol gets 404 for nonexisting project", 404, e.getStatusCode());
59+
assertEquals(404, e.getStatusCode(), "carol gets 404 for nonexisting project");
5360
e = assertThrows(FailingHttpStatusCodeException.class, () -> carol.goTo("bypass/myproject"));
54-
Assert.assertEquals("carol gets 404 for invisible project", 404, e.getStatusCode());
61+
assertEquals(404, e.getStatusCode(), "carol gets 404 for invisible project");
5562
}
5663

5764
@TestExtension

test/src/test/java/hudson/ExtensionFinderTest.java

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@
2626

2727
import static org.hamcrest.MatcherAssert.assertThat;
2828
import static org.hamcrest.Matchers.instanceOf;
29-
import static org.junit.Assert.assertEquals;
30-
import static org.junit.Assert.assertNotNull;
31-
import static org.junit.Assert.assertNull;
32-
import static org.junit.Assert.assertTrue;
29+
import static org.junit.jupiter.api.Assertions.assertEquals;
30+
import static org.junit.jupiter.api.Assertions.assertNotNull;
31+
import static org.junit.jupiter.api.Assertions.assertNull;
32+
import static org.junit.jupiter.api.Assertions.assertTrue;
3333

3434
import com.google.inject.AbstractModule;
3535
import com.google.inject.ImplementedBy;
@@ -38,33 +38,40 @@
3838
import jakarta.inject.Qualifier;
3939
import java.lang.annotation.Retention;
4040
import java.lang.annotation.RetentionPolicy;
41-
import org.junit.Rule;
42-
import org.junit.Test;
41+
import org.junit.jupiter.api.BeforeEach;
42+
import org.junit.jupiter.api.Test;
4343
import org.jvnet.hudson.test.Issue;
4444
import org.jvnet.hudson.test.JenkinsRule;
4545
import org.jvnet.hudson.test.TestEnvironment;
4646
import org.jvnet.hudson.test.TestExtension;
47+
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
4748

4849
/**
4950
* @author Kohsuke Kawaguchi
5051
*/
51-
public class ExtensionFinderTest {
52+
@WithJenkins
53+
class ExtensionFinderTest {
5254

53-
@Rule
54-
public JenkinsRule r = new JenkinsRule();
55+
private JenkinsRule r;
56+
57+
@BeforeEach
58+
void setUp(JenkinsRule rule) {
59+
r = rule;
60+
}
5561

5662
/**
5763
* It's OK for some extensions to fail to load. The system needs to tolerate that.
5864
*/
5965
@Test
60-
public void failingInstance() {
66+
void failingInstance() {
6167
FailingExtension i = PageDecorator.all().get(FailingExtension.class);
62-
assertNull("Instantiation should have failed", i);
63-
assertTrue("Instantiation should have been attempted", FailingExtension.error);
68+
assertNull(i, "Instantiation should have failed");
69+
assertTrue(FailingExtension.error, "Instantiation should have been attempted");
6470
}
6571

6672
@TestExtension("failingInstance")
6773
public static class FailingExtension extends PageDecorator {
74+
@SuppressWarnings("checkstyle:redundantmodifier")
6875
public FailingExtension() {
6976
super(FailingExtension.class);
7077
error = true;
@@ -75,14 +82,11 @@ public FailingExtension() {
7582
}
7683

7784

78-
79-
80-
8185
/**
8286
* Extensions are Guice components, so it should support injection.
8387
*/
8488
@Test
85-
public void injection() {
89+
void injection() {
8690
InjectingExtension i = PageDecorator.all().get(InjectingExtension.class);
8791
assertNotNull(i.foo);
8892
assertEquals("lion king", i.value);
@@ -96,7 +100,7 @@ public static class InjectingExtension extends PageDecorator {
96100
@Inject @LionKing
97101
String value;
98102

99-
103+
@SuppressWarnings("checkstyle:redundantmodifier")
100104
public InjectingExtension() {
101105
super(InjectingExtension.class);
102106
}
@@ -108,7 +112,7 @@ public static class Foo {}
108112
* Extensions are Guice components, so it should support injection.
109113
*/
110114
@Test
111-
public void legacyInjection() {
115+
void legacyInjection() {
112116
LegacyInjectingExtension i = PageDecorator.all().get(LegacyInjectingExtension.class);
113117
assertNotNull(i.foo);
114118
assertEquals("lion king", i.value);
@@ -123,6 +127,7 @@ public static class LegacyInjectingExtension extends PageDecorator {
123127
@LionKing
124128
String value;
125129

130+
@SuppressWarnings("checkstyle:redundantmodifier")
126131
public LegacyInjectingExtension() {
127132
super(LegacyInjectingExtension.class);
128133
}
@@ -154,13 +159,14 @@ protected void configure() {
154159
* One failure in binding definition shouldn't prevent Jenkins from booting.
155160
*/
156161
@Test
157-
public void errorRecovery() {
162+
void errorRecovery() {
158163
BrokenExtension i = PageDecorator.all().get(BrokenExtension.class);
159164
assertNull(i);
160165
}
161166

162167
@TestExtension("errorRecovery")
163168
public static class BrokenExtension extends PageDecorator {
169+
@SuppressWarnings("checkstyle:redundantmodifier")
164170
public BrokenExtension() {
165171
super(InjectingExtension.class);
166172

@@ -169,7 +175,7 @@ public BrokenExtension() {
169175
}
170176

171177
@Test
172-
public void injectMutualRecursion() {
178+
void injectMutualRecursion() {
173179
A a = ExtensionList.lookupSingleton(A.class);
174180
B b = ExtensionList.lookupSingleton(B.class);
175181
assertEquals(b, a.b);
@@ -188,7 +194,7 @@ public static final class B {
188194

189195
@Issue("JENKINS-60816")
190196
@Test
191-
public void injectInterface() {
197+
void injectInterface() {
192198
assertThat(ExtensionList.lookupSingleton(X.class).xface, instanceOf(Impl.class));
193199
}
194200

test/src/test/java/hudson/ExtensionListTest.java

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package hudson;
22

3-
import static org.junit.Assert.assertEquals;
4-
import static org.junit.Assert.assertFalse;
5-
import static org.junit.Assert.assertNotNull;
6-
import static org.junit.Assert.assertSame;
7-
import static org.junit.Assert.assertTrue;
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertFalse;
5+
import static org.junit.jupiter.api.Assertions.assertNotNull;
6+
import static org.junit.jupiter.api.Assertions.assertSame;
7+
import static org.junit.jupiter.api.Assertions.assertTrue;
88

99
import hudson.model.Describable;
1010
import hudson.model.Descriptor;
@@ -13,20 +13,18 @@
1313
import java.util.Collection;
1414
import java.util.List;
1515
import jenkins.model.Jenkins;
16-
import org.junit.Rule;
17-
import org.junit.Test;
16+
import org.junit.jupiter.api.Test;
1817
import org.jvnet.hudson.test.Issue;
1918
import org.jvnet.hudson.test.JenkinsRule;
2019
import org.jvnet.hudson.test.WithoutJenkins;
20+
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
2121

2222
/**
2323
* @author Kohsuke Kawaguchi
2424
*/
25+
@WithJenkins
2526
public class ExtensionListTest {
2627

27-
@Rule
28-
public JenkinsRule j = new JenkinsRule();
29-
3028
//
3129
//
3230
// non-Descriptor extension point
@@ -46,7 +44,7 @@ public static class Cat implements Animal {
4644

4745

4846
@Test
49-
public void autoDiscovery() {
47+
void autoDiscovery(JenkinsRule j) {
5048
ExtensionList<Animal> list = ExtensionList.lookup(Animal.class);
5149
assertEquals(2, list.size());
5250
assertNotNull(list.get(Dog.class));
@@ -55,14 +53,14 @@ public void autoDiscovery() {
5553

5654
@Test
5755
@WithoutJenkins
58-
public void nullJenkinsInstance() {
56+
void nullJenkinsInstance() {
5957
ExtensionList<Animal> list = ExtensionList.lookup(Animal.class);
6058
assertEquals(0, list.size());
6159
assertFalse(list.iterator().hasNext());
6260
}
6361

6462
@Test
65-
public void extensionListView() {
63+
void extensionListView(JenkinsRule j) {
6664
// this is how legacy list like UserNameResolver.LIST gets created.
6765
List<Animal> LIST = ExtensionListView.createList(Animal.class);
6866

@@ -117,7 +115,7 @@ public static final class DescriptorImpl extends FishDescriptor {}
117115
* Verifies that the automated {@link Descriptor} lookup works.
118116
*/
119117
@Test
120-
public void descriptorLookup() {
118+
void descriptorLookup(JenkinsRule j) {
121119
Descriptor<Fish> d = new Sishamo().getDescriptor();
122120

123121
DescriptorExtensionList<Fish, Descriptor<Fish>> list = j.jenkins.getDescriptorList(Fish.class);
@@ -127,7 +125,7 @@ public void descriptorLookup() {
127125
}
128126

129127
@Test
130-
public void fishDiscovery() {
128+
void fishDiscovery(JenkinsRule j) {
131129
// imagine that this is a static instance, like it is in many LIST static field in Hudson.
132130
DescriptorList<Fish> LIST = new DescriptorList<>(Fish.class);
133131

@@ -156,7 +154,7 @@ public void fishDiscovery() {
156154
}
157155

158156
@Test
159-
public void legacyDescriptorList() {
157+
void legacyDescriptorList(JenkinsRule j) {
160158
// created in a legacy fashion without any tie to ExtensionList
161159
DescriptorList<Fish> LIST = new DescriptorList<>();
162160

@@ -199,7 +197,7 @@ public Toyota() {
199197
* Makes sure sorting of the components work as expected.
200198
*/
201199
@Test
202-
public void ordinals() {
200+
void ordinals(JenkinsRule j) {
203201
ExtensionList<Car> list = j.jenkins.getExtensionList(Car.class);
204202
assertEquals("honda", list.get(0).name);
205203
assertEquals("mazda", list.get(1).name);
@@ -208,7 +206,7 @@ public void ordinals() {
208206

209207
@Issue("JENKINS-39520")
210208
@Test
211-
public void removeAll() {
209+
void removeAll(JenkinsRule j) {
212210
ExtensionList<Animal> list = ExtensionList.lookup(Animal.class);
213211
assertTrue(list.removeAll(new ArrayList<>(list)));
214212
assertEquals(0, list.size());
@@ -218,7 +216,7 @@ public void removeAll() {
218216

219217
@Issue("JENKINS-62056")
220218
@Test
221-
public void checkSort() {
219+
void checkSort(JenkinsRule j) {
222220
ExtensionList.lookup(Object.class).get(0); // exceptions are a problem
223221
}
224222
}

0 commit comments

Comments
 (0)