@ -24,24 +24,26 @@ import lombok.Data;
import javax.sql.DataSource ;
import javax.sql.DataSource ;
import org.junit.ClassRule ;
import org.junit.Rule ;
import org.junit.Test ;
import org.junit.Test ;
import org.junit.runner.RunWith ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.context.ApplicationEventPublisher ;
import org.springframework.context.ApplicationEventPublisher ;
import org.springframework.context.annotation.Bean ;
import org.springframework.context.annotation.Bean ;
import org.springframework.context.annotation.ComponentScan ;
import org.springframework.context.annotation.Configuration ;
import org.springframework.context.annotation.Configuration ;
import org.springframework.data.annotation.Id ;
import org.springframework.data.annotation.Id ;
import org.springframework.data.jdbc.repository.JdbcRepositoryIntegrationTests.TestConfiguration ;
import org.springframework.data.jdbc.repository.JdbcRepositoryIntegrationTests.TestConfiguration ;
import org.springframework.data.jdbc.repository.support.JdbcRepositoryFactory ;
import org.springframework.data.jdbc.repository.support.JdbcRepositoryFactory ;
import org.springframework.data.repository.CrudRepository ;
import org.springframework.data.repository.CrudRepository ;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSourc e ;
import org.springframework.jdbc.core.JdbcTemplat e ;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate ;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate ;
import org.springframework.jdbc.datasource.DataSourceTransactionManager ;
import org.springframework.jdbc.datasource.DataSourceTransactionManager ;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase ;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase ;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder ;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType ;
import org.springframework.test.context.ContextConfiguration ;
import org.springframework.test.context.ContextConfiguration ;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner ;
import org.springframework.test.context.junit4.rules.SpringClassRule ;
import org.springframework.test.context.junit4.rules.SpringMethodRule ;
import org.springframework.test.jdbc.JdbcTestUtils ;
import org.springframework.transaction.PlatformTransactionManager ;
import org.springframework.transaction.PlatformTransactionManager ;
import org.springframework.transaction.annotation.Transactional ;
import org.springframework.transaction.annotation.Transactional ;
@ -50,26 +52,40 @@ import org.springframework.transaction.annotation.Transactional;
*
*
* @author Jens Schauder
* @author Jens Schauder
* /
* /
@RunWith ( SpringJUnit4ClassRunner . class )
@ContextConfiguration ( classes = TestConfiguration . class )
@ContextConfiguration ( classes = TestConfiguration . class )
@Transactional
@Transactional
public class JdbcRepositoryIntegrationTests {
public class JdbcRepositoryIntegrationTests {
@Autowired NamedParameterJdbcTemplate template ;
@ClassRule public static final SpringClassRule classRule = new SpringClassRule ( ) ;
@Rule public SpringMethodRule methodRule = new SpringMethodRule ( ) ;
@Autowired NamedParameterJdbcTemplate template ;
@Autowired DummyEntityRepository repository ;
@Autowired DummyEntityRepository repository ;
private DummyEntity entity = createDummyEntity ( ) ;
private DummyEntity entity = createDummyEntity ( ) ;
private static DummyEntityRepository createRepository ( EmbeddedDatabase db ) {
return new JdbcRepositoryFactory ( new NamedParameterJdbcTemplate ( db ) , mock ( ApplicationEventPublisher . class ) )
. getRepository ( DummyEntityRepository . class ) ;
}
private static DummyEntity createDummyEntity ( ) {
DummyEntity entity = new DummyEntity ( ) ;
entity . setName ( "Entity Name" ) ;
return entity ;
}
@Test // DATAJDBC-95
@Test // DATAJDBC-95
public void savesAnEntity ( ) {
public void savesAnEntity ( ) {
entity = repository . save ( entity ) ;
entity = repository . save ( entity ) ;
int count = template . queryForObject ( //
int count = JdbcTestUtils . countRowsInTableWhere ( //
"SELECT count(*) FROM dummyentity WHERE idProp = :id" , //
( JdbcTemplate ) template . getJdbcOperations ( ) , //
new MapSqlParameterSource ( "id" , entity . getIdProp ( ) ) , //
"dummyentity" , //
Integer . class //
"idProp = " + entity . getIdProp ( ) //
) ;
) ;
assertEquals ( 1 , count ) ;
assertEquals ( 1 , count ) ;
@ -95,9 +111,7 @@ public class JdbcRepositoryIntegrationTests {
assertThat ( repository . findAll ( ) ) //
assertThat ( repository . findAll ( ) ) //
. extracting ( DummyEntity : : getIdProp ) //
. extracting ( DummyEntity : : getIdProp ) //
. containsExactlyInAnyOrder ( //
. containsExactlyInAnyOrder ( entity . getIdProp ( ) , other . getIdProp ( ) ) ;
entity . getIdProp ( ) , other . getIdProp ( ) //
) ;
}
}
@Test // DATAJDBC-97
@Test // DATAJDBC-97
@ -125,9 +139,9 @@ public class JdbcRepositoryIntegrationTests {
@Test // DATAJDBC-97
@Test // DATAJDBC-97
public void findAllFindsAllSpecifiedEntities ( ) {
public void findAllFindsAllSpecifiedEntities ( ) {
entity = repository . save ( entity ) ;
DummyEntity two = repository . save ( createDummyEntity ( ) ) ;
DummyEntity two = repository . save ( createDummyEntity ( ) ) ;
DummyEntity three = repository . save ( createDummyEntity ( ) ) ;
DummyEntity three = repository . save ( createDummyEntity ( ) ) ;
entity = repository . save ( entity ) ;
Iterable < DummyEntity > all = repository . findAll ( asList ( entity . getIdProp ( ) , three . getIdProp ( ) ) ) ;
Iterable < DummyEntity > all = repository . findAll ( asList ( entity . getIdProp ( ) , three . getIdProp ( ) ) ) ;
@ -155,9 +169,7 @@ public class JdbcRepositoryIntegrationTests {
assertThat ( repository . findAll ( ) ) //
assertThat ( repository . findAll ( ) ) //
. extracting ( DummyEntity : : getIdProp ) //
. extracting ( DummyEntity : : getIdProp ) //
. containsExactlyInAnyOrder ( //
. containsExactlyInAnyOrder ( entity . getIdProp ( ) , three . getIdProp ( ) ) ;
entity . getIdProp ( ) , three . getIdProp ( ) //
) ;
}
}
@Test // DATAJDBC-97
@Test // DATAJDBC-97
@ -171,9 +183,7 @@ public class JdbcRepositoryIntegrationTests {
assertThat ( repository . findAll ( ) ) //
assertThat ( repository . findAll ( ) ) //
. extracting ( DummyEntity : : getIdProp ) //
. extracting ( DummyEntity : : getIdProp ) //
. containsExactlyInAnyOrder ( //
. containsExactlyInAnyOrder ( two . getIdProp ( ) , three . getIdProp ( ) ) ;
two . getIdProp ( ) , three . getIdProp ( ) //
) ;
}
}
@Test // DATAJDBC-97
@Test // DATAJDBC-97
@ -185,7 +195,9 @@ public class JdbcRepositoryIntegrationTests {
repository . delete ( asList ( entity , three ) ) ;
repository . delete ( asList ( entity , three ) ) ;
assertThat ( repository . findAll ( ) ) . extracting ( DummyEntity : : getIdProp ) . containsExactlyInAnyOrder ( two . getIdProp ( ) ) ;
assertThat ( repository . findAll ( ) ) //
. extracting ( DummyEntity : : getIdProp ) //
. containsExactlyInAnyOrder ( two . getIdProp ( ) ) ;
}
}
@Test // DATAJDBC-97
@Test // DATAJDBC-97
@ -225,52 +237,31 @@ public class JdbcRepositoryIntegrationTests {
repository . save ( asList ( entity , other ) ) ;
repository . save ( asList ( entity , other ) ) ;
assertThat ( repository . findAll ( ) ) . extracting ( DummyEntity : : getName ) . containsExactlyInAnyOrder ( entity . getName ( ) ,
assertThat ( repository . findAll ( ) ) //
other . getName ( ) ) ;
. extracting ( DummyEntity : : getName ) //
}
. containsExactlyInAnyOrder ( entity . getName ( ) , other . getName ( ) ) ;
private static DummyEntityRepository createRepository ( EmbeddedDatabase db ) {
return new JdbcRepositoryFactory ( new NamedParameterJdbcTemplate ( db ) , mock ( ApplicationEventPublisher . class ) )
. getRepository ( DummyEntityRepository . class ) ;
}
private static DummyEntity createDummyEntity ( ) {
DummyEntity entity = new DummyEntity ( ) ;
entity . setName ( "Entity Name" ) ;
return entity ;
}
}
private interface DummyEntityRepository extends CrudRepository < DummyEntity , Long > {
private interface DummyEntityRepository extends CrudRepository < DummyEntity , Long > { }
}
@Data
@Data
static class DummyEntity {
static class DummyEntity {
@Id private Long idProp ;
String name ;
String name ;
@Id private Long idProp ;
}
}
@Configuration
@Configuration
@ComponentScan ( "org.springframework.data.jdbc.testing" )
static class TestConfiguration {
static class TestConfiguration {
@Bean
@Bean
EmbeddedDatabase dataSource ( ) {
Class < ? > testClass ( ) {
return JdbcRepositoryIntegrationTests . class ;
System . out . println ( " creating datasource" ) ;
return new EmbeddedDatabaseBuilder ( ) //
. generateUniqueName ( true ) //
. setType ( EmbeddedDatabaseType . HSQL ) //
. setScriptEncoding ( "UTF-8" ) //
. ignoreFailedDrops ( true ) //
. addScript ( "org.springframework.data.jdbc.repository/jdbc-repository-integration-tests.sql" ) //
. build ( ) ;
}
}
@Bean
@Bean
NamedParameterJdbcTemplate template ( EmbeddedDatabas e db ) {
NamedParameterJdbcTemplate template ( DataSource db ) {
return new NamedParameterJdbcTemplate ( db ) ;
return new NamedParameterJdbcTemplate ( db ) ;
}
}
@ -285,6 +276,5 @@ public class JdbcRepositoryIntegrationTests {
PlatformTransactionManager transactionManager ( DataSource db ) {
PlatformTransactionManager transactionManager ( DataSource db ) {
return new DataSourceTransactionManager ( db ) ;
return new DataSourceTransactionManager ( db ) ;
}
}
}
}
}
}