* Add query creation test in JpaNamedQueryProviderTests
* Make JpaPagingItemReaderNamedQueryIntegrationTests consistent
  with JpaPagingItemReaderNativeQueryIntegrationTests
This commit is contained in:
Mahmoud Ben Hassine
2020-06-26 13:51:35 +02:00
parent 3b58fecfe4
commit 4e0d6a982b
5 changed files with 65 additions and 24 deletions

View File

@@ -39,7 +39,7 @@ public class JpaNamedQueryProvider<E> extends AbstractJpaQueryProvider {
@Override
public Query createQuery() {
return getEntityManager().createNamedQuery(namedQuery, entityClass);
return getEntityManager().createNamedQuery(this.namedQuery, this.entityClass);
}
/**
@@ -58,7 +58,7 @@ public class JpaNamedQueryProvider<E> extends AbstractJpaQueryProvider {
@Override
public void afterPropertiesSet() throws Exception {
Assert.isTrue(StringUtils.hasText(namedQuery), "Named query cannot be empty");
Assert.notNull(entityClass, "Entity class cannot be NULL");
Assert.isTrue(StringUtils.hasText(this.namedQuery), "Named query cannot be empty");
Assert.notNull(this.entityClass, "Entity class cannot be NULL");
}
}

View File

@@ -17,41 +17,45 @@ package org.springframework.batch.item.database;
import javax.persistence.EntityManagerFactory;
import org.springframework.batch.item.ItemReader;
import org.junit.runner.RunWith;
import org.springframework.batch.item.database.orm.JpaNamedQueryProvider;
import org.springframework.batch.item.sample.Foo;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* Integration Test for {@link JpaPagingItemReader} and {@link JpaNamedQueryProvider}.
*
* @author Parikshit Dutta
* @author Mahmoud Ben Hassine
*/
public class JpaPagingItemReaderNamedQueryIntegrationTests
extends AbstractGenericDataSourceItemReaderIntegrationTests {
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"JpaPagingItemReaderParameterTests-context.xml"})
public class JpaPagingItemReaderNamedQueryIntegrationTests extends AbstractPagingItemReaderParameterTests {
@Autowired
private EntityManagerFactory entityManagerFactory;
@Override
protected ItemReader<Foo> createItemReader() throws Exception {
LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
factoryBean.setDataSource(dataSource);
factoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
factoryBean.setPersistenceUnitName("bar");
factoryBean.afterPropertiesSet();
protected AbstractPagingItemReader<Foo> getItemReader() throws Exception {
EntityManagerFactory entityManagerFactory = factoryBean.getObject();
String namedQuery = "foosStartingFrom2";
JpaPagingItemReader<Foo> reader = new JpaPagingItemReader<>();
//creating a named query provider as it would be created in configuration
JpaNamedQueryProvider<Foo> jpaNamedQueryProvider = new JpaNamedQueryProvider<>();
jpaNamedQueryProvider.setNamedQuery("allFoos");
jpaNamedQueryProvider.setNamedQuery(namedQuery);
jpaNamedQueryProvider.setEntityClass(Foo.class);
jpaNamedQueryProvider.afterPropertiesSet();
JpaPagingItemReader<Foo> inputSource = new JpaPagingItemReader<>();
inputSource.setEntityManagerFactory(entityManagerFactory);
inputSource.setQueryProvider(jpaNamedQueryProvider);
inputSource.afterPropertiesSet();
inputSource.setSaveState(true);
reader.setEntityManagerFactory(entityManagerFactory);
reader.setQueryProvider(jpaNamedQueryProvider);
reader.afterPropertiesSet();
reader.setSaveState(true);
return inputSource;
return reader;
}
}

View File

@@ -52,6 +52,7 @@ import static org.junit.Assert.fail;
/**
* @author Michael Minella
* @author Parikshit Dutta
* @author Mahmoud Ben Hassine
*/
public class JpaPagingItemReaderBuilderTests {
@@ -173,7 +174,7 @@ public class JpaPagingItemReaderBuilderTests {
}
@Test
public void testConfigurationQueryProvider() throws Exception {
public void testConfigurationNativeQueryProvider() throws Exception {
JpaNativeQueryProvider<Foo> provider = new JpaNativeQueryProvider<>();
provider.setEntityClass(Foo.class);

View File

@@ -15,15 +15,26 @@
*/
package org.springframework.batch.item.database.orm;
import static org.junit.Assert.*;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.persistence.TypedQuery;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.batch.item.sample.Foo;
import org.springframework.util.Assert;
/**
* Test for {@link JpaNamedQueryProvider}s.
*
* @author Parikshit Dutta
* @author Mahmoud Ben Hassine
*/
public class JpaNamedQueryProviderTests {
@@ -52,4 +63,25 @@ public class JpaNamedQueryProviderTests {
assertEquals("Entity class cannot be NULL", exception.getMessage());
}
}
@Test
public void testNamedQueryCreation() throws Exception {
// given
String namedQuery = "allFoos";
TypedQuery<Foo> query = mock(TypedQuery.class);
EntityManager entityManager = Mockito.mock(EntityManager.class);
when(entityManager.createNamedQuery(namedQuery, Foo.class)).thenReturn(query);
JpaNamedQueryProvider<Foo> jpaNamedQueryProvider = new JpaNamedQueryProvider<>();
jpaNamedQueryProvider.setEntityManager(entityManager);
jpaNamedQueryProvider.setEntityClass(Foo.class);
jpaNamedQueryProvider.setNamedQuery(namedQuery);
jpaNamedQueryProvider.afterPropertiesSet();
// when
Query result = jpaNamedQueryProvider.createQuery();
// then
Assert.notNull(result, "Result query must not be null");
verify(entityManager).createNamedQuery(namedQuery, Foo.class);
}
}

View File

@@ -15,4 +15,8 @@
<query name="allFoos">
from Foo
</query>
<query name="foosStartingFrom2">
from Foo where value >= 2
</query>
</hibernate-mapping>