diff --git a/src/main/java/org/springframework/data/keyvalue/repository/support/KeyValueRepositoryFactoryBean.java b/src/main/java/org/springframework/data/keyvalue/repository/support/KeyValueRepositoryFactoryBean.java index df08c7c..897cb7b 100644 --- a/src/main/java/org/springframework/data/keyvalue/repository/support/KeyValueRepositoryFactoryBean.java +++ b/src/main/java/org/springframework/data/keyvalue/repository/support/KeyValueRepositoryFactoryBean.java @@ -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, S, ID extends Serializable> extends RepositoryFactoryBeanSupport { @@ -36,7 +39,15 @@ public class KeyValueRepositoryFactoryBean, S, ID ex private KeyValueOperations operations; private Class> 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, S, ID ex super.setMappingContext(mappingContext); } + /** + * Configures the {@link QueryCreatorType} to be used. + * + * @param queryCreator must not be {@literal null}. + */ public void setQueryCreator(Class> queryCreator) { + + Assert.notNull(queryCreator, "Query creator type must not be null!"); + this.queryCreator = queryCreator; } @@ -58,8 +77,22 @@ public class KeyValueRepositoryFactoryBean, 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> queryCreator) { + + return new KeyValueRepositoryFactory(operations, queryCreator); } /* @@ -68,6 +101,10 @@ public class KeyValueRepositoryFactoryBean, 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(); } } diff --git a/src/test/java/org/springframework/data/keyvalue/repository/support/KeyValueRepositoryFactoryBeanUnitTests.java b/src/test/java/org/springframework/data/keyvalue/repository/support/KeyValueRepositoryFactoryBeanUnitTests.java new file mode 100644 index 0000000..0f2d6aa --- /dev/null +++ b/src/test/java/org/springframework/data/keyvalue/repository/support/KeyValueRepositoryFactoryBeanUnitTests.java @@ -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, 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> creatorType = (Class>) 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> creatorType = (Class>) 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> creatorType = (Class>) mock( + AbstractQueryCreator.class).getClass(); + + factoryBean.setQueryCreator(creatorType); + factoryBean.setKeyValueOperations(mock(KeyValueOperations.class)); + + assertThat(factoryBean.createRepositoryFactory(), is(notNullValue())); + } +}