From 9842b1cad5a6990710a8b45946e54d31d1b262fe Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Fri, 9 Aug 2024 11:31:27 +0200 Subject: [PATCH] Add support for using custom BeanNameGenerator. Closes: #496 --- .../config/EnableLdapRepositories.java | 9 ++ .../LdapRepositoriesRegistrarUnitTests.java | 86 +++++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 src/test/java/org/springframework/data/ldap/repository/config/LdapRepositoriesRegistrarUnitTests.java diff --git a/src/main/java/org/springframework/data/ldap/repository/config/EnableLdapRepositories.java b/src/main/java/org/springframework/data/ldap/repository/config/EnableLdapRepositories.java index 0965258..210fcc9 100644 --- a/src/main/java/org/springframework/data/ldap/repository/config/EnableLdapRepositories.java +++ b/src/main/java/org/springframework/data/ldap/repository/config/EnableLdapRepositories.java @@ -22,6 +22,7 @@ import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; +import org.springframework.beans.factory.support.BeanNameGenerator; import org.springframework.context.annotation.ComponentScan.Filter; import org.springframework.context.annotation.Import; import org.springframework.data.ldap.repository.support.LdapRepositoryFactoryBean; @@ -34,6 +35,7 @@ * * @author Mattias Hellborg Arthursson * @author Mark Paluch + * @author Christoph Strobl */ @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @@ -114,6 +116,13 @@ */ Class repositoryBaseClass() default DefaultRepositoryBaseClass.class; + /** + * Configure a specific {@link BeanNameGenerator} to be used when creating the repository beans. + * @return the {@link BeanNameGenerator} to be used or the base {@link BeanNameGenerator} interface to indicate context default. + * @since 3.4 + */ + Class nameGenerator() default BeanNameGenerator.class; + /** * Configures the name of the {@link org.springframework.ldap.core.LdapTemplate} bean to be used with the repositories * detected. diff --git a/src/test/java/org/springframework/data/ldap/repository/config/LdapRepositoriesRegistrarUnitTests.java b/src/test/java/org/springframework/data/ldap/repository/config/LdapRepositoriesRegistrarUnitTests.java new file mode 100644 index 0000000..878f74e --- /dev/null +++ b/src/test/java/org/springframework/data/ldap/repository/config/LdapRepositoriesRegistrarUnitTests.java @@ -0,0 +1,86 @@ +/* + * Copyright 2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://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.ldap.repository.config; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.Arrays; +import java.util.stream.Stream; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; +import org.springframework.beans.factory.config.BeanDefinition; +import org.springframework.beans.factory.support.BeanDefinitionRegistry; +import org.springframework.beans.factory.support.DefaultListableBeanFactory; +import org.springframework.context.annotation.AnnotationBeanNameGenerator; +import org.springframework.core.env.StandardEnvironment; +import org.springframework.core.io.DefaultResourceLoader; +import org.springframework.core.type.AnnotationMetadata; +import org.springframework.data.ldap.config.DummyLdapRepository; + +/** + * @author Christoph Strobl + */ +class LdapRepositoriesRegistrarUnitTests { + + private BeanDefinitionRegistry registry; + + @BeforeEach + void setUp() { + registry = new DefaultListableBeanFactory(); + } + + @ParameterizedTest // GH-496 + @MethodSource(value = {"args"}) + void configuresRepositoriesCorrectly(AnnotationMetadata metadata, String[] beanNames) { + + LdapRepositoriesRegistrar registrar = new LdapRepositoriesRegistrar(); + registrar.setResourceLoader(new DefaultResourceLoader()); + registrar.setEnvironment(new StandardEnvironment()); + registrar.registerBeanDefinitions(metadata, registry); + + Iterable names = Arrays.asList(registry.getBeanDefinitionNames()); + assertThat(names).contains(beanNames); + } + + static Stream args() { + return Stream.of( + Arguments.of(AnnotationMetadata.introspect(Config.class), + new String[]{"dummyLdapRepository"}), + Arguments.of(AnnotationMetadata.introspect(ConfigWithBeanNameGenerator.class), + new String[]{"dummyLdapREPO"})); + } + + @EnableLdapRepositories(basePackageClasses = DummyLdapRepository.class) + private class Config { + + } + + @EnableLdapRepositories(basePackageClasses = DummyLdapRepository.class, nameGenerator = MyBeanNameGenerator.class) + private class ConfigWithBeanNameGenerator { + + } + + static class MyBeanNameGenerator extends AnnotationBeanNameGenerator { + + @Override + public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) { + return super.generateBeanName(definition, registry).replaceAll("Repository", "REPO"); + } + } +}