Skip to content

Commit db2b44d

Browse files
DATAJDBC-155 - Polishing.
Update Javadoc, assert state, default nullable properties in afterPropertiesSets and add tests. Original pull request: #54.
1 parent aaae9a5 commit db2b44d

File tree

3 files changed

+63
-13
lines changed

3 files changed

+63
-13
lines changed

src/main/java/org/springframework/data/jdbc/repository/support/JdbcRepositoryFactory.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,15 @@
3030
import org.springframework.data.repository.core.support.RepositoryFactorySupport;
3131
import org.springframework.data.repository.query.EvaluationContextProvider;
3232
import org.springframework.data.repository.query.QueryLookupStrategy;
33+
import org.springframework.util.Assert;
3334

3435
/**
3536
* Creates repository implementation based on JDBC.
3637
*
3738
* @author Jens Schauder
3839
* @author Greg Turnquist
39-
* @since 2.0
40+
* @author Christoph Strobl
41+
* @since 1.0
4042
*/
4143
public class JdbcRepositoryFactory extends RepositoryFactorySupport {
4244

@@ -89,7 +91,12 @@ protected Optional<QueryLookupStrategy> getQueryLookupStrategy(QueryLookupStrate
8991
return Optional.of(new JdbcQueryLookupStrategy(evaluationContextProvider, context, accessStrategy, rowMapperMap));
9092
}
9193

94+
/**
95+
* @param rowMapperMap must not be {@literal null} consider {@link RowMapperMap#EMPTY} instead.
96+
*/
9297
public void setRowMapperMap(RowMapperMap rowMapperMap) {
98+
99+
Assert.notNull(rowMapperMap, "RowMapperMap must not be null!");
93100
this.rowMapperMap = rowMapperMap;
94101
}
95102
}

src/main/java/org/springframework/data/jdbc/repository/support/JdbcRepositoryFactoryBean.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
*
3737
* @author Jens Schauder
3838
* @author Greg Turnquist
39-
* @since 2.0
39+
* @author Christoph Strobl
40+
* @since 1.0
4041
*/
4142
public class JdbcRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extends Serializable> //
4243
extends TransactionalRepositoryFactoryBeanSupport<T, S, ID> implements ApplicationEventPublisherAware {
@@ -67,10 +68,7 @@ protected RepositoryFactorySupport doCreateRepositoryFactory() {
6768

6869
JdbcRepositoryFactory jdbcRepositoryFactory = new JdbcRepositoryFactory(publisher, mappingContext,
6970
dataAccessStrategy);
70-
71-
if (rowMapperMap != null) {
72-
jdbcRepositoryFactory.setRowMapperMap(rowMapperMap);
73-
}
71+
jdbcRepositoryFactory.setRowMapperMap(rowMapperMap);
7472

7573
return jdbcRepositoryFactory;
7674
}
@@ -82,11 +80,18 @@ protected void setMappingContext(JdbcMappingContext mappingContext) {
8280
this.mappingContext = mappingContext;
8381
}
8482

83+
/**
84+
* @param dataAccessStrategy can be {@literal null}.
85+
*/
8586
@Autowired(required = false)
8687
public void setDataAccessStrategy(DataAccessStrategy dataAccessStrategy) {
8788
this.dataAccessStrategy = dataAccessStrategy;
8889
}
8990

91+
/**
92+
* @param rowMapperMap can be {@literal null}. {@link #afterPropertiesSet()} defaults to {@link RowMapperMap#EMPTY} if
93+
* {@literal null}.
94+
*/
9095
@Autowired(required = false)
9196
public void setRowMapperMap(RowMapperMap rowMapperMap) {
9297
this.rowMapperMap = rowMapperMap;
@@ -95,7 +100,7 @@ public void setRowMapperMap(RowMapperMap rowMapperMap) {
95100
@Override
96101
public void afterPropertiesSet() {
97102

98-
Assert.notNull(this.mappingContext, "MappingContext must not be null!");
103+
Assert.state(this.mappingContext != null, "MappingContext is required and must not be null!");
99104

100105
if (dataAccessStrategy == null) {
101106

@@ -104,6 +109,10 @@ public void afterPropertiesSet() {
104109
mappingContext);
105110
}
106111

112+
if (rowMapperMap == null) {
113+
rowMapperMap = RowMapperMap.EMPTY;
114+
}
115+
107116
super.afterPropertiesSet();
108117
}
109118
}

src/test/java/org/springframework/data/jdbc/repository/support/JdbcRepositoryFactoryBeanUnitTests.java

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,49 @@
1+
/*
2+
* Copyright 2017-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
116
package org.springframework.data.jdbc.repository.support;
217

3-
import static org.assertj.core.api.Assertions.assertThat;
4-
import static org.mockito.Mockito.mock;
18+
import static org.assertj.core.api.Assertions.*;
19+
import static org.mockito.Mockito.*;
520

621
import org.junit.Before;
722
import org.junit.Test;
823
import org.junit.runner.RunWith;
924
import org.mockito.Mock;
1025
import org.mockito.junit.MockitoJUnitRunner;
1126
import org.springframework.beans.factory.BeanFactory;
12-
import org.springframework.beans.factory.ListableBeanFactory;
1327
import org.springframework.data.annotation.Id;
1428
import org.springframework.data.jdbc.core.DataAccessStrategy;
29+
import org.springframework.data.jdbc.core.DefaultDataAccessStrategy;
1530
import org.springframework.data.jdbc.mapping.model.JdbcMappingContext;
31+
import org.springframework.data.jdbc.repository.RowMapperMap;
1632
import org.springframework.data.repository.CrudRepository;
17-
import org.springframework.data.repository.Repository;
33+
import org.springframework.test.util.ReflectionTestUtils;
1834

1935
/**
2036
* Tests the dependency injection for {@link JdbcRepositoryFactoryBean}.
2137
*
2238
* @author Jens Schauder
2339
* @author Greg Turnquist
40+
* @author Christoph Strobl
2441
*/
2542
@RunWith(MockitoJUnitRunner.class)
2643
public class JdbcRepositoryFactoryBeanUnitTests {
2744

2845
JdbcRepositoryFactoryBean<DummyEntityRepository, DummyEntity, Long> factoryBean;
2946

30-
@Mock ListableBeanFactory beanFactory;
31-
@Mock Repository<?, ?> repository;
3247
@Mock DataAccessStrategy dataAccessStrategy;
3348
@Mock JdbcMappingContext mappingContext;
3449

@@ -55,6 +70,25 @@ public void requiresListableBeanFactory() {
5570
factoryBean.setBeanFactory(mock(BeanFactory.class));
5671
}
5772

73+
@Test(expected = IllegalStateException.class) // DATAJDBC-155
74+
public void afterPropertiesThowsExceptionWhenNoMappingContextSet() {
75+
76+
factoryBean.setMappingContext(null);
77+
factoryBean.afterPropertiesSet();
78+
}
79+
80+
@Test // DATAJDBC-155
81+
public void afterPropertiesSetDefaultsNullablePropertiesCorrectly() {
82+
83+
factoryBean.setMappingContext(mappingContext);
84+
factoryBean.afterPropertiesSet();
85+
86+
assertThat(factoryBean.getObject()).isNotNull();
87+
assertThat(ReflectionTestUtils.getField(factoryBean, "dataAccessStrategy"))
88+
.isInstanceOf(DefaultDataAccessStrategy.class);
89+
assertThat(ReflectionTestUtils.getField(factoryBean, "rowMapperMap")).isEqualTo(RowMapperMap.EMPTY);
90+
}
91+
5892
private static class DummyEntity {
5993
@Id private Long id;
6094
}

0 commit comments

Comments
 (0)