diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/builder/JpaItemWriterBuilder.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/builder/JpaItemWriterBuilder.java new file mode 100644 index 0000000000..dab6b2a8a1 --- /dev/null +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/builder/JpaItemWriterBuilder.java @@ -0,0 +1,61 @@ +/* + * Copyright 2018 the original author or authors. + * + * 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 org.springframework.batch.item.database.builder; + +import javax.persistence.EntityManagerFactory; + +import org.springframework.batch.item.database.JpaItemWriter; +import org.springframework.util.Assert; + +/** + * A builder for the {@link JpaItemWriter}. + * + * @author Mahmoud Ben Hassine + * @since 4.1 + * @see JpaItemWriter + */ +public class JpaItemWriterBuilder { + + private EntityManagerFactory entityManagerFactory; + + /** + * The JPA {@link EntityManagerFactory} to obtain an entity manager from. Required. + * + * @param entityManagerFactory the {@link EntityManagerFactory} + * @return this instance for method chaining + * @see JpaItemWriter#setEntityManagerFactory(EntityManagerFactory) + */ + public JpaItemWriterBuilder entityManagerFactory(EntityManagerFactory entityManagerFactory) { + this.entityManagerFactory = entityManagerFactory; + + return this; + } + + /** + * Returns a fully built {@link JpaItemWriter}. + * + * @return the writer + */ + public JpaItemWriter build() { + Assert.state(this.entityManagerFactory != null, + "EntityManagerFactory must be provided"); + + JpaItemWriter writer = new JpaItemWriter<>(); + writer.setEntityManagerFactory(this.entityManagerFactory); + + return writer; + } +} diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/builder/JpaItemWriterBuilderTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/builder/JpaItemWriterBuilderTests.java new file mode 100644 index 0000000000..b8e957073b --- /dev/null +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/builder/JpaItemWriterBuilderTests.java @@ -0,0 +1,87 @@ +/* + * Copyright 2018 the original author or authors. + * + * 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 org.springframework.batch.item.database.builder; + +import java.util.Arrays; +import java.util.List; +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import org.springframework.batch.item.database.JpaItemWriter; +import org.springframework.orm.jpa.EntityManagerHolder; +import org.springframework.transaction.support.TransactionSynchronizationManager; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; +import static org.mockito.Mockito.verify; + +/** + * @author Mahmoud Ben Hassine + */ +public class JpaItemWriterBuilderTests { + + @Mock + private EntityManagerFactory entityManagerFactory; + + @Mock + private EntityManager entityManager; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + TransactionSynchronizationManager.bindResource(this.entityManagerFactory, + new EntityManagerHolder(this.entityManager)); + } + + @After + public void tearDown() { + TransactionSynchronizationManager.unbindResource(this.entityManagerFactory); + } + + @Test + public void testConfiguration() throws Exception { + JpaItemWriter itemWriter = new JpaItemWriterBuilder() + .entityManagerFactory(this.entityManagerFactory) + .build(); + + itemWriter.afterPropertiesSet(); + + List items = Arrays.asList("foo", "bar"); + + itemWriter.write(items); + + verify(this.entityManager).merge(items.get(0)); + verify(this.entityManager).merge(items.get(1)); + } + + @Test + public void testValidation() { + try { + new JpaItemWriterBuilder() + .build(); + fail("Should fail if no EntityManagerFactory is provided"); + } + catch (IllegalStateException ise) { + assertEquals("Incorrect message", "EntityManagerFactory must be provided", ise.getMessage()); + } + } +}