DATAKV-123 - Introduced dedicated KeyValueRepositoryFactoryBean.createRepositoryFactory(…).
We now expose a dedicated method for implementors to use to create the repository factory instance based on the configuration of the factory. Added unit tests to make sure the factory bean fails fast if it's configured improperly. Original Pull Request: #17
This commit is contained in:
committed by
Christoph Strobl
parent
40fe2addac
commit
874b2bc880
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014 the original author or authors.
|
||||
* Copyright 2014-2016 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.
|
||||
@@ -19,16 +19,19 @@ import java.io.Serializable;
|
||||
|
||||
import org.springframework.data.keyvalue.core.KeyValueOperations;
|
||||
import org.springframework.data.keyvalue.repository.KeyValueRepository;
|
||||
import org.springframework.data.keyvalue.repository.config.QueryCreatorType;
|
||||
import org.springframework.data.mapping.context.MappingContext;
|
||||
import org.springframework.data.repository.Repository;
|
||||
import org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport;
|
||||
import org.springframework.data.repository.core.support.RepositoryFactorySupport;
|
||||
import org.springframework.data.repository.query.parser.AbstractQueryCreator;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* {@link org.springframework.beans.factory.FactoryBean} to create {@link KeyValueRepository}.
|
||||
*
|
||||
* @author Christoph Strobl
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public class KeyValueRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extends Serializable> extends
|
||||
RepositoryFactoryBeanSupport<T, S, ID> {
|
||||
@@ -36,7 +39,15 @@ public class KeyValueRepositoryFactoryBean<T extends Repository<S, ID>, S, ID ex
|
||||
private KeyValueOperations operations;
|
||||
private Class<? extends AbstractQueryCreator<?, ?>> queryCreator;
|
||||
|
||||
/**
|
||||
* Configures the {@link KeyValueOperations} to be used for the repositories.
|
||||
*
|
||||
* @param operations must not be {@literal null}.
|
||||
*/
|
||||
public void setKeyValueOperations(KeyValueOperations operations) {
|
||||
|
||||
Assert.notNull(operations, "KeyValueOperations must not be null!");
|
||||
|
||||
this.operations = operations;
|
||||
}
|
||||
|
||||
@@ -49,7 +60,15 @@ public class KeyValueRepositoryFactoryBean<T extends Repository<S, ID>, S, ID ex
|
||||
super.setMappingContext(mappingContext);
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures the {@link QueryCreatorType} to be used.
|
||||
*
|
||||
* @param queryCreator must not be {@literal null}.
|
||||
*/
|
||||
public void setQueryCreator(Class<? extends AbstractQueryCreator<?, ?>> queryCreator) {
|
||||
|
||||
Assert.notNull(queryCreator, "Query creator type must not be null!");
|
||||
|
||||
this.queryCreator = queryCreator;
|
||||
}
|
||||
|
||||
@@ -58,8 +77,22 @@ public class KeyValueRepositoryFactoryBean<T extends Repository<S, ID>, S, ID ex
|
||||
* @see org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport#createRepositoryFactory()
|
||||
*/
|
||||
@Override
|
||||
protected RepositoryFactorySupport createRepositoryFactory() {
|
||||
return new KeyValueRepositoryFactory(this.operations, this.queryCreator);
|
||||
protected final RepositoryFactorySupport createRepositoryFactory() {
|
||||
return createRepositoryFactory(operations, queryCreator);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the repository factory to be used to create repositories.
|
||||
*
|
||||
* @param operations will never be {@literal null}.
|
||||
* @param queryCreator will never be {@literal null}.
|
||||
* @return must not be {@literal null}.
|
||||
* @since 1.1
|
||||
*/
|
||||
protected KeyValueRepositoryFactory createRepositoryFactory(KeyValueOperations operations,
|
||||
Class<? extends AbstractQueryCreator<?, ?>> queryCreator) {
|
||||
|
||||
return new KeyValueRepositoryFactory(operations, queryCreator);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -68,6 +101,10 @@ public class KeyValueRepositoryFactoryBean<T extends Repository<S, ID>, S, ID ex
|
||||
*/
|
||||
@Override
|
||||
public void afterPropertiesSet() {
|
||||
|
||||
Assert.notNull(operations, "KeyValueOperations must not be null!");
|
||||
Assert.notNull(queryCreator, "Query creator type must not be null!");
|
||||
|
||||
super.afterPropertiesSet();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,130 @@
|
||||
/*
|
||||
* Copyright 2016 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.keyvalue.repository.support;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.springframework.data.keyvalue.core.KeyValueOperations;
|
||||
import org.springframework.data.repository.Repository;
|
||||
import org.springframework.data.repository.query.parser.AbstractQueryCreator;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link KeyValueRepositoryFactoryBean}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public class KeyValueRepositoryFactoryBeanUnitTests {
|
||||
|
||||
public @Rule ExpectedException exception = ExpectedException.none();
|
||||
|
||||
KeyValueRepositoryFactoryBean<?, ?, ?> factoryBean;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
this.factoryBean = new KeyValueRepositoryFactoryBean<Repository<Object, Serializable>, Object, Serializable>();
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAKV-123
|
||||
*/
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void rejectsNullKeyValueOperations() {
|
||||
factoryBean.setKeyValueOperations(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAKV-123
|
||||
*/
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void rejectsNullQueryCreator() {
|
||||
factoryBean.setQueryCreator(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAKV-123
|
||||
*/
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void rejectsUninitializedInstance() {
|
||||
factoryBean.afterPropertiesSet();
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAKV-123
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void rejectsInstanceWithoutKeyValueOperations() {
|
||||
|
||||
Class<? extends AbstractQueryCreator<?, ?>> creatorType = (Class<? extends AbstractQueryCreator<?, ?>>) mock(
|
||||
AbstractQueryCreator.class).getClass();
|
||||
|
||||
factoryBean.setQueryCreator(creatorType);
|
||||
factoryBean.afterPropertiesSet();
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAKV-123
|
||||
*/
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void rejectsInstanceWithoutQueryCreator() {
|
||||
|
||||
factoryBean.setKeyValueOperations(mock(KeyValueOperations.class));
|
||||
factoryBean.afterPropertiesSet();
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAKV-123
|
||||
*/
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void initializesConfiguredFactory() {
|
||||
|
||||
Class<? extends AbstractQueryCreator<?, ?>> creatorType = (Class<? extends AbstractQueryCreator<?, ?>>) mock(
|
||||
AbstractQueryCreator.class).getClass();
|
||||
|
||||
factoryBean.setQueryCreator(creatorType);
|
||||
factoryBean.setKeyValueOperations(mock(KeyValueOperations.class));
|
||||
|
||||
exception.expect(IllegalArgumentException.class);
|
||||
exception.expectMessage("Repository interface");
|
||||
|
||||
factoryBean.afterPropertiesSet();
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAKV-123
|
||||
*/
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void createsRepositoryFactory() {
|
||||
|
||||
Class<? extends AbstractQueryCreator<?, ?>> creatorType = (Class<? extends AbstractQueryCreator<?, ?>>) mock(
|
||||
AbstractQueryCreator.class).getClass();
|
||||
|
||||
factoryBean.setQueryCreator(creatorType);
|
||||
factoryBean.setKeyValueOperations(mock(KeyValueOperations.class));
|
||||
|
||||
assertThat(factoryBean.createRepositoryFactory(), is(notNullValue()));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user