提交 08a4f042 编写于 作者: S Stefan Spieker

minor code cleanup, simplified asserts and try with resources

上级 d5b416eb
......@@ -240,9 +240,9 @@ public class CLITest {
WebResponse rsp = wc.goTo("cli-proxy/").getWebResponse();
assertEquals(rsp.getContentAsString(), HttpURLConnection.HTTP_MOVED_TEMP, rsp.getStatusCode());
assertEquals(rsp.getContentAsString(), null, rsp.getResponseHeaderValue("X-Jenkins"));
assertEquals(rsp.getContentAsString(), null, rsp.getResponseHeaderValue("X-Jenkins-CLI-Port"));
assertEquals(rsp.getContentAsString(), null, rsp.getResponseHeaderValue("X-SSH-Endpoint"));
assertNull(rsp.getContentAsString(), rsp.getResponseHeaderValue("X-Jenkins"));
assertNull(rsp.getContentAsString(), rsp.getResponseHeaderValue("X-Jenkins-CLI-Port"));
assertNull(rsp.getContentAsString(), rsp.getResponseHeaderValue("X-SSH-Endpoint"));
for (String transport: Arrays.asList("-http", "-ssh")) {
......
......@@ -47,11 +47,8 @@ public class BuildExecutionTest {
FreeStyleBuild b = r.assertBuildStatus(Result.FAILURE, p.scheduleBuild2(0).get());
r.assertLogContains(Messages.Build_post_build_steps_failed(), b);
FilePath ws = r.jenkins.getWorkspaceFor(p);
WorkspaceList.Lease lease = r.jenkins.toComputer().getWorkspaceList().allocate(ws);
try {
try (WorkspaceList.Lease lease = r.jenkins.toComputer().getWorkspaceList().allocate(ws)) {
assertEquals(ws, lease.path);
} finally {
lease.close();
}
}
......
......@@ -26,6 +26,7 @@ package hudson.model;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
......@@ -78,7 +79,7 @@ public class FreeStyleProjectTest {
assertEquals(1,builders.size());
assertEquals(Shell.class,builders.get(0).getClass());
assertEquals("echo hello",((Shell)builders.get(0)).getCommand().trim());
assertTrue(builders.get(0)!=shell);
assertNotSame(builders.get(0), shell);
}
/**
......@@ -127,7 +128,7 @@ public class FreeStyleProjectTest {
assertEquals(1,builders.size());
assertEquals(Shell.class,builders.get(0).getClass());
assertEquals("echo hello",((Shell)builders.get(0)).getCommand().trim());
assertTrue(builders.get(0)!=shell);
assertNotSame(builders.get(0), shell);
System.out.println(project.getConfigFile().asString());
}
......
......@@ -24,6 +24,7 @@
package hudson.model;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import com.gargoylesoftware.htmlunit.html.DomNodeUtil;
......@@ -65,7 +66,7 @@ public class ManagementLinkTest {
@Test @Issue("JENKINS-33683")
public void invisibleLinks() throws Exception {
assertEquals(null, j.jenkins.getDynamic("and_fail_trying"));
assertNull(j.jenkins.getDynamic("and_fail_trying"));
}
@TestExtension // Intentionally hooked in all tests
......
......@@ -98,7 +98,7 @@ public class ParametersAction2Test {
@Issue("SECURITY-170")
public void parametersDefinitionChange() throws Exception {
FreeStyleProject p = j.createFreeStyleProject();
p.addProperty(new ParametersDefinitionProperty(Arrays.<ParameterDefinition>asList(
p.addProperty(new ParametersDefinitionProperty(Arrays.asList(
new StringParameterDefinition("foo", "foo"),
new StringParameterDefinition("bar", "bar"))));
......@@ -108,28 +108,28 @@ public class ParametersAction2Test {
new StringParameterValue("undef", "undef")
)));
assertTrue("undef parameter is not listed in getParameters",
!hasParameterWithName(build.getAction(ParametersAction.class), "undef"));
assertFalse("undef parameter is not listed in getParameters",
hasParameterWithName(build.getAction(ParametersAction.class), "undef"));
p.removeProperty(ParametersDefinitionProperty.class);
p.addProperty(new ParametersDefinitionProperty(Arrays.<ParameterDefinition>asList(
p.addProperty(new ParametersDefinitionProperty(Arrays.asList(
new StringParameterDefinition("foo", "foo"),
new StringParameterDefinition("bar", "bar"),
new StringParameterDefinition("undef", "undef"))));
// undef is still not listed even after being added to the job parameters definition
assertTrue("undef parameter is not listed in getParameters",
!hasParameterWithName(build.getAction(ParametersAction.class), "undef"));
assertFalse("undef parameter is not listed in getParameters",
hasParameterWithName(build.getAction(ParametersAction.class), "undef"));
// remove bar and undef from parameters definition
p.removeProperty(ParametersDefinitionProperty.class);
p.addProperty(new ParametersDefinitionProperty(Arrays.<ParameterDefinition>asList(
p.addProperty(new ParametersDefinitionProperty(Arrays.asList(
new StringParameterDefinition("foo", "foo"))));
assertTrue("the build still have 2 parameters", build.getAction(ParametersAction.class).getParameters().size() == 2);
assertEquals("the build still have 2 parameters", 2, build.getAction(ParametersAction.class).getParameters().size());
p.removeProperty(ParametersDefinitionProperty.class);
assertTrue("the build still have 2 parameters", build.getAction(ParametersAction.class).getParameters().size() == 2);
assertEquals("the build still have 2 parameters", 2, build.getAction(ParametersAction.class).getParameters().size());
}
@Test
......@@ -163,13 +163,13 @@ public class ParametersAction2Test {
public void whitelistedParameterByOverride() throws Exception {
FreeStyleProject p = j.createFreeStyleProject();
String name = p.getFullName();
p.addProperty(new ParametersDefinitionProperty(Arrays.<ParameterDefinition>asList(
p.addProperty(new ParametersDefinitionProperty(Arrays.asList(
new StringParameterDefinition("foo", "foo"),
new StringParameterDefinition("bar", "bar"))));
try {
ParametersAction action = new ParametersAction(
Arrays.<ParameterValue>asList(
Arrays.asList(
new StringParameterValue("foo", "baz"),
new StringParameterValue("bar", "bar"),
new StringParameterValue("whitelisted1", "x"),
......@@ -185,8 +185,6 @@ public class ParametersAction2Test {
hasParameterWithName(build.getAction(ParametersAction.class), "whitelisted2"));
assertFalse("whitelisted3 parameter is listed in getParameters",
hasParameterWithName(build.getAction(ParametersAction.class), "whitelisted3"));
p = null;
build = null;
j.jenkins.reload();
//Test again after reload
p = j.jenkins.getItemByFullName(name, FreeStyleProject.class);
......@@ -207,7 +205,7 @@ public class ParametersAction2Test {
public void whitelistedParameterSameAfterChange() throws Exception {
FreeStyleProject p = j.createFreeStyleProject();
String name = p.getFullName();
p.addProperty(new ParametersDefinitionProperty(Arrays.<ParameterDefinition>asList(
p.addProperty(new ParametersDefinitionProperty(Arrays.asList(
new StringParameterDefinition("foo", "foo"),
new StringParameterDefinition("bar", "bar"))));
......@@ -231,8 +229,6 @@ public class ParametersAction2Test {
hasParameterWithName(build.getAction(ParametersAction.class), "whitelisted4"));
System.setProperty(ParametersAction.SAFE_PARAMETERS_SYSTEM_PROPERTY_NAME, "whitelisted3,whitelisted4");
p = null;
build = null;
j.jenkins.reload();
p = j.jenkins.getItemByFullName(name, FreeStyleProject.class);
build = p.getLastBuild();
......@@ -261,10 +257,10 @@ public class ParametersAction2Test {
new StringParameterValue("bar", "bar")
)));
assertTrue("foo parameter is not listed in getParameters",
!hasParameterWithName(build.getAction(ParametersAction.class), "foo"));
assertTrue("bar parameter is not listed in getParameters",
!hasParameterWithName(build.getAction(ParametersAction.class), "bar"));
assertFalse("foo parameter is not listed in getParameters",
hasParameterWithName(build.getAction(ParametersAction.class), "foo"));
assertFalse("bar parameter is not listed in getParameters",
hasParameterWithName(build.getAction(ParametersAction.class), "bar"));
}
@Test
......@@ -348,13 +344,13 @@ public class ParametersAction2Test {
if (expectLegacyBehavior) {
assertTrue("undef parameter is listed in getParameters", hasParameterWithName(pa.getParameters(), "undef"));
assertTrue("undef parameter is listed in iterator", hasParameterWithName(pa, "undef"));
assertTrue("undef in environment", build.getEnvironment(listener).keySet().contains("undef"));
assertTrue("UNDEF in environment", build.getEnvironment(listener).keySet().contains("UNDEF"));
assertTrue("undef in environment", build.getEnvironment(listener).containsKey("undef"));
assertTrue("UNDEF in environment", build.getEnvironment(listener).containsKey("UNDEF"));
} else {
assertFalse("undef parameter is not listed in getParameters", hasParameterWithName(pa.getParameters(), "undef"));
assertFalse("undef parameter is not listed in iterator", hasParameterWithName(pa, "undef"));
assertFalse("undef not in environment", build.getEnvironment(listener).keySet().contains("undef"));
assertFalse("UNDEF not in environment", build.getEnvironment(listener).keySet().contains("UNDEF"));
assertFalse("undef not in environment", build.getEnvironment(listener).containsKey("undef"));
assertFalse("UNDEF not in environment", build.getEnvironment(listener).containsKey("UNDEF"));
}
assertTrue("undef parameter is listed in getAllParameters", hasParameterWithName(pa.getAllParameters(), "undef"));
......
package hudson.scm;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import hudson.Extension;
import hudson.MarkupText;
......@@ -30,7 +31,7 @@ public class ChangeLogSetTest {
notCaught = true;
}
assertEquals((new EntryImpl()).getMsg(), change.getMsg());
assertEquals(false, notCaught);
assertFalse(notCaught);
}
@Extension
......
......@@ -101,7 +101,7 @@ public class HudsonPrivateSecurityRealmTest {
String secure = PASSWORD_ENCODER.encodePassword("hello world", null);
assertTrue(PASSWORD_ENCODER.isPasswordValid(old,"hello world",null));
assertFalse(secure.equals(old));
assertNotEquals(secure, old);
}
......@@ -284,14 +284,14 @@ public class HudsonPrivateSecurityRealmTest {
assertTrue(spySecurityListener.loggedInUsernames.isEmpty());
createFirstAccount("admin");
assertTrue(spySecurityListener.loggedInUsernames.get(0).equals("admin"));
assertEquals("admin", spySecurityListener.loggedInUsernames.get(0));
createAccountByAdmin("alice");
// no new event in such case
assertTrue(spySecurityListener.loggedInUsernames.isEmpty());
assertEquals(true, spySecurityListener.loggedInUsernames.isEmpty());
selfRegistration("bob");
assertTrue(spySecurityListener.loggedInUsernames.get(0).equals("bob"));
assertEquals("bob", spySecurityListener.loggedInUsernames.get(0));
}
@Issue("JENKINS-55307")
......@@ -306,8 +306,8 @@ public class HudsonPrivateSecurityRealmTest {
selfRegistration("bob");
selfRegistration("charlie");
assertTrue(spySecurityListener.createdUsers.get(0).equals("bob"));
assertTrue(spySecurityListener.createdUsers.get(1).equals("charlie"));
assertEquals("bob", spySecurityListener.createdUsers.get(0));
assertEquals("charlie", spySecurityListener.createdUsers.get(1));
}
@Issue("JENKINS-55307")
......@@ -327,8 +327,8 @@ public class HudsonPrivateSecurityRealmTest {
u2.setFullName("Debbie User");
u2.save();
assertTrue(spySecurityListener.createdUsers.get(0).equals("alice"));
assertTrue(spySecurityListener.createdUsers.get(1).equals("debbie"));
assertEquals("alice", spySecurityListener.createdUsers.get(0));
assertEquals("debbie", spySecurityListener.createdUsers.get(1));
}
@Issue("JENKINS-55307")
......@@ -342,7 +342,7 @@ public class HudsonPrivateSecurityRealmTest {
securityRealm.createAccountWithHashedPassword("charlie_hashed", "#jbcrypt:" + BCrypt.hashpw("charliePassword", BCrypt.gensalt()));
assertTrue(spySecurityListener.createdUsers.get(0).equals("charlie_hashed"));
assertEquals("charlie_hashed", spySecurityListener.createdUsers.get(0));
}
private void createFirstAccount(String login) throws Exception {
......
......@@ -106,7 +106,6 @@ public class SecurityRealmTest {
calendar.add(Calendar.DAY_OF_YEAR, 1);
Date tomorrow = calendar.getTime();
Collections.nCopies(8, 1)
.stream()
.forEach(i -> addSessionCookie(manager, LOCALHOST, "/jenkins", tomorrow));
addSessionCookie(manager, LOCALHOST, WILL_NOT_BE_SENT, tomorrow);
......
......@@ -135,7 +135,7 @@ public class TokenBasedRememberMeServices2Test {
wc.executeOnServer(() -> {
Authentication a = Jenkins.getAuthentication();
assertEquals("bob", a.getName());
assertEquals(ImmutableList.of("authenticated", "myteam"), Arrays.asList(a.getAuthorities()).stream().map(GrantedAuthority::getAuthority).collect(Collectors.toList()));
assertEquals(ImmutableList.of("authenticated", "myteam"), Arrays.stream(a.getAuthorities()).map(GrantedAuthority::getAuthority).collect(Collectors.toList()));
return null;
});
}
......
......@@ -262,8 +262,8 @@ public class FingerprinterTest {
fingerprints = action.getFingerprints().values();
for (Fingerprint f: fingerprints) {
assertTrue(f.getOriginal().is(upstream));
assertTrue(f.getOriginal().getName().equals(renamedProject1));
assertFalse(f.getOriginal().getName().equals(oldUpstreamName));
assertEquals(f.getOriginal().getName(), renamedProject1);
assertNotEquals(f.getOriginal().getName(), oldUpstreamName);
}
// Verify that usage entry in fingerprint record is changed after
......
......@@ -360,7 +360,7 @@ public class MavenTest {
MavenInstallation maven = ToolInstallations.configureMaven3();
MavenInstallation maven2 = ToolInstallations.configureMaven3();
assertEquals(maven.hashCode(), maven2.hashCode());
assertTrue(maven.equals(maven2));
assertEquals(maven, maven2);
}
@Issue("JENKINS-34138")
......@@ -368,6 +368,6 @@ public class MavenTest {
MavenInstallation maven3 = ToolInstallations.configureMaven3();
MavenInstallation maven2 = ToolInstallations.configureDefaultMaven();
assertNotEquals(maven3.hashCode(), maven2.hashCode());
assertFalse(maven3.equals(maven2));
assertNotEquals(maven3, maven2);
}
}
......@@ -35,6 +35,8 @@ import java.util.TreeMap;
import java.util.concurrent.Callable;
import static jenkins.model.ModelObjectWithContextMenu.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.Issue;
......@@ -64,7 +66,7 @@ public class ContextMenuTest {
f.visible = false;
menu = j.executeOnServer(doContextMenu);
parsed = parse(menu);
assertEquals(parsed.toString(), null, parsed.get("testing"));
assertNull(parsed.toString(), parsed.get("testing"));
}
@TestExtension public static class ActionFactory extends TransientProjectActionFactory {
......
......@@ -25,7 +25,6 @@ import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import java.util.stream.Stream;
import java.util.stream.Collectors;
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertEquals;
......@@ -287,9 +286,8 @@ public class JenkinsBuildsAndWorkspacesDirectoriesTest {
private boolean logWasFoundAtLevel(String searched, Level level) {
return loggerRule.getRecords().stream()
.filter(record -> record.getMessage().contains(searched))
.filter(record -> record.getLevel().equals(level))
.collect(Collectors.toList()).size() > 0;
.filter(record -> record.getMessage().contains(searched))
.filter(record -> record.getLevel().equals(level)).count() > 0;
}
@Test
......
......@@ -383,7 +383,7 @@ public class JenkinsTest {
}
public HttpResponse doDynamic() {
assertTrue(Jenkins.get().getAuthentication().getName().equals("anonymous"));
assertEquals("anonymous", Jenkins.get().getAuthentication().getName());
count++;
return HttpResponses.html("OK");
}
......@@ -411,7 +411,7 @@ public class JenkinsTest {
j.jenkins.setAuthorizationStrategy(auth);
// no anonymous read access
assertTrue(!Jenkins.get().hasPermission(Jenkins.ANONYMOUS, Jenkins.READ));
assertFalse(Jenkins.get().hasPermission(Jenkins.ANONYMOUS, Jenkins.READ));
WebClient wc = j.createWebClient()
.withThrowExceptionOnFailingStatusCode(false);
......
package jenkins.model;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertEquals;
import hudson.model.Node.Mode;
......@@ -17,8 +17,8 @@ public class MasterBuildConfigurationTest {
@Issue("JENKINS-23966")
public void retainMasterLabelWhenNoSlaveDefined() throws Exception {
Jenkins jenkins = j.getInstance();
assertTrue("Test is for master with no slave", jenkins.getComputers().length == 1);
assertEquals("Test is for master with no slave", 1, jenkins.getComputers().length);
// set our own label & mode
final String myTestLabel = "TestLabelx0123";
......@@ -29,7 +29,7 @@ public class MasterBuildConfigurationTest {
j.configRoundtrip();
// make sure settings were not lost
assertTrue("Master's label is lost", myTestLabel.equals(jenkins.getLabelString()));
assertTrue("Master's mode is lost", Mode.EXCLUSIVE.equals(jenkins.getMode()));
assertEquals("Master's label is lost", myTestLabel, jenkins.getLabelString());
assertEquals("Master's mode is lost", Mode.EXCLUSIVE, jenkins.getMode());
}
}
......@@ -48,8 +48,8 @@ public class LazyBuildMixInTest {
FreeStyleBuild b3 = r.buildAndAssertSuccess(p);
assertEquals(b2, b1.getNextBuild());
assertEquals(b3, b2.getNextBuild());
assertEquals(null, b3.getNextBuild());
assertEquals(null, b1.getPreviousBuild());
assertNull(b3.getNextBuild());
assertNull(b1.getPreviousBuild());
assertEquals(b1, b2.getPreviousBuild());
assertEquals(b2, b3.getPreviousBuild());
b1.getRunMixIn().createReference().clear();
......@@ -70,8 +70,8 @@ public class LazyBuildMixInTest {
FreeStyleBuild b3 = r.buildAndAssertSuccess(p);
assertEquals(b2, b1.getNextBuild());
assertEquals(b3, b2.getNextBuild());
assertEquals(null, b3.getNextBuild());
assertEquals(null, b1.getPreviousBuild());
assertNull(b3.getNextBuild());
assertNull(b1.getPreviousBuild());
assertEquals(b1, b2.getPreviousBuild());
assertEquals(b2, b3.getPreviousBuild());
b2.delete();
......
......@@ -63,11 +63,8 @@ public class FilePathSecureTest {
dir.mkdirs();
dir.child("stuff").write("hello", null);
FilePath tar = root.child("dir.tar");
OutputStream os = tar.write();
try {
try (OutputStream os = tar.write()) {
dir.tar(os, new DirScanner.Full());
} finally {
os.close();
}
tar.untar(remote, FilePath.TarCompression.NONE);
assertEquals("hello", remote.child("dir/stuff").readToString());
......@@ -88,11 +85,8 @@ public class FilePathSecureTest {
dir.mkdirs();
dir.child("stuff").write("hello", null);
FilePath tar = root.child("dir.tar");
OutputStream os = tar.write();
try {
try (OutputStream os = tar.write()) {
dir.tar(os, new DirScanner.Full());
} finally {
os.close();
}
tar.untar(root, FilePath.TarCompression.NONE);
assertEquals("hello", remote.child("dir/stuff").readToString());
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册