Browse Source

DATAJDBC-234 - Fixes the default query naming and polishing.

The default name now does include the name of the domain class.
The default name now is no longer used when no query with the name specified in the `@Query` annotation is found.

Simplified the unit tests by extracting common functionality and improving naming.
Simplified test configuration.

Original pull request: #180.
pull/182/head
Jens Schauder 6 years ago
parent
commit
09fd0fcbd6
No known key found for this signature in database
GPG Key ID: 996B1389BA0721C3
  1. 2
      spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/Query.java
  2. 3
      spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/support/JdbcQueryLookupStrategy.java
  3. 43
      spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/support/JdbcQueryMethod.java
  4. 2
      spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/support/JdbcRepositoryQuery.java
  5. 38
      spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryIntegrationTests.java
  6. 132
      spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/support/JdbcQueryMethodUnitTests.java
  7. 18
      spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/support/JdbcRepositoryQueryUnitTests.java
  8. 54
      spring-data-jdbc/src/test/java/org/springframework/data/jdbc/testing/QueryNamedTestConfiguration.java
  9. 12
      spring-data-jdbc/src/test/java/org/springframework/data/jdbc/testing/TestConfiguration.java
  10. 2
      spring-data-jdbc/src/test/resources/META-INF/jdbc-named-queries.properties

2
spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/Query.java

@ -46,7 +46,7 @@ public @interface Query { @@ -46,7 +46,7 @@ public @interface Query {
/**
* The named query to be used. If not defined, the name of
* {@code $ domainClass}.${queryMethodName}} will be used.
* {@code ${domainClass}.${queryMethodName}} will be used.
*/
String name() default "";

3
spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/support/JdbcQueryLookupStrategy.java

@ -54,7 +54,6 @@ class JdbcQueryLookupStrategy implements QueryLookupStrategy { @@ -54,7 +54,6 @@ class JdbcQueryLookupStrategy implements QueryLookupStrategy {
private final JdbcConverter converter;
private final QueryMappingConfiguration queryMappingConfiguration;
private final NamedParameterJdbcOperations operations;
/*
* (non-Javadoc)
@ -64,7 +63,7 @@ class JdbcQueryLookupStrategy implements QueryLookupStrategy { @@ -64,7 +63,7 @@ class JdbcQueryLookupStrategy implements QueryLookupStrategy {
public RepositoryQuery resolveQuery(Method method, RepositoryMetadata repositoryMetadata,
ProjectionFactory projectionFactory, NamedQueries namedQueries) {
JdbcQueryMethod queryMethod = new JdbcQueryMethod(method, repositoryMetadata, projectionFactory,namedQueries);
JdbcQueryMethod queryMethod = new JdbcQueryMethod(method, repositoryMetadata, projectionFactory, namedQueries);
RowMapper<?> mapper = queryMethod.isModifyingQuery() ? null : createMapper(queryMethod);

43
spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/support/JdbcQueryMethod.java

@ -22,18 +22,17 @@ import org.springframework.core.annotation.AnnotationUtils; @@ -22,18 +22,17 @@ import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.data.jdbc.repository.query.Modifying;
import org.springframework.data.jdbc.repository.query.Query;
import org.springframework.data.projection.ProjectionFactory;
import org.springframework.data.repository.core.NamedQueries;
import org.springframework.data.repository.core.RepositoryMetadata;
import org.springframework.data.repository.query.QueryMethod;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.lang.Nullable;
import org.springframework.util.StringUtils;
import org.springframework.data.repository.core.NamedQueries;
/**
* {@link QueryMethod} implementation that implements a method by executing the
* query from a {@link Query} annotation on that method. Binds method arguments
* to named parameters in the SQL statement.
* {@link QueryMethod} implementation that implements a method by executing the query from a {@link Query} annotation on
* that method. Binds method arguments to named parameters in the SQL statement.
*
* @author Jens Schauder
* @author Kazuki Shimizu
@ -59,49 +58,50 @@ public class JdbcQueryMethod extends QueryMethod { @@ -59,49 +58,50 @@ public class JdbcQueryMethod extends QueryMethod {
*
* @return May be {@code null}.
*/
@Nullable
public String getAnnotatedQuery() {
String getDeclaredQuery() {
String annotatedValue = getQueryValue();
return StringUtils.hasText(annotatedValue) ? annotatedValue : getNamedQuery();
}
/**
* Returns the annotated query with key value if it exists.
* Returns the annotated query if it exists.
*
* @return May be {@code null}.
*/
@Nullable
String getQueryValue() {
private String getQueryValue() {
return getMergedAnnotationAttribute("value");
}
/**
* Returns the annotated query name.
* Returns the named query for this method if it exists.
*
* @return May be {@code null}.
*/
@Nullable
String getQueryName() {
return getMergedAnnotationAttribute("name");
private String getNamedQuery() {
String name = getQueryName();
return this.namedQueries.hasQuery(name) ? this.namedQueries.getQuery(name) : null;
}
/**
* Returns the annotated query with key name if it exists.
* Returns the annotated query name.
*
* @return May be {@code null}.
*/
@Nullable
String getNamedQuery() {
private String getQueryName() {
String annotatedName = getMergedAnnotationAttribute("name");
return (StringUtils.hasText(annotatedName) && this.namedQueries.hasQuery(annotatedName))
? this.namedQueries.getQuery(annotatedName)
: this.namedQueries.getQuery(super.getName());
return StringUtils.hasText(annotatedName) ? annotatedName : getNamedQueryName();
}
/*
* Returns the class to be used as {@link
* org.springframework.jdbc.core.RowMapper}
* Returns the class to be used as {@link org.springframework.jdbc.core.RowMapper}
*
* @return May be {@code null}.
*/
@ -111,8 +111,7 @@ public class JdbcQueryMethod extends QueryMethod { @@ -111,8 +111,7 @@ public class JdbcQueryMethod extends QueryMethod {
}
/**
* Returns the class to be used as
* {@link org.springframework.jdbc.core.ResultSetExtractor}
* Returns the class to be used as {@link org.springframework.jdbc.core.ResultSetExtractor}
*
* @return May be {@code null}.
*/

2
spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/support/JdbcRepositoryQuery.java

@ -194,7 +194,7 @@ class JdbcRepositoryQuery implements RepositoryQuery { @@ -194,7 +194,7 @@ class JdbcRepositoryQuery implements RepositoryQuery {
private String determineQuery() {
String query = queryMethod.getAnnotatedQuery();
String query = queryMethod.getDeclaredQuery();
if (StringUtils.isEmpty(query)) {
throw new IllegalStateException(String.format("No query specified on %s", queryMethod.getName()));

38
spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryIntegrationTests.java

@ -20,6 +20,9 @@ import static org.assertj.core.api.Assertions.*; @@ -20,6 +20,9 @@ import static org.assertj.core.api.Assertions.*;
import lombok.Data;
import java.io.IOException;
import java.util.List;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
@ -30,27 +33,19 @@ import org.springframework.context.annotation.Configuration; @@ -30,27 +33,19 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.annotation.Id;
import org.springframework.data.jdbc.repository.query.Query;
import org.springframework.data.jdbc.repository.support.JdbcRepositoryFactory;
import org.springframework.data.jdbc.testing.QueryNamedTestConfiguration;
import org.springframework.data.jdbc.testing.TestConfiguration;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.core.NamedQueries;
import org.springframework.data.repository.core.support.PropertiesBasedNamedQueries;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
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.annotation.Transactional;
import java.io.IOException;
import java.util.List;
import javax.annotation.PostConstruct;
;
/**
* Very simple use cases for creation and usage of JdbcRepositories.
*
@ -59,14 +54,12 @@ import javax.annotation.PostConstruct; @@ -59,14 +54,12 @@ import javax.annotation.PostConstruct;
@ContextConfiguration
@Transactional
public class JdbcRepositoryIntegrationTests {
public static final String DUMMY_SELECT_NAME = "DUMMY.SELECT";
@Configuration
@Import(QueryNamedTestConfiguration.class)
@Import(TestConfiguration.class)
static class Config {
@Autowired
JdbcRepositoryFactory factory;
@Autowired JdbcRepositoryFactory factory;
@Bean
Class<?> testClass() {
@ -76,7 +69,17 @@ public class JdbcRepositoryIntegrationTests { @@ -76,7 +69,17 @@ public class JdbcRepositoryIntegrationTests {
@Bean
DummyEntityRepository dummyEntityRepository() {
return factory.getRepository(DummyEntityRepository.class);
}
}
@Bean
NamedQueries namedQueries() throws IOException {
PropertiesFactoryBean properties = new PropertiesFactoryBean();
properties.setLocation(new ClassPathResource("META-INF/jdbc-named-queries.properties"));
properties.afterPropertiesSet();
return new PropertiesBasedNamedQueries(properties.getObject());
}
}
@ClassRule public static final SpringClassRule classRule = new SpringClassRule();
@ -256,10 +259,10 @@ public class JdbcRepositoryIntegrationTests { @@ -256,10 +259,10 @@ public class JdbcRepositoryIntegrationTests {
}
@Test // DATAJDBC-234
public void findAllQueryName() {
public void findAllByQueryName() {
repository.save(createDummyEntity());
assertThat(repository.findAllQueryName().size() > 0);
assertThat(repository.findAllByNamedQuery()).hasSize(1);
}
private static DummyEntity createDummyEntity() {
@ -271,8 +274,7 @@ public class JdbcRepositoryIntegrationTests { @@ -271,8 +274,7 @@ public class JdbcRepositoryIntegrationTests {
interface DummyEntityRepository extends CrudRepository<DummyEntity, Long> {
@Query(name = DUMMY_SELECT_NAME)
List<DummyEntity> findAllQueryName();
List<DummyEntity> findAllByNamedQuery();
}
@Data

132
spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/support/JdbcQueryMethodUnitTests.java

@ -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_VALUE = "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_NAME = "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 returnsNullNoSqlQuery() throws NoSuchMethodException {
public void returnsImplicitlyNamedQuery() 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> {

18
spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/support/JdbcRepositoryQueryUnitTests.java

@ -78,7 +78,7 @@ public class JdbcRepositoryQueryUnitTests { @@ -78,7 +78,7 @@ public class JdbcRepositoryQueryUnitTests {
@Test // DATAJDBC-165
public void emptyQueryThrowsException() {
doReturn(null).when(queryMethod).getAnnotatedQuery();
doReturn(null).when(queryMethod).getDeclaredQuery();
Assertions.assertThatExceptionOfType(IllegalStateException.class) //
.isThrownBy(
@ -89,7 +89,7 @@ public class JdbcRepositoryQueryUnitTests { @@ -89,7 +89,7 @@ public class JdbcRepositoryQueryUnitTests {
@Test // DATAJDBC-165
public void defaultRowMapperIsUsedByDefault() {
doReturn("some sql statement").when(queryMethod).getAnnotatedQuery();
doReturn("some sql statement").when(queryMethod).getDeclaredQuery();
doReturn(RowMapper.class).when(queryMethod).getRowMapperClass();
JdbcRepositoryQuery query = new JdbcRepositoryQuery(publisher, callbacks, context, queryMethod, operations,
defaultRowMapper);
@ -102,7 +102,7 @@ public class JdbcRepositoryQueryUnitTests { @@ -102,7 +102,7 @@ public class JdbcRepositoryQueryUnitTests {
@Test // DATAJDBC-165
public void defaultRowMapperIsUsedForNull() {
doReturn("some sql statement").when(queryMethod).getAnnotatedQuery();
doReturn("some sql statement").when(queryMethod).getDeclaredQuery();
JdbcRepositoryQuery query = new JdbcRepositoryQuery(publisher, callbacks, context, queryMethod, operations,
defaultRowMapper);
@ -114,7 +114,7 @@ public class JdbcRepositoryQueryUnitTests { @@ -114,7 +114,7 @@ public class JdbcRepositoryQueryUnitTests {
@Test // DATAJDBC-165
public void customRowMapperIsUsedWhenSpecified() {
doReturn("some sql statement").when(queryMethod).getAnnotatedQuery();
doReturn("some sql statement").when(queryMethod).getDeclaredQuery();
doReturn(CustomRowMapper.class).when(queryMethod).getRowMapperClass();
new JdbcRepositoryQuery(publisher, callbacks, context, queryMethod, operations, defaultRowMapper)
@ -127,7 +127,7 @@ public class JdbcRepositoryQueryUnitTests { @@ -127,7 +127,7 @@ public class JdbcRepositoryQueryUnitTests {
@Test // DATAJDBC-290
public void customResultSetExtractorIsUsedWhenSpecified() {
doReturn("some sql statement").when(queryMethod).getAnnotatedQuery();
doReturn("some sql statement").when(queryMethod).getDeclaredQuery();
doReturn(CustomResultSetExtractor.class).when(queryMethod).getResultSetExtractorClass();
new JdbcRepositoryQuery(publisher, callbacks, context, queryMethod, operations, defaultRowMapper)
@ -145,7 +145,7 @@ public class JdbcRepositoryQueryUnitTests { @@ -145,7 +145,7 @@ public class JdbcRepositoryQueryUnitTests {
@Test // DATAJDBC-290
public void customResultSetExtractorAndRowMapperGetCombined() {
doReturn("some sql statement").when(queryMethod).getAnnotatedQuery();
doReturn("some sql statement").when(queryMethod).getDeclaredQuery();
doReturn(CustomResultSetExtractor.class).when(queryMethod).getResultSetExtractorClass();
doReturn(CustomRowMapper.class).when(queryMethod).getRowMapperClass();
@ -164,7 +164,7 @@ public class JdbcRepositoryQueryUnitTests { @@ -164,7 +164,7 @@ public class JdbcRepositoryQueryUnitTests {
@Test // DATAJDBC-263, DATAJDBC-354
public void publishesSingleEventWhenQueryReturnsSingleAggregate() {
doReturn("some sql statement").when(queryMethod).getAnnotatedQuery();
doReturn("some sql statement").when(queryMethod).getDeclaredQuery();
doReturn(false).when(queryMethod).isCollectionQuery();
doReturn(new DummyEntity(1L)).when(operations).queryForObject(anyString(), any(SqlParameterSource.class),
any(RowMapper.class));
@ -181,7 +181,7 @@ public class JdbcRepositoryQueryUnitTests { @@ -181,7 +181,7 @@ public class JdbcRepositoryQueryUnitTests {
@Test // DATAJDBC-263, DATAJDBC-354
public void publishesAsManyEventsAsReturnedAggregates() {
doReturn("some sql statement").when(queryMethod).getAnnotatedQuery();
doReturn("some sql statement").when(queryMethod).getDeclaredQuery();
doReturn(true).when(queryMethod).isCollectionQuery();
doReturn(Arrays.asList(new DummyEntity(1L), new DummyEntity(1L))).when(operations).query(anyString(),
any(SqlParameterSource.class), any(RowMapper.class));
@ -198,7 +198,7 @@ public class JdbcRepositoryQueryUnitTests { @@ -198,7 +198,7 @@ public class JdbcRepositoryQueryUnitTests {
@Test // DATAJDBC-400
public void publishesCallbacks() {
doReturn("some sql statement").when(queryMethod).getAnnotatedQuery();
doReturn("some sql statement").when(queryMethod).getDeclaredQuery();
doReturn(false).when(queryMethod).isCollectionQuery();
DummyEntity dummyEntity = new DummyEntity(1L);
doReturn(dummyEntity).when(operations).queryForObject(anyString(), any(SqlParameterSource.class),

54
spring-data-jdbc/src/test/java/org/springframework/data/jdbc/testing/QueryNamedTestConfiguration.java

@ -1,54 +0,0 @@ @@ -1,54 +0,0 @@
/*
* Copyright 2017-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.data.jdbc.testing;
import java.io.IOException;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.jdbc.repository.support.JdbcRepositoryFactory;
import org.springframework.data.repository.core.NamedQueries;
import org.springframework.data.repository.core.support.PropertiesBasedNamedQueries;
/**
* Infrastructure configuration for integration tests.
*
* @author Moises Cisneros
*/
@Configuration
@ComponentScan // To pick up configuration classes (per activated profile)
@Import(TestConfiguration.class)
public class QueryNamedTestConfiguration {
@Autowired JdbcRepositoryFactory factory;
@PostConstruct()
public void factory() throws IOException {
PropertiesFactoryBean properties = new PropertiesFactoryBean();
properties.setLocation(new ClassPathResource("META-INF/jdbc-named-queries.properties"));
NamedQueries namedQueries = null;
properties.afterPropertiesSet();
namedQueries = new PropertiesBasedNamedQueries(properties.getObject());
factory.setNamedQueries(namedQueries);
}
}

12
spring-data-jdbc/src/test/java/org/springframework/data/jdbc/testing/TestConfiguration.java

@ -20,8 +20,6 @@ import java.util.Optional; @@ -20,8 +20,6 @@ import java.util.Optional;
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationEventPublisher;
@ -40,9 +38,9 @@ import org.springframework.data.jdbc.core.convert.RelationResolver; @@ -40,9 +38,9 @@ import org.springframework.data.jdbc.core.convert.RelationResolver;
import org.springframework.data.jdbc.core.convert.SqlGeneratorSource;
import org.springframework.data.jdbc.core.mapping.JdbcMappingContext;
import org.springframework.data.jdbc.repository.support.JdbcRepositoryFactory;
import org.springframework.data.mapping.callback.EntityCallbacks;
import org.springframework.data.relational.core.mapping.NamingStrategy;
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
import org.springframework.data.repository.core.NamedQueries;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
@ -67,8 +65,12 @@ public class TestConfiguration { @@ -67,8 +65,12 @@ public class TestConfiguration {
@Bean
JdbcRepositoryFactory jdbcRepositoryFactory(
@Qualifier("defaultDataAccessStrategy") DataAccessStrategy dataAccessStrategy, RelationalMappingContext context,
JdbcConverter converter) {
return new JdbcRepositoryFactory(dataAccessStrategy, context, converter, publisher, namedParameterJdbcTemplate());
JdbcConverter converter, Optional<NamedQueries> namedQueries) {
JdbcRepositoryFactory factory = new JdbcRepositoryFactory(dataAccessStrategy, context, converter, publisher,
namedParameterJdbcTemplate());
namedQueries.ifPresent(factory::setNamedQueries);
return factory;
}
@Bean

2
spring-data-jdbc/src/test/resources/META-INF/jdbc-named-queries.properties

@ -1 +1 @@ @@ -1 +1 @@
DUMMY.SELECT=SELECT * FROM DUMMY_ENTITY
DummyEntity.findAllByNamedQuery=SELECT * FROM DUMMY_ENTITY
Loading…
Cancel
Save