From 09fd0fcbd63111da13fcfec7b9377004729dd1a4 Mon Sep 17 00:00:00 2001 From: Jens Schauder Date: Tue, 7 Jan 2020 15:24:10 +0100 Subject: [PATCH] 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. --- .../data/jdbc/repository/query/Query.java | 2 +- .../support/JdbcQueryLookupStrategy.java | 3 +- .../repository/support/JdbcQueryMethod.java | 51 ++++--- .../support/JdbcRepositoryQuery.java | 2 +- .../JdbcRepositoryIntegrationTests.java | 38 ++--- .../support/JdbcQueryMethodUnitTests.java | 140 ++++++++---------- .../support/JdbcRepositoryQueryUnitTests.java | 18 +-- .../testing/QueryNamedTestConfiguration.java | 54 ------- .../data/jdbc/testing/TestConfiguration.java | 12 +- .../META-INF/jdbc-named-queries.properties | 2 +- 10 files changed, 124 insertions(+), 198 deletions(-) delete mode 100644 spring-data-jdbc/src/test/java/org/springframework/data/jdbc/testing/QueryNamedTestConfiguration.java diff --git a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/Query.java b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/Query.java index 01b96045..a97c9f67 100644 --- a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/Query.java +++ b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/Query.java @@ -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 ""; diff --git a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/support/JdbcQueryLookupStrategy.java b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/support/JdbcQueryLookupStrategy.java index 89ea6dab..77776c0c 100644 --- a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/support/JdbcQueryLookupStrategy.java +++ b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/support/JdbcQueryLookupStrategy.java @@ -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 { 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); diff --git a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/support/JdbcQueryMethod.java b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/support/JdbcQueryMethod.java index 09d1813e..aad43061 100644 --- a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/support/JdbcQueryMethod.java +++ b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/support/JdbcQueryMethod.java @@ -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 { * * @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 named query for this method if it exists. + * + * @return May be {@code null}. + */ + @Nullable + private String getNamedQuery() { + + String name = getQueryName(); + return this.namedQueries.hasQuery(name) ? this.namedQueries.getQuery(name) : null; + } /** * Returns the annotated query name. * * @return May be {@code null}. */ - @Nullable - String getQueryName() { - return getMergedAnnotationAttribute("name"); - } - /** - * Returns the annotated query with key name if it exists. - * - * @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 { } /** - * 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}. */ diff --git a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/support/JdbcRepositoryQuery.java b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/support/JdbcRepositoryQuery.java index 267047e5..bfef9b4c 100644 --- a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/support/JdbcRepositoryQuery.java +++ b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/support/JdbcRepositoryQuery.java @@ -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())); diff --git a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryIntegrationTests.java b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryIntegrationTests.java index 92b56c89..4369b0af 100644 --- a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryIntegrationTests.java +++ b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryIntegrationTests.java @@ -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; 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; @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 { @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 { } @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 { interface DummyEntityRepository extends CrudRepository { - @Query(name = DUMMY_SELECT_NAME) - List findAllQueryName(); + List findAllByNamedQuery(); } @Data diff --git a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/support/JdbcQueryMethodUnitTests.java b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/support/JdbcQueryMethodUnitTests.java index d3e6c790..5072a79f 100644 --- a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/support/JdbcQueryMethodUnitTests.java +++ b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/support/JdbcQueryMethodUnitTests.java @@ -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; */ 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"; + + NamedQueries namedQueries; + RepositoryMetadata metadata; + + @Before + public void before() { + + Properties properties = new Properties(); + 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)); + } @Test // DATAJDBC-165 public void returnsSqlStatement() throws NoSuchMethodException { - RepositoryMetadata metadata = mock(RepositoryMetadata.class); + JdbcQueryMethod queryMethod = createJdbcQueryMethod("queryMethod"); - 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); + assertThat(queryMethod.getDeclaredQuery()).isEqualTo(QUERY); } @Test // DATAJDBC-165 public void returnsSpecifiedRowMapperClass() 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("queryMethod"), metadata, - mock(ProjectionFactory.class), nameQueries); + 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); - - } - @Test // DATAJDBC-234 - public void returnsSqlStatementNameAndValue() 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("queryMethodNameAndValue"), metadata, - mock(ProjectionFactory.class), nameQueries); - assertThat(queryMethod.getAnnotatedQuery()).isEqualTo(DUMMY_SELECT_NAME_VALUE); + JdbcQueryMethod queryMethod = createJdbcQueryMethod("queryMethodName"); + assertThat(queryMethod.getDeclaredQuery()).isEqualTo(QUERY); } @Test // DATAJDBC-234 - public void returnsNullNoSqlQuery() throws NoSuchMethodException { + public void returnsSpecifiedSqlStatementIfNameAndValueAreGiven() 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 = new JdbcQueryMethod( - JdbcQueryMethodUnitTests.class.getDeclaredMethod("queryWhitoutQueryAnnotation"), metadata, - mock(ProjectionFactory.class), nameQueries); - assertThat(queryMethod.getAnnotatedQuery()).isEqualTo(DUMMY_SELECT_VALUE); + JdbcQueryMethod queryMethod = createJdbcQueryMethod("queryMethodWithNameAndValue"); + assertThat(queryMethod.getDeclaredQuery()).isEqualTo(QUERY2); } - @Query(value = DUMMY_SELECT_VALUE, rowMapperClass = CustomRowMapper.class) - private void queryMethod() { + @NotNull + private JdbcQueryMethod createJdbcQueryMethod(String methodName) throws NoSuchMethodException { + + Method method = JdbcQueryMethodUnitTests.class.getDeclaredMethod(methodName); + return new JdbcQueryMethod(method, metadata, mock(ProjectionFactory.class), namedQueries); } - @Query(name = DUMMY_SELECT_NAME) - private void queryMethodName() { + @Test // DATAJDBC-234 + public void returnsImplicitlyNamedQuery() throws NoSuchMethodException { + + JdbcQueryMethod queryMethod = createJdbcQueryMethod("methodWithImplicitlyNamedQuery"); + assertThat(queryMethod.getDeclaredQuery()).isEqualTo(QUERY2); } - @Query(value = DUMMY_SELECT_NAME_VALUE, name = DUMMY_SELECT_NAME) - private void queryMethodNameAndValue() { + @Test // DATAJDBC-234 + public void returnsNullIfNoQueryIsFound() throws NoSuchMethodException { + + JdbcQueryMethod queryMethod = createJdbcQueryMethod("methodWithoutAnyQuery"); + assertThat(queryMethod.getDeclaredQuery()).isEqualTo(null); } - private void queryWhitoutQueryAnnotation() { - } + @Query(value = QUERY, rowMapperClass = CustomRowMapper.class) + private void queryMethod() {} + + @Query(name = QUERY_NAME) + private void queryMethodName() {} + + @Query(value = QUERY2, name = QUERY_NAME) + private void queryMethodWithNameAndValue() {} + + private void methodWithImplicitlyNamedQuery() {} + + private void methodWithoutAnyQuery() {} private class CustomRowMapper implements RowMapper { diff --git a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/support/JdbcRepositoryQueryUnitTests.java b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/support/JdbcRepositoryQueryUnitTests.java index aac45969..40a7cd39 100644 --- a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/support/JdbcRepositoryQueryUnitTests.java +++ b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/support/JdbcRepositoryQueryUnitTests.java @@ -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 { @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 { @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 { @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 { @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 { @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 { @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 { @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 { @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), diff --git a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/testing/QueryNamedTestConfiguration.java b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/testing/QueryNamedTestConfiguration.java deleted file mode 100644 index 2f410776..00000000 --- a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/testing/QueryNamedTestConfiguration.java +++ /dev/null @@ -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); - } -} - diff --git a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/testing/TestConfiguration.java b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/testing/TestConfiguration.java index 40b997f4..66803880 100644 --- a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/testing/TestConfiguration.java +++ b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/testing/TestConfiguration.java @@ -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; 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 { @Bean JdbcRepositoryFactory jdbcRepositoryFactory( @Qualifier("defaultDataAccessStrategy") DataAccessStrategy dataAccessStrategy, RelationalMappingContext context, - JdbcConverter converter) { - return new JdbcRepositoryFactory(dataAccessStrategy, context, converter, publisher, namedParameterJdbcTemplate()); + JdbcConverter converter, Optional namedQueries) { + + JdbcRepositoryFactory factory = new JdbcRepositoryFactory(dataAccessStrategy, context, converter, publisher, + namedParameterJdbcTemplate()); + namedQueries.ifPresent(factory::setNamedQueries); + return factory; } @Bean diff --git a/spring-data-jdbc/src/test/resources/META-INF/jdbc-named-queries.properties b/spring-data-jdbc/src/test/resources/META-INF/jdbc-named-queries.properties index e9cb683b..6d47e585 100644 --- a/spring-data-jdbc/src/test/resources/META-INF/jdbc-named-queries.properties +++ b/spring-data-jdbc/src/test/resources/META-INF/jdbc-named-queries.properties @@ -1 +1 @@ -DUMMY.SELECT=SELECT * FROM DUMMY_ENTITY \ No newline at end of file +DummyEntity.findAllByNamedQuery=SELECT * FROM DUMMY_ENTITY \ No newline at end of file