|
18 | 18 | import static org.assertj.core.api.Assertions.assertThat;
|
19 | 19 |
|
20 | 20 | import java.util.Arrays;
|
| 21 | +import java.util.stream.Stream; |
21 | 22 |
|
22 | 23 | import org.junit.jupiter.api.BeforeEach;
|
23 |
| -import org.junit.jupiter.api.Test; |
| 24 | +import org.junit.jupiter.params.ParameterizedTest; |
| 25 | +import org.junit.jupiter.params.provider.Arguments; |
| 26 | +import org.junit.jupiter.params.provider.MethodSource; |
| 27 | +import org.springframework.beans.factory.config.BeanDefinition; |
24 | 28 | import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
25 | 29 | import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
| 30 | +import org.springframework.context.annotation.AnnotationBeanNameGenerator; |
26 | 31 | import org.springframework.core.env.StandardEnvironment;
|
27 | 32 | import org.springframework.core.io.DefaultResourceLoader;
|
28 | 33 | import org.springframework.core.type.AnnotationMetadata;
|
|
34 | 39 | * @author Oliver Gierke
|
35 | 40 | * @author Jens Schauder
|
36 | 41 | * @author Erik Pellizzon
|
| 42 | + * @author Christoph Strobl |
37 | 43 | */
|
38 | 44 | class JpaRepositoriesRegistrarUnitTests {
|
39 | 45 |
|
40 | 46 | private BeanDefinitionRegistry registry;
|
41 |
| - private AnnotationMetadata metadata; |
42 | 47 |
|
43 | 48 | @BeforeEach
|
44 | 49 | void setUp() {
|
45 |
| - |
46 |
| - metadata = AnnotationMetadata.introspect(Config.class); |
47 | 50 | registry = new DefaultListableBeanFactory();
|
48 | 51 | }
|
49 | 52 |
|
50 |
| - @Test |
51 |
| - void configuresRepositoriesCorrectly() { |
| 53 | + |
| 54 | + @ParameterizedTest // GH-499, GH-3440 |
| 55 | + @MethodSource(value = { "args" }) |
| 56 | + void configuresRepositoriesCorrectly(AnnotationMetadata metadata, String[] beanNames) { |
52 | 57 |
|
53 | 58 | JpaRepositoriesRegistrar registrar = new JpaRepositoriesRegistrar();
|
54 | 59 | registrar.setResourceLoader(new DefaultResourceLoader());
|
55 | 60 | registrar.setEnvironment(new StandardEnvironment());
|
56 | 61 | registrar.registerBeanDefinitions(metadata, registry);
|
57 | 62 |
|
58 | 63 | Iterable<String> names = Arrays.asList(registry.getBeanDefinitionNames());
|
59 |
| - assertThat(names).contains("userRepository", "auditableUserRepository", "roleRepository"); |
| 64 | + assertThat(names).contains(beanNames); |
| 65 | + } |
| 66 | + |
| 67 | + static Stream<Arguments> args() { |
| 68 | + return Stream.of( |
| 69 | + Arguments.of(AnnotationMetadata.introspect(Config.class), |
| 70 | + new String[] { "userRepository", "auditableUserRepository", "roleRepository" }), |
| 71 | + Arguments.of(AnnotationMetadata.introspect(ConfigWithBeanNameGenerator.class), |
| 72 | + new String[] { "userREPO", "auditableUserREPO", "roleREPO" })); |
60 | 73 | }
|
61 | 74 |
|
62 | 75 | @EnableJpaRepositories(basePackageClasses = UserRepository.class)
|
63 | 76 | private class Config {
|
64 | 77 |
|
65 | 78 | }
|
| 79 | + |
| 80 | + @EnableJpaRepositories(basePackageClasses = UserRepository.class, nameGenerator = MyBeanNameGenerator.class) |
| 81 | + private class ConfigWithBeanNameGenerator { |
| 82 | + |
| 83 | + } |
| 84 | + |
| 85 | + static class MyBeanNameGenerator extends AnnotationBeanNameGenerator { |
| 86 | + |
| 87 | + @Override |
| 88 | + public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) { |
| 89 | + return super.generateBeanName(definition, registry).replaceAll("Repository", "REPO"); |
| 90 | + } |
| 91 | + } |
66 | 92 | }
|
0 commit comments