diff --git a/spring-batch-plus-sample/single-class-reader-processor-writer-kotlin-sample/src/test/kotlin/com/navercorp/spring/batch/plus/sample/flux/readerprocessor/SampleApplicationTest.kt b/spring-batch-plus-sample/single-class-reader-processor-writer-kotlin-sample/src/test/kotlin/com/navercorp/spring/batch/plus/sample/flux/readerprocessor/SampleApplicationTest.kt new file mode 100644 index 00000000..d00f4482 --- /dev/null +++ b/spring-batch-plus-sample/single-class-reader-processor-writer-kotlin-sample/src/test/kotlin/com/navercorp/spring/batch/plus/sample/flux/readerprocessor/SampleApplicationTest.kt @@ -0,0 +1,46 @@ +/* + * Spring Batch Plus + * + * Copyright 2022-present NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.navercorp.spring.batch.plus.sample.flux.readerprocessor + +import org.junit.jupiter.api.Test +import org.springframework.batch.core.BatchStatus +import org.springframework.batch.core.Job +import org.springframework.batch.core.JobParametersBuilder +import org.springframework.batch.core.launch.JobLauncher +import org.springframework.beans.factory.getBean +import org.springframework.boot.autoconfigure.SpringBootApplication +import org.springframework.boot.runApplication + +@SpringBootApplication +open class SampleApplicationTest { + @Test + fun run() { + val applicationContext = runApplication() + val jobLauncher = applicationContext.getBean() + val job = applicationContext.getBean("testJob") + + val jobParameters = JobParametersBuilder() + .addLong("totalCount", 20L) + .toJobParameters() + val jobExecution = jobLauncher.run(job, jobParameters) + + assert(BatchStatus.COMPLETED == jobExecution.status) + println(jobExecution) + } +} diff --git a/spring-batch-plus-sample/single-class-reader-processor-writer-kotlin-sample/src/test/kotlin/com/navercorp/spring/batch/plus/sample/flux/readerprocessor/SampleTasklet.kt b/spring-batch-plus-sample/single-class-reader-processor-writer-kotlin-sample/src/test/kotlin/com/navercorp/spring/batch/plus/sample/flux/readerprocessor/SampleTasklet.kt new file mode 100644 index 00000000..6d0ec653 --- /dev/null +++ b/spring-batch-plus-sample/single-class-reader-processor-writer-kotlin-sample/src/test/kotlin/com/navercorp/spring/batch/plus/sample/flux/readerprocessor/SampleTasklet.kt @@ -0,0 +1,50 @@ +/* + * Spring Batch Plus + * + * Copyright 2022-present NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.navercorp.spring.batch.plus.sample.flux.readerprocessor + +import com.navercorp.spring.batch.plus.step.adapter.ItemStreamFluxReaderProcessor +import org.springframework.batch.core.configuration.annotation.StepScope +import org.springframework.batch.item.ExecutionContext +import org.springframework.beans.factory.annotation.Value +import org.springframework.stereotype.Component +import reactor.core.publisher.Flux + +@Component +@StepScope +open class SampleTasklet( + @Value("#{jobParameters['totalCount']}") private var totalCount: Long, +) : ItemStreamFluxReaderProcessor { + private var count = 0 + + override fun readFlux(executionContext: ExecutionContext): Flux { + println("totalCount: $totalCount") + return Flux.generate { sink -> + if (count < totalCount) { + sink.next(count) + ++count + } else { + sink.complete() + } + } + } + + override fun process(item: Int): String? { + return "'$item'" + } +} diff --git a/spring-batch-plus-sample/single-class-reader-processor-writer-kotlin-sample/src/test/kotlin/com/navercorp/spring/batch/plus/sample/flux/readerprocessor/TestJobConfig.kt b/spring-batch-plus-sample/single-class-reader-processor-writer-kotlin-sample/src/test/kotlin/com/navercorp/spring/batch/plus/sample/flux/readerprocessor/TestJobConfig.kt new file mode 100644 index 00000000..23dc2f79 --- /dev/null +++ b/spring-batch-plus-sample/single-class-reader-processor-writer-kotlin-sample/src/test/kotlin/com/navercorp/spring/batch/plus/sample/flux/readerprocessor/TestJobConfig.kt @@ -0,0 +1,48 @@ +/* + * Spring Batch Plus + * + * Copyright 2022-present NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.navercorp.spring.batch.plus.sample.flux.readerprocessor + +import com.navercorp.spring.batch.plus.kotlin.configuration.BatchDsl +import com.navercorp.spring.batch.plus.kotlin.step.adapter.asItemProcessor +import com.navercorp.spring.batch.plus.kotlin.step.adapter.asItemStreamReader +import org.springframework.batch.core.Job +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration +import org.springframework.transaction.PlatformTransactionManager + +@Configuration +open class TestJobConfig( + private val batch: BatchDsl, + private val transactionManager: PlatformTransactionManager, +) { + @Bean + open fun testJob( + sampleTasklet: SampleTasklet, + ): Job = batch { + job("testJob") { + step("testStep") { + chunk(3, transactionManager) { + reader(sampleTasklet.asItemStreamReader()) + processor(sampleTasklet.asItemProcessor()) + writer { chunk -> println(chunk.items) } + } + } + } + } +} diff --git a/spring-batch-plus-sample/single-class-reader-processor-writer-kotlin-sample/src/test/kotlin/com/navercorp/spring/batch/plus/sample/iterable/readerprocessor/SampleApplicationTest.kt b/spring-batch-plus-sample/single-class-reader-processor-writer-kotlin-sample/src/test/kotlin/com/navercorp/spring/batch/plus/sample/iterable/readerprocessor/SampleApplicationTest.kt new file mode 100644 index 00000000..e808d9a0 --- /dev/null +++ b/spring-batch-plus-sample/single-class-reader-processor-writer-kotlin-sample/src/test/kotlin/com/navercorp/spring/batch/plus/sample/iterable/readerprocessor/SampleApplicationTest.kt @@ -0,0 +1,46 @@ +/* + * Spring Batch Plus + * + * Copyright 2022-present NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.navercorp.spring.batch.plus.sample.iterable.readerprocessor + +import org.junit.jupiter.api.Test +import org.springframework.batch.core.BatchStatus +import org.springframework.batch.core.Job +import org.springframework.batch.core.JobParametersBuilder +import org.springframework.batch.core.launch.JobLauncher +import org.springframework.beans.factory.getBean +import org.springframework.boot.autoconfigure.SpringBootApplication +import org.springframework.boot.runApplication + +@SpringBootApplication +open class SampleApplicationTest { + @Test + fun run() { + val applicationContext = runApplication() + val jobLauncher = applicationContext.getBean() + val job = applicationContext.getBean("testJob") + + val jobParameters = JobParametersBuilder() + .addLong("totalCount", 20L) + .toJobParameters() + val jobExecution = jobLauncher.run(job, jobParameters) + + assert(BatchStatus.COMPLETED == jobExecution.status) + println(jobExecution) + } +} diff --git a/spring-batch-plus-sample/single-class-reader-processor-writer-kotlin-sample/src/test/kotlin/com/navercorp/spring/batch/plus/sample/iterable/readerprocessor/SampleTasklet.kt b/spring-batch-plus-sample/single-class-reader-processor-writer-kotlin-sample/src/test/kotlin/com/navercorp/spring/batch/plus/sample/iterable/readerprocessor/SampleTasklet.kt new file mode 100644 index 00000000..1ad20bc1 --- /dev/null +++ b/spring-batch-plus-sample/single-class-reader-processor-writer-kotlin-sample/src/test/kotlin/com/navercorp/spring/batch/plus/sample/iterable/readerprocessor/SampleTasklet.kt @@ -0,0 +1,52 @@ +/* + * Spring Batch Plus + * + * Copyright 2022-present NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.navercorp.spring.batch.plus.sample.iterable.readerprocessor + +import com.navercorp.spring.batch.plus.step.adapter.ItemStreamIterableReaderProcessor +import org.springframework.batch.core.configuration.annotation.StepScope +import org.springframework.batch.item.ExecutionContext +import org.springframework.beans.factory.annotation.Value +import org.springframework.stereotype.Component + +@Component +@StepScope +open class SampleTasklet( + @Value("#{jobParameters['totalCount']}") private var totalCount: Long, +) : ItemStreamIterableReaderProcessor { + private var count = 0 + + override fun readIterable(executionContext: ExecutionContext): Iterable { + println("totalCount: $totalCount") + return Iterable { + object : Iterator { + override fun hasNext(): Boolean { + return count < totalCount + } + + override fun next(): Int { + return count++ + } + } + } + } + + override fun process(item: Int): String? { + return "'$item'" + } +} diff --git a/spring-batch-plus-sample/single-class-reader-processor-writer-kotlin-sample/src/test/kotlin/com/navercorp/spring/batch/plus/sample/iterable/readerprocessor/TestJobConfig.kt b/spring-batch-plus-sample/single-class-reader-processor-writer-kotlin-sample/src/test/kotlin/com/navercorp/spring/batch/plus/sample/iterable/readerprocessor/TestJobConfig.kt new file mode 100644 index 00000000..1da5c164 --- /dev/null +++ b/spring-batch-plus-sample/single-class-reader-processor-writer-kotlin-sample/src/test/kotlin/com/navercorp/spring/batch/plus/sample/iterable/readerprocessor/TestJobConfig.kt @@ -0,0 +1,48 @@ +/* + * Spring Batch Plus + * + * Copyright 2022-present NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.navercorp.spring.batch.plus.sample.iterable.readerprocessor + +import com.navercorp.spring.batch.plus.kotlin.configuration.BatchDsl +import com.navercorp.spring.batch.plus.kotlin.step.adapter.asItemProcessor +import com.navercorp.spring.batch.plus.kotlin.step.adapter.asItemStreamReader +import org.springframework.batch.core.Job +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration +import org.springframework.transaction.PlatformTransactionManager + +@Configuration +open class TestJobConfig( + private val batch: BatchDsl, + private val transactionManager: PlatformTransactionManager, +) { + @Bean + open fun testJob( + sampleTasklet: SampleTasklet, + ): Job = batch { + job("testJob") { + step("testStep") { + chunk(3, transactionManager) { + reader(sampleTasklet.asItemStreamReader()) + processor(sampleTasklet.asItemProcessor()) + writer { chunk -> println(chunk.items) } + } + } + } + } +} diff --git a/spring-batch-plus-sample/single-class-reader-processor-writer-kotlin-sample/src/test/kotlin/com/navercorp/spring/batch/plus/sample/iterator/readerprocessor/SampleApplicationTest.kt b/spring-batch-plus-sample/single-class-reader-processor-writer-kotlin-sample/src/test/kotlin/com/navercorp/spring/batch/plus/sample/iterator/readerprocessor/SampleApplicationTest.kt new file mode 100644 index 00000000..cdecc7f8 --- /dev/null +++ b/spring-batch-plus-sample/single-class-reader-processor-writer-kotlin-sample/src/test/kotlin/com/navercorp/spring/batch/plus/sample/iterator/readerprocessor/SampleApplicationTest.kt @@ -0,0 +1,46 @@ +/* + * Spring Batch Plus + * + * Copyright 2022-present NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.navercorp.spring.batch.plus.sample.iterator.readerprocessor + +import org.junit.jupiter.api.Test +import org.springframework.batch.core.BatchStatus +import org.springframework.batch.core.Job +import org.springframework.batch.core.JobParametersBuilder +import org.springframework.batch.core.launch.JobLauncher +import org.springframework.beans.factory.getBean +import org.springframework.boot.autoconfigure.SpringBootApplication +import org.springframework.boot.runApplication + +@SpringBootApplication +open class SampleApplicationTest { + @Test + fun run() { + val applicationContext = runApplication() + val jobLauncher = applicationContext.getBean() + val job = applicationContext.getBean("testJob") + + val jobParameters = JobParametersBuilder() + .addLong("totalCount", 20L) + .toJobParameters() + val jobExecution = jobLauncher.run(job, jobParameters) + + assert(BatchStatus.COMPLETED == jobExecution.status) + println(jobExecution) + } +} diff --git a/spring-batch-plus-sample/single-class-reader-processor-writer-kotlin-sample/src/test/kotlin/com/navercorp/spring/batch/plus/sample/iterator/readerprocessor/SampleTasklet.kt b/spring-batch-plus-sample/single-class-reader-processor-writer-kotlin-sample/src/test/kotlin/com/navercorp/spring/batch/plus/sample/iterator/readerprocessor/SampleTasklet.kt new file mode 100644 index 00000000..54900579 --- /dev/null +++ b/spring-batch-plus-sample/single-class-reader-processor-writer-kotlin-sample/src/test/kotlin/com/navercorp/spring/batch/plus/sample/iterator/readerprocessor/SampleTasklet.kt @@ -0,0 +1,50 @@ +/* + * Spring Batch Plus + * + * Copyright 2022-present NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.navercorp.spring.batch.plus.sample.iterator.readerprocessor + +import com.navercorp.spring.batch.plus.step.adapter.ItemStreamIteratorReaderProcessor +import org.springframework.batch.core.configuration.annotation.StepScope +import org.springframework.batch.item.ExecutionContext +import org.springframework.beans.factory.annotation.Value +import org.springframework.stereotype.Component + +@Component +@StepScope +open class SampleTasklet( + @Value("#{jobParameters['totalCount']}") private var totalCount: Long, +) : ItemStreamIteratorReaderProcessor { + private var count = 0 + + override fun readIterator(executionContext: ExecutionContext): Iterator { + println("totalCount: $totalCount") + return object : Iterator { + override fun hasNext(): Boolean { + return count < totalCount + } + + override fun next(): Int { + return count++ + } + } + } + + override fun process(item: Int): String? { + return "'$item'" + } +} diff --git a/spring-batch-plus-sample/single-class-reader-processor-writer-kotlin-sample/src/test/kotlin/com/navercorp/spring/batch/plus/sample/iterator/readerprocessor/TestJobConfig.kt b/spring-batch-plus-sample/single-class-reader-processor-writer-kotlin-sample/src/test/kotlin/com/navercorp/spring/batch/plus/sample/iterator/readerprocessor/TestJobConfig.kt new file mode 100644 index 00000000..3ed24a0f --- /dev/null +++ b/spring-batch-plus-sample/single-class-reader-processor-writer-kotlin-sample/src/test/kotlin/com/navercorp/spring/batch/plus/sample/iterator/readerprocessor/TestJobConfig.kt @@ -0,0 +1,48 @@ +/* + * Spring Batch Plus + * + * Copyright 2022-present NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.navercorp.spring.batch.plus.sample.iterator.readerprocessor + +import com.navercorp.spring.batch.plus.kotlin.configuration.BatchDsl +import com.navercorp.spring.batch.plus.kotlin.step.adapter.asItemProcessor +import com.navercorp.spring.batch.plus.kotlin.step.adapter.asItemStreamReader +import org.springframework.batch.core.Job +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration +import org.springframework.transaction.PlatformTransactionManager + +@Configuration +open class TestJobConfig( + private val batch: BatchDsl, + private val transactionManager: PlatformTransactionManager, +) { + @Bean + open fun testJob( + sampleTasklet: SampleTasklet, + ): Job = batch { + job("testJob") { + step("testStep") { + chunk(3, transactionManager) { + reader(sampleTasklet.asItemStreamReader()) + processor(sampleTasklet.asItemProcessor()) + writer { chunk -> println(chunk.items) } + } + } + } + } +} diff --git a/spring-batch-plus-sample/single-class-reader-processor-writer-kotlin-sample/src/test/kotlin/com/navercorp/spring/batch/plus/sample/simple/readerprocessor/SampleApplicationTest.kt b/spring-batch-plus-sample/single-class-reader-processor-writer-kotlin-sample/src/test/kotlin/com/navercorp/spring/batch/plus/sample/simple/readerprocessor/SampleApplicationTest.kt new file mode 100644 index 00000000..0f0261fb --- /dev/null +++ b/spring-batch-plus-sample/single-class-reader-processor-writer-kotlin-sample/src/test/kotlin/com/navercorp/spring/batch/plus/sample/simple/readerprocessor/SampleApplicationTest.kt @@ -0,0 +1,46 @@ +/* + * Spring Batch Plus + * + * Copyright 2022-present NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.navercorp.spring.batch.plus.sample.simple.readerprocessor + +import org.junit.jupiter.api.Test +import org.springframework.batch.core.BatchStatus +import org.springframework.batch.core.Job +import org.springframework.batch.core.JobParametersBuilder +import org.springframework.batch.core.launch.JobLauncher +import org.springframework.beans.factory.getBean +import org.springframework.boot.autoconfigure.SpringBootApplication +import org.springframework.boot.runApplication + +@SpringBootApplication +open class SampleApplicationTest { + @Test + fun run() { + val applicationContext = runApplication() + val jobLauncher = applicationContext.getBean() + val job = applicationContext.getBean("testJob") + + val jobParameters = JobParametersBuilder() + .addLong("totalCount", 20L) + .toJobParameters() + val jobExecution = jobLauncher.run(job, jobParameters) + + assert(BatchStatus.COMPLETED == jobExecution.status) + println(jobExecution) + } +} diff --git a/spring-batch-plus-sample/single-class-reader-processor-writer-kotlin-sample/src/test/kotlin/com/navercorp/spring/batch/plus/sample/simple/readerprocessor/SampleTasklet.kt b/spring-batch-plus-sample/single-class-reader-processor-writer-kotlin-sample/src/test/kotlin/com/navercorp/spring/batch/plus/sample/simple/readerprocessor/SampleTasklet.kt new file mode 100644 index 00000000..402f1156 --- /dev/null +++ b/spring-batch-plus-sample/single-class-reader-processor-writer-kotlin-sample/src/test/kotlin/com/navercorp/spring/batch/plus/sample/simple/readerprocessor/SampleTasklet.kt @@ -0,0 +1,44 @@ +/* + * Spring Batch Plus + * + * Copyright 2022-present NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.navercorp.spring.batch.plus.sample.simple.readerprocessor + +import com.navercorp.spring.batch.plus.step.adapter.ItemStreamSimpleReaderProcessor +import org.springframework.batch.core.configuration.annotation.StepScope +import org.springframework.beans.factory.annotation.Value +import org.springframework.stereotype.Component + +@Component +@StepScope +open class SampleTasklet( + @Value("#{jobParameters['totalCount']}") private var totalCount: Long, +) : ItemStreamSimpleReaderProcessor { + private var count = 0 + + override fun read(): Int? { + return if (count < totalCount) { + count++ + } else { + null + } + } + + override fun process(item: Int): String? { + return "'$item'" + } +} diff --git a/spring-batch-plus-sample/single-class-reader-processor-writer-kotlin-sample/src/test/kotlin/com/navercorp/spring/batch/plus/sample/simple/readerprocessor/TestJobConfig.kt b/spring-batch-plus-sample/single-class-reader-processor-writer-kotlin-sample/src/test/kotlin/com/navercorp/spring/batch/plus/sample/simple/readerprocessor/TestJobConfig.kt new file mode 100644 index 00000000..64dc8b94 --- /dev/null +++ b/spring-batch-plus-sample/single-class-reader-processor-writer-kotlin-sample/src/test/kotlin/com/navercorp/spring/batch/plus/sample/simple/readerprocessor/TestJobConfig.kt @@ -0,0 +1,48 @@ +/* + * Spring Batch Plus + * + * Copyright 2022-present NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.navercorp.spring.batch.plus.sample.simple.readerprocessor + +import com.navercorp.spring.batch.plus.kotlin.configuration.BatchDsl +import com.navercorp.spring.batch.plus.kotlin.step.adapter.asItemProcessor +import com.navercorp.spring.batch.plus.kotlin.step.adapter.asItemStreamReader +import org.springframework.batch.core.Job +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration +import org.springframework.transaction.PlatformTransactionManager + +@Configuration +open class TestJobConfig( + private val batch: BatchDsl, + private val transactionManager: PlatformTransactionManager, +) { + @Bean + open fun testJob( + sampleTasklet: SampleTasklet, + ): Job = batch { + job("testJob") { + step("testStep") { + chunk(3, transactionManager) { + reader(sampleTasklet.asItemStreamReader()) + processor(sampleTasklet.asItemProcessor()) + writer { chunk -> println(chunk.items) } + } + } + } + } +} diff --git a/spring-batch-plus-sample/single-class-reader-processor-writer-kotlin-sample/src/test/kotlin/com/navercorp/spring/batch/plus/sample/simple/readerprocessorwriter/SampleApplicationTest.kt b/spring-batch-plus-sample/single-class-reader-processor-writer-kotlin-sample/src/test/kotlin/com/navercorp/spring/batch/plus/sample/simple/readerprocessorwriter/SampleApplicationTest.kt index 947b554e..2fbbad97 100644 --- a/spring-batch-plus-sample/single-class-reader-processor-writer-kotlin-sample/src/test/kotlin/com/navercorp/spring/batch/plus/sample/simple/readerprocessorwriter/SampleApplicationTest.kt +++ b/spring-batch-plus-sample/single-class-reader-processor-writer-kotlin-sample/src/test/kotlin/com/navercorp/spring/batch/plus/sample/simple/readerprocessorwriter/SampleApplicationTest.kt @@ -31,8 +31,7 @@ import org.springframework.boot.runApplication open class SampleApplicationTest { @Test fun run() { - val applicationContext = - runApplication() + val applicationContext = runApplication() val jobLauncher = applicationContext.getBean() val job = applicationContext.getBean("testJob") diff --git a/spring-batch-plus-sample/single-class-reader-processor-writer-sample/src/test/java/com/navercorp/spring/batch/plus/sample/flux/readerprocessor/SampleApplicationTest.java b/spring-batch-plus-sample/single-class-reader-processor-writer-sample/src/test/java/com/navercorp/spring/batch/plus/sample/flux/readerprocessor/SampleApplicationTest.java new file mode 100644 index 00000000..d9eeba3d --- /dev/null +++ b/spring-batch-plus-sample/single-class-reader-processor-writer-sample/src/test/java/com/navercorp/spring/batch/plus/sample/flux/readerprocessor/SampleApplicationTest.java @@ -0,0 +1,48 @@ +/* + * Spring Batch Plus + * + * Copyright 2022-present NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.navercorp.spring.batch.plus.sample.flux.readerprocessor; + +import org.junit.jupiter.api.Test; +import org.springframework.batch.core.BatchStatus; +import org.springframework.batch.core.Job; +import org.springframework.batch.core.JobExecution; +import org.springframework.batch.core.JobParameters; +import org.springframework.batch.core.JobParametersBuilder; +import org.springframework.batch.core.launch.JobLauncher; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.ApplicationContext; + +@SpringBootApplication +public class SampleApplicationTest { + @Test + void run() throws Exception { + ApplicationContext applicationContext = SpringApplication.run(SampleApplicationTest.class); + JobLauncher jobLauncher = applicationContext.getBean(JobLauncher.class); + Job job = applicationContext.getBean("testJob", Job.class); + + JobParameters jobParameters = new JobParametersBuilder() + .addLong("totalCount", 20L) + .toJobParameters(); + JobExecution jobExecution = jobLauncher.run(job, jobParameters); + + assert BatchStatus.COMPLETED.equals(jobExecution.getStatus()); + System.out.printf("%s%n", jobExecution); + } +} diff --git a/spring-batch-plus-sample/single-class-reader-processor-writer-sample/src/test/java/com/navercorp/spring/batch/plus/sample/flux/readerprocessor/SampleTasklet.java b/spring-batch-plus-sample/single-class-reader-processor-writer-sample/src/test/java/com/navercorp/spring/batch/plus/sample/flux/readerprocessor/SampleTasklet.java new file mode 100644 index 00000000..abeb765b --- /dev/null +++ b/spring-batch-plus-sample/single-class-reader-processor-writer-sample/src/test/java/com/navercorp/spring/batch/plus/sample/flux/readerprocessor/SampleTasklet.java @@ -0,0 +1,58 @@ +/* + * Spring Batch Plus + * + * Copyright 2022-present NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.navercorp.spring.batch.plus.sample.flux.readerprocessor; + +import org.springframework.batch.core.configuration.annotation.StepScope; +import org.springframework.batch.item.ExecutionContext; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.lang.NonNull; +import org.springframework.stereotype.Component; + +import reactor.core.publisher.Flux; + +import com.navercorp.spring.batch.plus.step.adapter.ItemStreamFluxReaderProcessor; + +@Component +@StepScope +class SampleTasklet implements ItemStreamFluxReaderProcessor { + + @Value("#{jobParameters['totalCount']}") + private long totalCount; + + private int count = 0; + + @NonNull + @Override + public Flux readFlux(@NonNull ExecutionContext executionContext) { + System.out.println("totalCount: " + totalCount); + return Flux.generate(sink -> { + if (count < totalCount) { + sink.next(count); + ++count; + } else { + sink.complete(); + } + }); + } + + @Override + public String process(@NonNull Integer item) { + return "'" + item.toString() + "'"; + } +} diff --git a/spring-batch-plus-sample/single-class-reader-processor-writer-sample/src/test/java/com/navercorp/spring/batch/plus/sample/flux/readerprocessor/TestJobConfig.java b/spring-batch-plus-sample/single-class-reader-processor-writer-sample/src/test/java/com/navercorp/spring/batch/plus/sample/flux/readerprocessor/TestJobConfig.java new file mode 100644 index 00000000..3b1d6c1b --- /dev/null +++ b/spring-batch-plus-sample/single-class-reader-processor-writer-sample/src/test/java/com/navercorp/spring/batch/plus/sample/flux/readerprocessor/TestJobConfig.java @@ -0,0 +1,52 @@ +/* + * Spring Batch Plus + * + * Copyright 2022-present NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.navercorp.spring.batch.plus.sample.flux.readerprocessor; + +import static com.navercorp.spring.batch.plus.step.adapter.AdapterFactory.itemProcessor; +import static com.navercorp.spring.batch.plus.step.adapter.AdapterFactory.itemStreamReader; + +import org.springframework.batch.core.Job; +import org.springframework.batch.core.job.builder.JobBuilder; +import org.springframework.batch.core.repository.JobRepository; +import org.springframework.batch.core.step.builder.StepBuilder; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.transaction.PlatformTransactionManager; + +@Configuration +public class TestJobConfig { + + @Bean + public Job testJob( + SampleTasklet sampleTasklet, + JobRepository jobRepository, + PlatformTransactionManager transactionManager + ) { + return new JobBuilder("testJob", jobRepository) + .start( + new StepBuilder("testStep", jobRepository) + .chunk(3, transactionManager) + .reader(itemStreamReader(sampleTasklet)) + .processor(itemProcessor(sampleTasklet)) + .writer(chunk -> System.out.println(chunk.getItems())) + .build() + ) + .build(); + } +} diff --git a/spring-batch-plus-sample/single-class-reader-processor-writer-sample/src/test/java/com/navercorp/spring/batch/plus/sample/iterable/readerprocessor/SampleApplicationTest.java b/spring-batch-plus-sample/single-class-reader-processor-writer-sample/src/test/java/com/navercorp/spring/batch/plus/sample/iterable/readerprocessor/SampleApplicationTest.java new file mode 100644 index 00000000..874cace3 --- /dev/null +++ b/spring-batch-plus-sample/single-class-reader-processor-writer-sample/src/test/java/com/navercorp/spring/batch/plus/sample/iterable/readerprocessor/SampleApplicationTest.java @@ -0,0 +1,48 @@ +/* + * Spring Batch Plus + * + * Copyright 2022-present NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.navercorp.spring.batch.plus.sample.iterable.readerprocessor; + +import org.junit.jupiter.api.Test; +import org.springframework.batch.core.BatchStatus; +import org.springframework.batch.core.Job; +import org.springframework.batch.core.JobExecution; +import org.springframework.batch.core.JobParameters; +import org.springframework.batch.core.JobParametersBuilder; +import org.springframework.batch.core.launch.JobLauncher; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.ApplicationContext; + +@SpringBootApplication +public class SampleApplicationTest { + @Test + void run() throws Exception { + ApplicationContext applicationContext = SpringApplication.run(SampleApplicationTest.class); + JobLauncher jobLauncher = applicationContext.getBean(JobLauncher.class); + Job job = applicationContext.getBean("testJob", Job.class); + + JobParameters jobParameters = new JobParametersBuilder() + .addLong("totalCount", 20L) + .toJobParameters(); + JobExecution jobExecution = jobLauncher.run(job, jobParameters); + + assert BatchStatus.COMPLETED.equals(jobExecution.getStatus()); + System.out.printf("%s%n", jobExecution); + } +} diff --git a/spring-batch-plus-sample/single-class-reader-processor-writer-sample/src/test/java/com/navercorp/spring/batch/plus/sample/iterable/readerprocessor/SampleTasklet.java b/spring-batch-plus-sample/single-class-reader-processor-writer-sample/src/test/java/com/navercorp/spring/batch/plus/sample/iterable/readerprocessor/SampleTasklet.java new file mode 100644 index 00000000..53028024 --- /dev/null +++ b/spring-batch-plus-sample/single-class-reader-processor-writer-sample/src/test/java/com/navercorp/spring/batch/plus/sample/iterable/readerprocessor/SampleTasklet.java @@ -0,0 +1,61 @@ +/* + * Spring Batch Plus + * + * Copyright 2022-present NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.navercorp.spring.batch.plus.sample.iterable.readerprocessor; + +import java.util.Iterator; + +import org.springframework.batch.core.configuration.annotation.StepScope; +import org.springframework.batch.item.ExecutionContext; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.lang.NonNull; +import org.springframework.stereotype.Component; + +import com.navercorp.spring.batch.plus.step.adapter.ItemStreamIterableReaderProcessor; + +@Component +@StepScope +class SampleTasklet implements ItemStreamIterableReaderProcessor { + + @Value("#{jobParameters['totalCount']}") + private long totalCount; + + private int count = 0; + + @NonNull + @Override + public Iterable readIterable(@NonNull ExecutionContext executionContext) { + System.out.println("totalCount: " + totalCount); + return () -> new Iterator<>() { + @Override + public boolean hasNext() { + return count < totalCount; + } + + @Override + public Integer next() { + return count++; + } + }; + } + + @Override + public String process(@NonNull Integer item) { + return "'" + item.toString() + "'"; + } +} diff --git a/spring-batch-plus-sample/single-class-reader-processor-writer-sample/src/test/java/com/navercorp/spring/batch/plus/sample/iterable/readerprocessor/TestJobConfig.java b/spring-batch-plus-sample/single-class-reader-processor-writer-sample/src/test/java/com/navercorp/spring/batch/plus/sample/iterable/readerprocessor/TestJobConfig.java new file mode 100644 index 00000000..7ff6e342 --- /dev/null +++ b/spring-batch-plus-sample/single-class-reader-processor-writer-sample/src/test/java/com/navercorp/spring/batch/plus/sample/iterable/readerprocessor/TestJobConfig.java @@ -0,0 +1,52 @@ +/* + * Spring Batch Plus + * + * Copyright 2022-present NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.navercorp.spring.batch.plus.sample.iterable.readerprocessor; + +import static com.navercorp.spring.batch.plus.step.adapter.AdapterFactory.itemProcessor; +import static com.navercorp.spring.batch.plus.step.adapter.AdapterFactory.itemStreamReader; + +import org.springframework.batch.core.Job; +import org.springframework.batch.core.job.builder.JobBuilder; +import org.springframework.batch.core.repository.JobRepository; +import org.springframework.batch.core.step.builder.StepBuilder; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.transaction.PlatformTransactionManager; + +@Configuration +public class TestJobConfig { + + @Bean + public Job testJob( + SampleTasklet sampleTasklet, + JobRepository jobRepository, + PlatformTransactionManager transactionManager + ) { + return new JobBuilder("testJob", jobRepository) + .start( + new StepBuilder("testStep", jobRepository) + .chunk(3, transactionManager) + .reader(itemStreamReader(sampleTasklet)) + .processor(itemProcessor(sampleTasklet)) + .writer(chunk -> System.out.println(chunk.getItems())) + .build() + ) + .build(); + } +} diff --git a/spring-batch-plus-sample/single-class-reader-processor-writer-sample/src/test/java/com/navercorp/spring/batch/plus/sample/iterator/readerprocessor/SampleApplicationTest.java b/spring-batch-plus-sample/single-class-reader-processor-writer-sample/src/test/java/com/navercorp/spring/batch/plus/sample/iterator/readerprocessor/SampleApplicationTest.java new file mode 100644 index 00000000..7f7a57c2 --- /dev/null +++ b/spring-batch-plus-sample/single-class-reader-processor-writer-sample/src/test/java/com/navercorp/spring/batch/plus/sample/iterator/readerprocessor/SampleApplicationTest.java @@ -0,0 +1,48 @@ +/* + * Spring Batch Plus + * + * Copyright 2022-present NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.navercorp.spring.batch.plus.sample.iterator.readerprocessor; + +import org.junit.jupiter.api.Test; +import org.springframework.batch.core.BatchStatus; +import org.springframework.batch.core.Job; +import org.springframework.batch.core.JobExecution; +import org.springframework.batch.core.JobParameters; +import org.springframework.batch.core.JobParametersBuilder; +import org.springframework.batch.core.launch.JobLauncher; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.ApplicationContext; + +@SpringBootApplication +public class SampleApplicationTest { + @Test + void run() throws Exception { + ApplicationContext applicationContext = SpringApplication.run(SampleApplicationTest.class); + JobLauncher jobLauncher = applicationContext.getBean(JobLauncher.class); + Job job = applicationContext.getBean("testJob", Job.class); + + JobParameters jobParameters = new JobParametersBuilder() + .addLong("totalCount", 20L) + .toJobParameters(); + JobExecution jobExecution = jobLauncher.run(job, jobParameters); + + assert BatchStatus.COMPLETED.equals(jobExecution.getStatus()); + System.out.printf("%s%n", jobExecution); + } +} diff --git a/spring-batch-plus-sample/single-class-reader-processor-writer-sample/src/test/java/com/navercorp/spring/batch/plus/sample/iterator/readerprocessor/SampleTasklet.java b/spring-batch-plus-sample/single-class-reader-processor-writer-sample/src/test/java/com/navercorp/spring/batch/plus/sample/iterator/readerprocessor/SampleTasklet.java new file mode 100644 index 00000000..db14fdbd --- /dev/null +++ b/spring-batch-plus-sample/single-class-reader-processor-writer-sample/src/test/java/com/navercorp/spring/batch/plus/sample/iterator/readerprocessor/SampleTasklet.java @@ -0,0 +1,61 @@ +/* + * Spring Batch Plus + * + * Copyright 2022-present NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.navercorp.spring.batch.plus.sample.iterator.readerprocessor; + +import java.util.Iterator; + +import org.springframework.batch.core.configuration.annotation.StepScope; +import org.springframework.batch.item.ExecutionContext; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.lang.NonNull; +import org.springframework.stereotype.Component; + +import com.navercorp.spring.batch.plus.step.adapter.ItemStreamIteratorReaderProcessor; + +@Component +@StepScope +class SampleTasklet implements ItemStreamIteratorReaderProcessor { + + @Value("#{jobParameters['totalCount']}") + private long totalCount; + + private int count = 0; + + @NonNull + @Override + public Iterator readIterator(@NonNull ExecutionContext executionContext) { + System.out.println("totalCount: " + totalCount); + return new Iterator<>() { + @Override + public boolean hasNext() { + return count < totalCount; + } + + @Override + public Integer next() { + return count++; + } + }; + } + + @Override + public String process(@NonNull Integer item) { + return "'" + item.toString() + "'"; + } +} diff --git a/spring-batch-plus-sample/single-class-reader-processor-writer-sample/src/test/java/com/navercorp/spring/batch/plus/sample/iterator/readerprocessor/TestJobConfig.java b/spring-batch-plus-sample/single-class-reader-processor-writer-sample/src/test/java/com/navercorp/spring/batch/plus/sample/iterator/readerprocessor/TestJobConfig.java new file mode 100644 index 00000000..13aa896e --- /dev/null +++ b/spring-batch-plus-sample/single-class-reader-processor-writer-sample/src/test/java/com/navercorp/spring/batch/plus/sample/iterator/readerprocessor/TestJobConfig.java @@ -0,0 +1,52 @@ +/* + * Spring Batch Plus + * + * Copyright 2022-present NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.navercorp.spring.batch.plus.sample.iterator.readerprocessor; + +import static com.navercorp.spring.batch.plus.step.adapter.AdapterFactory.itemProcessor; +import static com.navercorp.spring.batch.plus.step.adapter.AdapterFactory.itemStreamReader; + +import org.springframework.batch.core.Job; +import org.springframework.batch.core.job.builder.JobBuilder; +import org.springframework.batch.core.repository.JobRepository; +import org.springframework.batch.core.step.builder.StepBuilder; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.transaction.PlatformTransactionManager; + +@Configuration +public class TestJobConfig { + + @Bean + public Job testJob( + SampleTasklet sampleTasklet, + JobRepository jobRepository, + PlatformTransactionManager transactionManager + ) { + return new JobBuilder("testJob", jobRepository) + .start( + new StepBuilder("testStep", jobRepository) + .chunk(3, transactionManager) + .reader(itemStreamReader(sampleTasklet)) + .processor(itemProcessor(sampleTasklet)) + .writer(chunk -> System.out.println(chunk.getItems())) + .build() + ) + .build(); + } +} diff --git a/spring-batch-plus-sample/single-class-reader-processor-writer-sample/src/test/java/com/navercorp/spring/batch/plus/sample/simple/readerprocessor/SampleApplicationTest.java b/spring-batch-plus-sample/single-class-reader-processor-writer-sample/src/test/java/com/navercorp/spring/batch/plus/sample/simple/readerprocessor/SampleApplicationTest.java new file mode 100644 index 00000000..ce39cd1c --- /dev/null +++ b/spring-batch-plus-sample/single-class-reader-processor-writer-sample/src/test/java/com/navercorp/spring/batch/plus/sample/simple/readerprocessor/SampleApplicationTest.java @@ -0,0 +1,48 @@ +/* + * Spring Batch Plus + * + * Copyright 2022-present NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.navercorp.spring.batch.plus.sample.simple.readerprocessor; + +import org.junit.jupiter.api.Test; +import org.springframework.batch.core.BatchStatus; +import org.springframework.batch.core.Job; +import org.springframework.batch.core.JobExecution; +import org.springframework.batch.core.JobParameters; +import org.springframework.batch.core.JobParametersBuilder; +import org.springframework.batch.core.launch.JobLauncher; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.ApplicationContext; + +@SpringBootApplication +public class SampleApplicationTest { + @Test + void run() throws Exception { + ApplicationContext applicationContext = SpringApplication.run(SampleApplicationTest.class); + JobLauncher jobLauncher = applicationContext.getBean(JobLauncher.class); + Job job = applicationContext.getBean("testJob", Job.class); + + JobParameters jobParameters = new JobParametersBuilder() + .addLong("totalCount", 20L) + .toJobParameters(); + JobExecution jobExecution = jobLauncher.run(job, jobParameters); + + assert BatchStatus.COMPLETED.equals(jobExecution.getStatus()); + System.out.printf("%s%n", jobExecution); + } +} diff --git a/spring-batch-plus-sample/single-class-reader-processor-writer-sample/src/test/java/com/navercorp/spring/batch/plus/sample/simple/readerprocessor/SampleTasklet.java b/spring-batch-plus-sample/single-class-reader-processor-writer-sample/src/test/java/com/navercorp/spring/batch/plus/sample/simple/readerprocessor/SampleTasklet.java new file mode 100644 index 00000000..c91c95e1 --- /dev/null +++ b/spring-batch-plus-sample/single-class-reader-processor-writer-sample/src/test/java/com/navercorp/spring/batch/plus/sample/simple/readerprocessor/SampleTasklet.java @@ -0,0 +1,50 @@ +/* + * Spring Batch Plus + * + * Copyright 2022-present NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.navercorp.spring.batch.plus.sample.simple.readerprocessor; + +import org.springframework.batch.core.configuration.annotation.StepScope; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.lang.NonNull; +import org.springframework.stereotype.Component; + +import com.navercorp.spring.batch.plus.step.adapter.ItemStreamSimpleReaderProcessor; + +@Component +@StepScope +class SampleTasklet implements ItemStreamSimpleReaderProcessor { + + @Value("#{jobParameters['totalCount']}") + private long totalCount; + + private int count = 0; + + @Override + public Integer read() { + if (count < totalCount) { + return count++; + } else { + return null; + } + } + + @Override + public String process(@NonNull Integer item) { + return "'" + item.toString() + "'"; + } +} diff --git a/spring-batch-plus-sample/single-class-reader-processor-writer-sample/src/test/java/com/navercorp/spring/batch/plus/sample/simple/readerprocessor/TestJobConfig.java b/spring-batch-plus-sample/single-class-reader-processor-writer-sample/src/test/java/com/navercorp/spring/batch/plus/sample/simple/readerprocessor/TestJobConfig.java new file mode 100644 index 00000000..716ad77b --- /dev/null +++ b/spring-batch-plus-sample/single-class-reader-processor-writer-sample/src/test/java/com/navercorp/spring/batch/plus/sample/simple/readerprocessor/TestJobConfig.java @@ -0,0 +1,52 @@ +/* + * Spring Batch Plus + * + * Copyright 2022-present NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.navercorp.spring.batch.plus.sample.simple.readerprocessor; + +import static com.navercorp.spring.batch.plus.step.adapter.AdapterFactory.itemProcessor; +import static com.navercorp.spring.batch.plus.step.adapter.AdapterFactory.itemStreamReader; + +import org.springframework.batch.core.Job; +import org.springframework.batch.core.job.builder.JobBuilder; +import org.springframework.batch.core.repository.JobRepository; +import org.springframework.batch.core.step.builder.StepBuilder; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.transaction.PlatformTransactionManager; + +@Configuration +public class TestJobConfig { + + @Bean + public Job testJob( + SampleTasklet sampleTasklet, + JobRepository jobRepository, + PlatformTransactionManager transactionManager + ) { + return new JobBuilder("testJob", jobRepository) + .start( + new StepBuilder("testStep", jobRepository) + .chunk(3, transactionManager) + .reader(itemStreamReader(sampleTasklet)) + .processor(itemProcessor(sampleTasklet)) + .writer(chunk -> System.out.println(chunk.getItems())) + .build() + ) + .build(); + } +}