mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-08-07 17:49:17 +00:00
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:
@@ -81,13 +81,13 @@ public class ExceptionUtils
|
|||||||
* <li>
|
* <li>
|
||||||
* Calling a local method which throws a {@code RuntimeException}. (An expression lambda)
|
* Calling a local method which throws a {@code RuntimeException}. (An expression lambda)
|
||||||
* <pre>
|
* <pre>
|
||||||
* intercept(RuntimeException.class, () -> badMethod() );
|
* expectedException(RuntimeException.class, () -> badMethod() );
|
||||||
* </pre>
|
* </pre>
|
||||||
* </li>
|
* </li>
|
||||||
* <li>
|
* <li>
|
||||||
* Executing a block of code. (Requires return statement)
|
* Executing a block of code. (Requires return statement)
|
||||||
* <pre>
|
* <pre>
|
||||||
* intercept(RuntimeException.class, () -> {
|
* expectedException(RuntimeException.class, () -> {
|
||||||
* for (int i = 0; i < 10; i++) {
|
* for (int i = 0; i < 10; i++) {
|
||||||
* goodMethod();
|
* goodMethod();
|
||||||
* }
|
* }
|
||||||
@@ -99,7 +99,7 @@ public class ExceptionUtils
|
|||||||
* <li>
|
* <li>
|
||||||
* Examining the expected exception e.g. to assert the root cause is correct.
|
* Examining the expected exception e.g. to assert the root cause is correct.
|
||||||
* <pre>
|
* <pre>
|
||||||
* UnsupportedOperationException e = intercept(UnsupportedOperationException.class, () -> badMethod2() );
|
* UnsupportedOperationException e = expectedException(UnsupportedOperationException.class, () -> badMethod2() );
|
||||||
* assertEquals(RuntimeException.class, e.getCause().getClass());
|
* assertEquals(RuntimeException.class, e.getCause().getClass());
|
||||||
* </pre>
|
* </pre>
|
||||||
* </li>
|
* </li>
|
||||||
@@ -107,8 +107,8 @@ public class ExceptionUtils
|
|||||||
* Note that if your lambda expression returns 'void' then you cannot use an expression
|
* Note that if your lambda expression returns 'void' then you cannot use an expression
|
||||||
* and must explicitly return null from a lambda block.
|
* and must explicitly return null from a lambda block.
|
||||||
* <pre>
|
* <pre>
|
||||||
* intercept(Exception.class, () -> { methodReturningVoid(); return null; } );
|
* expectedException(Exception.class, () -> { methodReturningVoid(); return null; } );
|
||||||
* intercept(Exception.class, () -> { methodReturningVoid("parameter"); return null; } );
|
* expectedException(Exception.class, () -> { methodReturningVoid("parameter"); return null; } );
|
||||||
* </pre>
|
* </pre>
|
||||||
* </li>
|
* </li>
|
||||||
* </ul>
|
* </ul>
|
||||||
@@ -121,7 +121,7 @@ public class ExceptionUtils
|
|||||||
* @throws UnexpectedThrowableException if a non-matching throwable was thrown out of the code block.
|
* @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.
|
* @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.
|
// The code block may throw an exception or it may not.
|
||||||
Optional<Throwable> maybeThrownByCode;
|
Optional<Throwable> maybeThrownByCode;
|
||||||
|
@@ -24,7 +24,7 @@ import org.junit.Test;
|
|||||||
|
|
||||||
import java.io.IOException;
|
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.*;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -44,10 +44,10 @@ public class ExceptionUtilsUsageExamplesTest
|
|||||||
@Test public void swallowExpectedExceptions()
|
@Test public void swallowExpectedExceptions()
|
||||||
{
|
{
|
||||||
// Calling a local method. (An expression lambda)
|
// Calling a local method. (An expression lambda)
|
||||||
intercept(RuntimeException.class, () -> badMethod1() );
|
expectedException(RuntimeException.class, () -> badMethod1() );
|
||||||
|
|
||||||
// Executing a block of code. (Requires return statement)
|
// Executing a block of code. (Requires return statement)
|
||||||
intercept(RuntimeException.class, () ->
|
expectedException(RuntimeException.class, () ->
|
||||||
{
|
{
|
||||||
for (int i = 0; i < 10; i++) {
|
for (int i = 0; i < 10; i++) {
|
||||||
goodMethod();
|
goodMethod();
|
||||||
@@ -60,14 +60,14 @@ public class ExceptionUtilsUsageExamplesTest
|
|||||||
|
|
||||||
@Test public void examineTheExpectedException()
|
@Test public void examineTheExpectedException()
|
||||||
{
|
{
|
||||||
UnsupportedOperationException e = intercept(UnsupportedOperationException.class, () -> badMethod2() );
|
UnsupportedOperationException e = expectedException(UnsupportedOperationException.class, () -> badMethod2() );
|
||||||
assertEquals(RuntimeException.class, e.getCause().getClass());
|
assertEquals(RuntimeException.class, e.getCause().getClass());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected=MissingThrowableException.class)
|
@Test(expected=MissingThrowableException.class)
|
||||||
public void expectedExceptionNotThrown()
|
public void expectedExceptionNotThrown()
|
||||||
{
|
{
|
||||||
intercept(IOException.class, () ->
|
expectedException(IOException.class, () ->
|
||||||
{
|
{
|
||||||
// Do nothing
|
// Do nothing
|
||||||
return null;
|
return null;
|
||||||
@@ -77,7 +77,7 @@ public class ExceptionUtilsUsageExamplesTest
|
|||||||
@Test(expected=UnexpectedThrowableException.class)
|
@Test(expected=UnexpectedThrowableException.class)
|
||||||
public void unexpectedExceptionThrown()
|
public void unexpectedExceptionThrown()
|
||||||
{
|
{
|
||||||
intercept(IOException.class, () ->
|
expectedException(IOException.class, () ->
|
||||||
{
|
{
|
||||||
throw new UnsupportedOperationException();
|
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.
|
// If you use lambdas that return void, then they cannot be lambda expressions. They must be blocks.
|
||||||
@Test public void usingVoidLambdas()
|
@Test public void usingVoidLambdas()
|
||||||
{
|
{
|
||||||
intercept(IllegalStateException.class, () -> {
|
expectedException(IllegalStateException.class, () -> {
|
||||||
onlySideEffectsHere();
|
onlySideEffectsHere();
|
||||||
return null;
|
return null;
|
||||||
});
|
});
|
||||||
|
|
||||||
intercept(IllegalStateException.class, () -> {
|
expectedException(IllegalStateException.class, () -> {
|
||||||
onlySideEffectsHere("hello");
|
onlySideEffectsHere("hello");
|
||||||
return null;
|
return null;
|
||||||
});
|
});
|
||||||
|
@@ -33,19 +33,19 @@ public class RMParameterCheckTest
|
|||||||
public void checkNotBlank()
|
public void checkNotBlank()
|
||||||
{
|
{
|
||||||
// Check that supplying null causes an exception.
|
// Check that supplying null causes an exception.
|
||||||
ExceptionUtils.intercept(IllegalArgumentException.class, () -> {
|
ExceptionUtils.expectedException(IllegalArgumentException.class, () -> {
|
||||||
RMParameterCheck.checkNotBlank("name", null);
|
RMParameterCheck.checkNotBlank("name", null);
|
||||||
return null;
|
return null;
|
||||||
});
|
});
|
||||||
|
|
||||||
// Check that supplying an empty string causes an exception.
|
// Check that supplying an empty string causes an exception.
|
||||||
ExceptionUtils.intercept(IllegalArgumentException.class, () -> {
|
ExceptionUtils.expectedException(IllegalArgumentException.class, () -> {
|
||||||
RMParameterCheck.checkNotBlank("name", "");
|
RMParameterCheck.checkNotBlank("name", "");
|
||||||
return null;
|
return null;
|
||||||
});
|
});
|
||||||
|
|
||||||
// Check that supplying a whitespace only string causes an exception.
|
// Check that supplying a whitespace only string causes an exception.
|
||||||
ExceptionUtils.intercept(IllegalArgumentException.class, () -> {
|
ExceptionUtils.expectedException(IllegalArgumentException.class, () -> {
|
||||||
RMParameterCheck.checkNotBlank("name", "\n\r \t");
|
RMParameterCheck.checkNotBlank("name", "\n\r \t");
|
||||||
return null;
|
return null;
|
||||||
});
|
});
|
||||||
|
Reference in New Issue
Block a user