diff --git a/src/main/java/org/springframework/data/jdbc/repository/support/JdbcRepositoryFactory.java b/src/main/java/org/springframework/data/jdbc/repository/support/JdbcRepositoryFactory.java index 288bc452c..77abd4a32 100644 --- a/src/main/java/org/springframework/data/jdbc/repository/support/JdbcRepositoryFactory.java +++ b/src/main/java/org/springframework/data/jdbc/repository/support/JdbcRepositoryFactory.java @@ -30,13 +30,15 @@ import org.springframework.data.repository.core.RepositoryMetadata; import org.springframework.data.repository.core.support.RepositoryFactorySupport; import org.springframework.data.repository.query.EvaluationContextProvider; import org.springframework.data.repository.query.QueryLookupStrategy; +import org.springframework.util.Assert; /** * Creates repository implementation based on JDBC. * * @author Jens Schauder * @author Greg Turnquist - * @since 2.0 + * @author Christoph Strobl + * @since 1.0 */ public class JdbcRepositoryFactory extends RepositoryFactorySupport { @@ -89,7 +91,12 @@ public class JdbcRepositoryFactory extends RepositoryFactorySupport { return Optional.of(new JdbcQueryLookupStrategy(evaluationContextProvider, context, accessStrategy, rowMapperMap)); } + /** + * @param rowMapperMap must not be {@literal null} consider {@link RowMapperMap#EMPTY} instead. + */ public void setRowMapperMap(RowMapperMap rowMapperMap) { + + Assert.notNull(rowMapperMap, "RowMapperMap must not be null!"); this.rowMapperMap = rowMapperMap; } } diff --git a/src/main/java/org/springframework/data/jdbc/repository/support/JdbcRepositoryFactoryBean.java b/src/main/java/org/springframework/data/jdbc/repository/support/JdbcRepositoryFactoryBean.java index 1e3e9820f..e6ec1e1c6 100644 --- a/src/main/java/org/springframework/data/jdbc/repository/support/JdbcRepositoryFactoryBean.java +++ b/src/main/java/org/springframework/data/jdbc/repository/support/JdbcRepositoryFactoryBean.java @@ -36,7 +36,8 @@ import org.springframework.util.Assert; * * @author Jens Schauder * @author Greg Turnquist - * @since 2.0 + * @author Christoph Strobl + * @since 1.0 */ public class JdbcRepositoryFactoryBean, S, ID extends Serializable> // extends TransactionalRepositoryFactoryBeanSupport implements ApplicationEventPublisherAware { @@ -67,10 +68,7 @@ public class JdbcRepositoryFactoryBean, S, ID extend JdbcRepositoryFactory jdbcRepositoryFactory = new JdbcRepositoryFactory(publisher, mappingContext, dataAccessStrategy); - - if (rowMapperMap != null) { - jdbcRepositoryFactory.setRowMapperMap(rowMapperMap); - } + jdbcRepositoryFactory.setRowMapperMap(rowMapperMap); return jdbcRepositoryFactory; } @@ -82,11 +80,18 @@ public class JdbcRepositoryFactoryBean, S, ID extend this.mappingContext = mappingContext; } + /** + * @param dataAccessStrategy can be {@literal null}. + */ @Autowired(required = false) public void setDataAccessStrategy(DataAccessStrategy dataAccessStrategy) { this.dataAccessStrategy = dataAccessStrategy; } + /** + * @param rowMapperMap can be {@literal null}. {@link #afterPropertiesSet()} defaults to {@link RowMapperMap#EMPTY} if + * {@literal null}. + */ @Autowired(required = false) public void setRowMapperMap(RowMapperMap rowMapperMap) { this.rowMapperMap = rowMapperMap; @@ -95,7 +100,7 @@ public class JdbcRepositoryFactoryBean, S, ID extend @Override public void afterPropertiesSet() { - Assert.notNull(this.mappingContext, "MappingContext must not be null!"); + Assert.state(this.mappingContext != null, "MappingContext is required and must not be null!"); if (dataAccessStrategy == null) { @@ -104,6 +109,10 @@ public class JdbcRepositoryFactoryBean, S, ID extend mappingContext); } + if (rowMapperMap == null) { + rowMapperMap = RowMapperMap.EMPTY; + } + super.afterPropertiesSet(); } } diff --git a/src/test/java/org/springframework/data/jdbc/repository/support/JdbcRepositoryFactoryBeanUnitTests.java b/src/test/java/org/springframework/data/jdbc/repository/support/JdbcRepositoryFactoryBeanUnitTests.java index cc92d592d..41f0ff9aa 100644 --- a/src/test/java/org/springframework/data/jdbc/repository/support/JdbcRepositoryFactoryBeanUnitTests.java +++ b/src/test/java/org/springframework/data/jdbc/repository/support/JdbcRepositoryFactoryBeanUnitTests.java @@ -1,7 +1,22 @@ +/* + * Copyright 2017-2018 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 + * + * http://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.repository.support; -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.mock; +import static org.assertj.core.api.Assertions.*; +import static org.mockito.Mockito.*; import org.junit.Before; import org.junit.Test; @@ -9,26 +24,26 @@ import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; import org.springframework.beans.factory.BeanFactory; -import org.springframework.beans.factory.ListableBeanFactory; import org.springframework.data.annotation.Id; import org.springframework.data.jdbc.core.DataAccessStrategy; +import org.springframework.data.jdbc.core.DefaultDataAccessStrategy; import org.springframework.data.jdbc.mapping.model.JdbcMappingContext; +import org.springframework.data.jdbc.repository.RowMapperMap; import org.springframework.data.repository.CrudRepository; -import org.springframework.data.repository.Repository; +import org.springframework.test.util.ReflectionTestUtils; /** * Tests the dependency injection for {@link JdbcRepositoryFactoryBean}. * * @author Jens Schauder * @author Greg Turnquist + * @author Christoph Strobl */ @RunWith(MockitoJUnitRunner.class) public class JdbcRepositoryFactoryBeanUnitTests { JdbcRepositoryFactoryBean factoryBean; - @Mock ListableBeanFactory beanFactory; - @Mock Repository repository; @Mock DataAccessStrategy dataAccessStrategy; @Mock JdbcMappingContext mappingContext; @@ -55,6 +70,25 @@ public class JdbcRepositoryFactoryBeanUnitTests { factoryBean.setBeanFactory(mock(BeanFactory.class)); } + @Test(expected = IllegalStateException.class) // DATAJDBC-155 + public void afterPropertiesThowsExceptionWhenNoMappingContextSet() { + + factoryBean.setMappingContext(null); + factoryBean.afterPropertiesSet(); + } + + @Test // DATAJDBC-155 + public void afterPropertiesSetDefaultsNullablePropertiesCorrectly() { + + factoryBean.setMappingContext(mappingContext); + factoryBean.afterPropertiesSet(); + + assertThat(factoryBean.getObject()).isNotNull(); + assertThat(ReflectionTestUtils.getField(factoryBean, "dataAccessStrategy")) + .isInstanceOf(DefaultDataAccessStrategy.class); + assertThat(ReflectionTestUtils.getField(factoryBean, "rowMapperMap")).isEqualTo(RowMapperMap.EMPTY); + } + private static class DummyEntity { @Id private Long id; }