Skip to content

Migrate tests to JUnit5 (core / jenkins / 3) #10578

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

Merged
Merged
11 changes: 8 additions & 3 deletions core/src/main/java/jenkins/security/ConfidentialStore.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package jenkins.security;

import com.google.common.annotations.VisibleForTesting;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Extension;
Expand All @@ -19,6 +20,8 @@
import java.util.logging.Logger;
import jenkins.model.Jenkins;
import org.kohsuke.MetaInfServices;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;

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

static final class Mock extends ConfidentialStore {
@Restricted(NoExternalUse.class)
@VisibleForTesting
public static final class Mock extends ConfidentialStore {

static final Mock INSTANCE = new Mock();
public static final Mock INSTANCE = new Mock();

private final SecureRandom rand;

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

void clear() {
public void clear() {
data.clear();
}

Expand Down
44 changes: 23 additions & 21 deletions core/src/test/java/hudson/util/SecretTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.nio.charset.StandardCharsets;
import java.util.Base64;
Expand All @@ -39,19 +39,21 @@
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import jenkins.model.Jenkins;
import jenkins.security.ConfidentialStoreRule;
import org.junit.Rule;
import org.junit.Test;
import jenkins.security.ConfidentialStore;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class SecretTest {

@Rule
public ConfidentialStoreRule confidentialStore = new ConfidentialStoreRule();
class SecretTest {

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

@BeforeEach
void setUp() {
ConfidentialStore.Mock.INSTANCE.clear();
}

@Test
public void encrypt() {
void encrypt() {
Secret secret = Secret.fromString("abc");
assertEquals("abc", secret.getPlainText());

Expand All @@ -68,7 +70,7 @@ public void encrypt() {
}

@Test
public void encryptedValuePattern() {
void encryptedValuePattern() {
final Random random = new Random();
for (int i = 1; i < 100; i++) {
String plaintext = random(i, random);
Expand All @@ -95,20 +97,20 @@ private static String random(int count, Random random) {
}

@Test
public void decrypt() {
void decrypt() {
assertEquals("abc", Secret.toString(Secret.fromString("abc")));
}

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

Object o = Jenkins.XSTREAM.fromXML(xml);
assertEquals(xml, s, o);
assertEquals(s, o, xml);
}

public static class Foo {
Expand All @@ -119,7 +121,7 @@ public static class Foo {
* Makes sure the serialization form is backward compatible with String.
*/
@Test
public void testCompatibilityFromString() {
void testCompatibilityFromString() {
String tagName = Foo.class.getName().replace("$", "_-");
String xml = "<" + tagName + "><password>secret</password></" + tagName + ">";
Foo foo = new Foo();
Expand All @@ -132,15 +134,15 @@ public void testCompatibilityFromString() {
*/
@Test
@SuppressWarnings("deprecation")
public void migrationFromLegacyKeyToConfidentialStore() throws Exception {
void migrationFromLegacyKeyToConfidentialStore() throws Exception {
SecretKey legacy = HistoricalSecrets.getLegacyKey();
for (String str : new String[] {"Hello world", "", "\u0000unprintable"}) {
Cipher cipher = Secret.getCipher("AES");
cipher.init(Cipher.ENCRYPT_MODE, legacy);
String old = Base64.getEncoder().encodeToString(cipher.doFinal((str + HistoricalSecrets.MAGIC).getBytes(StandardCharsets.UTF_8)));
Secret s = Secret.fromString(old);
assertEquals("secret by the old key should decrypt", str, s.getPlainText());
assertNotEquals("but when encrypting, ConfidentialKey should be in use", old, s.getEncryptedValue());
assertEquals(str, s.getPlainText(), "secret by the old key should decrypt");
assertNotEquals(old, s.getEncryptedValue(), "but when encrypting, ConfidentialKey should be in use");
}
}

Expand Down
8 changes: 4 additions & 4 deletions core/src/test/java/jenkins/ResilientJsonObjectTest.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
package jenkins;

import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

import net.sf.json.JSONObject;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.jvnet.hudson.test.Issue;

/**
* @author Kohsuke Kawaguchi
*/
public class ResilientJsonObjectTest {
class ResilientJsonObjectTest {
public static class Foo { public int a; }

/**
* {@link JSONObject} databinding should be able to ignore non-existent fields.
*/
@Test
@Issue("JENKINS-15105")
public void databindingShouldIgnoreUnrecognizedJsonProperty() {
void databindingShouldIgnoreUnrecognizedJsonProperty() {
JSONObject o = JSONObject.fromObject("{a:1,b:2}");
Foo f = (Foo) JSONObject.toBean(o, Foo.class);
assertEquals(1, f.a);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,39 @@
import hudson.model.Job;
import hudson.model.TaskListener;
import java.io.File;
import java.io.IOException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.jvnet.hudson.test.Issue;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.MockitoAnnotations;

public class CoreEnvironmentContributorTest {
CoreEnvironmentContributor instance;
class CoreEnvironmentContributorTest {
private CoreEnvironmentContributor instance;

private AutoCloseable mocks;

@Mock
Job job;
private Job job;

@Mock
TaskListener listener;
private TaskListener listener;

@After
public void tearDown() throws Exception {
@AfterEach
void tearDown() throws Exception {
mocks.close();
}

@Before
public void setUp() {
@BeforeEach
void setUp() {
mocks = MockitoAnnotations.openMocks(this);
instance = new CoreEnvironmentContributor();
}

@Issue("JENKINS-19307")
@Test
public void buildEnvironmentForJobShouldntUseCurrentComputer() throws IOException, InterruptedException {
void buildEnvironmentForJobShouldntUseCurrentComputer() throws Exception {
Computer computer = mock(Computer.class);
Jenkins jenkins = mock(Jenkins.class);
try (
Expand Down
36 changes: 18 additions & 18 deletions core/src/test/java/jenkins/model/IdStrategyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.Test;
import org.junit.jupiter.api.Test;

public class IdStrategyTest {
class IdStrategyTest {

@SuppressWarnings("deprecation")
@Test
public void caseInsensitive() {
void caseInsensitive() {
IdStrategy idStrategy = new IdStrategy.CaseInsensitive();

assertRestrictedNames(idStrategy);
Expand Down Expand Up @@ -48,7 +48,7 @@ public void caseInsensitive() {

@SuppressWarnings("deprecation")
@Test
public void caseInsensitivePassesThroughOldLegacy() {
void caseInsensitivePassesThroughOldLegacy() {
IdStrategy idStrategy = new IdStrategy.CaseInsensitive();

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

@SuppressWarnings("deprecation")
@Test
public void caseSensitive() {
void caseSensitive() {
IdStrategy idStrategy = new IdStrategy.CaseSensitive();

assertRestrictedNames(idStrategy);
Expand Down Expand Up @@ -98,7 +98,7 @@ public void caseSensitive() {

@SuppressWarnings("deprecation")
@Test
public void caseSensitivePassesThroughOldLegacy() {
void caseSensitivePassesThroughOldLegacy() {
IdStrategy idStrategy = new IdStrategy.CaseSensitive();

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

@Test
public void testEqualsCaseInsensitive() {
void testEqualsCaseInsensitive() {
IdStrategy idStrategy = IdStrategy.CASE_INSENSITIVE;
assertTrue(idStrategy.equals("user1", "User1"));
assertTrue(idStrategy.equals("User1", "user1"));
Expand All @@ -117,7 +117,7 @@ public void testEqualsCaseInsensitive() {
}

@Test
public void testEqualsCaseSensitive() {
void testEqualsCaseSensitive() {
IdStrategy idStrategy = new IdStrategy.CaseSensitive();
assertFalse(idStrategy.equals("user1", "User1"));
assertFalse(idStrategy.equals("User1", "user1"));
Expand All @@ -127,7 +127,7 @@ public void testEqualsCaseSensitive() {
}

@Test
public void testEqualsCaseSensitiveEmailAddress() {
void testEqualsCaseSensitiveEmailAddress() {
IdStrategy idStrategy = new IdStrategy.CaseSensitiveEmailAddress();
assertFalse(idStrategy.equals("john.smith@acme.org", "John.Smith@acme.org"));
assertFalse(idStrategy.equals("john.smith@acme.org", "John.Smith@ACME.org"));
Expand All @@ -142,23 +142,23 @@ public void testEqualsCaseSensitiveEmailAddress() {
}

@Test
public void testKeyForCaseInsensitive() {
void testKeyForCaseInsensitive() {
IdStrategy idStrategy = IdStrategy.CASE_INSENSITIVE;
assertThat(idStrategy.keyFor("user1"), is("user1"));
assertThat(idStrategy.keyFor("User1"), is("user1"));
assertThat(idStrategy.keyFor("USER1"), is("user1"));
}

@Test
public void testKeyForCaseSensitive() {
void testKeyForCaseSensitive() {
IdStrategy idStrategy = new IdStrategy.CaseSensitive();
assertThat(idStrategy.keyFor("user1"), is("user1"));
assertThat(idStrategy.keyFor("User1"), is("User1"));
assertThat(idStrategy.keyFor("USER1"), is("USER1"));
}

@Test
public void testKeyForCaseSensitiveEmailAddress() {
void testKeyForCaseSensitiveEmailAddress() {
IdStrategy idStrategy = new IdStrategy.CaseSensitiveEmailAddress();
assertThat(idStrategy.keyFor("john.smith@acme.org"), is("john.smith@acme.org"));
assertThat(idStrategy.keyFor("John.Smith@acme.org"), is("John.Smith@acme.org"));
Expand All @@ -171,7 +171,7 @@ public void testKeyForCaseSensitiveEmailAddress() {
}

@Test
public void testCompareCaseInsensitive() {
void testCompareCaseInsensitive() {
IdStrategy idStrategy = IdStrategy.CASE_INSENSITIVE;
assertTrue(idStrategy.compare("user1", "user2") < 0);
assertTrue(idStrategy.compare("user2", "user1") > 0);
Expand All @@ -182,7 +182,7 @@ public void testCompareCaseInsensitive() {
}

@Test
public void testCompareCaseSensitive() {
void testCompareCaseSensitive() {
IdStrategy idStrategy = new IdStrategy.CaseSensitive();
assertTrue(idStrategy.compare("user1", "user2") < 0);
assertTrue(idStrategy.compare("user2", "user1") > 0);
Expand All @@ -193,7 +193,7 @@ public void testCompareCaseSensitive() {
}

@Test
public void testCompareCaseSensitiveEmail() {
void testCompareCaseSensitiveEmail() {
IdStrategy idStrategy = new IdStrategy.CaseSensitiveEmailAddress();
assertEquals(0, idStrategy.compare("john.smith@acme.org", "john.smith@acme.org"));
assertEquals(0, idStrategy.compare("John.Smith@acme.org", "John.Smith@acme.org"));
Expand Down
13 changes: 7 additions & 6 deletions core/src/test/java/jenkins/model/JDKNameTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,28 @@
import static org.hamcrest.Matchers.is;

import hudson.model.JDK;
import org.junit.Test;
import org.junit.jupiter.api.Test;

class JDKNameTest {

public class JDKNameTest {
@Test
public void nullIsDefaultName() {
void nullIsDefaultName() {
assertThat(JDK.isDefaultName(null), is(true));
}

@Test
public void recognizeOldDefaultName() {
void recognizeOldDefaultName() {
// DEFAULT_NAME took this value prior to 1.598.
assertThat(JDK.isDefaultName("(Default)"), is(true));
}

@Test
public void recognizeDefaultName() {
void recognizeDefaultName() {
assertThat(JDK.isDefaultName(JDK.DEFAULT_NAME), is(true));
}

@Test
public void othernameNotDefault() {
void othernameNotDefault() {
assertThat(JDK.isDefaultName("I'm a customized name"), is(false));
}

Expand Down
Loading
Loading