@ -16,13 +16,15 @@
@@ -16,13 +16,15 @@
package org.springframework.data.jdbc.repository.support ;
import static org.assertj.core.api.Assertions.* ;
import static org.mockito.ArgumentMatchers.any ;
import static org.mockito.ArgumentMatchers.* ;
import static org.mockito.Mockito.* ;
import java.lang.reflect.Method ;
import java.sql.ResultSet ;
import java.util.Properties ;
import org.jetbrains.annotations.NotNull ;
import org.junit.Before ;
import org.junit.Test ;
import org.springframework.data.jdbc.repository.query.Query ;
import org.springframework.data.projection.ProjectionFactory ;
@ -40,117 +42,93 @@ import org.springframework.jdbc.core.RowMapper;
@@ -40,117 +42,93 @@ import org.springframework.jdbc.core.RowMapper;
* /
public class JdbcQueryMethodUnitTests {
public static final String DUMMY_SELECT_VALU E = "SELECT something " ;
public static final String DUMMY_SELECT_NAME = "DUMMY. SELECT" ;
public static final String DUMMY_SELECT_METHOD = "queryWhitoutQueryAnnotation ";
public static final String DUMMY_SELECT_NAME_VALUE = "SELECT something NAME AND VALUE" ;
public static final String QUERY_NAM E = "DUMMY. SELECT" ;
public static final String QUERY = "SELECT something " ;
public static final String METHOD_WITHOUT_QUERY_ANNOTATION = "methodWithImplicitlyNamedQuery ";
public static final String QUERY2 = "SELECT something NAME AND VALUE" ;
@Test // DATAJDBC-165
public void returnsSqlStatement ( ) throws NoSuchMethodException {
NamedQueries namedQueries ;
RepositoryMetadata metadata ;
RepositoryMetadata metadata = mock ( RepositoryMetadata . class ) ;
@Before
public void before ( ) {
doReturn ( String . class ) . when ( metadata ) . getReturnedDomainClass ( any ( Method . class ) ) ;
Properties properties = new Properties ( ) ;
properties . setProperty ( DUMMY_SELECT_NAME , DUMMY_SELECT_VALUE ) ;
NamedQueries nameQueries = new PropertiesBasedNamedQueries ( properties ) ;
JdbcQueryMethod queryMethod = new JdbcQueryMethod (
JdbcQueryMethodUnitTests . class . getDeclaredMethod ( "queryMethod" ) , metadata ,
mock ( ProjectionFactory . class ) , nameQueries ) ;
assertThat ( queryMethod . getAnnotatedQuery ( ) ) . isEqualTo ( DUMMY_SELECT_VALUE ) ;
}
@Test // DATAJDBC-165
public void returnsSpecifiedRowMapperClass ( ) throws NoSuchMethodException {
RepositoryMetadata metadata = mock ( RepositoryMetadata . class ) ;
properties . setProperty ( QUERY_NAME , QUERY ) ;
// String is used as domain class because the methods used for testing aren't part of a repository and therefore the
// return type is used as the domain type.
properties . setProperty ( "String." + METHOD_WITHOUT_QUERY_ANNOTATION , QUERY2 ) ;
namedQueries = new PropertiesBasedNamedQueries ( properties ) ;
metadata = mock ( RepositoryMetadata . class ) ;
doReturn ( String . class ) . when ( metadata ) . getReturnedDomainClass ( any ( Method . class ) ) ;
Properties properties = new Properties ( ) ;
properties . setProperty ( DUMMY_SELECT_NAME , DUMMY_SELECT_VALUE ) ;
NamedQueries nameQueries = new PropertiesBasedNamedQueries ( properties ) ;
JdbcQueryMethod queryMethod = new JdbcQueryMethod (
JdbcQueryMethodUnitTests . class . getDeclaredMethod ( "queryMethod" ) , metadata ,
mock ( ProjectionFactory . class ) , nameQueries ) ;
assertThat ( queryMethod . getRowMapperClass ( ) ) . isEqualTo ( CustomRowMapper . class ) ;
}
@Test // DATAJDBC-165
public void returnsSqlStatement ( ) throws NoSuchMethodException {
JdbcQueryMethod queryMethod = createJdbcQueryMethod ( "queryMethod" ) ;
assertThat ( queryMethod . getDeclaredQuery ( ) ) . isEqualTo ( QUERY ) ;
}
@Test // DATAJDBC-165
public void returnsSpecifiedRowMapperClass ( ) throws NoSuchMethodException {
JdbcQueryMethod queryMethod = createJdbcQueryMethod ( "queryMethod" ) ;
assertThat ( queryMethod . getRowMapperClass ( ) ) . isEqualTo ( CustomRowMapper . class ) ;
}
@Test // DATAJDBC-234
public void returnsSqlStatementName ( ) throws NoSuchMethodException {
RepositoryMetadata metadata = mock ( RepositoryMetadata . class ) ;
doReturn ( String . class ) . when ( metadata ) . getReturnedDomainClass ( any ( Method . class ) ) ;
Properties properties = new Properties ( ) ;
properties . setProperty ( DUMMY_SELECT_NAME , DUMMY_SELECT_VALUE ) ;
NamedQueries nameQueries = new PropertiesBasedNamedQueries ( properties ) ;
JdbcQueryMethod queryMethod = new JdbcQueryMethod (
JdbcQueryMethodUnitTests . class . getDeclaredMethod ( "queryMethodName" ) , metadata ,
mock ( ProjectionFactory . class ) , nameQueries ) ;
assertThat ( queryMethod . getAnnotatedQuery ( ) ) . isEqualTo ( DUMMY_SELECT_VALUE ) ;
JdbcQueryMethod queryMethod = createJdbcQueryMethod ( "queryMethodName" ) ;
assertThat ( queryMethod . getDeclaredQuery ( ) ) . isEqualTo ( QUERY ) ;
}
@Test // DATAJDBC-234
public void returnsSqlStatementNameAndValue ( ) throws NoSuchMethodException {
RepositoryMetadata metadata = mock ( RepositoryMetadata . class ) ;
@Test // DATAJDBC-234
public void returnsSpecifiedSqlStatementIfNameAndValueAreGiven ( ) throws NoSuchMethodException {
doReturn ( String . class ) . when ( metadata ) . getReturnedDomainClass ( any ( Method . class ) ) ;
JdbcQueryMethod queryMethod = createJdbcQueryMethod ( "queryMethodWithNameAndValue" ) ;
assertThat ( queryMethod . getDeclaredQuery ( ) ) . isEqualTo ( QUERY2 ) ;
Properties properties = new Properties ( ) ;
properties . setProperty ( DUMMY_SELECT_NAME , DUMMY_SELECT_VALUE ) ;
NamedQueries nameQueries = new PropertiesBasedNamedQueries ( properties ) ;
}
JdbcQueryMethod queryMethod = new JdbcQueryMethod (
JdbcQueryMethodUnitTests . class . getDeclaredMethod ( "queryMethodNameAndValue" ) , metadata ,
mock ( ProjectionFactory . class ) , nameQueries ) ;
assertThat ( queryMethod . getAnnotatedQuery ( ) ) . isEqualTo ( DUMMY_SELECT_NAME_VALUE ) ;
@NotNull
private JdbcQueryMethod createJdbcQueryMethod ( String methodName ) throws NoSuchMethodException {
Method method = JdbcQueryMethodUnitTests . class . getDeclaredMethod ( methodName ) ;
return new JdbcQueryMethod ( method , metadata , mock ( ProjectionFactory . class ) , namedQueries ) ;
}
@Test // DATAJDBC-234
public void returnsNullNoSql Query ( ) throws NoSuchMethodException {
public void returnsImplicitlyNamed Query ( ) throws NoSuchMethodException {
RepositoryMetadata metadata = mock ( RepositoryMetadata . class ) ;
Properties properties = new Properties ( ) ;
properties . setProperty ( DUMMY_SELECT_METHOD , DUMMY_SELECT_VALUE ) ;
NamedQueries nameQueries = new PropertiesBasedNamedQueries ( properties ) ;
doReturn ( String . class ) . when ( metadata ) . getReturnedDomainClass ( any ( Method . class ) ) ;
JdbcQueryMethod queryMethod = createJdbcQueryMethod ( "methodWithImplicitlyNamedQuery" ) ;
assertThat ( queryMethod . getDeclaredQuery ( ) ) . isEqualTo ( QUERY2 ) ;
}
JdbcQueryMethod queryMethod = new JdbcQueryMethod (
JdbcQueryMethodUnitTests . class . getDeclaredMethod ( "queryWhitoutQueryAnnotation" ) , metadata ,
mock ( ProjectionFactory . class ) , nameQueries ) ;
assertThat ( queryMethod . getAnnotatedQuery ( ) ) . isEqualTo ( DUMMY_SELECT_VALUE ) ;
@Test // DATAJDBC-234
public void returnsNullIfNoQueryIsFound ( ) throws NoSuchMethodException {
JdbcQueryMethod queryMethod = createJdbcQueryMethod ( "methodWithoutAnyQuery" ) ;
assertThat ( queryMethod . getDeclaredQuery ( ) ) . isEqualTo ( null ) ;
}
@Query ( value = DUMMY_SELECT_VALUE , rowMapperClass = CustomRowMapper . class )
private void queryMethod ( ) {
}
@Query ( value = QUERY , rowMapperClass = CustomRowMapper . class )
private void queryMethod ( ) { }
@Query ( name = DUMMY_SELECT_NAME )
private void queryMethodName ( ) {
}
@Query ( name = QUERY_NAME )
private void queryMethodName ( ) { }
@Query ( value = DUMMY_SELECT_NAME_VALUE , name = DUMMY_SELECT_NAME )
private void queryMethodNameAndValue ( ) {
}
@Query ( value = QUERY2 , name = QUERY_NAME )
private void queryMethodWithNameAndValue ( ) { }
private void queryWhitoutQueryAnnotation ( ) {
}
private void methodWithImplicitlyNamedQuery ( ) { }
private void methodWithoutAnyQuery ( ) { }
private class CustomRowMapper implements RowMapper < Object > {