mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-14 22:58:36 +00:00
deps: Refactor dependencies (#3224)
* remove spring dep move junit, logging, mockito under dep mgmt * upgrade anti-corruption-layer deps * async method invocation * balking, bloc * bridge to bytecode * caching * callback - cqrs * component - health check * hexagonal - metadata mapping * rest of the patterns * remove checkstyle, take spotless into use
This commit is contained in:
+6
-10
@@ -24,26 +24,22 @@
|
||||
*/
|
||||
package com.iluwatar.async.method.invocation;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/**
|
||||
* Application test
|
||||
*/
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/** Application test */
|
||||
class AppTest {
|
||||
|
||||
/**
|
||||
* Issue: Add at least one assertion to this test case.
|
||||
*
|
||||
* Solution: Inserted assertion to check whether the execution of the main method in {@link App}
|
||||
* throws an exception.
|
||||
* <p>Solution: Inserted assertion to check whether the execution of the main method in {@link
|
||||
* App} throws an exception.
|
||||
*/
|
||||
|
||||
@Test
|
||||
void shouldExecuteApplicationWithoutException() {
|
||||
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
|
||||
assertDoesNotThrow(() -> App.main(new String[] {}));
|
||||
}
|
||||
}
|
||||
|
||||
+199
-184
@@ -33,7 +33,6 @@ import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.mockito.internal.verification.VerificationModeFactory.times;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
@@ -43,49 +42,43 @@ import org.mockito.Captor;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
|
||||
/**
|
||||
* ThreadAsyncExecutorTest
|
||||
*
|
||||
*/
|
||||
/** ThreadAsyncExecutorTest */
|
||||
class ThreadAsyncExecutorTest {
|
||||
|
||||
@Captor
|
||||
private ArgumentCaptor<Exception> exceptionCaptor;
|
||||
@Captor private ArgumentCaptor<Exception> exceptionCaptor;
|
||||
|
||||
@Mock
|
||||
private Callable<Object> task;
|
||||
@Mock private Callable<Object> task;
|
||||
|
||||
@Mock
|
||||
private AsyncCallback<Object> callback;
|
||||
@Mock private AsyncCallback<Object> callback;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
MockitoAnnotations.openMocks(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
void testSuccessfulTaskWithoutCallback() {
|
||||
assertTimeout(ofMillis(3000), () -> {
|
||||
// Instantiate a new executor and start a new 'null' task ...
|
||||
final var executor = new ThreadAsyncExecutor();
|
||||
assertTimeout(
|
||||
ofMillis(3000),
|
||||
() -> {
|
||||
// Instantiate a new executor and start a new 'null' task ...
|
||||
final var executor = new ThreadAsyncExecutor();
|
||||
|
||||
final var result = new Object();
|
||||
when(task.call()).thenReturn(result);
|
||||
final var result = new Object();
|
||||
when(task.call()).thenReturn(result);
|
||||
|
||||
final var asyncResult = executor.startProcess(task);
|
||||
assertNotNull(asyncResult);
|
||||
asyncResult.await(); // Prevent timing issues, and wait until the result is available
|
||||
assertTrue(asyncResult.isCompleted());
|
||||
final var asyncResult = executor.startProcess(task);
|
||||
assertNotNull(asyncResult);
|
||||
asyncResult.await(); // Prevent timing issues, and wait until the result is available
|
||||
assertTrue(asyncResult.isCompleted());
|
||||
|
||||
// Our task should only execute once ...
|
||||
verify(task, times(1)).call();
|
||||
// Our task should only execute once ...
|
||||
verify(task, times(1)).call();
|
||||
|
||||
// ... and the result should be exactly the same object
|
||||
assertSame(result, asyncResult.getValue());
|
||||
});
|
||||
// ... and the result should be exactly the same object
|
||||
assertSame(result, asyncResult.getValue());
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -94,28 +87,30 @@ class ThreadAsyncExecutorTest {
|
||||
*/
|
||||
@Test
|
||||
void testSuccessfulTaskWithCallback() {
|
||||
assertTimeout(ofMillis(3000), () -> {
|
||||
// Instantiate a new executor and start a new 'null' task ...
|
||||
final var executor = new ThreadAsyncExecutor();
|
||||
assertTimeout(
|
||||
ofMillis(3000),
|
||||
() -> {
|
||||
// Instantiate a new executor and start a new 'null' task ...
|
||||
final var executor = new ThreadAsyncExecutor();
|
||||
|
||||
final var result = new Object();
|
||||
when(task.call()).thenReturn(result);
|
||||
final var result = new Object();
|
||||
when(task.call()).thenReturn(result);
|
||||
|
||||
final var asyncResult = executor.startProcess(task, callback);
|
||||
assertNotNull(asyncResult);
|
||||
asyncResult.await(); // Prevent timing issues, and wait until the result is available
|
||||
assertTrue(asyncResult.isCompleted());
|
||||
final var asyncResult = executor.startProcess(task, callback);
|
||||
assertNotNull(asyncResult);
|
||||
asyncResult.await(); // Prevent timing issues, and wait until the result is available
|
||||
assertTrue(asyncResult.isCompleted());
|
||||
|
||||
// Our task should only execute once ...
|
||||
verify(task, times(1)).call();
|
||||
// Our task should only execute once ...
|
||||
verify(task, times(1)).call();
|
||||
|
||||
// ... same for the callback, we expect our object
|
||||
verify(callback, times(1)).onComplete(eq(result));
|
||||
verify(callback, times(0)).onError(exceptionCaptor.capture());
|
||||
// ... same for the callback, we expect our object
|
||||
verify(callback, times(1)).onComplete(eq(result));
|
||||
verify(callback, times(0)).onError(exceptionCaptor.capture());
|
||||
|
||||
// ... and the result should be exactly the same object
|
||||
assertSame(result, asyncResult.getValue());
|
||||
});
|
||||
// ... and the result should be exactly the same object
|
||||
assertSame(result, asyncResult.getValue());
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -124,38 +119,43 @@ class ThreadAsyncExecutorTest {
|
||||
*/
|
||||
@Test
|
||||
void testLongRunningTaskWithoutCallback() {
|
||||
assertTimeout(ofMillis(5000), () -> {
|
||||
// Instantiate a new executor and start a new 'null' task ...
|
||||
final var executor = new ThreadAsyncExecutor();
|
||||
assertTimeout(
|
||||
ofMillis(5000),
|
||||
() -> {
|
||||
// Instantiate a new executor and start a new 'null' task ...
|
||||
final var executor = new ThreadAsyncExecutor();
|
||||
|
||||
final var result = new Object();
|
||||
when(task.call()).thenAnswer(i -> {
|
||||
Thread.sleep(1500);
|
||||
return result;
|
||||
});
|
||||
final var result = new Object();
|
||||
when(task.call())
|
||||
.thenAnswer(
|
||||
i -> {
|
||||
Thread.sleep(1500);
|
||||
return result;
|
||||
});
|
||||
|
||||
final var asyncResult = executor.startProcess(task);
|
||||
assertNotNull(asyncResult);
|
||||
assertFalse(asyncResult.isCompleted());
|
||||
final var asyncResult = executor.startProcess(task);
|
||||
assertNotNull(asyncResult);
|
||||
assertFalse(asyncResult.isCompleted());
|
||||
|
||||
try {
|
||||
asyncResult.getValue();
|
||||
fail("Expected IllegalStateException when calling AsyncResult#getValue on a non-completed task");
|
||||
} catch (IllegalStateException e) {
|
||||
assertNotNull(e.getMessage());
|
||||
}
|
||||
try {
|
||||
asyncResult.getValue();
|
||||
fail(
|
||||
"Expected IllegalStateException when calling AsyncResult#getValue on a non-completed task");
|
||||
} catch (IllegalStateException e) {
|
||||
assertNotNull(e.getMessage());
|
||||
}
|
||||
|
||||
// Our task should only execute once, but it can take a while ...
|
||||
verify(task, timeout(3000).times(1)).call();
|
||||
// Our task should only execute once, but it can take a while ...
|
||||
verify(task, timeout(3000).times(1)).call();
|
||||
|
||||
// Prevent timing issues, and wait until the result is available
|
||||
asyncResult.await();
|
||||
assertTrue(asyncResult.isCompleted());
|
||||
verifyNoMoreInteractions(task);
|
||||
// Prevent timing issues, and wait until the result is available
|
||||
asyncResult.await();
|
||||
assertTrue(asyncResult.isCompleted());
|
||||
verifyNoMoreInteractions(task);
|
||||
|
||||
// ... and the result should be exactly the same object
|
||||
assertSame(result, asyncResult.getValue());
|
||||
});
|
||||
// ... and the result should be exactly the same object
|
||||
assertSame(result, asyncResult.getValue());
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -164,42 +164,47 @@ class ThreadAsyncExecutorTest {
|
||||
*/
|
||||
@Test
|
||||
void testLongRunningTaskWithCallback() {
|
||||
assertTimeout(ofMillis(5000), () -> {
|
||||
// Instantiate a new executor and start a new 'null' task ...
|
||||
final var executor = new ThreadAsyncExecutor();
|
||||
assertTimeout(
|
||||
ofMillis(5000),
|
||||
() -> {
|
||||
// Instantiate a new executor and start a new 'null' task ...
|
||||
final var executor = new ThreadAsyncExecutor();
|
||||
|
||||
final var result = new Object();
|
||||
when(task.call()).thenAnswer(i -> {
|
||||
Thread.sleep(1500);
|
||||
return result;
|
||||
});
|
||||
final var result = new Object();
|
||||
when(task.call())
|
||||
.thenAnswer(
|
||||
i -> {
|
||||
Thread.sleep(1500);
|
||||
return result;
|
||||
});
|
||||
|
||||
final var asyncResult = executor.startProcess(task, callback);
|
||||
assertNotNull(asyncResult);
|
||||
assertFalse(asyncResult.isCompleted());
|
||||
final var asyncResult = executor.startProcess(task, callback);
|
||||
assertNotNull(asyncResult);
|
||||
assertFalse(asyncResult.isCompleted());
|
||||
|
||||
verifyNoMoreInteractions(callback);
|
||||
verifyNoMoreInteractions(callback);
|
||||
|
||||
try {
|
||||
asyncResult.getValue();
|
||||
fail("Expected IllegalStateException when calling AsyncResult#getValue on a non-completed task");
|
||||
} catch (IllegalStateException e) {
|
||||
assertNotNull(e.getMessage());
|
||||
}
|
||||
try {
|
||||
asyncResult.getValue();
|
||||
fail(
|
||||
"Expected IllegalStateException when calling AsyncResult#getValue on a non-completed task");
|
||||
} catch (IllegalStateException e) {
|
||||
assertNotNull(e.getMessage());
|
||||
}
|
||||
|
||||
// Our task should only execute once, but it can take a while ...
|
||||
verify(task, timeout(3000).times(1)).call();
|
||||
verify(callback, timeout(3000).times(1)).onComplete(eq(result));
|
||||
verify(callback, times(0)).onError(isA(Exception.class));
|
||||
// Our task should only execute once, but it can take a while ...
|
||||
verify(task, timeout(3000).times(1)).call();
|
||||
verify(callback, timeout(3000).times(1)).onComplete(eq(result));
|
||||
verify(callback, times(0)).onError(isA(Exception.class));
|
||||
|
||||
// Prevent timing issues, and wait until the result is available
|
||||
asyncResult.await();
|
||||
assertTrue(asyncResult.isCompleted());
|
||||
verifyNoMoreInteractions(task, callback);
|
||||
// Prevent timing issues, and wait until the result is available
|
||||
asyncResult.await();
|
||||
assertTrue(asyncResult.isCompleted());
|
||||
verifyNoMoreInteractions(task, callback);
|
||||
|
||||
// ... and the result should be exactly the same object
|
||||
assertSame(result, asyncResult.getValue());
|
||||
});
|
||||
// ... and the result should be exactly the same object
|
||||
assertSame(result, asyncResult.getValue());
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -209,35 +214,40 @@ class ThreadAsyncExecutorTest {
|
||||
*/
|
||||
@Test
|
||||
void testEndProcess() {
|
||||
assertTimeout(ofMillis(5000), () -> {
|
||||
// Instantiate a new executor and start a new 'null' task ...
|
||||
final var executor = new ThreadAsyncExecutor();
|
||||
assertTimeout(
|
||||
ofMillis(5000),
|
||||
() -> {
|
||||
// Instantiate a new executor and start a new 'null' task ...
|
||||
final var executor = new ThreadAsyncExecutor();
|
||||
|
||||
final var result = new Object();
|
||||
when(task.call()).thenAnswer(i -> {
|
||||
Thread.sleep(1500);
|
||||
return result;
|
||||
});
|
||||
final var result = new Object();
|
||||
when(task.call())
|
||||
.thenAnswer(
|
||||
i -> {
|
||||
Thread.sleep(1500);
|
||||
return result;
|
||||
});
|
||||
|
||||
final var asyncResult = executor.startProcess(task);
|
||||
assertNotNull(asyncResult);
|
||||
assertFalse(asyncResult.isCompleted());
|
||||
final var asyncResult = executor.startProcess(task);
|
||||
assertNotNull(asyncResult);
|
||||
assertFalse(asyncResult.isCompleted());
|
||||
|
||||
try {
|
||||
asyncResult.getValue();
|
||||
fail("Expected IllegalStateException when calling AsyncResult#getValue on a non-completed task");
|
||||
} catch (IllegalStateException e) {
|
||||
assertNotNull(e.getMessage());
|
||||
}
|
||||
try {
|
||||
asyncResult.getValue();
|
||||
fail(
|
||||
"Expected IllegalStateException when calling AsyncResult#getValue on a non-completed task");
|
||||
} catch (IllegalStateException e) {
|
||||
assertNotNull(e.getMessage());
|
||||
}
|
||||
|
||||
assertSame(result, executor.endProcess(asyncResult));
|
||||
verify(task, times(1)).call();
|
||||
assertTrue(asyncResult.isCompleted());
|
||||
assertSame(result, executor.endProcess(asyncResult));
|
||||
verify(task, times(1)).call();
|
||||
assertTrue(asyncResult.isCompleted());
|
||||
|
||||
// Calling end process a second time while already finished should give the same result
|
||||
assertSame(result, executor.endProcess(asyncResult));
|
||||
verifyNoMoreInteractions(task);
|
||||
});
|
||||
// Calling end process a second time while already finished should give the same result
|
||||
assertSame(result, executor.endProcess(asyncResult));
|
||||
verifyNoMoreInteractions(task);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -246,25 +256,28 @@ class ThreadAsyncExecutorTest {
|
||||
*/
|
||||
@Test
|
||||
void testNullTask() {
|
||||
assertTimeout(ofMillis(3000), () -> {
|
||||
// Instantiate a new executor and start a new 'null' task ...
|
||||
final var executor = new ThreadAsyncExecutor();
|
||||
final var asyncResult = executor.startProcess(null);
|
||||
assertTimeout(
|
||||
ofMillis(3000),
|
||||
() -> {
|
||||
// Instantiate a new executor and start a new 'null' task ...
|
||||
final var executor = new ThreadAsyncExecutor();
|
||||
final var asyncResult = executor.startProcess(null);
|
||||
|
||||
assertNotNull(asyncResult, "The AsyncResult should not be 'null', even though the task was 'null'.");
|
||||
asyncResult.await(); // Prevent timing issues, and wait until the result is available
|
||||
assertTrue(asyncResult.isCompleted());
|
||||
|
||||
try {
|
||||
asyncResult.getValue();
|
||||
fail("Expected ExecutionException with NPE as cause");
|
||||
} catch (final ExecutionException e) {
|
||||
assertNotNull(e.getMessage());
|
||||
assertNotNull(e.getCause());
|
||||
assertEquals(NullPointerException.class, e.getCause().getClass());
|
||||
}
|
||||
});
|
||||
assertNotNull(
|
||||
asyncResult,
|
||||
"The AsyncResult should not be 'null', even though the task was 'null'.");
|
||||
asyncResult.await(); // Prevent timing issues, and wait until the result is available
|
||||
assertTrue(asyncResult.isCompleted());
|
||||
|
||||
try {
|
||||
asyncResult.getValue();
|
||||
fail("Expected ExecutionException with NPE as cause");
|
||||
} catch (final ExecutionException e) {
|
||||
assertNotNull(e.getMessage());
|
||||
assertNotNull(e.getCause());
|
||||
assertEquals(NullPointerException.class, e.getCause().getClass());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -273,32 +286,35 @@ class ThreadAsyncExecutorTest {
|
||||
*/
|
||||
@Test
|
||||
void testNullTaskWithCallback() {
|
||||
assertTimeout(ofMillis(3000), () -> {
|
||||
// Instantiate a new executor and start a new 'null' task ...
|
||||
final var executor = new ThreadAsyncExecutor();
|
||||
final var asyncResult = executor.startProcess(null, callback);
|
||||
assertTimeout(
|
||||
ofMillis(3000),
|
||||
() -> {
|
||||
// Instantiate a new executor and start a new 'null' task ...
|
||||
final var executor = new ThreadAsyncExecutor();
|
||||
final var asyncResult = executor.startProcess(null, callback);
|
||||
|
||||
assertNotNull(asyncResult, "The AsyncResult should not be 'null', even though the task was 'null'.");
|
||||
asyncResult.await(); // Prevent timing issues, and wait until the result is available
|
||||
assertTrue(asyncResult.isCompleted());
|
||||
verify(callback, times(0)).onComplete(any());
|
||||
verify(callback, times(1)).onError(exceptionCaptor.capture());
|
||||
assertNotNull(
|
||||
asyncResult,
|
||||
"The AsyncResult should not be 'null', even though the task was 'null'.");
|
||||
asyncResult.await(); // Prevent timing issues, and wait until the result is available
|
||||
assertTrue(asyncResult.isCompleted());
|
||||
verify(callback, times(0)).onComplete(any());
|
||||
verify(callback, times(1)).onError(exceptionCaptor.capture());
|
||||
|
||||
final var exception = exceptionCaptor.getValue();
|
||||
assertNotNull(exception);
|
||||
final var exception = exceptionCaptor.getValue();
|
||||
assertNotNull(exception);
|
||||
|
||||
assertEquals(NullPointerException.class, exception.getClass());
|
||||
|
||||
try {
|
||||
asyncResult.getValue();
|
||||
fail("Expected ExecutionException with NPE as cause");
|
||||
} catch (final ExecutionException e) {
|
||||
assertNotNull(e.getMessage());
|
||||
assertNotNull(e.getCause());
|
||||
assertEquals(NullPointerException.class, e.getCause().getClass());
|
||||
}
|
||||
});
|
||||
assertEquals(NullPointerException.class, exception.getClass());
|
||||
|
||||
try {
|
||||
asyncResult.getValue();
|
||||
fail("Expected ExecutionException with NPE as cause");
|
||||
} catch (final ExecutionException e) {
|
||||
assertNotNull(e.getMessage());
|
||||
assertNotNull(e.getCause());
|
||||
assertEquals(NullPointerException.class, e.getCause().getClass());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -307,28 +323,27 @@ class ThreadAsyncExecutorTest {
|
||||
*/
|
||||
@Test
|
||||
void testNullTaskWithNullCallback() {
|
||||
assertTimeout(ofMillis(3000), () -> {
|
||||
// Instantiate a new executor and start a new 'null' task ...
|
||||
final var executor = new ThreadAsyncExecutor();
|
||||
final var asyncResult = executor.startProcess(null, null);
|
||||
assertTimeout(
|
||||
ofMillis(3000),
|
||||
() -> {
|
||||
// Instantiate a new executor and start a new 'null' task ...
|
||||
final var executor = new ThreadAsyncExecutor();
|
||||
final var asyncResult = executor.startProcess(null, null);
|
||||
|
||||
assertNotNull(
|
||||
asyncResult,
|
||||
"The AsyncResult should not be 'null', even though the task and callback were 'null'."
|
||||
);
|
||||
asyncResult.await(); // Prevent timing issues, and wait until the result is available
|
||||
assertTrue(asyncResult.isCompleted());
|
||||
|
||||
try {
|
||||
asyncResult.getValue();
|
||||
fail("Expected ExecutionException with NPE as cause");
|
||||
} catch (final ExecutionException e) {
|
||||
assertNotNull(e.getMessage());
|
||||
assertNotNull(e.getCause());
|
||||
assertEquals(NullPointerException.class, e.getCause().getClass());
|
||||
}
|
||||
});
|
||||
assertNotNull(
|
||||
asyncResult,
|
||||
"The AsyncResult should not be 'null', even though the task and callback were 'null'.");
|
||||
asyncResult.await(); // Prevent timing issues, and wait until the result is available
|
||||
assertTrue(asyncResult.isCompleted());
|
||||
|
||||
try {
|
||||
asyncResult.getValue();
|
||||
fail("Expected ExecutionException with NPE as cause");
|
||||
} catch (final ExecutionException e) {
|
||||
assertNotNull(e.getMessage());
|
||||
assertNotNull(e.getCause());
|
||||
assertEquals(NullPointerException.class, e.getCause().getClass());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user