BAEL 5932 - Callback Functions in Java (#13335)

This commit is contained in:
Olu 2023-01-27 21:37:47 +01:00 committed by GitHub
parent b5d8f4e93c
commit d7e857f427
10 changed files with 199 additions and 0 deletions

View File

@ -14,4 +14,30 @@
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>${mockito-inline.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>core-java-functional</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<properties>
<mockito-inline.version>3.8.0</mockito-inline.version>
<assertj.version>3.22.0</assertj.version>
<commons-lang3.version>3.12.0</commons-lang3.version>
<vavr.version>0.10.4</vavr.version>
</properties>
</project>

View File

@ -0,0 +1,17 @@
package com.baeldung.callbackfunctions;
import java.util.function.Consumer;
public class ConsumerCallback {
public void getAge(int initialAge, Consumer<Integer> callback) {
callback.accept(initialAge);
}
public void increaseAge(int initialAge, int ageDifference, Consumer<Integer> callback) {
System.out.println("===== Increase age ====");
int newAge = initialAge + ageDifference;
callback.accept(newAge);
}
}

View File

@ -0,0 +1,12 @@
package com.baeldung.callbackfunctions;
public interface EventListener {
String onTrigger();
void respondToTrigger();
}

View File

@ -0,0 +1,21 @@
package com.baeldung.callbackfunctions.asynchronous;
import com.baeldung.callbackfunctions.EventListener;
public class AsynchronousEventConsumer{
private EventListener listener;
public AsynchronousEventConsumer(EventListener listener) {
this.listener = listener;
}
public void doAsynchronousOperation()
{
System.out.println("Performing operation in Asynchronous Task");
new Thread(() -> listener.onTrigger()).start();
}
}

View File

@ -0,0 +1,19 @@
package com.baeldung.callbackfunctions.asynchronous;
import com.baeldung.callbackfunctions.EventListener;
public class AsynchronousEventListenerImpl implements EventListener {
@Override
public String onTrigger()
{
respondToTrigger();
return "Asynchronously running callback function";
}
@Override
public void respondToTrigger(){
System.out.println("This is a side effect of the asynchronous trigger.");
}
}

View File

@ -0,0 +1,22 @@
package com.baeldung.callbackfunctions.synchronous;
import com.baeldung.callbackfunctions.EventListener;
public class SynchronousEventConsumer {
private final EventListener eventListener;
public SynchronousEventConsumer(EventListener listener)
{
this.eventListener = listener;
}
public String doSynchronousOperation()
{
System.out.println("Performing callback before synchronous Task");
return eventListener.onTrigger();
}
}

View File

@ -0,0 +1,18 @@
package com.baeldung.callbackfunctions.synchronous;
import com.baeldung.callbackfunctions.EventListener;
public class SynchronousEventListenerImpl implements EventListener {
@Override
public String onTrigger()
{
return "Synchronously running callback function";
}
@Override
public void respondToTrigger(){
System.out.println("Response to trigger");
}
}

View File

@ -0,0 +1,22 @@
package com.baeldung.callbackfunctions;
import org.junit.Test;
import org.mockito.Mockito;
import com.baeldung.callbackfunctions.EventListener;
import com.baeldung.callbackfunctions.asynchronous.AsynchronousEventConsumer;
import com.baeldung.callbackfunctions.asynchronous.AsynchronousEventListenerImpl;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
public class AsynchronousCallbackUnitTest {
@Test
public void whenCallbackIsInvokedAsynchronously_shouldRunAsynchronousOperation(){
EventListener listener = Mockito.mock(AsynchronousEventListenerImpl.class);
AsynchronousEventConsumer asynchronousEventListenerConsumer = new AsynchronousEventConsumer(listener);
asynchronousEventListenerConsumer.doAsynchronousOperation();
verify(listener, times(1)).onTrigger();
}
}

View File

@ -0,0 +1,20 @@
package com.baeldung.callbackfunctions;
import org.junit.jupiter.api.Test;
import com.baeldung.callbackfunctions.ConsumerCallback;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class ConsumerCallbackUnitTest {
@Test
public void whenIncreasingInitialAgeByGivenValueThroughCallback_shouldIncreaseAge(){
ConsumerCallback consumerCallback = new ConsumerCallback();
consumerCallback.getAge(20, (initialAge) -> {
int ageDifference = 10;
consumerCallback.increaseAge(initialAge, ageDifference, (newAge) -> {
assertEquals(initialAge + ageDifference, newAge);
});
});
}
}

View File

@ -0,0 +1,22 @@
package callbackFunctions;
import org.junit.jupiter.api.Test;
import com.baeldung.callbackfunctions.EventListener;
import com.baeldung.callbackfunctions.synchronous.SynchronousEventConsumer;
import com.baeldung.callbackfunctions.synchronous.SynchronousEventListenerImpl;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
public class SynchronousCallbackUnitTest {
@Test
public void whenCallbackIsInvokedSynchronously_shouldRunSynchronousOperation(){
EventListener listener = new SynchronousEventListenerImpl();
SynchronousEventConsumer synchronousEventConsumer = new SynchronousEventConsumer(listener);
String result = synchronousEventConsumer.doSynchronousOperation();
assertNotNull(result);
assertEquals("Synchronously running callback function", result);
}
}