Skip to content

Commit

Permalink
Refactor CLI tests
Browse files Browse the repository at this point in the history
  • Loading branch information
olivergondza committed Jan 29, 2014
1 parent de132fb commit 3aa812f
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 57 deletions.
12 changes: 3 additions & 9 deletions test/src/test/java/hudson/cli/CopyJobCommandTest.java
Expand Up @@ -35,9 +35,7 @@

import static org.hamcrest.MatcherAssert.assertThat;

import static hudson.cli.CLICommandInvoker.Matcher.hasNoStandardOutput;
import static hudson.cli.CLICommandInvoker.Matcher.hasNoErrorOutput;
import static hudson.cli.CLICommandInvoker.Matcher.succeeded;
import static hudson.cli.CLICommandInvoker.Matcher.succeededSilently;

@SuppressWarnings("DM_DEFAULT_ENCODING")
public class CopyJobCommandTest {
Expand All @@ -56,9 +54,7 @@ public class CopyJobCommandTest {

CLICommandInvoker.Result result = command.invokeWithArgs("dir1/p1", "dir2/p2");

assertThat(result, succeeded());
assertThat(result, hasNoStandardOutput());
assertThat(result, hasNoErrorOutput());
assertThat(result, succeededSilently());

assertNotNull(j.jenkins.getItemByFullName("dir2/p2"));
// TODO test copying from/to root, or into nonexistent folder
Expand All @@ -72,9 +68,7 @@ public class CopyJobCommandTest {

CLICommandInvoker.Result result = command.invokeWithArgs(p1.getName(), copiedProjectName);

assertThat(result, succeeded());
assertThat(result, hasNoStandardOutput());
assertThat(result, hasNoErrorOutput());
assertThat(result, succeededSilently());

FreeStyleProject p2 = (FreeStyleProject)j.jenkins.getItem(copiedProjectName);

Expand Down
25 changes: 12 additions & 13 deletions test/src/test/java/hudson/cli/CreateNodeCommandTest.java
Expand Up @@ -28,7 +28,9 @@
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.text.IsEmptyString.isEmptyString;
import static hudson.cli.CLICommandInvoker.Matcher.failedWith;
import static hudson.cli.CLICommandInvoker.Matcher.hasNoStandardOutput;
import static hudson.cli.CLICommandInvoker.Matcher.succeededSilently;
import hudson.model.Computer;
import hudson.model.Node;
import hudson.model.Slave;
Expand Down Expand Up @@ -59,8 +61,8 @@ public class CreateNodeCommandTest {
;

assertThat(result.stderr(), containsString("user is missing the Slave/Create permission"));
assertThat("No output expected", result.stdout(), isEmptyString());
assertThat("Command is expected to fail", result.returnCode(), equalTo(-1));
assertThat(result, hasNoStandardOutput());
assertThat(result, failedWith(-1));
}

@Test public void createNode() throws Exception {
Expand All @@ -71,8 +73,7 @@ public class CreateNodeCommandTest {
.invoke()
;

assertThat("No error output expected", result.stderr(), isEmptyString());
assertThat("Command is expected to succeed", result.returnCode(), equalTo(0));
assertThat(result, succeededSilently());

final Slave updatedSlave = (Slave) j.jenkins.getNode("SlaveFromXML");
assertThat(updatedSlave.getNodeName(), equalTo("SlaveFromXML"));
Expand All @@ -88,8 +89,7 @@ public class CreateNodeCommandTest {
.invokeWithArgs("CustomSlaveName")
;

assertThat("No error output expected", result.stderr(), isEmptyString());
assertThat("Command is expected to succeed", result.returnCode(), equalTo(0));
assertThat(result, succeededSilently());

assertThat("A slave with original name should not exist", j.jenkins.getNode("SlaveFromXml"), nullValue());

Expand All @@ -109,8 +109,7 @@ public class CreateNodeCommandTest {
.invokeWithArgs("CustomSlaveName")
;

assertThat("No error output expected", result.stderr(), isEmptyString());
assertThat("Command is expected to succeed", result.returnCode(), equalTo(0));
assertThat(result, succeededSilently());

assertThat("A slave with original name should be left untouched", j.jenkins.getNode("SlaveFromXml"), equalTo(originalSlave));

Expand All @@ -131,8 +130,8 @@ public class CreateNodeCommandTest {
;

assertThat(result.stderr(), containsString("Node 'SlaveFromXML' already exists"));
assertThat("No output expected", result.stdout(), isEmptyString());
assertThat("Command is expected to fail", result.returnCode(), equalTo(-1));
assertThat(result, hasNoStandardOutput());
assertThat(result, failedWith(-1));
}

@Test public void createNodeShouldFailIfNodeAlreadyExistWhenNameSpecifiedExplicitly() throws Exception {
Expand All @@ -146,7 +145,7 @@ public class CreateNodeCommandTest {
;

assertThat(result.stderr(), containsString("Node 'ExistingSlave' already exists"));
assertThat("No output expected", result.stdout(), isEmptyString());
assertThat("Command is expected to fail", result.returnCode(), equalTo(-1));
assertThat(result, hasNoStandardOutput());
assertThat(result, failedWith(-1));
}
}
9 changes: 3 additions & 6 deletions test/src/test/java/hudson/cli/CreateViewCommandTest.java
Expand Up @@ -28,6 +28,7 @@
import static hudson.cli.CLICommandInvoker.Matcher.hasNoStandardOutput;
import static hudson.cli.CLICommandInvoker.Matcher.hasNoErrorOutput;
import static hudson.cli.CLICommandInvoker.Matcher.succeeded;
import static hudson.cli.CLICommandInvoker.Matcher.succeededSilently;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
Expand Down Expand Up @@ -76,9 +77,7 @@ public class CreateViewCommandTest {
.invoke()
;

assertThat(result, succeeded());
assertThat(result, hasNoErrorOutput());
assertThat(result, hasNoStandardOutput());
assertThat(result, succeededSilently());

final View updatedView = j.jenkins.getView("ViewFromXML");
assertThat(updatedView.getViewName(), equalTo("ViewFromXML"));
Expand All @@ -94,9 +93,7 @@ public class CreateViewCommandTest {
.invokeWithArgs("CustomViewName")
;

assertThat(result, succeeded());
assertThat(result, hasNoErrorOutput());
assertThat(result, hasNoStandardOutput());
assertThat(result, succeededSilently());

assertThat("A view with original name should not exist", j.jenkins.getView("ViewFromXML"), nullValue());

Expand Down
5 changes: 2 additions & 3 deletions test/src/test/java/hudson/cli/DeleteViewCommandTest.java
Expand Up @@ -31,6 +31,7 @@
import static hudson.cli.CLICommandInvoker.Matcher.hasNoStandardOutput;
import static hudson.cli.CLICommandInvoker.Matcher.hasNoErrorOutput;
import static hudson.cli.CLICommandInvoker.Matcher.succeeded;
import static hudson.cli.CLICommandInvoker.Matcher.succeededSilently;
import static hudson.cli.CLICommandInvoker.Matcher.failedWith;

import java.io.IOException;
Expand Down Expand Up @@ -78,9 +79,7 @@ public class DeleteViewCommandTest {
.invokeWithArgs("aView")
;

assertThat(result, succeeded());
assertThat(result, hasNoStandardOutput());
assertThat(result, hasNoErrorOutput());
assertThat(result, succeededSilently());
assertThat(j.jenkins.getView("aView"), nullValue());
}

Expand Down
16 changes: 10 additions & 6 deletions test/src/test/java/hudson/cli/GetNodeCommandTest.java
Expand Up @@ -24,6 +24,10 @@

package hudson.cli;

import static hudson.cli.CLICommandInvoker.Matcher.failedWith;
import static hudson.cli.CLICommandInvoker.Matcher.hasNoStandardOutput;
import static hudson.cli.CLICommandInvoker.Matcher.hasNoErrorOutput;
import static hudson.cli.CLICommandInvoker.Matcher.succeeded;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
Expand Down Expand Up @@ -58,8 +62,8 @@ public class GetNodeCommandTest {
;

assertThat(result.stderr(), containsString("user is missing the Slave/ExtendedRead permission"));
assertThat("No output expected", result.stdout(), isEmptyString());
assertThat("Command is expected to fail", result.returnCode(), equalTo(-1));
assertThat(result, failedWith(-1));
assertThat(result, hasNoStandardOutput());
}

@Test public void getNodeShouldYieldConfigXml() throws Exception {
Expand All @@ -73,8 +77,8 @@ public class GetNodeCommandTest {

assertThat(result.stdout(), startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
assertThat(result.stdout(), containsString("<name>MySlave</name>"));
assertThat("No error output expected", result.stderr(), isEmptyString());
assertThat("Command is expected to succeed", result.returnCode(), equalTo(0));
assertThat(result, hasNoErrorOutput());
assertThat(result, succeeded());
}

@Test public void getNodeShouldFailIfNodeDoesNotExist() throws Exception {
Expand All @@ -85,7 +89,7 @@ public class GetNodeCommandTest {
;

assertThat(result.stderr(), containsString("No such node 'MySlave'"));
assertThat("No output expected", result.stdout(), isEmptyString());
assertThat("Command is expected to fail", result.returnCode(), equalTo(-1));
assertThat(result, failedWith(-1));
assertThat(result, hasNoStandardOutput());
}
}
10 changes: 5 additions & 5 deletions test/src/test/java/hudson/cli/HelpCommandTest.java
Expand Up @@ -24,9 +24,9 @@

package hudson.cli;

import static hudson.cli.CLICommandInvoker.Matcher.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.text.IsEmptyString.isEmptyString;
import static org.hamcrest.text.StringContainsInOrder.stringContainsInOrder;

import java.io.PrintStream;
Expand Down Expand Up @@ -58,15 +58,15 @@ public class HelpCommandTest {
assertContainsOverviewOfMethodCommand(generalHelp);

Result result = command.invokeWithArgs(ClassCommand.NAME);
assertThat(result.returnCode(), equalTo(0));
assertThat(result.stdout(), isEmptyString());
assertThat(result, succeeded());
assertThat(result, hasNoStandardOutput());

assertContainsUsageOfClassCommand(result.stderr());

result = command.invokeWithArgs("offline-node");

assertThat(result.returnCode(), equalTo(0));
assertThat(result.stdout(), isEmptyString());
assertThat(result, succeeded());
assertThat(result, hasNoStandardOutput());

assertContainsUsageOfMethodCommand(result.stderr());
}
Expand Down
Expand Up @@ -24,10 +24,12 @@

package hudson.cli;

import static hudson.cli.CLICommandInvoker.Matcher.failedWith;
import static hudson.cli.CLICommandInvoker.Matcher.hasNoStandardOutput;
import static hudson.cli.CLICommandInvoker.Matcher.succeededSilently;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.text.IsEmptyString.isEmptyString;
import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;

Expand Down Expand Up @@ -56,8 +58,8 @@ public class SetBuildDisplayNameCommandTest {
;

assertThat(result.stderr(), containsString("Build #42 does not exist"));
assertThat("No output expected", result.stdout(), isEmptyString());
assertThat("Command is expected to fail", result.returnCode(), equalTo(-1));
assertThat(result, hasNoStandardOutput());
assertThat(result, failedWith(-1));
}

@Test public void setDescriptionSuccesfully() throws Exception {
Expand All @@ -69,9 +71,7 @@ public class SetBuildDisplayNameCommandTest {
.invokeWithArgs("project", "1", "DisplayName")
;

assertThat("No output expected", result.stdout(), isEmptyString());
assertThat("No error output expected", result.stderr(), isEmptyString());
assertThat("Command is expected to succeed", result.returnCode(), equalTo(0));
assertThat(result, succeededSilently());
assertThat(build.getDisplayName(), equalTo("DisplayName"));
}
}
15 changes: 8 additions & 7 deletions test/src/test/java/hudson/cli/UpdateNodeCommandTest.java
Expand Up @@ -28,7 +28,9 @@
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.text.IsEmptyString.isEmptyString;
import static hudson.cli.CLICommandInvoker.Matcher.failedWith;
import static hudson.cli.CLICommandInvoker.Matcher.hasNoStandardOutput;
import static hudson.cli.CLICommandInvoker.Matcher.succeededSilently;
import hudson.model.Computer;
import hudson.model.Node;
import jenkins.model.Jenkins;
Expand Down Expand Up @@ -59,8 +61,8 @@ public class UpdateNodeCommandTest {
;

assertThat(result.stderr(), containsString("user is missing the Slave/Configure permission"));
assertThat("No output expected", result.stdout(), isEmptyString());
assertThat("Command is expected to fail", result.returnCode(), equalTo(-1));
assertThat(result, failedWith(-1));
assertThat(result, hasNoStandardOutput());
}

@Test public void updateNodeShouldModifyNodeConfiguration() throws Exception {
Expand All @@ -73,8 +75,7 @@ public class UpdateNodeCommandTest {
.invokeWithArgs("MySlave")
;

assertThat("No error output expected", result.stderr(), isEmptyString());
assertThat("Command is expected to succeed", result.returnCode(), equalTo(0));
assertThat(result, succeededSilently());

assertThat("A slave with old name should not exist", j.jenkins.getNode("MySlave"), nullValue());

Expand All @@ -92,7 +93,7 @@ public class UpdateNodeCommandTest {
;

assertThat(result.stderr(), containsString("No such node 'MySlave'"));
assertThat("No output expected", result.stdout(), isEmptyString());
assertThat("Command is expected to fail", result.returnCode(), equalTo(-1));
assertThat(result, failedWith(-1));
assertThat(result, hasNoStandardOutput());
}
}
4 changes: 2 additions & 2 deletions test/src/test/java/hudson/cli/UpdateViewCommandTest.java
Expand Up @@ -28,6 +28,7 @@
import static hudson.cli.CLICommandInvoker.Matcher.hasNoStandardOutput;
import static hudson.cli.CLICommandInvoker.Matcher.hasNoErrorOutput;
import static hudson.cli.CLICommandInvoker.Matcher.succeeded;
import static hudson.cli.CLICommandInvoker.Matcher.succeededSilently;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
Expand Down Expand Up @@ -77,8 +78,7 @@ public class UpdateViewCommandTest {
.invokeWithArgs("aView")
;

assertThat(result, succeeded());
assertThat(result, hasNoErrorOutput());
assertThat(result, succeededSilently());

assertThat("Update should not modify view name", j.jenkins.getView("ViewFromXML"), nullValue());

Expand Down

0 comments on commit 3aa812f

Please sign in to comment.