Skip to content

Commit

Permalink
Add new adapter interfaces factory methods
Browse files Browse the repository at this point in the history
Signed-off-by: Taeik Lim <[email protected]>
  • Loading branch information
acktsap committed Mar 10, 2024
1 parent bdea73c commit 15527e6
Show file tree
Hide file tree
Showing 4 changed files with 195 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,40 @@ package com.navercorp.spring.batch.plus.kotlin.item.adapter

import com.navercorp.spring.batch.plus.item.adapter.AdapterFactory
import com.navercorp.spring.batch.plus.item.adapter.ItemProcessorDelegate
import com.navercorp.spring.batch.plus.item.adapter.ItemStreamFluxReaderDelegate
import com.navercorp.spring.batch.plus.item.adapter.ItemStreamIterableReaderDelegate
import com.navercorp.spring.batch.plus.item.adapter.ItemStreamIteratorReaderDelegate
import com.navercorp.spring.batch.plus.item.adapter.ItemStreamReaderDelegate
import com.navercorp.spring.batch.plus.item.adapter.ItemStreamSimpleReaderDelegate
import com.navercorp.spring.batch.plus.item.adapter.ItemStreamWriterDelegate
import org.springframework.batch.item.ItemProcessor
import org.springframework.batch.item.ItemStreamReader
import org.springframework.batch.item.ItemStreamWriter

/**
* An extensions to invoke [AdapterFactory.itemStreamReader].
*/
fun <T : Any> ItemStreamFluxReaderDelegate<T>.asItemStreamReader(): ItemStreamReader<T> =
AdapterFactory.itemStreamReader(this)

/**
* An extensions to invoke [AdapterFactory.itemStreamReader].
*/
fun <T : Any> ItemStreamIterableReaderDelegate<T>.asItemStreamReader(): ItemStreamReader<T> =
AdapterFactory.itemStreamReader(this)

/**
* An extensions to invoke [AdapterFactory.itemStreamReader].
*/
fun <T : Any> ItemStreamIteratorReaderDelegate<T>.asItemStreamReader(): ItemStreamReader<T> =
AdapterFactory.itemStreamReader(this)

/**
* An extensions to invoke [AdapterFactory.itemStreamReader].
*/
fun <T : Any> ItemStreamSimpleReaderDelegate<T>.asItemStreamReader(): ItemStreamReader<T> =
AdapterFactory.itemStreamReader(this)

/**
* An extensions to invoke [AdapterFactory.itemStreamReader].
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,16 @@
package com.navercorp.spring.batch.plus.kotlin.item.adapter

import com.navercorp.spring.batch.plus.item.adapter.ItemProcessorAdapter
import com.navercorp.spring.batch.plus.item.adapter.ItemProcessorDelegate
import com.navercorp.spring.batch.plus.item.adapter.ItemStreamFluxReaderDelegate
import com.navercorp.spring.batch.plus.item.adapter.ItemStreamIterableReaderDelegate
import com.navercorp.spring.batch.plus.item.adapter.ItemStreamIteratorReaderDelegate
import com.navercorp.spring.batch.plus.item.adapter.ItemStreamReaderProcessorWriter
import com.navercorp.spring.batch.plus.item.adapter.ItemStreamSimpleReaderDelegate
import com.navercorp.spring.batch.plus.item.adapter.ItemStreamWriterAdapter
import com.navercorp.spring.batch.plus.item.adapter.ItemStreamWriterDelegate
import com.navercorp.spring.batch.plus.item.adapter.StepScopeItemStreamReader
import io.mockk.mockk
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import org.springframework.batch.item.Chunk
Expand All @@ -30,6 +37,67 @@ import reactor.core.publisher.Flux

internal class ItemDelegatesTest {

@Test
fun testAsItemStreamReaderOnItemStreamFluxReaderDelegate() {
// when
val delegate = mockk<ItemStreamFluxReaderDelegate<*>>()
val itemStreamReader = delegate.asItemStreamReader()

// then
assertThat(itemStreamReader).isInstanceOf(StepScopeItemStreamReader::class.java)
}

@Test
fun testAsItemStreamReaderOnItemStreamIterableReaderDelegate() {
// when
val delegate = mockk<ItemStreamIterableReaderDelegate<*>>()
val itemStreamReader = delegate.asItemStreamReader()

// then
assertThat(itemStreamReader).isInstanceOf(StepScopeItemStreamReader::class.java)
}

@Test
fun testAsItemStreamReaderOnItemStreamIteratorReaderDelegate() {
// when
val delegate = mockk<ItemStreamIteratorReaderDelegate<*>>()
val itemStreamReader = delegate.asItemStreamReader()

// then
assertThat(itemStreamReader).isInstanceOf(StepScopeItemStreamReader::class.java)
}

@Test
fun testAsItemStreamReaderOnItemStreamSimpleReaderDelegate() {
// when
val delegate = mockk<ItemStreamSimpleReaderDelegate<*>>()
val itemStreamReader = delegate.asItemStreamReader()

// then
assertThat(itemStreamReader).isInstanceOf(StepScopeItemStreamReader::class.java)
}

@Test
fun testAsItemProcessor() {
// when
val delegate = mockk<ItemProcessorDelegate<*, *>>()
val itemProcessor = delegate.asItemProcessor()

// then
assertThat(itemProcessor).isInstanceOf(ItemProcessorAdapter::class.java)
}

@Test
fun testAsItemStreamWriter() {
// when
val delegate = mockk<ItemStreamWriterDelegate<*>>()
val itemStreamWriter = delegate.asItemStreamWriter()

// then
assertThat(itemStreamWriter).isInstanceOf(ItemStreamWriterAdapter::class.java)
}

// TODO: delete after ItemStreamReaderDelegate is removed
@Test
fun testExtensions() {
// when
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,58 @@
*/
public final class AdapterFactory {

/**
* Create an adapter which adapt {@link ItemStreamFluxReaderDelegate} to {@link ItemStreamReader}
* with {@link StepScope} bound proxy implementation. It creates new instance for every {@link StepScope}.
*
* @param delegate a delegate
* @return an adapted ItemStreamReader
* @param <T> a read item type
*/
public static <T> ItemStreamReader<T> itemStreamReader(@NonNull ItemStreamFluxReaderDelegate<T> delegate) {
Objects.requireNonNull(delegate, "ItemStreamReader delegate is null");
return StepScopeItemStreamReader.of(() -> ItemStreamFluxReaderAdapter.of(delegate));
}

/**
* Create an adapter which adapt {@link ItemStreamIterableReaderDelegate} to {@link ItemStreamReader}
* with {@link StepScope} bound proxy implementation. It creates new instance for every {@link StepScope}.
*
* @param delegate a delegate
* @return an adapted ItemStreamReader
* @param <T> a read item type
*/
public static <T> ItemStreamReader<T> itemStreamReader(@NonNull ItemStreamIterableReaderDelegate<T> delegate) {
Objects.requireNonNull(delegate, "ItemStreamReader delegate is null");
return StepScopeItemStreamReader.of(() -> ItemStreamIterableReaderAdapter.of(delegate));
}

/**
* Create an adapter which adapt {@link ItemStreamIteratorReaderDelegate} to {@link ItemStreamReader}
* with {@link StepScope} bound proxy implementation. It creates new instance for every {@link StepScope}.
*
* @param delegate a delegate
* @return an adapted ItemStreamReader
* @param <T> a read item type
*/
public static <T> ItemStreamReader<T> itemStreamReader(@NonNull ItemStreamIteratorReaderDelegate<T> delegate) {
Objects.requireNonNull(delegate, "ItemStreamReader delegate is null");
return StepScopeItemStreamReader.of(() -> ItemStreamIteratorReaderAdapter.of(delegate));
}

/**
* Create an adapter which adapt {@link ItemStreamSimpleReaderDelegate} to {@link ItemStreamReader}
* with {@link StepScope} bound proxy implementation. It creates new instance for every {@link StepScope}.
*
* @param delegate a delegate
* @return an adapted ItemStreamReader
* @param <T> a read item type
*/
public static <T> ItemStreamReader<T> itemStreamReader(@NonNull ItemStreamSimpleReaderDelegate<T> delegate) {
Objects.requireNonNull(delegate, "ItemStreamReader delegate is null");
return StepScopeItemStreamReader.of(() -> ItemStreamSimpleReaderAdapter.of(delegate));
}

/**
* Create an adapter which adapt {@link ItemStreamReaderDelegate} to {@link ItemStreamReader}
* with {@link StepScope} bound proxy implementation. It creates new instance for every {@link StepScope}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import java.util.Collections;
import java.util.List;

import org.junit.jupiter.api.Test;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemStreamReader;
Expand All @@ -30,6 +33,46 @@

class AdapterFactoryTest {

@Test
void testItemReaderWithFlux() {
// when
ItemStreamFluxReaderDelegate<Integer> delegate = executionContext -> Flux.empty();
ItemStreamReader<Integer> actual = AdapterFactory.itemStreamReader(delegate);

// then
assertThat(actual).isInstanceOf(StepScopeItemStreamReader.class);
}

@Test
void testItemReaderWithIterable() {
// when
ItemStreamIterableReaderDelegate<Integer> delegate = executionContext -> List.of();
ItemStreamReader<Integer> actual = AdapterFactory.itemStreamReader(delegate);

// then
assertThat(actual).isInstanceOf(StepScopeItemStreamReader.class);
}

@Test
void testItemReaderWithIterator() {
// when
ItemStreamIteratorReaderDelegate<Integer> delegate = executionContext -> Collections.emptyIterator();
ItemStreamReader<Integer> actual = AdapterFactory.itemStreamReader(delegate);

// then
assertThat(actual).isInstanceOf(StepScopeItemStreamReader.class);
}

@Test
void testItemReaderWithSimple() {
// when
ItemStreamSimpleReaderDelegate<Integer> delegate = () -> null;
ItemStreamReader<Integer> actual = AdapterFactory.itemStreamReader(delegate);

// then
assertThat(actual).isInstanceOf(StepScopeItemStreamReader.class);
}

@Test
void testItemReader() {
// when
Expand Down Expand Up @@ -64,6 +107,10 @@ void testItemWriter() {
@SuppressWarnings({"ResultOfMethodCallIgnored", "ConstantConditions"})
@Test
void testPassingNull() {
assertThatThrownBy(() -> AdapterFactory.itemStreamReader((ItemStreamFluxReaderDelegate<?>)null));
assertThatThrownBy(() -> AdapterFactory.itemStreamReader((ItemStreamIterableReaderDelegate<?>)null));
assertThatThrownBy(() -> AdapterFactory.itemStreamReader((ItemStreamIteratorReaderDelegate<?>)null));
assertThatThrownBy(() -> AdapterFactory.itemStreamReader((ItemStreamSimpleReaderDelegate<?>)null));
assertThatThrownBy(() -> AdapterFactory.itemStreamReader((ItemStreamReaderDelegate<?>)null));
assertThatThrownBy(() -> AdapterFactory.itemProcessor(null));
assertThatThrownBy(() -> AdapterFactory.itemStreamWriter(null));
Expand Down

0 comments on commit 15527e6

Please sign in to comment.