diff --git a/spring-batch-infrastructure-tests/src/test/java/org/springframework/batch/item/database/JpaItemWriterIntegrationTests.java b/spring-batch-infrastructure-tests/src/test/java/org/springframework/batch/item/database/JpaItemWriterIntegrationTests.java new file mode 100644 index 000000000..cd214500f --- /dev/null +++ b/spring-batch-infrastructure-tests/src/test/java/org/springframework/batch/item/database/JpaItemWriterIntegrationTests.java @@ -0,0 +1,146 @@ +/* + * Copyright 2019 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 + * + * https://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; + +import java.util.Arrays; +import java.util.List; +import javax.persistence.EntityManagerFactory; +import javax.sql.DataSource; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.batch.item.sample.Person; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; +import org.springframework.orm.jpa.JpaTransactionManager; +import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; +import org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager; +import org.springframework.orm.jpa.persistenceunit.PersistenceUnitManager; +import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.jdbc.JdbcTestUtils; +import org.springframework.transaction.PlatformTransactionManager; +import org.springframework.transaction.annotation.Transactional; + +import static org.junit.Assert.assertEquals; + +@RunWith(SpringRunner.class) +@ContextConfiguration(classes = JpaItemWriterIntegrationTests.JpaConfiguration.class) +@Transactional +@DirtiesContext +public class JpaItemWriterIntegrationTests { + + @Autowired + private EntityManagerFactory entityManagerFactory; + + @Autowired + private JdbcTemplate jdbcTemplate; + + @Before + public void init() { + this.jdbcTemplate.update("create table person (id int not null primary key, name varchar(32))"); + } + + @After + public void destroy() { + JdbcTestUtils.dropTables(this.jdbcTemplate, "person"); + } + + @Test + public void testMerge() throws Exception { + // given + JpaItemWriter writer = new JpaItemWriter<>(); + writer.setEntityManagerFactory(this.entityManagerFactory); + writer.afterPropertiesSet(); + List items = Arrays.asList( + new Person(1, "foo"), + new Person(2, "bar")); + + // when + writer.write(items); + + // then + assertEquals(2, JdbcTestUtils.countRowsInTable(this.jdbcTemplate, "person")); + } + + @Test + public void testPersist() throws Exception { + // given + JpaItemWriter writer = new JpaItemWriter<>(); + writer.setEntityManagerFactory(this.entityManagerFactory); + writer.setUsePersist(true); + writer.afterPropertiesSet(); + List items = Arrays.asList( + new Person(1, "foo"), + new Person(2, "bar")); + + // when + writer.write(items); + + // then + assertEquals(2, JdbcTestUtils.countRowsInTable(this.jdbcTemplate, "person")); + } + + @Configuration + public static class JpaConfiguration { + + @Bean + public DataSource dataSource() { + return new EmbeddedDatabaseBuilder() + .setType(EmbeddedDatabaseType.HSQL) + .build(); + } + + @Bean + public JdbcTemplate jdbcTemplate(DataSource dataSource) { + return new JdbcTemplate(dataSource); + } + + @Bean + public PersistenceUnitManager persistenceUnitManager() { + DefaultPersistenceUnitManager persistenceUnitManager = new DefaultPersistenceUnitManager(); + persistenceUnitManager.setDefaultDataSource(dataSource()); + persistenceUnitManager.setPackagesToScan("org.springframework.batch.item.sample"); + persistenceUnitManager.afterPropertiesSet(); + return persistenceUnitManager; + } + + @Bean + public EntityManagerFactory entityManagerFactory() { + LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean(); + factoryBean.setDataSource(dataSource()); + factoryBean.setPersistenceUnitManager(persistenceUnitManager()); + factoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter()); + factoryBean.afterPropertiesSet(); + return factoryBean.getObject(); + } + + @Bean + public PlatformTransactionManager transactionManager() { + return new JpaTransactionManager(entityManagerFactory()); + } + } + +} diff --git a/spring-batch-infrastructure-tests/src/test/java/org/springframework/batch/item/sample/Person.java b/spring-batch-infrastructure-tests/src/test/java/org/springframework/batch/item/sample/Person.java new file mode 100644 index 000000000..70d855611 --- /dev/null +++ b/spring-batch-infrastructure-tests/src/test/java/org/springframework/batch/item/sample/Person.java @@ -0,0 +1,68 @@ +/* + * Copyright 2019 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 + * + * https://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.sample; + +import java.util.Objects; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +@Entity +@Table(name = "person") +public class Person { + + @Id + private int id; + private String name; + + private Person() { + } + + public Person(int id, String name) { + this.id = id; + this.name = name; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Person person = (Person) o; + return id == person.id && + Objects.equals(name, person.name); + } + + @Override + public int hashCode() { + return Objects.hash(id, name); + } +} diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/JpaItemWriterPersistTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/JpaItemWriterPersistTests.java deleted file mode 100644 index 92b16e706..000000000 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/JpaItemWriterPersistTests.java +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Copyright 2006-2008 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; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -import java.util.Arrays; -import java.util.List; - -import javax.persistence.EntityManager; -import javax.persistence.EntityManagerFactory; - -import org.junit.Before; -import org.junit.Test; -import org.springframework.orm.jpa.EntityManagerHolder; -import org.springframework.transaction.support.TransactionSynchronizationManager; - -/** - * @author Chris Cranford - * - */ -public class JpaItemWriterPersistTests { - - EntityManagerFactory emf; - - JpaItemWriter writer; - - @Before - public void setUp() throws Exception { - if (TransactionSynchronizationManager.isSynchronizationActive()) { - TransactionSynchronizationManager.clearSynchronization(); - } - writer = new JpaItemWriter(); - writer.setUsePersist(true); - emf = mock(EntityManagerFactory.class,"emf"); - writer.setEntityManagerFactory(emf); - } - - @Test - public void testAfterPropertiesSet() throws Exception { - writer = new JpaItemWriter(); - try { - writer.afterPropertiesSet(); - fail("Expected IllegalArgumentException"); - } - catch (IllegalArgumentException e) { - // expected - assertTrue("Wrong message for exception: " + e.getMessage(), - e.getMessage().indexOf("EntityManagerFactory") >= 0); - } - } - - @Test - public void testPersist() throws Exception { - EntityManager em = mock(EntityManager.class, "em"); - TransactionSynchronizationManager.bindResource(emf, new EntityManagerHolder(em)); - List items = Arrays.asList(new String[] { "persist1", "persist2" }); - writer.write(items); - TransactionSynchronizationManager.unbindResource(emf); - } - -} diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/JpaItemWriterTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/JpaItemWriterTests.java index 48cca9d2b..032a1b6f1 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/JpaItemWriterTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/JpaItemWriterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2008 the original author or authors. + * Copyright 2006-2019 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. @@ -20,6 +20,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import java.util.Arrays; @@ -36,7 +37,8 @@ import org.springframework.transaction.support.TransactionSynchronizationManager /** * @author Thomas Risberg * @author Will Schipp - * + * @author Chris Cranford + * @author Mahmoud Ben Hassine */ public class JpaItemWriterTests { @@ -84,6 +86,18 @@ public class JpaItemWriterTests { TransactionSynchronizationManager.unbindResource(emf); } + @Test + public void testPersist() throws Exception { + writer.setUsePersist(true); + EntityManager em = mock(EntityManager.class, "em"); + TransactionSynchronizationManager.bindResource(emf, new EntityManagerHolder(em)); + List items = Arrays.asList("persist1", "persist2"); + writer.write(items); + verify(em).persist(items.get(0)); + verify(em).persist(items.get(1)); + TransactionSynchronizationManager.unbindResource(emf); + } + @Test public void testWriteAndFlushWithFailure() throws Exception { final RuntimeException ex = new RuntimeException("ERROR");