Rename ExceptionUtils.intercept to expectedException.

+review RM-10

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/modules/recordsmanagement/HEAD@101338 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Tom Page
2015-04-08 10:32:40 +00:00
parent a932b34621
commit d7ab9b5480
3 changed files with 17 additions and 17 deletions

View File

@@ -81,13 +81,13 @@ public class ExceptionUtils
* <li>
* Calling a local method which throws a {@code RuntimeException}. (An expression lambda)
* <pre>
* intercept(RuntimeException.class, () -> badMethod() );
* expectedException(RuntimeException.class, () -> badMethod() );
* </pre>
* </li>
* <li>
* Executing a block of code. (Requires return statement)
* <pre>
* intercept(RuntimeException.class, () -> {
* expectedException(RuntimeException.class, () -> {
* for (int i = 0; i < 10; i++) {
* goodMethod();
* }
@@ -99,7 +99,7 @@ public class ExceptionUtils
* <li>
* Examining the expected exception e.g. to assert the root cause is correct.
* <pre>
* UnsupportedOperationException e = intercept(UnsupportedOperationException.class, () -> badMethod2() );
* UnsupportedOperationException e = expectedException(UnsupportedOperationException.class, () -> badMethod2() );
* assertEquals(RuntimeException.class, e.getCause().getClass());
* </pre>
* </li>
@@ -107,8 +107,8 @@ public class ExceptionUtils
* Note that if your lambda expression returns 'void' then you cannot use an expression
* and must explicitly return null from a lambda block.
* <pre>
* intercept(Exception.class, () -> { methodReturningVoid(); return null; } );
* intercept(Exception.class, () -> { methodReturningVoid("parameter"); return null; } );
* expectedException(Exception.class, () -> { methodReturningVoid(); return null; } );
* expectedException(Exception.class, () -> { methodReturningVoid("parameter"); return null; } );
* </pre>
* </li>
* </ul>
@@ -121,7 +121,7 @@ public class ExceptionUtils
* @throws UnexpectedThrowableException if a non-matching throwable was thrown out of the code block.
* @throws MissingThrowableException if the expected throwable was not thrown out of the code block.
*/
public static <R, T extends Throwable> T intercept(final Class<T> expected, final Supplier<R> code)
public static <R, T extends Throwable> T expectedException(final Class<T> expected, final Supplier<R> code)
{
// The code block may throw an exception or it may not.
Optional<Throwable> maybeThrownByCode;

View File

@@ -24,7 +24,7 @@ import org.junit.Test;
import java.io.IOException;
import static org.alfresco.module.org_alfresco_module_rm.test.util.ExceptionUtils.intercept;
import static org.alfresco.module.org_alfresco_module_rm.test.util.ExceptionUtils.expectedException;
import static org.junit.Assert.*;
/**
@@ -44,10 +44,10 @@ public class ExceptionUtilsUsageExamplesTest
@Test public void swallowExpectedExceptions()
{
// Calling a local method. (An expression lambda)
intercept(RuntimeException.class, () -> badMethod1() );
expectedException(RuntimeException.class, () -> badMethod1() );
// Executing a block of code. (Requires return statement)
intercept(RuntimeException.class, () ->
expectedException(RuntimeException.class, () ->
{
for (int i = 0; i < 10; i++) {
goodMethod();
@@ -60,14 +60,14 @@ public class ExceptionUtilsUsageExamplesTest
@Test public void examineTheExpectedException()
{
UnsupportedOperationException e = intercept(UnsupportedOperationException.class, () -> badMethod2() );
UnsupportedOperationException e = expectedException(UnsupportedOperationException.class, () -> badMethod2() );
assertEquals(RuntimeException.class, e.getCause().getClass());
}
@Test(expected=MissingThrowableException.class)
public void expectedExceptionNotThrown()
{
intercept(IOException.class, () ->
expectedException(IOException.class, () ->
{
// Do nothing
return null;
@@ -77,7 +77,7 @@ public class ExceptionUtilsUsageExamplesTest
@Test(expected=UnexpectedThrowableException.class)
public void unexpectedExceptionThrown()
{
intercept(IOException.class, () ->
expectedException(IOException.class, () ->
{
throw new UnsupportedOperationException();
});
@@ -90,12 +90,12 @@ public class ExceptionUtilsUsageExamplesTest
// If you use lambdas that return void, then they cannot be lambda expressions. They must be blocks.
@Test public void usingVoidLambdas()
{
intercept(IllegalStateException.class, () -> {
expectedException(IllegalStateException.class, () -> {
onlySideEffectsHere();
return null;
});
intercept(IllegalStateException.class, () -> {
expectedException(IllegalStateException.class, () -> {
onlySideEffectsHere("hello");
return null;
});

View File

@@ -33,19 +33,19 @@ public class RMParameterCheckTest
public void checkNotBlank()
{
// Check that supplying null causes an exception.
ExceptionUtils.intercept(IllegalArgumentException.class, () -> {
ExceptionUtils.expectedException(IllegalArgumentException.class, () -> {
RMParameterCheck.checkNotBlank("name", null);
return null;
});
// Check that supplying an empty string causes an exception.
ExceptionUtils.intercept(IllegalArgumentException.class, () -> {
ExceptionUtils.expectedException(IllegalArgumentException.class, () -> {
RMParameterCheck.checkNotBlank("name", "");
return null;
});
// Check that supplying a whitespace only string causes an exception.
ExceptionUtils.intercept(IllegalArgumentException.class, () -> {
ExceptionUtils.expectedException(IllegalArgumentException.class, () -> {
RMParameterCheck.checkNotBlank("name", "\n\r \t");
return null;
});