Skip to content

Commit 874b2bc

Browse files
odrotbohmchristophstrobl
authored andcommitted
DATAKV-123 - Introduced dedicated KeyValueRepositoryFactoryBean.createRepositoryFactory(…).
We now expose a dedicated method for implementors to use to create the repository factory instance based on the configuration of the factory. Added unit tests to make sure the factory bean fails fast if it's configured improperly. Original Pull Request: #17
1 parent 40fe2ad commit 874b2bc

File tree

2 files changed

+170
-3
lines changed

2 files changed

+170
-3
lines changed

src/main/java/org/springframework/data/keyvalue/repository/support/KeyValueRepositoryFactoryBean.java

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014 the original author or authors.
2+
* Copyright 2014-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,24 +19,35 @@
1919

2020
import org.springframework.data.keyvalue.core.KeyValueOperations;
2121
import org.springframework.data.keyvalue.repository.KeyValueRepository;
22+
import org.springframework.data.keyvalue.repository.config.QueryCreatorType;
2223
import org.springframework.data.mapping.context.MappingContext;
2324
import org.springframework.data.repository.Repository;
2425
import org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport;
2526
import org.springframework.data.repository.core.support.RepositoryFactorySupport;
2627
import org.springframework.data.repository.query.parser.AbstractQueryCreator;
28+
import org.springframework.util.Assert;
2729

2830
/**
2931
* {@link org.springframework.beans.factory.FactoryBean} to create {@link KeyValueRepository}.
3032
*
3133
* @author Christoph Strobl
34+
* @author Oliver Gierke
3235
*/
3336
public class KeyValueRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extends Serializable> extends
3437
RepositoryFactoryBeanSupport<T, S, ID> {
3538

3639
private KeyValueOperations operations;
3740
private Class<? extends AbstractQueryCreator<?, ?>> queryCreator;
3841

42+
/**
43+
* Configures the {@link KeyValueOperations} to be used for the repositories.
44+
*
45+
* @param operations must not be {@literal null}.
46+
*/
3947
public void setKeyValueOperations(KeyValueOperations operations) {
48+
49+
Assert.notNull(operations, "KeyValueOperations must not be null!");
50+
4051
this.operations = operations;
4152
}
4253

@@ -49,7 +60,15 @@ public void setMappingContext(MappingContext<?, ?> mappingContext) {
4960
super.setMappingContext(mappingContext);
5061
}
5162

63+
/**
64+
* Configures the {@link QueryCreatorType} to be used.
65+
*
66+
* @param queryCreator must not be {@literal null}.
67+
*/
5268
public void setQueryCreator(Class<? extends AbstractQueryCreator<?, ?>> queryCreator) {
69+
70+
Assert.notNull(queryCreator, "Query creator type must not be null!");
71+
5372
this.queryCreator = queryCreator;
5473
}
5574

@@ -58,8 +77,22 @@ public void setQueryCreator(Class<? extends AbstractQueryCreator<?, ?>> queryCre
5877
* @see org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport#createRepositoryFactory()
5978
*/
6079
@Override
61-
protected RepositoryFactorySupport createRepositoryFactory() {
62-
return new KeyValueRepositoryFactory(this.operations, this.queryCreator);
80+
protected final RepositoryFactorySupport createRepositoryFactory() {
81+
return createRepositoryFactory(operations, queryCreator);
82+
}
83+
84+
/**
85+
* Create the repository factory to be used to create repositories.
86+
*
87+
* @param operations will never be {@literal null}.
88+
* @param queryCreator will never be {@literal null}.
89+
* @return must not be {@literal null}.
90+
* @since 1.1
91+
*/
92+
protected KeyValueRepositoryFactory createRepositoryFactory(KeyValueOperations operations,
93+
Class<? extends AbstractQueryCreator<?, ?>> queryCreator) {
94+
95+
return new KeyValueRepositoryFactory(operations, queryCreator);
6396
}
6497

6598
/*
@@ -68,6 +101,10 @@ protected RepositoryFactorySupport createRepositoryFactory() {
68101
*/
69102
@Override
70103
public void afterPropertiesSet() {
104+
105+
Assert.notNull(operations, "KeyValueOperations must not be null!");
106+
Assert.notNull(queryCreator, "Query creator type must not be null!");
107+
71108
super.afterPropertiesSet();
72109
}
73110
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/*
2+
* Copyright 2016 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+
*/
16+
package org.springframework.data.keyvalue.repository.support;
17+
18+
import static org.hamcrest.CoreMatchers.*;
19+
import static org.junit.Assert.*;
20+
import static org.mockito.Mockito.*;
21+
22+
import java.io.Serializable;
23+
24+
import org.junit.Before;
25+
import org.junit.Rule;
26+
import org.junit.Test;
27+
import org.junit.rules.ExpectedException;
28+
import org.springframework.data.keyvalue.core.KeyValueOperations;
29+
import org.springframework.data.repository.Repository;
30+
import org.springframework.data.repository.query.parser.AbstractQueryCreator;
31+
32+
/**
33+
* Unit tests for {@link KeyValueRepositoryFactoryBean}.
34+
*
35+
* @author Oliver Gierke
36+
*/
37+
public class KeyValueRepositoryFactoryBeanUnitTests {
38+
39+
public @Rule ExpectedException exception = ExpectedException.none();
40+
41+
KeyValueRepositoryFactoryBean<?, ?, ?> factoryBean;
42+
43+
@Before
44+
public void setUp() {
45+
this.factoryBean = new KeyValueRepositoryFactoryBean<Repository<Object, Serializable>, Object, Serializable>();
46+
}
47+
48+
/**
49+
* @see DATAKV-123
50+
*/
51+
@Test(expected = IllegalArgumentException.class)
52+
public void rejectsNullKeyValueOperations() {
53+
factoryBean.setKeyValueOperations(null);
54+
}
55+
56+
/**
57+
* @see DATAKV-123
58+
*/
59+
@Test(expected = IllegalArgumentException.class)
60+
public void rejectsNullQueryCreator() {
61+
factoryBean.setQueryCreator(null);
62+
}
63+
64+
/**
65+
* @see DATAKV-123
66+
*/
67+
@Test(expected = IllegalArgumentException.class)
68+
public void rejectsUninitializedInstance() {
69+
factoryBean.afterPropertiesSet();
70+
}
71+
72+
/**
73+
* @see DATAKV-123
74+
*/
75+
@SuppressWarnings("unchecked")
76+
@Test(expected = IllegalArgumentException.class)
77+
public void rejectsInstanceWithoutKeyValueOperations() {
78+
79+
Class<? extends AbstractQueryCreator<?, ?>> creatorType = (Class<? extends AbstractQueryCreator<?, ?>>) mock(
80+
AbstractQueryCreator.class).getClass();
81+
82+
factoryBean.setQueryCreator(creatorType);
83+
factoryBean.afterPropertiesSet();
84+
}
85+
86+
/**
87+
* @see DATAKV-123
88+
*/
89+
@Test(expected = IllegalArgumentException.class)
90+
public void rejectsInstanceWithoutQueryCreator() {
91+
92+
factoryBean.setKeyValueOperations(mock(KeyValueOperations.class));
93+
factoryBean.afterPropertiesSet();
94+
}
95+
96+
/**
97+
* @see DATAKV-123
98+
*/
99+
@Test
100+
@SuppressWarnings("unchecked")
101+
public void initializesConfiguredFactory() {
102+
103+
Class<? extends AbstractQueryCreator<?, ?>> creatorType = (Class<? extends AbstractQueryCreator<?, ?>>) mock(
104+
AbstractQueryCreator.class).getClass();
105+
106+
factoryBean.setQueryCreator(creatorType);
107+
factoryBean.setKeyValueOperations(mock(KeyValueOperations.class));
108+
109+
exception.expect(IllegalArgumentException.class);
110+
exception.expectMessage("Repository interface");
111+
112+
factoryBean.afterPropertiesSet();
113+
}
114+
115+
/**
116+
* @see DATAKV-123
117+
*/
118+
@Test
119+
@SuppressWarnings("unchecked")
120+
public void createsRepositoryFactory() {
121+
122+
Class<? extends AbstractQueryCreator<?, ?>> creatorType = (Class<? extends AbstractQueryCreator<?, ?>>) mock(
123+
AbstractQueryCreator.class).getClass();
124+
125+
factoryBean.setQueryCreator(creatorType);
126+
factoryBean.setKeyValueOperations(mock(KeyValueOperations.class));
127+
128+
assertThat(factoryBean.createRepositoryFactory(), is(notNullValue()));
129+
}
130+
}

0 commit comments

Comments
 (0)