-
Notifications
You must be signed in to change notification settings - Fork 350
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for using custom BeanNameGenerator.
Closes: #1853
- Loading branch information
1 parent
c91d3bd
commit 2040247
Showing
4 changed files
with
190 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
...a/org/springframework/data/jdbc/repository/config/JdbcRepositoriesRegistrarUnitTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* 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.jdbc.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.repository.CrudRepository; | ||
|
||
/** | ||
* @author Christoph Strobl | ||
*/ | ||
public class JdbcRepositoriesRegistrarUnitTests { | ||
|
||
private BeanDefinitionRegistry registry; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
registry = new DefaultListableBeanFactory(); | ||
} | ||
|
||
@ParameterizedTest // GH-1853 | ||
@MethodSource(value = { "args" }) | ||
void configuresRepositoriesCorrectly(AnnotationMetadata metadata, String[] beanNames) { | ||
|
||
JdbcRepositoriesRegistrar registrar = new JdbcRepositoriesRegistrar(); | ||
registrar.setResourceLoader(new DefaultResourceLoader()); | ||
registrar.setEnvironment(new StandardEnvironment()); | ||
registrar.registerBeanDefinitions(metadata, registry); | ||
|
||
Iterable<String> names = Arrays.asList(registry.getBeanDefinitionNames()); | ||
assertThat(names).contains(beanNames); | ||
} | ||
|
||
static Stream<Arguments> args() { | ||
return Stream.of( | ||
Arguments.of(AnnotationMetadata.introspect(Config.class), | ||
new String[] { "jdbcRepositoriesRegistrarUnitTests.PersonRepository" }), | ||
Arguments.of(AnnotationMetadata.introspect(ConfigWithBeanNameGenerator.class), | ||
new String[] { "jdbcRepositoriesRegistrarUnitTests.PersonREPO" })); | ||
} | ||
|
||
@EnableJdbcRepositories(basePackageClasses = PersonRepository.class, considerNestedRepositories = true) | ||
private class Config { | ||
|
||
} | ||
|
||
@EnableJdbcRepositories(basePackageClasses = PersonRepository.class, nameGenerator = MyBeanNameGenerator.class, | ||
considerNestedRepositories = true) | ||
private class ConfigWithBeanNameGenerator { | ||
|
||
} | ||
|
||
static class MyBeanNameGenerator extends AnnotationBeanNameGenerator { | ||
|
||
@Override | ||
public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) { | ||
return super.generateBeanName(definition, registry).replaceAll("Repository", "REPO"); | ||
} | ||
} | ||
|
||
static class Person {} | ||
|
||
interface PersonRepository extends CrudRepository<Person, String> {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
...a/org/springframework/data/r2dbc/repository/config/R2dbcRepositoryRegistrarUnitTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* 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.r2dbc.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; | ||
|
||
/** | ||
* @author Christoph Strobl | ||
*/ | ||
public class R2dbcRepositoryRegistrarUnitTests { | ||
|
||
private BeanDefinitionRegistry registry; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
registry = new DefaultListableBeanFactory(); | ||
} | ||
|
||
@ParameterizedTest // GH-1853 | ||
@MethodSource(value = { "args" }) | ||
void configuresRepositoriesCorrectly(AnnotationMetadata metadata, String[] beanNames) { | ||
|
||
R2dbcRepositoriesRegistrar registrar = new R2dbcRepositoriesRegistrar(); | ||
registrar.setResourceLoader(new DefaultResourceLoader()); | ||
registrar.setEnvironment(new StandardEnvironment()); | ||
registrar.registerBeanDefinitions(metadata, registry); | ||
|
||
Iterable<String> names = Arrays.asList(registry.getBeanDefinitionNames()); | ||
assertThat(names).contains(beanNames); | ||
} | ||
|
||
static Stream<Arguments> args() { | ||
return Stream.of(Arguments.of(AnnotationMetadata.introspect(Config.class), new String[] { "personRepository" }), | ||
Arguments.of(AnnotationMetadata.introspect(ConfigWithBeanNameGenerator.class), new String[] { "personREPO" })); | ||
} | ||
|
||
@EnableR2dbcRepositories(basePackageClasses = PersonRepository.class) | ||
private class Config { | ||
|
||
} | ||
|
||
@EnableR2dbcRepositories(basePackageClasses = PersonRepository.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"); | ||
} | ||
} | ||
} |