Refactor acceptance tests to use ExpectedException

Move the declaration of ExpectedException out of AccountIT up to the
AbstractDaemonTest class, so it can be reused in all the derived test
classes.

Refactor the acceptance tests to use:

 exception.expect(Exception.class);

rather than surrounding the code under test in a try-catch-block and
explicitly calling fail() if the exception is not raised.

and:

 exception.expectMessage("Expected message");

rather than catching the exception and then checking the message with:

 assertThat(exception.getMessage()).isEqualTo("Expected message");

Change-Id: I6bbd476265898b19d1336385bc428ec488a5e47e
This commit is contained in:
David Pursehouse
2015-05-28 16:42:18 +09:00
parent 321ec7c07e
commit f5460957dc
9 changed files with 63 additions and 122 deletions

View File

@@ -23,7 +23,6 @@ import static org.apache.http.HttpStatus.SC_CONFLICT;
import static org.apache.http.HttpStatus.SC_NOT_FOUND;
import static org.apache.http.HttpStatus.SC_NO_CONTENT;
import static org.apache.http.HttpStatus.SC_OK;
import static org.junit.Assert.fail;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableMap;
@@ -288,20 +287,24 @@ public class ChangeEditIT extends AbstractDaemonTest {
}
@Test
public void updateMessage() throws Exception {
public void updateMessageNoChange() throws Exception {
assertThat(modifier.createEdit(change, getCurrentPatchSet(changeId)))
.isEqualTo(RefUpdate.Result.NEW);
Optional<ChangeEdit> edit = editUtil.byChange(change);
try {
modifier.modifyMessage(
edit.get(),
edit.get().getEditCommit().getFullMessage());
fail("UnchangedCommitMessageException expected");
} catch (UnchangedCommitMessageException ex) {
assertThat(ex.getMessage()).isEqualTo(
"New commit message cannot be same as existing commit message");
}
exception.expect(UnchangedCommitMessageException.class);
exception.expectMessage(
"New commit message cannot be same as existing commit message");
modifier.modifyMessage(
edit.get(),
edit.get().getEditCommit().getFullMessage());
}
@Test
public void updateMessage() throws Exception {
assertThat(modifier.createEdit(change, getCurrentPatchSet(changeId)))
.isEqualTo(RefUpdate.Result.NEW);
Optional<ChangeEdit> edit = editUtil.byChange(change);
String msg = String.format("New commit message\n\nChange-Id: %s",
change.getKey());
@@ -385,12 +388,9 @@ public class ChangeEditIT extends AbstractDaemonTest {
assertThat(modifier.deleteFile(edit.get(), FILE_NAME)).isEqualTo(
RefUpdate.Result.FORCED);
edit = editUtil.byChange(change);
try {
fileUtil.getContent(projectCache.get(edit.get().getChange().getProject()),
ObjectId.fromString(edit.get().getRevision().get()), FILE_NAME);
fail("ResourceNotFoundException expected");
} catch (ResourceNotFoundException rnfe) {
}
exception.expect(ResourceNotFoundException.class);
fileUtil.getContent(projectCache.get(edit.get().getChange().getProject()),
ObjectId.fromString(edit.get().getRevision().get()), FILE_NAME);
}
@Test
@@ -402,12 +402,9 @@ public class ChangeEditIT extends AbstractDaemonTest {
edit = editUtil.byChange(change);
assertByteArray(fileUtil.getContent(projectCache.get(edit.get().getChange().getProject()),
ObjectId.fromString(edit.get().getRevision().get()), FILE_NAME3), CONTENT_OLD);
try {
fileUtil.getContent(projectCache.get(edit.get().getChange().getProject()),
ObjectId.fromString(edit.get().getRevision().get()), FILE_NAME);
fail("ResourceNotFoundException expected");
} catch (ResourceNotFoundException rnfe) {
}
exception.expect(ResourceNotFoundException.class);
fileUtil.getContent(projectCache.get(edit.get().getChange().getProject()),
ObjectId.fromString(edit.get().getRevision().get()), FILE_NAME);
}
@Test
@@ -415,12 +412,9 @@ public class ChangeEditIT extends AbstractDaemonTest {
RestResponse r = adminSession.delete(urlEditFile());
assertThat(r.getStatusCode()).isEqualTo(SC_NO_CONTENT);
Optional<ChangeEdit> edit = editUtil.byChange(change);
try {
fileUtil.getContent(projectCache.get(edit.get().getChange().getProject()),
ObjectId.fromString(edit.get().getRevision().get()), FILE_NAME);
fail("ResourceNotFoundException expected");
} catch (ResourceNotFoundException rnfe) {
}
exception.expect(ResourceNotFoundException.class);
fileUtil.getContent(projectCache.get(edit.get().getChange().getProject()),
ObjectId.fromString(edit.get().getRevision().get()), FILE_NAME);
}
@Test
@@ -435,12 +429,9 @@ public class ChangeEditIT extends AbstractDaemonTest {
assertThat(adminSession.delete(urlEditFile()).getStatusCode()).isEqualTo(
SC_NO_CONTENT);
Optional<ChangeEdit> edit = editUtil.byChange(change);
try {
fileUtil.getContent(projectCache.get(edit.get().getChange().getProject()),
ObjectId.fromString(edit.get().getRevision().get()), FILE_NAME);
fail("ResourceNotFoundException expected");
} catch (ResourceNotFoundException rnfe) {
}
exception.expect(ResourceNotFoundException.class);
fileUtil.getContent(projectCache.get(edit.get().getChange().getProject()),
ObjectId.fromString(edit.get().getRevision().get()), FILE_NAME);
}
@Test
@@ -490,12 +481,9 @@ public class ChangeEditIT extends AbstractDaemonTest {
Optional<ChangeEdit> edit = editUtil.byChange(change);
assertByteArray(fileUtil.getContent(projectCache.get(edit.get().getChange().getProject()),
ObjectId.fromString(edit.get().getRevision().get()), FILE_NAME3), CONTENT_OLD);
try {
fileUtil.getContent(projectCache.get(edit.get().getChange().getProject()),
ObjectId.fromString(edit.get().getRevision().get()), FILE_NAME);
fail("ResourceNotFoundException expected");
} catch (ResourceNotFoundException rnfe) {
}
exception.expect(ResourceNotFoundException.class);
fileUtil.getContent(projectCache.get(edit.get().getChange().getProject()),
ObjectId.fromString(edit.get().getRevision().get()), FILE_NAME);
}
@Test
@@ -595,14 +583,11 @@ public class ChangeEditIT extends AbstractDaemonTest {
assertThat(adminSession.delete(urlEditFile()).getStatusCode()).isEqualTo(
SC_NO_CONTENT);
Optional<ChangeEdit> edit = editUtil.byChange(change);
try {
fileUtil.getContent(projectCache.get(edit.get().getChange().getProject()),
ObjectId.fromString(edit.get().getRevision().get()), FILE_NAME);
fail("ResourceNotFoundException expected");
} catch (ResourceNotFoundException rnfe) {
}
RestResponse r = adminSession.get(urlEditFile());
assertThat(r.getStatusCode()).isEqualTo(SC_NO_CONTENT);
exception.expect(ResourceNotFoundException.class);
fileUtil.getContent(projectCache.get(edit.get().getChange().getProject()),
ObjectId.fromString(edit.get().getRevision().get()), FILE_NAME);
}
@Test
@@ -635,15 +620,12 @@ public class ChangeEditIT extends AbstractDaemonTest {
@Test
public void writeNoChanges() throws Exception {
assertThat(modifier.createEdit(change, ps)).isEqualTo(RefUpdate.Result.NEW);
try {
modifier.modifyFile(
editUtil.byChange(change).get(),
FILE_NAME,
RestSession.newRawInput(CONTENT_OLD));
fail();
} catch (InvalidChangeOperationException e) {
assertThat(e.getMessage()).isEqualTo("no changes were made");
}
exception.expect(InvalidChangeOperationException.class);
exception.expectMessage("no changes were made");
modifier.modifyFile(
editUtil.byChange(change).get(),
FILE_NAME,
RestSession.newRawInput(CONTENT_OLD));
}
@Test