@ -15,7 +15,24 @@
* /
* /
package org.springframework.data.jdbc.core ;
package org.springframework.data.jdbc.core ;
import static java.util.Arrays.* ;
import static org.assertj.core.api.Assertions.* ;
import static org.mockito.Mockito.* ;
import lombok.RequiredArgsConstructor ;
import lombok.RequiredArgsConstructor ;
import java.sql.ResultSet ;
import java.sql.SQLException ;
import java.util.AbstractMap.SimpleEntry ;
import java.util.ArrayList ;
import java.util.HashMap ;
import java.util.HashSet ;
import java.util.List ;
import java.util.Map ;
import java.util.Set ;
import javax.naming.OperationNotSupportedException ;
import org.junit.Test ;
import org.junit.Test ;
import org.mockito.invocation.InvocationOnMock ;
import org.mockito.invocation.InvocationOnMock ;
import org.mockito.stubbing.Answer ;
import org.mockito.stubbing.Answer ;
@ -23,7 +40,6 @@ import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.core.convert.support.GenericConversionService ;
import org.springframework.core.convert.support.GenericConversionService ;
import org.springframework.data.annotation.Id ;
import org.springframework.data.annotation.Id ;
import org.springframework.data.convert.Jsr310Converters ;
import org.springframework.data.convert.Jsr310Converters ;
import org.springframework.data.jdbc.mapping.model.DefaultNamingStrategy ;
import org.springframework.data.jdbc.mapping.model.JdbcMappingContext ;
import org.springframework.data.jdbc.mapping.model.JdbcMappingContext ;
import org.springframework.data.jdbc.mapping.model.JdbcPersistentEntity ;
import org.springframework.data.jdbc.mapping.model.JdbcPersistentEntity ;
import org.springframework.data.jdbc.mapping.model.JdbcPersistentProperty ;
import org.springframework.data.jdbc.mapping.model.JdbcPersistentProperty ;
@ -31,21 +47,6 @@ import org.springframework.data.jdbc.mapping.model.NamingStrategy;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations ;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations ;
import org.springframework.util.Assert ;
import org.springframework.util.Assert ;
import javax.naming.OperationNotSupportedException ;
import java.sql.ResultSet ;
import java.sql.SQLException ;
import java.util.AbstractMap.SimpleEntry ;
import java.util.ArrayList ;
import java.util.HashMap ;
import java.util.HashSet ;
import java.util.List ;
import java.util.Map ;
import java.util.Set ;
import static java.util.Arrays.* ;
import static org.assertj.core.api.Assertions.* ;
import static org.mockito.Mockito.* ;
/ * *
/ * *
* Tests the extraction of entities from a { @link ResultSet } by the { @link EntityRowMapper } .
* Tests the extraction of entities from a { @link ResultSet } by the { @link EntityRowMapper } .
*
*
@ -56,10 +57,10 @@ public class EntityRowMapperUnitTests {
public static final long ID_FOR_ENTITY_REFERENCING_MAP = 42L ;
public static final long ID_FOR_ENTITY_REFERENCING_MAP = 42L ;
public static final long ID_FOR_ENTITY_REFERENCING_LIST = 4711L ;
public static final long ID_FOR_ENTITY_REFERENCING_LIST = 4711L ;
public static final long ID_FOR_ENTITY_NOT_REFERENCING_MAP = 23L ;
public static final long ID_FOR_ENTITY_NOT_REFERENCING_MAP = 23L ;
public static final Default NamingStrategy X_APPENDING_NAMINGSTRATEGY = new Default NamingStrategy( ) {
public static final NamingStrategy X_APPENDING_NAMINGSTRATEGY = new NamingStrategy ( ) {
@Override
@Override
public String getColumnName ( JdbcPersistentProperty property ) {
public String getColumnName ( JdbcPersistentProperty property ) {
return super . getColumnName ( property ) + "x" ;
return NamingStrategy . super . getColumnName ( property ) + "x" ;
}
}
} ;
} ;
@ -169,7 +170,7 @@ public class EntityRowMapperUnitTests {
}
}
private < T > EntityRowMapper < T > createRowMapper ( Class < T > type ) {
private < T > EntityRowMapper < T > createRowMapper ( Class < T > type ) {
return createRowMapper ( type , new DefaultNamingStrategy ( ) ) ;
return createRowMapper ( type , NamingStrategy . INSTANCE ) ;
}
}
private < T > EntityRowMapper < T > createRowMapper ( Class < T > type , NamingStrategy namingStrategy ) {
private < T > EntityRowMapper < T > createRowMapper ( Class < T > type , NamingStrategy namingStrategy ) {
@ -177,15 +178,14 @@ public class EntityRowMapperUnitTests {
JdbcMappingContext context = new JdbcMappingContext ( //
JdbcMappingContext context = new JdbcMappingContext ( //
namingStrategy , //
namingStrategy , //
mock ( NamedParameterJdbcOperations . class ) , //
mock ( NamedParameterJdbcOperations . class ) , //
__ - > {
__ - > { } //
} //
) ;
) ;
DataAccessStrategy accessStrategy = mock ( DataAccessStrategy . class ) ;
DataAccessStrategy accessStrategy = mock ( DataAccessStrategy . class ) ;
// the ID of the entity is used to determine what kind of ResultSet is needed for subsequent selects.
// the ID of the entity is used to determine what kind of ResultSet is needed for subsequent selects.
doReturn ( new HashSet < > ( asList ( new Trivial ( ) , new Trivial ( ) ) ) ) . when ( accessStrategy ) . findAllByProperty ( eq ( ID_FOR_ENTITY_NOT_REFERENCING_MAP ) ,
doReturn ( new HashSet < > ( asList ( new Trivial ( ) , new Trivial ( ) ) ) ) . when ( accessStrategy )
any ( JdbcPersistentProperty . class ) ) ;
. findAllByProperty ( eq ( ID_FOR_ENTITY_NOT_REFERENCING_MAP ) , any ( JdbcPersistentProperty . class ) ) ;
doReturn ( new HashSet < > ( asList ( //
doReturn ( new HashSet < > ( asList ( //
new SimpleEntry ( "one" , new Trivial ( ) ) , //
new SimpleEntry ( "one" , new Trivial ( ) ) , //
@ -202,8 +202,8 @@ public class EntityRowMapperUnitTests {
DefaultConversionService . addDefaultConverters ( conversionService ) ;
DefaultConversionService . addDefaultConverters ( conversionService ) ;
Jsr310Converters . getConvertersToRegister ( ) . forEach ( conversionService : : addConverter ) ;
Jsr310Converters . getConvertersToRegister ( ) . forEach ( conversionService : : addConverter ) ;
return new EntityRowMapper < > ( ( JdbcPersistentEntity < T > ) context . getRequiredPersistentEntity ( type ) ,
return new EntityRowMapper < > ( ( JdbcPersistentEntity < T > ) context . getRequiredPersistentEntity ( type ) , context ,
context , accessStrategy ) ;
accessStrategy ) ;
}
}
private static ResultSet mockResultSet ( List < String > columns , Object . . . values ) {
private static ResultSet mockResultSet ( List < String > columns , Object . . . values ) {
@ -291,7 +291,8 @@ public class EntityRowMapperUnitTests {
Map < String , Object > rowMap = values . get ( index ) ;
Map < String , Object > rowMap = values . get ( index ) ;
Assert . isTrue ( rowMap . containsKey ( column ) , String . format ( "Trying to access a column (%s) that does not exist" , column ) ) ;
Assert . isTrue ( rowMap . containsKey ( column ) ,
String . format ( "Trying to access a column (%s) that does not exist" , column ) ) ;
return rowMap . get ( column ) ;
return rowMap . get ( column ) ;
}
}
@ -306,46 +307,40 @@ public class EntityRowMapperUnitTests {
@RequiredArgsConstructor
@RequiredArgsConstructor
static class TrivialImmutable {
static class TrivialImmutable {
@Id
@Id private final Long id ;
private final Long id ;
private final String name ;
private final String name ;
}
}
static class Trivial {
static class Trivial {
@Id
@Id Long id ;
Long id ;
String name ;
String name ;
}
}
static class OneToOne {
static class OneToOne {
@Id
@Id Long id ;
Long id ;
String name ;
String name ;
Trivial child ;
Trivial child ;
}
}
static class OneToSet {
static class OneToSet {
@Id
@Id Long id ;
Long id ;
String name ;
String name ;
Set < Trivial > children ;
Set < Trivial > children ;
}
}
static class OneToMap {
static class OneToMap {
@Id
@Id Long id ;
Long id ;
String name ;
String name ;
Map < String , Trivial > children ;
Map < String , Trivial > children ;
}
}
static class OneToList {
static class OneToList {
@Id
@Id Long id ;
Long id ;
String name ;
String name ;
List < Trivial > children ;
List < Trivial > children ;
}
}