Skip to content

Commit 1808e41

Browse files
Migrate tests to JUnit5 (core / jenkins / 3)
* Migrate annotations and imports * Migrate assertions * Remove public visibility for test classes and methods * Minor code cleanup
1 parent e97adbd commit 1808e41

File tree

60 files changed

+2072
-2027
lines changed

Some content is hidden

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

60 files changed

+2072
-2027
lines changed

core/src/main/java/jenkins/security/ConfidentialStore.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package jenkins.security;
22

3+
import com.google.common.annotations.VisibleForTesting;
34
import edu.umd.cs.findbugs.annotations.CheckForNull;
45
import edu.umd.cs.findbugs.annotations.NonNull;
56
import hudson.Extension;
@@ -19,6 +20,8 @@
1920
import java.util.logging.Logger;
2021
import jenkins.model.Jenkins;
2122
import org.kohsuke.MetaInfServices;
23+
import org.kohsuke.accmod.Restricted;
24+
import org.kohsuke.accmod.restrictions.NoExternalUse;
2225

2326
/**
2427
* The actual storage for the data held by {@link ConfidentialKey}s, and the holder
@@ -98,9 +101,11 @@ public abstract class ConfidentialStore {
98101
return cs;
99102
}
100103

101-
static final class Mock extends ConfidentialStore {
104+
@Restricted(NoExternalUse.class)
105+
@VisibleForTesting
106+
public static final class Mock extends ConfidentialStore {
102107

103-
static final Mock INSTANCE = new Mock();
108+
public static final Mock INSTANCE = new Mock();
104109

105110
private final SecureRandom rand;
106111

@@ -116,7 +121,7 @@ static final class Mock extends ConfidentialStore {
116121
rand.setSeed(new byte[] {1, 2, 3, 4});
117122
}
118123

119-
void clear() {
124+
public void clear() {
120125
data.clear();
121126
}
122127

core/src/test/java/hudson/util/SecretTest.java

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@
2727
import static org.hamcrest.MatcherAssert.assertThat;
2828
import static org.hamcrest.Matchers.containsString;
2929
import static org.hamcrest.Matchers.not;
30-
import static org.junit.Assert.assertEquals;
31-
import static org.junit.Assert.assertFalse;
32-
import static org.junit.Assert.assertNotEquals;
33-
import static org.junit.Assert.assertTrue;
30+
import static org.junit.jupiter.api.Assertions.assertEquals;
31+
import static org.junit.jupiter.api.Assertions.assertFalse;
32+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
33+
import static org.junit.jupiter.api.Assertions.assertTrue;
3434

3535
import java.nio.charset.StandardCharsets;
3636
import java.util.Base64;
@@ -39,19 +39,21 @@
3939
import javax.crypto.Cipher;
4040
import javax.crypto.SecretKey;
4141
import jenkins.model.Jenkins;
42-
import jenkins.security.ConfidentialStoreRule;
43-
import org.junit.Rule;
44-
import org.junit.Test;
42+
import jenkins.security.ConfidentialStore;
43+
import org.junit.jupiter.api.BeforeEach;
44+
import org.junit.jupiter.api.Test;
4545

46-
public class SecretTest {
47-
48-
@Rule
49-
public ConfidentialStoreRule confidentialStore = new ConfidentialStoreRule();
46+
class SecretTest {
5047

5148
private static final Pattern ENCRYPTED_VALUE_PATTERN = Pattern.compile("\\{?[A-Za-z0-9+/]+={0,2}}?");
5249

50+
@BeforeEach
51+
void setUp() {
52+
ConfidentialStore.Mock.INSTANCE.clear();
53+
}
54+
5355
@Test
54-
public void encrypt() {
56+
void encrypt() {
5557
Secret secret = Secret.fromString("abc");
5658
assertEquals("abc", secret.getPlainText());
5759

@@ -68,7 +70,7 @@ public void encrypt() {
6870
}
6971

7072
@Test
71-
public void encryptedValuePattern() {
73+
void encryptedValuePattern() {
7274
final Random random = new Random();
7375
for (int i = 1; i < 100; i++) {
7476
String plaintext = random(i, random);
@@ -95,20 +97,20 @@ private static String random(int count, Random random) {
9597
}
9698

9799
@Test
98-
public void decrypt() {
100+
void decrypt() {
99101
assertEquals("abc", Secret.toString(Secret.fromString("abc")));
100102
}
101103

102104
@Test
103-
public void serialization() {
105+
void serialization() {
104106
Secret s = Secret.fromString("Mr.Jenkins");
105107
String xml = Jenkins.XSTREAM.toXML(s);
106108
assertThat(xml, not(containsString(s.getPlainText())));
107109
// TODO MatchesPattern not available until Hamcrest 2.0
108-
assertTrue(xml, xml.matches("<hudson[.]util[.]Secret>[{][A-Za-z0-9+/]+={0,2}[}]</hudson[.]util[.]Secret>"));
110+
assertTrue(xml.matches("<hudson[.]util[.]Secret>[{][A-Za-z0-9+/]+={0,2}[}]</hudson[.]util[.]Secret>"), xml);
109111

110112
Object o = Jenkins.XSTREAM.fromXML(xml);
111-
assertEquals(xml, s, o);
113+
assertEquals(s, o, xml);
112114
}
113115

114116
public static class Foo {
@@ -119,7 +121,7 @@ public static class Foo {
119121
* Makes sure the serialization form is backward compatible with String.
120122
*/
121123
@Test
122-
public void testCompatibilityFromString() {
124+
void testCompatibilityFromString() {
123125
String tagName = Foo.class.getName().replace("$", "_-");
124126
String xml = "<" + tagName + "><password>secret</password></" + tagName + ">";
125127
Foo foo = new Foo();
@@ -132,15 +134,15 @@ public void testCompatibilityFromString() {
132134
*/
133135
@Test
134136
@SuppressWarnings("deprecation")
135-
public void migrationFromLegacyKeyToConfidentialStore() throws Exception {
137+
void migrationFromLegacyKeyToConfidentialStore() throws Exception {
136138
SecretKey legacy = HistoricalSecrets.getLegacyKey();
137139
for (String str : new String[] {"Hello world", "", "\u0000unprintable"}) {
138140
Cipher cipher = Secret.getCipher("AES");
139141
cipher.init(Cipher.ENCRYPT_MODE, legacy);
140142
String old = Base64.getEncoder().encodeToString(cipher.doFinal((str + HistoricalSecrets.MAGIC).getBytes(StandardCharsets.UTF_8)));
141143
Secret s = Secret.fromString(old);
142-
assertEquals("secret by the old key should decrypt", str, s.getPlainText());
143-
assertNotEquals("but when encrypting, ConfidentialKey should be in use", old, s.getEncryptedValue());
144+
assertEquals(str, s.getPlainText(), "secret by the old key should decrypt");
145+
assertNotEquals(old, s.getEncryptedValue(), "but when encrypting, ConfidentialKey should be in use");
144146
}
145147
}
146148

core/src/test/java/jenkins/ResilientJsonObjectTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
package jenkins;
22

3-
import static org.junit.Assert.assertEquals;
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
44

55
import net.sf.json.JSONObject;
6-
import org.junit.Test;
6+
import org.junit.jupiter.api.Test;
77
import org.jvnet.hudson.test.Issue;
88

99
/**
1010
* @author Kohsuke Kawaguchi
1111
*/
12-
public class ResilientJsonObjectTest {
12+
class ResilientJsonObjectTest {
1313
public static class Foo { public int a; }
1414

1515
/**
1616
* {@link JSONObject} databinding should be able to ignore non-existent fields.
1717
*/
1818
@Test
1919
@Issue("JENKINS-15105")
20-
public void databindingShouldIgnoreUnrecognizedJsonProperty() {
20+
void databindingShouldIgnoreUnrecognizedJsonProperty() {
2121
JSONObject o = JSONObject.fromObject("{a:1,b:2}");
2222
Foo f = (Foo) JSONObject.toBean(o, Foo.class);
2323
assertEquals(1, f.a);

core/src/test/java/jenkins/model/CoreEnvironmentContributorTest.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,40 +10,39 @@
1010
import hudson.model.Job;
1111
import hudson.model.TaskListener;
1212
import java.io.File;
13-
import java.io.IOException;
14-
import org.junit.After;
15-
import org.junit.Before;
16-
import org.junit.Test;
13+
import org.junit.jupiter.api.AfterEach;
14+
import org.junit.jupiter.api.BeforeEach;
15+
import org.junit.jupiter.api.Test;
1716
import org.jvnet.hudson.test.Issue;
1817
import org.mockito.Mock;
1918
import org.mockito.MockedStatic;
2019
import org.mockito.MockitoAnnotations;
2120

22-
public class CoreEnvironmentContributorTest {
23-
CoreEnvironmentContributor instance;
21+
class CoreEnvironmentContributorTest {
22+
private CoreEnvironmentContributor instance;
2423

2524
private AutoCloseable mocks;
2625

2726
@Mock
28-
Job job;
27+
private Job job;
2928

3029
@Mock
31-
TaskListener listener;
30+
private TaskListener listener;
3231

33-
@After
34-
public void tearDown() throws Exception {
32+
@AfterEach
33+
void tearDown() throws Exception {
3534
mocks.close();
3635
}
3736

38-
@Before
39-
public void setUp() {
37+
@BeforeEach
38+
void setUp() {
4039
mocks = MockitoAnnotations.openMocks(this);
4140
instance = new CoreEnvironmentContributor();
4241
}
4342

4443
@Issue("JENKINS-19307")
4544
@Test
46-
public void buildEnvironmentForJobShouldntUseCurrentComputer() throws IOException, InterruptedException {
45+
void buildEnvironmentForJobShouldntUseCurrentComputer() throws Exception {
4746
Computer computer = mock(Computer.class);
4847
Jenkins jenkins = mock(Jenkins.class);
4948
try (

core/src/test/java/jenkins/model/IdStrategyTest.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22

33
import static org.hamcrest.MatcherAssert.assertThat;
44
import static org.hamcrest.Matchers.is;
5-
import static org.junit.Assert.assertEquals;
6-
import static org.junit.Assert.assertFalse;
7-
import static org.junit.Assert.assertTrue;
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
import static org.junit.jupiter.api.Assertions.assertFalse;
7+
import static org.junit.jupiter.api.Assertions.assertTrue;
88

9-
import org.junit.Test;
9+
import org.junit.jupiter.api.Test;
1010

11-
public class IdStrategyTest {
11+
class IdStrategyTest {
1212

1313
@SuppressWarnings("deprecation")
1414
@Test
15-
public void caseInsensitive() {
15+
void caseInsensitive() {
1616
IdStrategy idStrategy = new IdStrategy.CaseInsensitive();
1717

1818
assertRestrictedNames(idStrategy);
@@ -48,7 +48,7 @@ public void caseInsensitive() {
4848

4949
@SuppressWarnings("deprecation")
5050
@Test
51-
public void caseInsensitivePassesThroughOldLegacy() {
51+
void caseInsensitivePassesThroughOldLegacy() {
5252
IdStrategy idStrategy = new IdStrategy.CaseInsensitive();
5353

5454
assertThat(idStrategy.idFromFilename("make\u1000000"), is("make\u1000000"));
@@ -59,7 +59,7 @@ public void caseInsensitivePassesThroughOldLegacy() {
5959

6060
@SuppressWarnings("deprecation")
6161
@Test
62-
public void caseSensitive() {
62+
void caseSensitive() {
6363
IdStrategy idStrategy = new IdStrategy.CaseSensitive();
6464

6565
assertRestrictedNames(idStrategy);
@@ -98,7 +98,7 @@ public void caseSensitive() {
9898

9999
@SuppressWarnings("deprecation")
100100
@Test
101-
public void caseSensitivePassesThroughOldLegacy() {
101+
void caseSensitivePassesThroughOldLegacy() {
102102
IdStrategy idStrategy = new IdStrategy.CaseSensitive();
103103

104104
assertThat(idStrategy.idFromFilename("make\u1000000"), is("make\u1000000"));
@@ -107,7 +107,7 @@ public void caseSensitivePassesThroughOldLegacy() {
107107
}
108108

109109
@Test
110-
public void testEqualsCaseInsensitive() {
110+
void testEqualsCaseInsensitive() {
111111
IdStrategy idStrategy = IdStrategy.CASE_INSENSITIVE;
112112
assertTrue(idStrategy.equals("user1", "User1"));
113113
assertTrue(idStrategy.equals("User1", "user1"));
@@ -117,7 +117,7 @@ public void testEqualsCaseInsensitive() {
117117
}
118118

119119
@Test
120-
public void testEqualsCaseSensitive() {
120+
void testEqualsCaseSensitive() {
121121
IdStrategy idStrategy = new IdStrategy.CaseSensitive();
122122
assertFalse(idStrategy.equals("user1", "User1"));
123123
assertFalse(idStrategy.equals("User1", "user1"));
@@ -127,7 +127,7 @@ public void testEqualsCaseSensitive() {
127127
}
128128

129129
@Test
130-
public void testEqualsCaseSensitiveEmailAddress() {
130+
void testEqualsCaseSensitiveEmailAddress() {
131131
IdStrategy idStrategy = new IdStrategy.CaseSensitiveEmailAddress();
132132
assertFalse(idStrategy.equals("john.smith@acme.org", "John.Smith@acme.org"));
133133
assertFalse(idStrategy.equals("john.smith@acme.org", "John.Smith@ACME.org"));
@@ -142,23 +142,23 @@ public void testEqualsCaseSensitiveEmailAddress() {
142142
}
143143

144144
@Test
145-
public void testKeyForCaseInsensitive() {
145+
void testKeyForCaseInsensitive() {
146146
IdStrategy idStrategy = IdStrategy.CASE_INSENSITIVE;
147147
assertThat(idStrategy.keyFor("user1"), is("user1"));
148148
assertThat(idStrategy.keyFor("User1"), is("user1"));
149149
assertThat(idStrategy.keyFor("USER1"), is("user1"));
150150
}
151151

152152
@Test
153-
public void testKeyForCaseSensitive() {
153+
void testKeyForCaseSensitive() {
154154
IdStrategy idStrategy = new IdStrategy.CaseSensitive();
155155
assertThat(idStrategy.keyFor("user1"), is("user1"));
156156
assertThat(idStrategy.keyFor("User1"), is("User1"));
157157
assertThat(idStrategy.keyFor("USER1"), is("USER1"));
158158
}
159159

160160
@Test
161-
public void testKeyForCaseSensitiveEmailAddress() {
161+
void testKeyForCaseSensitiveEmailAddress() {
162162
IdStrategy idStrategy = new IdStrategy.CaseSensitiveEmailAddress();
163163
assertThat(idStrategy.keyFor("john.smith@acme.org"), is("john.smith@acme.org"));
164164
assertThat(idStrategy.keyFor("John.Smith@acme.org"), is("John.Smith@acme.org"));
@@ -171,7 +171,7 @@ public void testKeyForCaseSensitiveEmailAddress() {
171171
}
172172

173173
@Test
174-
public void testCompareCaseInsensitive() {
174+
void testCompareCaseInsensitive() {
175175
IdStrategy idStrategy = IdStrategy.CASE_INSENSITIVE;
176176
assertTrue(idStrategy.compare("user1", "user2") < 0);
177177
assertTrue(idStrategy.compare("user2", "user1") > 0);
@@ -182,7 +182,7 @@ public void testCompareCaseInsensitive() {
182182
}
183183

184184
@Test
185-
public void testCompareCaseSensitive() {
185+
void testCompareCaseSensitive() {
186186
IdStrategy idStrategy = new IdStrategy.CaseSensitive();
187187
assertTrue(idStrategy.compare("user1", "user2") < 0);
188188
assertTrue(idStrategy.compare("user2", "user1") > 0);
@@ -193,7 +193,7 @@ public void testCompareCaseSensitive() {
193193
}
194194

195195
@Test
196-
public void testCompareCaseSensitiveEmail() {
196+
void testCompareCaseSensitiveEmail() {
197197
IdStrategy idStrategy = new IdStrategy.CaseSensitiveEmailAddress();
198198
assertEquals(0, idStrategy.compare("john.smith@acme.org", "john.smith@acme.org"));
199199
assertEquals(0, idStrategy.compare("John.Smith@acme.org", "John.Smith@acme.org"));

core/src/test/java/jenkins/model/JDKNameTest.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,27 +28,28 @@
2828
import static org.hamcrest.Matchers.is;
2929

3030
import hudson.model.JDK;
31-
import org.junit.Test;
31+
import org.junit.jupiter.api.Test;
32+
33+
class JDKNameTest {
3234

33-
public class JDKNameTest {
3435
@Test
35-
public void nullIsDefaultName() {
36+
void nullIsDefaultName() {
3637
assertThat(JDK.isDefaultName(null), is(true));
3738
}
3839

3940
@Test
40-
public void recognizeOldDefaultName() {
41+
void recognizeOldDefaultName() {
4142
// DEFAULT_NAME took this value prior to 1.598.
4243
assertThat(JDK.isDefaultName("(Default)"), is(true));
4344
}
4445

4546
@Test
46-
public void recognizeDefaultName() {
47+
void recognizeDefaultName() {
4748
assertThat(JDK.isDefaultName(JDK.DEFAULT_NAME), is(true));
4849
}
4950

5051
@Test
51-
public void othernameNotDefault() {
52+
void othernameNotDefault() {
5253
assertThat(JDK.isDefaultName("I'm a customized name"), is(false));
5354
}
5455

0 commit comments

Comments
 (0)