#13 - Add @EnableR2dbcRepositories.
We now provide an annotation-based activation model for R2DBC repositories. Configuration classes can be annotated with @EnableR2dbcRepositories and configuration infrastructure will scan for bean definitions to implement declared R2DBC repositories. Original pull request: #17.
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
package org.springframework.data.r2dbc.repository.config;
|
||||
|
||||
/**
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
class Person {}
|
||||
@@ -0,0 +1,8 @@
|
||||
package org.springframework.data.r2dbc.repository.config;
|
||||
|
||||
import org.springframework.data.r2dbc.repository.R2dbcRepository;
|
||||
|
||||
/**
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
interface PersonRepository extends R2dbcRepository<Person, String> {}
|
||||
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Copyright 2016-2018 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
|
||||
*
|
||||
* 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.r2dbc.repository.config;
|
||||
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.r2dbc.function.DatabaseClient;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
/**
|
||||
* Integration tests for {@link R2dbcRepositoriesRegistrar}.
|
||||
*
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration
|
||||
public class R2dbcRepositoriesRegistrarTests {
|
||||
|
||||
@Configuration
|
||||
@EnableR2dbcRepositories(basePackages = "org.springframework.data.r2dbc.repository.config")
|
||||
static class Config {
|
||||
|
||||
@Bean
|
||||
public DatabaseClient databaseClient() {
|
||||
return mock(DatabaseClient.class);
|
||||
}
|
||||
}
|
||||
|
||||
@Autowired PersonRepository personRepository;
|
||||
@Autowired ApplicationContext context;
|
||||
|
||||
@Test // gh-13
|
||||
public void testConfiguration() {}
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
/*
|
||||
* Copyright 2016-2018 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
|
||||
*
|
||||
* 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.r2dbc.repository.config;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.core.env.StandardEnvironment;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
|
||||
import org.springframework.core.type.StandardAnnotationMetadata;
|
||||
import org.springframework.data.r2dbc.repository.R2dbcRepository;
|
||||
import org.springframework.data.relational.core.mapping.Table;
|
||||
import org.springframework.data.repository.config.AnnotationRepositoryConfigurationSource;
|
||||
import org.springframework.data.repository.config.RepositoryConfiguration;
|
||||
import org.springframework.data.repository.config.RepositoryConfigurationSource;
|
||||
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link R2dbcRepositoryConfigurationExtension}.
|
||||
*
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
public class R2dbcRepositoryConfigurationExtensionUnitTests {
|
||||
|
||||
StandardAnnotationMetadata metadata = new StandardAnnotationMetadata(Config.class, true);
|
||||
ResourceLoader loader = new PathMatchingResourcePatternResolver();
|
||||
Environment environment = new StandardEnvironment();
|
||||
BeanDefinitionRegistry registry = new DefaultListableBeanFactory();
|
||||
|
||||
RepositoryConfigurationSource configurationSource = new AnnotationRepositoryConfigurationSource(metadata,
|
||||
EnableR2dbcRepositories.class, loader, environment, registry);
|
||||
|
||||
@Test // gh-13
|
||||
public void isStrictMatchIfDomainTypeIsAnnotatedWithDocument() {
|
||||
|
||||
R2dbcRepositoryConfigurationExtension extension = new R2dbcRepositoryConfigurationExtension();
|
||||
assertHasRepo(SampleRepository.class, extension.getRepositoryConfigurations(configurationSource, loader, true));
|
||||
}
|
||||
|
||||
@Test // gh-13
|
||||
public void isStrictMatchIfRepositoryExtendsStoreSpecificBase() {
|
||||
|
||||
R2dbcRepositoryConfigurationExtension extension = new R2dbcRepositoryConfigurationExtension();
|
||||
assertHasRepo(StoreRepository.class, extension.getRepositoryConfigurations(configurationSource, loader, true));
|
||||
}
|
||||
|
||||
@Test // gh-13
|
||||
public void isNotStrictMatchIfDomainTypeIsNotAnnotatedWithDocument() {
|
||||
|
||||
R2dbcRepositoryConfigurationExtension extension = new R2dbcRepositoryConfigurationExtension();
|
||||
assertDoesNotHaveRepo(UnannotatedRepository.class,
|
||||
extension.getRepositoryConfigurations(configurationSource, loader, true));
|
||||
}
|
||||
|
||||
private static void assertHasRepo(Class<?> repositoryInterface,
|
||||
Collection<RepositoryConfiguration<RepositoryConfigurationSource>> configs) {
|
||||
|
||||
for (RepositoryConfiguration<?> config : configs) {
|
||||
if (config.getRepositoryInterface().equals(repositoryInterface.getName())) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
fail("Expected to find config for repository interface ".concat(repositoryInterface.getName()).concat(" but got ")
|
||||
.concat(configs.toString()));
|
||||
}
|
||||
|
||||
private static void assertDoesNotHaveRepo(Class<?> repositoryInterface,
|
||||
Collection<RepositoryConfiguration<RepositoryConfigurationSource>> configs) {
|
||||
|
||||
for (RepositoryConfiguration<?> config : configs) {
|
||||
if (config.getRepositoryInterface().equals(repositoryInterface.getName())) {
|
||||
fail("Expected not to find config for repository interface ".concat(repositoryInterface.getName()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@EnableR2dbcRepositories(considerNestedRepositories = true)
|
||||
static class Config {}
|
||||
|
||||
@Table("sample")
|
||||
static class Sample {}
|
||||
|
||||
static class Store {}
|
||||
|
||||
interface SampleRepository extends ReactiveCrudRepository<Sample, Long> {}
|
||||
|
||||
interface UnannotatedRepository extends ReactiveCrudRepository<Store, Long> {}
|
||||
|
||||
interface StoreRepository extends R2dbcRepository<Store, Long> {}
|
||||
}
|
||||
Reference in New Issue
Block a user