Browse Source
Adds the @EnableJdbcRepositories annotation, which when used on a Spring configuration enables scanning packages for JDBC repositories. Original pull request: #6.pull/6/merge
8 changed files with 626 additions and 10 deletions
@ -0,0 +1,98 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2017 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.jdbc.repository.config; |
||||||
|
|
||||||
|
import java.lang.annotation.Documented; |
||||||
|
import java.lang.annotation.ElementType; |
||||||
|
import java.lang.annotation.Inherited; |
||||||
|
import java.lang.annotation.Retention; |
||||||
|
import java.lang.annotation.RetentionPolicy; |
||||||
|
import java.lang.annotation.Target; |
||||||
|
|
||||||
|
import org.springframework.beans.factory.FactoryBean; |
||||||
|
import org.springframework.context.annotation.ComponentScan.Filter; |
||||||
|
import org.springframework.context.annotation.Import; |
||||||
|
import org.springframework.data.jdbc.repository.support.JdbcRepositoryFactoryBean; |
||||||
|
|
||||||
|
/** |
||||||
|
* Annotation to enable JDBC repositories. Will scan the package of the annotated configuration class for Spring Data |
||||||
|
* repositories by default. |
||||||
|
* |
||||||
|
* @author Jens Schauder |
||||||
|
* @since 2.0 |
||||||
|
*/ |
||||||
|
@Target(ElementType.TYPE) |
||||||
|
@Retention(RetentionPolicy.RUNTIME) |
||||||
|
@Documented |
||||||
|
@Inherited |
||||||
|
@Import(JdbcRepositoriesRegistrar.class) |
||||||
|
public @interface EnableJdbcRepositories { |
||||||
|
|
||||||
|
/** |
||||||
|
* Alias for the {@link #basePackages()} attribute. Allows for more concise annotation declarations e.g.: |
||||||
|
* {@code @EnableJpaRepositories("org.my.pkg")} instead of {@code @EnableJpaRepositories(basePackages="org.my.pkg")}. |
||||||
|
*/ |
||||||
|
String[] value() default {}; |
||||||
|
|
||||||
|
/** |
||||||
|
* Base packages to scan for annotated components. {@link #value()} is an alias for (and mutually exclusive with) this |
||||||
|
* attribute. Use {@link #basePackageClasses()} for a type-safe alternative to String-based package names. |
||||||
|
*/ |
||||||
|
String[] basePackages() default {}; |
||||||
|
|
||||||
|
/** |
||||||
|
* Type-safe alternative to {@link #basePackages()} for specifying the packages to scan for annotated components. The |
||||||
|
* package of each class specified will be scanned. Consider creating a special no-op marker class or interface in |
||||||
|
* each package that serves no purpose other than being referenced by this attribute. |
||||||
|
*/ |
||||||
|
Class<?>[] basePackageClasses() default {}; |
||||||
|
|
||||||
|
/** |
||||||
|
* Specifies which types are eligible for component scanning. Further narrows the set of candidate components from |
||||||
|
* everything in {@link #basePackages()} to everything in the base packages that matches the given filter or filters. |
||||||
|
*/ |
||||||
|
Filter[] includeFilters() default {}; |
||||||
|
|
||||||
|
/** |
||||||
|
* Specifies which types are not eligible for component scanning. |
||||||
|
*/ |
||||||
|
Filter[] excludeFilters() default {}; |
||||||
|
|
||||||
|
/** |
||||||
|
* Configures whether nested repository-interfaces (e.g. defined as inner classes) should be discovered by the |
||||||
|
* repositories infrastructure. |
||||||
|
*/ |
||||||
|
boolean considerNestedRepositories() default false; |
||||||
|
|
||||||
|
/** |
||||||
|
* Returns the {@link FactoryBean} class to be used for each repository instance. Defaults to |
||||||
|
* {@link JdbcRepositoryFactoryBean}. |
||||||
|
*/ |
||||||
|
Class<?> repositoryFactoryBeanClass() default JdbcRepositoryFactoryBean.class; |
||||||
|
|
||||||
|
/** |
||||||
|
* Configures the location of where to find the Spring Data named queries properties file. Will default to |
||||||
|
* {@code META-INF/jdbc-named-queries.properties}. |
||||||
|
*/ |
||||||
|
String namedQueriesLocation() default ""; |
||||||
|
|
||||||
|
/** |
||||||
|
* Returns the postfix to be used when looking up custom repository implementations. Defaults to {@literal Impl}. So |
||||||
|
* for a repository named {@code PersonRepository} the corresponding implementation class will be looked up scanning |
||||||
|
* for {@code PersonRepositoryImpl}. |
||||||
|
*/ |
||||||
|
String repositoryImplementationPostfix() default "Impl"; |
||||||
|
} |
||||||
@ -0,0 +1,49 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2017 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.jdbc.repository.config; |
||||||
|
|
||||||
|
import java.lang.annotation.Annotation; |
||||||
|
|
||||||
|
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; |
||||||
|
import org.springframework.data.repository.config.RepositoryBeanDefinitionRegistrarSupport; |
||||||
|
import org.springframework.data.repository.config.RepositoryConfigurationExtension; |
||||||
|
|
||||||
|
/** |
||||||
|
* {@link ImportBeanDefinitionRegistrar} to enable {@link EnableJdbcRepositories} annotation. |
||||||
|
* |
||||||
|
* @author Jens Schauder |
||||||
|
* @since 2.0 |
||||||
|
*/ |
||||||
|
class JdbcRepositoriesRegistrar extends RepositoryBeanDefinitionRegistrarSupport { |
||||||
|
|
||||||
|
/* |
||||||
|
* (non-Javadoc) |
||||||
|
* @see org.springframework.data.repository.config.RepositoryBeanDefinitionRegistrarSupport#getAnnotation() |
||||||
|
*/ |
||||||
|
@Override |
||||||
|
protected Class<? extends Annotation> getAnnotation() { |
||||||
|
return EnableJdbcRepositories.class; |
||||||
|
} |
||||||
|
|
||||||
|
/* |
||||||
|
* (non-Javadoc) |
||||||
|
* @see org.springframework.data.repository.config.RepositoryBeanDefinitionRegistrarSupport#getExtension() |
||||||
|
*/ |
||||||
|
@Override |
||||||
|
protected RepositoryConfigurationExtension getExtension() { |
||||||
|
return new JdbcRepositoryConfigExtension(); |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,59 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2017 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.jdbc.repository.config; |
||||||
|
|
||||||
|
import java.util.Locale; |
||||||
|
|
||||||
|
import org.springframework.data.jdbc.repository.support.JdbcRepositoryFactoryBean; |
||||||
|
import org.springframework.data.repository.config.RepositoryConfigurationExtensionSupport; |
||||||
|
|
||||||
|
/** |
||||||
|
* {@link org.springframework.data.repository.config.RepositoryConfigurationExtension} extending the repository |
||||||
|
* registration process by registering JDBC repositories. |
||||||
|
* |
||||||
|
* @author Jens Schauder |
||||||
|
* @since 2.0 |
||||||
|
*/ |
||||||
|
public class JdbcRepositoryConfigExtension extends RepositoryConfigurationExtensionSupport { |
||||||
|
|
||||||
|
/* |
||||||
|
* (non-Javadoc) |
||||||
|
* @see org.springframework.data.repository.config.RepositoryConfigurationExtension#getModuleName() |
||||||
|
*/ |
||||||
|
@Override |
||||||
|
public String getModuleName() { |
||||||
|
return "JDBC"; |
||||||
|
} |
||||||
|
|
||||||
|
/* |
||||||
|
* (non-Javadoc) |
||||||
|
* @see org.springframework.data.repository.config.RepositoryConfigurationExtensionSupport#getRepositoryFactoryBeanClassName() |
||||||
|
*/ |
||||||
|
@Override |
||||||
|
public String getRepositoryFactoryBeanClassName() { |
||||||
|
return JdbcRepositoryFactoryBean.class.getName(); |
||||||
|
} |
||||||
|
|
||||||
|
/* |
||||||
|
* (non-Javadoc) |
||||||
|
* @see org.springframework.data.repository.config.RepositoryConfigurationExtensionSupport#getModulePrefix() |
||||||
|
*/ |
||||||
|
@Override |
||||||
|
protected String getModulePrefix() { |
||||||
|
return getModuleName().toLowerCase(Locale.US); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,100 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2017 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.jdbc.repository.support; |
||||||
|
|
||||||
|
import java.io.Serializable; |
||||||
|
import java.util.Map; |
||||||
|
import java.util.Optional; |
||||||
|
|
||||||
|
import javax.sql.DataSource; |
||||||
|
|
||||||
|
import org.springframework.context.ApplicationContext; |
||||||
|
import org.springframework.context.ApplicationEventPublisher; |
||||||
|
import org.springframework.data.repository.Repository; |
||||||
|
import org.springframework.data.repository.core.support.RepositoryFactorySupport; |
||||||
|
import org.springframework.data.repository.core.support.TransactionalRepositoryFactoryBeanSupport; |
||||||
|
import org.springframework.data.util.Optionals; |
||||||
|
import org.springframework.jdbc.core.JdbcOperations; |
||||||
|
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations; |
||||||
|
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; |
||||||
|
|
||||||
|
/** |
||||||
|
* Special adapter for Springs {@link org.springframework.beans.factory.FactoryBean} interface to allow easy setup of |
||||||
|
* repository factories via Spring configuration. |
||||||
|
* |
||||||
|
* @author Jens Schauder |
||||||
|
* @since 2.0 |
||||||
|
*/ |
||||||
|
public class JdbcRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extends Serializable> //
|
||||||
|
extends TransactionalRepositoryFactoryBeanSupport<T, S, ID> { |
||||||
|
|
||||||
|
private static final String NO_NAMED_PARAMETER_JDBC_OPERATION_ERROR_MESSAGE = //
|
||||||
|
"No unique NamedParameterJdbcOperation could be found, " //
|
||||||
|
+ "nor JdbcOperations or DataSource to construct one from."; |
||||||
|
|
||||||
|
private static final String NAMED_PARAMETER_JDBC_OPERATIONS_BEAN_NAME = "namedParameterJdbcTemplate"; |
||||||
|
private static final String JDBC_OPERATIONS_BEAN_NAME = "jdbcTemplate"; |
||||||
|
private static final String DATA_SOURCE_BEAN_NAME = "dataSource"; |
||||||
|
|
||||||
|
private final ApplicationEventPublisher applicationEventPublisher; |
||||||
|
private final ApplicationContext context; |
||||||
|
|
||||||
|
JdbcRepositoryFactoryBean(Class<? extends T> repositoryInterface, ApplicationEventPublisher applicationEventPublisher, |
||||||
|
ApplicationContext context) { |
||||||
|
|
||||||
|
super(repositoryInterface); |
||||||
|
this.applicationEventPublisher = applicationEventPublisher; |
||||||
|
this.context = context; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
protected RepositoryFactorySupport doCreateRepositoryFactory() { |
||||||
|
return new JdbcRepositoryFactory(findOrCreateJdbcOperations(), applicationEventPublisher); |
||||||
|
} |
||||||
|
|
||||||
|
private NamedParameterJdbcOperations findOrCreateJdbcOperations() { |
||||||
|
|
||||||
|
return Optionals |
||||||
|
.firstNonEmpty( //
|
||||||
|
this::getNamedParameterJdbcOperations, //
|
||||||
|
() -> getJdbcOperations().map(NamedParameterJdbcTemplate::new), //
|
||||||
|
() -> getDataSource().map(NamedParameterJdbcTemplate::new)) //
|
||||||
|
.orElseThrow(() -> new IllegalStateException(NO_NAMED_PARAMETER_JDBC_OPERATION_ERROR_MESSAGE)); |
||||||
|
} |
||||||
|
|
||||||
|
private Optional<NamedParameterJdbcOperations> getNamedParameterJdbcOperations() { |
||||||
|
return getBean(NamedParameterJdbcOperations.class, NAMED_PARAMETER_JDBC_OPERATIONS_BEAN_NAME); |
||||||
|
} |
||||||
|
|
||||||
|
private Optional<JdbcOperations> getJdbcOperations() { |
||||||
|
return getBean(JdbcOperations.class, JDBC_OPERATIONS_BEAN_NAME); |
||||||
|
} |
||||||
|
|
||||||
|
private Optional<DataSource> getDataSource() { |
||||||
|
return getBean(DataSource.class, DATA_SOURCE_BEAN_NAME); |
||||||
|
} |
||||||
|
|
||||||
|
private <R> Optional<R> getBean(Class<R> type, String name) { |
||||||
|
|
||||||
|
Map<String, R> beansOfType = context.getBeansOfType(type); |
||||||
|
|
||||||
|
if (beansOfType.size() == 1) { |
||||||
|
return beansOfType.values().stream().findFirst(); |
||||||
|
} |
||||||
|
|
||||||
|
return Optional.ofNullable(beansOfType.get(name)); |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,74 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2017 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.jdbc.repository.config; |
||||||
|
|
||||||
|
import static org.junit.Assert.*; |
||||||
|
|
||||||
|
import lombok.Data; |
||||||
|
|
||||||
|
import org.junit.Test; |
||||||
|
import org.junit.runner.RunWith; |
||||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||||
|
import org.springframework.context.annotation.Bean; |
||||||
|
import org.springframework.context.annotation.ComponentScan; |
||||||
|
import org.springframework.data.annotation.Id; |
||||||
|
import org.springframework.data.jdbc.repository.JdbcRepositoryIntegrationTests; |
||||||
|
import org.springframework.data.jdbc.repository.config.EnableJdbcRepositoriesIntegrationTests.TestConfiguration; |
||||||
|
import org.springframework.data.repository.CrudRepository; |
||||||
|
import org.springframework.test.context.ContextConfiguration; |
||||||
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
||||||
|
|
||||||
|
/** |
||||||
|
* Tests the {@link EnableJdbcRepositories} annotation. |
||||||
|
* |
||||||
|
* @author Jens Schauder |
||||||
|
*/ |
||||||
|
@RunWith(SpringJUnit4ClassRunner.class) |
||||||
|
@ContextConfiguration(classes = TestConfiguration.class) |
||||||
|
public class EnableJdbcRepositoriesIntegrationTests { |
||||||
|
|
||||||
|
@Autowired DummyRepository repository; |
||||||
|
|
||||||
|
@Test // DATAJDBC-100
|
||||||
|
public void repositoryGetsPickedUp() { |
||||||
|
|
||||||
|
assertNotNull(repository); |
||||||
|
|
||||||
|
Iterable<DummyEntity> all = repository.findAll(); |
||||||
|
|
||||||
|
assertNotNull(all); |
||||||
|
} |
||||||
|
|
||||||
|
interface DummyRepository extends CrudRepository<DummyEntity, Long> { |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
@Data |
||||||
|
static class DummyEntity { |
||||||
|
@Id private Long id; |
||||||
|
} |
||||||
|
|
||||||
|
@ComponentScan("org.springframework.data.jdbc.testing") |
||||||
|
@EnableJdbcRepositories(considerNestedRepositories = true) |
||||||
|
static class TestConfiguration { |
||||||
|
|
||||||
|
@Bean |
||||||
|
Class<?> testClass() { |
||||||
|
return JdbcRepositoryIntegrationTests.class; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,209 @@ |
|||||||
|
package org.springframework.data.jdbc.repository.support; |
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.*; |
||||||
|
import static org.mockito.Mockito.*; |
||||||
|
import static org.springframework.test.util.ReflectionTestUtils.*; |
||||||
|
|
||||||
|
import java.util.HashMap; |
||||||
|
import java.util.Map; |
||||||
|
import java.util.function.Predicate; |
||||||
|
|
||||||
|
import javax.sql.DataSource; |
||||||
|
|
||||||
|
import org.assertj.core.api.Condition; |
||||||
|
import org.junit.Test; |
||||||
|
import org.springframework.context.ApplicationContext; |
||||||
|
import org.springframework.context.ApplicationEventPublisher; |
||||||
|
import org.springframework.data.annotation.Id; |
||||||
|
import org.springframework.data.repository.CrudRepository; |
||||||
|
import org.springframework.data.repository.core.support.RepositoryFactorySupport; |
||||||
|
import org.springframework.jdbc.core.JdbcOperations; |
||||||
|
import org.springframework.jdbc.core.JdbcTemplate; |
||||||
|
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations; |
||||||
|
|
||||||
|
/** |
||||||
|
* Tests the dependency injection for {@link JdbcRepositoryFactoryBean}. |
||||||
|
* |
||||||
|
* @author Jens Schauder |
||||||
|
*/ |
||||||
|
public class JdbcRepositoryFactoryBeanUnitTests { |
||||||
|
|
||||||
|
static final String JDBC_OPERATIONS_FIELD_NAME = "jdbcOperations"; |
||||||
|
static final String EXPECTED_JDBC_OPERATIONS_BEAN_NAME = "jdbcTemplate"; |
||||||
|
static final String EXPECTED_NAMED_PARAMETER_JDBC_OPERATIONS_BEAN_NAME = "namedParameterJdbcTemplate"; |
||||||
|
|
||||||
|
ApplicationEventPublisher eventPublisher = mock(ApplicationEventPublisher.class); |
||||||
|
ApplicationContext context = mock(ApplicationContext.class); |
||||||
|
|
||||||
|
Map<String, DataSource> dataSources = new HashMap<>(); |
||||||
|
Map<String, JdbcOperations> jdbcOperations = new HashMap<>(); |
||||||
|
Map<String, NamedParameterJdbcOperations> namedJdbcOperations = new HashMap<>(); |
||||||
|
|
||||||
|
{ |
||||||
|
|
||||||
|
when(context.getBeansOfType(DataSource.class)).thenReturn(dataSources); |
||||||
|
when(context.getBeansOfType(JdbcOperations.class)).thenReturn(jdbcOperations); |
||||||
|
when(context.getBeansOfType(NamedParameterJdbcOperations.class)).thenReturn(namedJdbcOperations); |
||||||
|
} |
||||||
|
|
||||||
|
@Test // DATAJDBC-100
|
||||||
|
public void exceptionWithUsefulMessage() { |
||||||
|
|
||||||
|
JdbcRepositoryFactoryBean<DummyEntityRepository, DummyEntity, Long> factoryBean = //
|
||||||
|
new JdbcRepositoryFactoryBean<>(DummyEntityRepository.class, eventPublisher, context); |
||||||
|
|
||||||
|
assertThatExceptionOfType(IllegalStateException.class) //
|
||||||
|
.isThrownBy(() -> factoryBean.doCreateRepositoryFactory()); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
@Test // DATAJDBC-100
|
||||||
|
public void singleDataSourceGetsUsedForCreatingRepositoryFactory() { |
||||||
|
|
||||||
|
DataSource expectedDataSource = mock(DataSource.class); |
||||||
|
dataSources.put("arbitraryName", expectedDataSource); |
||||||
|
|
||||||
|
JdbcRepositoryFactoryBean<DummyEntityRepository, DummyEntity, Long> factoryBean = //
|
||||||
|
new JdbcRepositoryFactoryBean<>(DummyEntityRepository.class, eventPublisher, context); |
||||||
|
|
||||||
|
assertThat(factoryBean.doCreateRepositoryFactory()).is(using(expectedDataSource)); |
||||||
|
} |
||||||
|
|
||||||
|
@Test // DATAJDBC-100
|
||||||
|
public void multipleDataSourcesGetDisambiguatedByName() { |
||||||
|
|
||||||
|
DataSource expectedDataSource = mock(DataSource.class); |
||||||
|
dataSources.put("dataSource", expectedDataSource); |
||||||
|
dataSources.put("arbitraryName", mock(DataSource.class)); |
||||||
|
|
||||||
|
JdbcRepositoryFactoryBean<DummyEntityRepository, DummyEntity, Long> factoryBean = //
|
||||||
|
new JdbcRepositoryFactoryBean<>(DummyEntityRepository.class, eventPublisher, context); |
||||||
|
|
||||||
|
assertThat(factoryBean.doCreateRepositoryFactory()).is(using(expectedDataSource)); |
||||||
|
} |
||||||
|
|
||||||
|
@Test // DATAJDBC-100
|
||||||
|
public void singleJdbcOperationsUsedForCreatingRepositoryFactory() { |
||||||
|
|
||||||
|
JdbcOperations expectedOperations = mock(JdbcOperations.class); |
||||||
|
jdbcOperations.put("arbitraryName", expectedOperations); |
||||||
|
|
||||||
|
JdbcRepositoryFactoryBean<DummyEntityRepository, DummyEntity, Long> factoryBean = //
|
||||||
|
new JdbcRepositoryFactoryBean<>(DummyEntityRepository.class, eventPublisher, context); |
||||||
|
|
||||||
|
assertThat(factoryBean.doCreateRepositoryFactory()).is(using(expectedOperations)); |
||||||
|
} |
||||||
|
|
||||||
|
@Test // DATAJDBC-100
|
||||||
|
public void multipleJdbcOperationsGetDisambiguatedByName() { |
||||||
|
|
||||||
|
JdbcOperations expectedOperations = mock(JdbcOperations.class); |
||||||
|
jdbcOperations.put(EXPECTED_JDBC_OPERATIONS_BEAN_NAME, expectedOperations); |
||||||
|
jdbcOperations.put("arbitraryName", mock(JdbcOperations.class)); |
||||||
|
|
||||||
|
JdbcRepositoryFactoryBean<DummyEntityRepository, DummyEntity, Long> factoryBean = //
|
||||||
|
new JdbcRepositoryFactoryBean<>(DummyEntityRepository.class, eventPublisher, context); |
||||||
|
|
||||||
|
assertThat(factoryBean.doCreateRepositoryFactory()).is(using(expectedOperations)); |
||||||
|
} |
||||||
|
|
||||||
|
@Test // DATAJDBC-100
|
||||||
|
public void singleNamedJdbcOperationsUsedForCreatingRepositoryFactory() { |
||||||
|
|
||||||
|
NamedParameterJdbcOperations expectedOperations = mock(NamedParameterJdbcOperations.class); |
||||||
|
namedJdbcOperations.put("arbitraryName", expectedOperations); |
||||||
|
|
||||||
|
JdbcRepositoryFactoryBean<DummyEntityRepository, DummyEntity, Long> factoryBean = //
|
||||||
|
new JdbcRepositoryFactoryBean<>(DummyEntityRepository.class, eventPublisher, context); |
||||||
|
|
||||||
|
assertThat(factoryBean.doCreateRepositoryFactory()).is(using(expectedOperations)); |
||||||
|
} |
||||||
|
|
||||||
|
@Test // DATAJDBC-100
|
||||||
|
public void multipleNamedJdbcOperationsGetDisambiguatedByName() { |
||||||
|
|
||||||
|
NamedParameterJdbcOperations expectedOperations = mock(NamedParameterJdbcOperations.class); |
||||||
|
namedJdbcOperations.put(EXPECTED_NAMED_PARAMETER_JDBC_OPERATIONS_BEAN_NAME, expectedOperations); |
||||||
|
namedJdbcOperations.put("arbitraryName", mock(NamedParameterJdbcOperations.class)); |
||||||
|
|
||||||
|
JdbcRepositoryFactoryBean<DummyEntityRepository, DummyEntity, Long> factoryBean = //
|
||||||
|
new JdbcRepositoryFactoryBean<>(DummyEntityRepository.class, eventPublisher, context); |
||||||
|
|
||||||
|
assertThat(factoryBean.doCreateRepositoryFactory()).is(using(expectedOperations)); |
||||||
|
} |
||||||
|
|
||||||
|
@Test // DATAJDBC-100
|
||||||
|
public void namedParameterJdbcOperationsTakePrecedenceOverDataSource() { |
||||||
|
|
||||||
|
NamedParameterJdbcOperations expectedOperations = mock(NamedParameterJdbcOperations.class); |
||||||
|
namedJdbcOperations.put("arbitraryName", expectedOperations); |
||||||
|
dataSources.put("arbitraryName", mock(DataSource.class)); |
||||||
|
|
||||||
|
JdbcRepositoryFactoryBean<DummyEntityRepository, DummyEntity, Long> factoryBean = //
|
||||||
|
new JdbcRepositoryFactoryBean<>(DummyEntityRepository.class, eventPublisher, context); |
||||||
|
|
||||||
|
assertThat(factoryBean.doCreateRepositoryFactory()).is(using(expectedOperations)); |
||||||
|
} |
||||||
|
|
||||||
|
@Test // DATAJDBC-100
|
||||||
|
public void jdbcOperationsTakePrecedenceOverDataSource() { |
||||||
|
|
||||||
|
JdbcOperations expectedOperations = mock(JdbcOperations.class); |
||||||
|
jdbcOperations.put("arbitraryName", expectedOperations); |
||||||
|
dataSources.put("arbitraryName", mock(DataSource.class)); |
||||||
|
|
||||||
|
JdbcRepositoryFactoryBean<DummyEntityRepository, DummyEntity, Long> factoryBean = //
|
||||||
|
new JdbcRepositoryFactoryBean<>(DummyEntityRepository.class, eventPublisher, context); |
||||||
|
|
||||||
|
assertThat(factoryBean.doCreateRepositoryFactory()).is(using(expectedOperations)); |
||||||
|
} |
||||||
|
|
||||||
|
@Test // DATAJDBC-100
|
||||||
|
public void namedParameterJdbcOperationsTakePrecedenceOverJdbcOperations() { |
||||||
|
|
||||||
|
NamedParameterJdbcOperations expectedOperations = mock(NamedParameterJdbcOperations.class); |
||||||
|
namedJdbcOperations.put("arbitraryName", expectedOperations); |
||||||
|
jdbcOperations.put("arbitraryName", mock(JdbcOperations.class)); |
||||||
|
|
||||||
|
JdbcRepositoryFactoryBean<DummyEntityRepository, DummyEntity, Long> factoryBean = //
|
||||||
|
new JdbcRepositoryFactoryBean<>(DummyEntityRepository.class, eventPublisher, context); |
||||||
|
|
||||||
|
assertThat(factoryBean.doCreateRepositoryFactory()).is(using(expectedOperations)); |
||||||
|
} |
||||||
|
|
||||||
|
private Condition<? super RepositoryFactorySupport> using(NamedParameterJdbcOperations expectedOperations) { |
||||||
|
|
||||||
|
Predicate<RepositoryFactorySupport> predicate = r -> getField(r, JDBC_OPERATIONS_FIELD_NAME) == expectedOperations; |
||||||
|
return new Condition<>(predicate, "uses " + expectedOperations); |
||||||
|
} |
||||||
|
|
||||||
|
private Condition<? super RepositoryFactorySupport> using(JdbcOperations expectedOperations) { |
||||||
|
|
||||||
|
Predicate<RepositoryFactorySupport> predicate = r -> { |
||||||
|
NamedParameterJdbcOperations namedOperations = (NamedParameterJdbcOperations) getField(r, |
||||||
|
JDBC_OPERATIONS_FIELD_NAME); |
||||||
|
return namedOperations.getJdbcOperations() == expectedOperations; |
||||||
|
}; |
||||||
|
|
||||||
|
return new Condition<>(predicate, "uses " + expectedOperations); |
||||||
|
} |
||||||
|
|
||||||
|
private Condition<? super RepositoryFactorySupport> using(DataSource expectedDataSource) { |
||||||
|
|
||||||
|
Predicate<RepositoryFactorySupport> predicate = r -> { |
||||||
|
|
||||||
|
NamedParameterJdbcOperations namedOperations = (NamedParameterJdbcOperations) getField(r, |
||||||
|
JDBC_OPERATIONS_FIELD_NAME); |
||||||
|
JdbcTemplate jdbcOperations = (JdbcTemplate) namedOperations.getJdbcOperations(); |
||||||
|
return jdbcOperations.getDataSource() == expectedDataSource; |
||||||
|
}; |
||||||
|
|
||||||
|
return new Condition<>(predicate, "using " + expectedDataSource); |
||||||
|
} |
||||||
|
|
||||||
|
private static class DummyEntity { |
||||||
|
@Id private Long id; |
||||||
|
} |
||||||
|
|
||||||
|
private interface DummyEntityRepository extends CrudRepository<DummyEntity, Long> {} |
||||||
|
} |
||||||
Loading…
Reference in new issue