|
| 1 | +/* |
| 2 | + * Copyright 2024 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 | + * https://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.ldap.repository.config; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | + |
| 20 | +import java.util.Arrays; |
| 21 | +import java.util.stream.Stream; |
| 22 | + |
| 23 | +import org.junit.jupiter.api.BeforeEach; |
| 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; |
| 28 | +import org.springframework.beans.factory.support.BeanDefinitionRegistry; |
| 29 | +import org.springframework.beans.factory.support.DefaultListableBeanFactory; |
| 30 | +import org.springframework.context.annotation.AnnotationBeanNameGenerator; |
| 31 | +import org.springframework.core.env.StandardEnvironment; |
| 32 | +import org.springframework.core.io.DefaultResourceLoader; |
| 33 | +import org.springframework.core.type.AnnotationMetadata; |
| 34 | +import org.springframework.data.ldap.config.DummyLdapRepository; |
| 35 | + |
| 36 | +/** |
| 37 | + * @author Christoph Strobl |
| 38 | + */ |
| 39 | +class LdapRepositoriesRegistrarUnitTests { |
| 40 | + |
| 41 | + private BeanDefinitionRegistry registry; |
| 42 | + |
| 43 | + @BeforeEach |
| 44 | + void setUp() { |
| 45 | + registry = new DefaultListableBeanFactory(); |
| 46 | + } |
| 47 | + |
| 48 | + @ParameterizedTest // GH-496 |
| 49 | + @MethodSource(value = {"args"}) |
| 50 | + void configuresRepositoriesCorrectly(AnnotationMetadata metadata, String[] beanNames) { |
| 51 | + |
| 52 | + LdapRepositoriesRegistrar registrar = new LdapRepositoriesRegistrar(); |
| 53 | + registrar.setResourceLoader(new DefaultResourceLoader()); |
| 54 | + registrar.setEnvironment(new StandardEnvironment()); |
| 55 | + registrar.registerBeanDefinitions(metadata, registry); |
| 56 | + |
| 57 | + Iterable<String> names = Arrays.asList(registry.getBeanDefinitionNames()); |
| 58 | + assertThat(names).contains(beanNames); |
| 59 | + } |
| 60 | + |
| 61 | + static Stream<Arguments> args() { |
| 62 | + return Stream.of( |
| 63 | + Arguments.of(AnnotationMetadata.introspect(Config.class), |
| 64 | + new String[]{"dummyLdapRepository"}), |
| 65 | + Arguments.of(AnnotationMetadata.introspect(ConfigWithBeanNameGenerator.class), |
| 66 | + new String[]{"dummyLdapREPO"})); |
| 67 | + } |
| 68 | + |
| 69 | + @EnableLdapRepositories(basePackageClasses = DummyLdapRepository.class) |
| 70 | + private class Config { |
| 71 | + |
| 72 | + } |
| 73 | + |
| 74 | + @EnableLdapRepositories(basePackageClasses = DummyLdapRepository.class, nameGenerator = MyBeanNameGenerator.class) |
| 75 | + private class ConfigWithBeanNameGenerator { |
| 76 | + |
| 77 | + } |
| 78 | + |
| 79 | + static class MyBeanNameGenerator extends AnnotationBeanNameGenerator { |
| 80 | + |
| 81 | + @Override |
| 82 | + public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) { |
| 83 | + return super.generateBeanName(definition, registry).replaceAll("Repository", "REPO"); |
| 84 | + } |
| 85 | + } |
| 86 | +} |
0 commit comments