/
RepositoriesUnitTests.java
executable file
·279 lines (210 loc) · 9.29 KB
/
RepositoriesUnitTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except
import org.springframework.data.repository.core.support.DefaultRepositoryMetadata;
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.repository.support;
import static org.assertj.core.api.Assertions.*;
import java.io.Serializable;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.annotation.Primary;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.mapping.context.SampleMappingContext;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.core.EntityInformation;
import org.springframework.data.repository.core.RepositoryInformation;
import org.springframework.data.repository.core.RepositoryMetadata;
import org.springframework.data.repository.core.support.DefaultRepositoryMetadata;
import org.springframework.data.repository.core.support.DummyEntityInformation;
import org.springframework.data.repository.core.support.DummyRepositoryFactoryBean;
import org.springframework.data.repository.core.support.DummyRepositoryInformation;
import org.springframework.data.repository.core.support.RepositoryFactoryInformation;
import org.springframework.data.repository.query.QueryMethod;
import org.springframework.util.ClassUtils;
/**
* Unit tests for {@link Repositories}.
*
* @author Oliver Gierke
* @author Thomas Darimont
*/
@RunWith(MockitoJUnitRunner.Silent.class)
public class RepositoriesUnitTests {
GenericApplicationContext context;
@Before
public void setUp() {
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
beanFactory.registerBeanDefinition("addressRepository", getRepositoryBeanDefinition(AddressRepository.class));
beanFactory.registerBeanDefinition("personRepository", getRepositoryBeanDefinition(PersonRepository.class));
context = new GenericApplicationContext(beanFactory);
context.refresh();
}
private AbstractBeanDefinition getRepositoryBeanDefinition(Class<?> repositoryInterface) {
BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(DummyRepositoryFactoryBean.class);
builder.addConstructorArgValue(repositoryInterface);
return builder.getBeanDefinition();
}
@Test
public void doesNotConsiderCrudRepositoriesOnly() {
Repositories repositories = new Repositories(context);
assertThat(repositories.hasRepositoryFor(Person.class)).isTrue();
assertThat(repositories.hasRepositoryFor(Address.class)).isTrue();
}
@Test
public void doesNotFindInformationForNonManagedDomainClass() {
Repositories repositories = new Repositories(context);
assertThat(repositories.hasRepositoryFor(String.class)).isFalse();
assertThat(repositories.getRepositoryFor(String.class)).isNotPresent();
}
@Test(expected = IllegalArgumentException.class)
public void rejectsNullBeanFactory() {
new Repositories(null);
}
@Test // DATACMNS-256
public void exposesPersistentEntityForDomainTypes() {
Repositories repositories = new Repositories(context);
assertThat(repositories.getPersistentEntity(Person.class)).isNotNull();
assertThat(repositories.getPersistentEntity(Address.class)).isNotNull();
}
@Test // DATACMNS-634
public void findsRepositoryForSubTypes() {
assertThat(new Repositories(context).getPersistentEntity(AdvancedAddress.class)).isNotNull();
}
@Test // DATACMNS-673
public void discoversRepositoryForAlternativeDomainType() {
RepositoryMetadata metadata = new CustomRepositoryMetadata(SampleRepository.class);
RepositoryFactoryInformation<?, ?> information = new SampleRepoFactoryInformation<>(metadata);
GenericApplicationContext context = new GenericApplicationContext();
context.getBeanFactory().registerSingleton("foo", information);
context.refresh();
Repositories repositories = new Repositories(context);
assertThat(repositories.getRepositoryFor(Sample.class)).isNotNull();
assertThat(repositories.getRepositoryFor(SampleEntity.class)).isNotNull();
context.close();
}
@Test // DATACMNS-794
public void exposesRepositoryFactoryInformationForRepository() {
Optional<RepositoryInformation> information = new Repositories(context)
.getRepositoryInformation(PersonRepository.class);
assertThat(information)
.hasValueSatisfying(it -> assertThat(it.getRepositoryInterface()).isEqualTo(PersonRepository.class));
}
@Test // DATACMNS-1215
public void exposesRepositoryForProxyType() {
ProxyFactory factory = new ProxyFactory();
factory.setTarget(new Person());
factory.setProxyTargetClass(true);
Object proxy = factory.getProxy();
assertThat(ClassUtils.isCglibProxy(proxy)).isTrue();
Repositories repositories = new Repositories(context);
assertThat(repositories.hasRepositoryFor(proxy.getClass())).isTrue();
assertThat(repositories.getRepositoryFor(proxy.getClass())).isNotEmpty();
}
@Test // DATACMNS-1448
public void keepsPrimaryRepositoryInCaseOfMultipleOnes() {
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
beanFactory.registerBeanDefinition("first", getRepositoryBeanDefinition(FirstRepository.class));
AbstractBeanDefinition definition = getRepositoryBeanDefinition(PrimaryRepository.class);
definition.setPrimary(true);
beanFactory.registerBeanDefinition("primary", definition);
beanFactory.registerBeanDefinition("third", getRepositoryBeanDefinition(ThirdRepository.class));
context = new GenericApplicationContext(beanFactory);
context.refresh();
Repositories repositories = new Repositories(beanFactory);
assertThat(repositories.getRepositoryFor(SomeEntity.class)).hasValueSatisfying(it -> {
assertThat(it).isInstanceOf(PrimaryRepository.class);
});
}
class Person {}
class Address {}
class AdvancedAddress extends Address {}
interface PersonRepository extends CrudRepository<Person, Long> {}
interface AddressRepository extends Repository<Address, Long> {}
static class SampleRepoFactoryInformation<T, S extends Serializable> implements RepositoryFactoryInformation<T, S> {
private final RepositoryMetadata repositoryMetadata;
private final SampleMappingContext mappingContext;
public SampleRepoFactoryInformation(Class<?> repositoryInterface) {
this(new DefaultRepositoryMetadata(repositoryInterface));
}
public SampleRepoFactoryInformation(RepositoryMetadata metadata) {
this.repositoryMetadata = metadata;
this.mappingContext = new SampleMappingContext();
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public EntityInformation<T, S> getEntityInformation() {
return new DummyEntityInformation(repositoryMetadata.getDomainType());
}
public RepositoryInformation getRepositoryInformation() {
return new DummyRepositoryInformation(repositoryMetadata);
}
public PersistentEntity<?, ?> getPersistentEntity() {
return mappingContext.getRequiredPersistentEntity(repositoryMetadata.getDomainType());
}
public List<QueryMethod> getQueryMethods() {
return Collections.emptyList();
}
}
static class CustomRepositoryMetadata extends DefaultRepositoryMetadata {
private final Class<?> domainType;
/**
* @param repositoryInterface
*/
public CustomRepositoryMetadata(Class<?> repositoryInterface) {
super(repositoryInterface);
String domainType = super.getDomainType().getName().concat("Entity");
try {
this.domainType = ClassUtils.forName(domainType, CustomRepositoryMetadata.class.getClassLoader());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.core.support.DefaultRepositoryMetadata#getDomainType()
*/
@Override
public Class<?> getDomainType() {
return this.domainType;
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.core.support.AbstractRepositoryMetadata#getAlternativeDomainTypes()
*/
@Override
public Set<Class<?>> getAlternativeDomainTypes() {
return Collections.singleton(super.getDomainType());
}
}
interface Sample {}
static class SampleEntity implements Sample {}
interface SampleRepository extends Repository<Sample, Long> {}
interface SomeEntity {}
interface FirstRepository extends Repository<SomeEntity, Long> {}
@Primary
interface PrimaryRepository extends Repository<SomeEntity, Long> {}
interface ThirdRepository extends Repository<SomeEntity, Long> {}
}