Apply code formatting rules to async-method-invocation example tests

This commit is contained in:
Ilkka Seppala 2015-12-06 21:42:06 +02:00
parent 601964a2d1
commit 6c1f025d0f

View File

@ -30,272 +30,287 @@ import static org.mockito.internal.verification.VerificationModeFactory.times;
*/ */
public class ThreadAsyncExecutorTest { public class ThreadAsyncExecutorTest {
/** /**
* Test used to verify the happy path of {@link ThreadAsyncExecutor#startProcess(Callable)} * Test used to verify the happy path of {@link ThreadAsyncExecutor#startProcess(Callable)}
*/ */
@Test(timeout = 3000) @Test(timeout = 3000)
public void testSuccessfulTaskWithoutCallback() throws Exception { public void testSuccessfulTaskWithoutCallback() throws Exception {
// Instantiate a new executor and start a new 'null' task ... // Instantiate a new executor and start a new 'null' task ...
final ThreadAsyncExecutor executor = new ThreadAsyncExecutor(); final ThreadAsyncExecutor executor = new ThreadAsyncExecutor();
final Object result = new Object(); final Object result = new Object();
final Callable<Object> task = mock(Callable.class); final Callable<Object> task = mock(Callable.class);
when(task.call()).thenReturn(result); when(task.call()).thenReturn(result);
final AsyncResult<Object> asyncResult = executor.startProcess(task); final AsyncResult<Object> asyncResult = executor.startProcess(task);
assertNotNull(asyncResult); assertNotNull(asyncResult);
asyncResult.await(); // Prevent timing issues, and wait until the result is available asyncResult.await(); // Prevent timing issues, and wait until the result is available
assertTrue(asyncResult.isCompleted()); assertTrue(asyncResult.isCompleted());
// Our task should only execute once ... // Our task should only execute once ...
verify(task, times(1)).call(); verify(task, times(1)).call();
// ... and the result should be exactly the same object // ... and the result should be exactly the same object
assertSame(result, asyncResult.getValue()); assertSame(result, asyncResult.getValue());
} }
/** /**
* Test used to verify the happy path of {@link ThreadAsyncExecutor#startProcess(Callable, AsyncCallback)} * Test used to verify the happy path of
*/ * {@link ThreadAsyncExecutor#startProcess(Callable, AsyncCallback)}
@Test(timeout = 3000) */
public void testSuccessfulTaskWithCallback() throws Exception { @Test(timeout = 3000)
// Instantiate a new executor and start a new 'null' task ... public void testSuccessfulTaskWithCallback() throws Exception {
final ThreadAsyncExecutor executor = new ThreadAsyncExecutor(); // Instantiate a new executor and start a new 'null' task ...
final ThreadAsyncExecutor executor = new ThreadAsyncExecutor();
final Object result = new Object(); final Object result = new Object();
final Callable<Object> task = mock(Callable.class); final Callable<Object> task = mock(Callable.class);
when(task.call()).thenReturn(result); when(task.call()).thenReturn(result);
final AsyncCallback callback = mock(AsyncCallback.class); final AsyncCallback callback = mock(AsyncCallback.class);
final AsyncResult<Object> asyncResult = executor.startProcess(task, callback); final AsyncResult<Object> asyncResult = executor.startProcess(task, callback);
assertNotNull(asyncResult); assertNotNull(asyncResult);
asyncResult.await(); // Prevent timing issues, and wait until the result is available asyncResult.await(); // Prevent timing issues, and wait until the result is available
assertTrue(asyncResult.isCompleted()); assertTrue(asyncResult.isCompleted());
// Our task should only execute once ... // Our task should only execute once ...
verify(task, times(1)).call(); verify(task, times(1)).call();
// ... same for the callback, we expect our object // ... same for the callback, we expect our object
final ArgumentCaptor<Optional<Exception>> optionalCaptor = ArgumentCaptor.forClass((Class) Optional.class); final ArgumentCaptor<Optional<Exception>> optionalCaptor = ArgumentCaptor
verify(callback, times(1)).onComplete(eq(result), optionalCaptor.capture()); .forClass((Class) Optional.class);
verify(callback, times(1)).onComplete(eq(result), optionalCaptor.capture());
final Optional<Exception> optionalException = optionalCaptor.getValue(); final Optional<Exception> optionalException = optionalCaptor.getValue();
assertNotNull(optionalException); assertNotNull(optionalException);
assertFalse(optionalException.isPresent()); assertFalse(optionalException.isPresent());
// ... and the result should be exactly the same object // ... and the result should be exactly the same object
assertSame(result, asyncResult.getValue()); assertSame(result, asyncResult.getValue());
} }
/** /**
* Test used to verify the happy path of {@link ThreadAsyncExecutor#startProcess(Callable)} * Test used to verify the happy path of {@link ThreadAsyncExecutor#startProcess(Callable)} when a
* when a task takes a while to execute * task takes a while to execute
*/ */
@Test(timeout = 5000) @Test(timeout = 5000)
public void testLongRunningTaskWithoutCallback() throws Exception { public void testLongRunningTaskWithoutCallback() throws Exception {
// Instantiate a new executor and start a new 'null' task ... // Instantiate a new executor and start a new 'null' task ...
final ThreadAsyncExecutor executor = new ThreadAsyncExecutor(); final ThreadAsyncExecutor executor = new ThreadAsyncExecutor();
final Object result = new Object(); final Object result = new Object();
final Callable<Object> task = mock(Callable.class); final Callable<Object> task = mock(Callable.class);
when(task.call()).thenAnswer(i -> { when(task.call()).thenAnswer(i -> {
Thread.sleep(1500); Thread.sleep(1500);
return result; return result;
}); });
final AsyncResult<Object> asyncResult = executor.startProcess(task); final AsyncResult<Object> asyncResult = executor.startProcess(task);
assertNotNull(asyncResult); assertNotNull(asyncResult);
assertFalse(asyncResult.isCompleted()); assertFalse(asyncResult.isCompleted());
try { try {
asyncResult.getValue(); asyncResult.getValue();
fail("Expected IllegalStateException when calling AsyncResult#getValue on a non-completed task"); fail(
} catch (IllegalStateException e) { "Expected IllegalStateException when calling AsyncResult#getValue on a non-completed task");
assertNotNull(e.getMessage()); } catch (IllegalStateException e) {
} assertNotNull(e.getMessage());
}
// Our task should only execute once, but it can take a while ... // Our task should only execute once, but it can take a while ...
verify(task, timeout(3000).times(1)).call(); verify(task, timeout(3000).times(1)).call();
// Prevent timing issues, and wait until the result is available // Prevent timing issues, and wait until the result is available
asyncResult.await(); asyncResult.await();
assertTrue(asyncResult.isCompleted()); assertTrue(asyncResult.isCompleted());
verifyNoMoreInteractions(task); verifyNoMoreInteractions(task);
// ... and the result should be exactly the same object // ... and the result should be exactly the same object
assertSame(result, asyncResult.getValue()); assertSame(result, asyncResult.getValue());
} }
/** /**
* Test used to verify the happy path of {@link ThreadAsyncExecutor#startProcess(Callable, AsyncCallback)} * Test used to verify the happy path of
* when a task takes a while to execute * {@link ThreadAsyncExecutor#startProcess(Callable, AsyncCallback)} when a task takes a while to
*/ * execute
@Test(timeout = 5000) */
public void testLongRunningTaskWithCallback() throws Exception { @Test(timeout = 5000)
// Instantiate a new executor and start a new 'null' task ... public void testLongRunningTaskWithCallback() throws Exception {
final ThreadAsyncExecutor executor = new ThreadAsyncExecutor(); // Instantiate a new executor and start a new 'null' task ...
final ThreadAsyncExecutor executor = new ThreadAsyncExecutor();
final Object result = new Object(); final Object result = new Object();
final Callable<Object> task = mock(Callable.class); final Callable<Object> task = mock(Callable.class);
when(task.call()).thenAnswer(i -> { when(task.call()).thenAnswer(i -> {
Thread.sleep(1500); Thread.sleep(1500);
return result; return result;
}); });
final AsyncCallback<Object> callback = mock(AsyncCallback.class); final AsyncCallback<Object> callback = mock(AsyncCallback.class);
final AsyncResult<Object> asyncResult = executor.startProcess(task, callback); final AsyncResult<Object> asyncResult = executor.startProcess(task, callback);
assertNotNull(asyncResult); assertNotNull(asyncResult);
assertFalse(asyncResult.isCompleted()); assertFalse(asyncResult.isCompleted());
verifyZeroInteractions(callback); verifyZeroInteractions(callback);
try { try {
asyncResult.getValue(); asyncResult.getValue();
fail("Expected IllegalStateException when calling AsyncResult#getValue on a non-completed task"); fail(
} catch (IllegalStateException e) { "Expected IllegalStateException when calling AsyncResult#getValue on a non-completed task");
assertNotNull(e.getMessage()); } catch (IllegalStateException e) {
} assertNotNull(e.getMessage());
}
// Our task should only execute once, but it can take a while ... // Our task should only execute once, but it can take a while ...
verify(task, timeout(3000).times(1)).call(); verify(task, timeout(3000).times(1)).call();
final ArgumentCaptor<Optional<Exception>> optionalCaptor = ArgumentCaptor.forClass((Class) Optional.class); final ArgumentCaptor<Optional<Exception>> optionalCaptor = ArgumentCaptor
verify(callback, timeout(3000).times(1)).onComplete(eq(result), optionalCaptor.capture()); .forClass((Class) Optional.class);
verify(callback, timeout(3000).times(1)).onComplete(eq(result), optionalCaptor.capture());
final Optional<Exception> optionalException = optionalCaptor.getValue(); final Optional<Exception> optionalException = optionalCaptor.getValue();
assertNotNull(optionalException); assertNotNull(optionalException);
assertFalse(optionalException.isPresent()); assertFalse(optionalException.isPresent());
// Prevent timing issues, and wait until the result is available // Prevent timing issues, and wait until the result is available
asyncResult.await(); asyncResult.await();
assertTrue(asyncResult.isCompleted()); assertTrue(asyncResult.isCompleted());
verifyNoMoreInteractions(task, callback); verifyNoMoreInteractions(task, callback);
// ... and the result should be exactly the same object // ... and the result should be exactly the same object
assertSame(result, asyncResult.getValue()); assertSame(result, asyncResult.getValue());
} }
/** /**
* Test used to verify the happy path of {@link ThreadAsyncExecutor#startProcess(Callable)} * Test used to verify the happy path of {@link ThreadAsyncExecutor#startProcess(Callable)} when a
* when a task takes a while to execute, while waiting on the result using {@link ThreadAsyncExecutor#endProcess(AsyncResult)} * task takes a while to execute, while waiting on the result using
*/ * {@link ThreadAsyncExecutor#endProcess(AsyncResult)}
@Test(timeout = 5000) */
public void testEndProcess() throws Exception { @Test(timeout = 5000)
// Instantiate a new executor and start a new 'null' task ... public void testEndProcess() throws Exception {
final ThreadAsyncExecutor executor = new ThreadAsyncExecutor(); // Instantiate a new executor and start a new 'null' task ...
final ThreadAsyncExecutor executor = new ThreadAsyncExecutor();
final Object result = new Object(); final Object result = new Object();
final Callable<Object> task = mock(Callable.class); final Callable<Object> task = mock(Callable.class);
when(task.call()).thenAnswer(i -> { when(task.call()).thenAnswer(i -> {
Thread.sleep(1500); Thread.sleep(1500);
return result; return result;
}); });
final AsyncResult<Object> asyncResult = executor.startProcess(task); final AsyncResult<Object> asyncResult = executor.startProcess(task);
assertNotNull(asyncResult); assertNotNull(asyncResult);
assertFalse(asyncResult.isCompleted()); assertFalse(asyncResult.isCompleted());
try { try {
asyncResult.getValue(); asyncResult.getValue();
fail("Expected IllegalStateException when calling AsyncResult#getValue on a non-completed task"); fail(
} catch (IllegalStateException e) { "Expected IllegalStateException when calling AsyncResult#getValue on a non-completed task");
assertNotNull(e.getMessage()); } catch (IllegalStateException e) {
} assertNotNull(e.getMessage());
}
assertSame(result, executor.endProcess(asyncResult)); assertSame(result, executor.endProcess(asyncResult));
verify(task, times(1)).call(); verify(task, times(1)).call();
assertTrue(asyncResult.isCompleted()); assertTrue(asyncResult.isCompleted());
// Calling end process a second time while already finished should give the same result // Calling end process a second time while already finished should give the same result
assertSame(result, executor.endProcess(asyncResult)); assertSame(result, executor.endProcess(asyncResult));
verifyNoMoreInteractions(task); verifyNoMoreInteractions(task);
} }
/** /**
* Test used to verify the behaviour of {@link ThreadAsyncExecutor#startProcess(Callable)} * Test used to verify the behaviour of {@link ThreadAsyncExecutor#startProcess(Callable)} when
* when the callable is 'null' * the callable is 'null'
*/ */
@Test(timeout = 3000) @Test(timeout = 3000)
public void testNullTask() throws Exception { public void testNullTask() throws Exception {
// Instantiate a new executor and start a new 'null' task ... // Instantiate a new executor and start a new 'null' task ...
final ThreadAsyncExecutor executor = new ThreadAsyncExecutor(); final ThreadAsyncExecutor executor = new ThreadAsyncExecutor();
final AsyncResult<Object> asyncResult = executor.startProcess(null); final AsyncResult<Object> asyncResult = executor.startProcess(null);
assertNotNull("The AsyncResult should not be 'null', even though the task was 'null'.", asyncResult); assertNotNull("The AsyncResult should not be 'null', even though the task was 'null'.",
asyncResult.await(); // Prevent timing issues, and wait until the result is available asyncResult);
assertTrue(asyncResult.isCompleted()); asyncResult.await(); // Prevent timing issues, and wait until the result is available
assertTrue(asyncResult.isCompleted());
try { try {
asyncResult.getValue(); asyncResult.getValue();
fail("Expected ExecutionException with NPE as cause"); fail("Expected ExecutionException with NPE as cause");
} catch (final ExecutionException e) { } catch (final ExecutionException e) {
assertNotNull(e.getMessage()); assertNotNull(e.getMessage());
assertNotNull(e.getCause()); assertNotNull(e.getCause());
assertEquals(NullPointerException.class, e.getCause().getClass()); assertEquals(NullPointerException.class, e.getCause().getClass());
} }
} }
/** /**
* Test used to verify the behaviour of {@link ThreadAsyncExecutor#startProcess(Callable, AsyncCallback)} * Test used to verify the behaviour of
* when the callable is 'null', but the asynchronous callback is provided * {@link ThreadAsyncExecutor#startProcess(Callable, AsyncCallback)} when the callable is 'null',
*/ * but the asynchronous callback is provided
@Test(timeout = 3000) */
public void testNullTaskWithCallback() throws Exception { @Test(timeout = 3000)
// Instantiate a new executor and start a new 'null' task ... public void testNullTaskWithCallback() throws Exception {
final ThreadAsyncExecutor executor = new ThreadAsyncExecutor(); // Instantiate a new executor and start a new 'null' task ...
final AsyncCallback<Object> callback = mock(AsyncCallback.class); final ThreadAsyncExecutor executor = new ThreadAsyncExecutor();
final AsyncResult<Object> asyncResult = executor.startProcess(null, callback); final AsyncCallback<Object> callback = mock(AsyncCallback.class);
final AsyncResult<Object> asyncResult = executor.startProcess(null, callback);
assertNotNull("The AsyncResult should not be 'null', even though the task was 'null'.", asyncResult); assertNotNull("The AsyncResult should not be 'null', even though the task was 'null'.",
asyncResult.await(); // Prevent timing issues, and wait until the result is available asyncResult);
assertTrue(asyncResult.isCompleted()); asyncResult.await(); // Prevent timing issues, and wait until the result is available
assertTrue(asyncResult.isCompleted());
final ArgumentCaptor<Optional<Exception>> optionalCaptor = ArgumentCaptor.forClass((Class) Optional.class); final ArgumentCaptor<Optional<Exception>> optionalCaptor = ArgumentCaptor
verify(callback, times(1)).onComplete(Matchers.isNull(), optionalCaptor.capture()); .forClass((Class) Optional.class);
verify(callback, times(1)).onComplete(Matchers.isNull(), optionalCaptor.capture());
final Optional<Exception> optionalException = optionalCaptor.getValue(); final Optional<Exception> optionalException = optionalCaptor.getValue();
assertNotNull(optionalException); assertNotNull(optionalException);
assertTrue(optionalException.isPresent()); assertTrue(optionalException.isPresent());
final Exception exception = optionalException.get(); final Exception exception = optionalException.get();
assertNotNull(exception); assertNotNull(exception);
assertEquals(NullPointerException.class, exception.getClass()); assertEquals(NullPointerException.class, exception.getClass());
try { try {
asyncResult.getValue(); asyncResult.getValue();
fail("Expected ExecutionException with NPE as cause"); fail("Expected ExecutionException with NPE as cause");
} catch (final ExecutionException e) { } catch (final ExecutionException e) {
assertNotNull(e.getMessage()); assertNotNull(e.getMessage());
assertNotNull(e.getCause()); assertNotNull(e.getCause());
assertEquals(NullPointerException.class, e.getCause().getClass()); assertEquals(NullPointerException.class, e.getCause().getClass());
} }
} }
/** /**
* Test used to verify the behaviour of {@link ThreadAsyncExecutor#startProcess(Callable, AsyncCallback)} * Test used to verify the behaviour of
* when both the callable and the asynchronous callback are 'null' * {@link ThreadAsyncExecutor#startProcess(Callable, AsyncCallback)} when both the callable and
*/ * the asynchronous callback are 'null'
@Test(timeout = 3000) */
public void testNullTaskWithNullCallback() throws Exception { @Test(timeout = 3000)
// Instantiate a new executor and start a new 'null' task ... public void testNullTaskWithNullCallback() throws Exception {
final ThreadAsyncExecutor executor = new ThreadAsyncExecutor(); // Instantiate a new executor and start a new 'null' task ...
final AsyncResult<Object> asyncResult = executor.startProcess(null, null); final ThreadAsyncExecutor executor = new ThreadAsyncExecutor();
final AsyncResult<Object> asyncResult = executor.startProcess(null, null);
assertNotNull("The AsyncResult should not be 'null', even though the task and callback were 'null'.", asyncResult); assertNotNull(
asyncResult.await(); // Prevent timing issues, and wait until the result is available "The AsyncResult should not be 'null', even though the task and callback were 'null'.",
assertTrue(asyncResult.isCompleted()); asyncResult);
asyncResult.await(); // Prevent timing issues, and wait until the result is available
assertTrue(asyncResult.isCompleted());
try { try {
asyncResult.getValue(); asyncResult.getValue();
fail("Expected ExecutionException with NPE as cause"); fail("Expected ExecutionException with NPE as cause");
} catch (final ExecutionException e) { } catch (final ExecutionException e) {
assertNotNull(e.getMessage()); assertNotNull(e.getMessage());
assertNotNull(e.getCause()); assertNotNull(e.getCause());
assertEquals(NullPointerException.class, e.getCause().getClass()); assertEquals(NullPointerException.class, e.getCause().getClass());
} }
} }
} }