DATACASS-257 - Polishing.

Added tests and provide repository identifying type information.
Enabled nested repository option in @EnableCassandraRepsoitories.

Original Pull Request: #46
This commit is contained in:
Christoph Strobl
2016-03-11 10:33:55 +01:00
parent 57a4077bee
commit 69e7f41fef
3 changed files with 143 additions and 5 deletions

View File

@@ -15,11 +15,16 @@
*/
package org.springframework.data.cassandra.repository.config;
import java.lang.annotation.Annotation;
import java.util.Collection;
import java.util.Collections;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.cassandra.config.xml.ParsingUtils;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.data.cassandra.config.DefaultBeanNames;
import org.springframework.data.cassandra.mapping.Table;
import org.springframework.data.cassandra.repository.CassandraRepository;
import org.springframework.data.cassandra.repository.support.CassandraRepositoryFactoryBean;
import org.springframework.data.repository.config.AnnotationRepositoryConfigurationSource;
import org.springframework.data.repository.config.RepositoryConfigurationExtension;
@@ -28,15 +33,12 @@ import org.springframework.data.repository.config.XmlRepositoryConfigurationSour
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;
import java.lang.annotation.Annotation;
import java.util.Collection;
import java.util.Collections;
/**
* {@link RepositoryConfigurationExtension} for Cassandra.
*
* @author Alex Shvid
* @author Mark Paluch
* @author Christoph Strobl
*/
public class CassandraRepositoryConfigurationExtension extends RepositoryConfigurationExtensionSupport {
@@ -81,4 +83,13 @@ public class CassandraRepositoryConfigurationExtension extends RepositoryConfigu
return Collections.<Class<? extends Annotation>> singleton(Table.class);
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.config.RepositoryConfigurationExtensionSupport#getIdentifyingTypes()
*/
@Override
protected Collection<Class<?>> getIdentifyingTypes() {
return Collections.<Class<?>> singleton(CassandraRepository.class);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2015 the original author or authors
* Copyright 2013-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.
@@ -37,6 +37,7 @@ import org.springframework.data.repository.query.QueryLookupStrategy.Key;
* @author Alex Shvid
* @author Matthew T. Adams
* @author Thomas Darimont
* @author Christoph Strobl
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@@ -123,4 +124,10 @@ public @interface EnableCassandraRepositories {
* @return
*/
String cassandraTemplateRef() default "cassandraTemplate";
/**
* Configures whether nested repository-interfaces (e.g. defined as inner classes) should be discovered by the
* repositories infrastructure.
*/
boolean considerNestedRepositories() default false;
}

View File

@@ -0,0 +1,120 @@
/*
* 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.cassandra.test.unit.repository.config;
import static org.junit.Assert.*;
import java.util.Collection;
import org.junit.Before;
import org.junit.Test;
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.cassandra.mapping.Table;
import org.springframework.data.cassandra.repository.CassandraRepository;
import org.springframework.data.cassandra.repository.config.CassandraRepositoryConfigurationExtension;
import org.springframework.data.cassandra.repository.config.EnableCassandraRepositories;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.config.AnnotationRepositoryConfigurationSource;
import org.springframework.data.repository.config.RepositoryConfiguration;
import org.springframework.data.repository.config.RepositoryConfigurationSource;
/**
* @author Christoph Strobl
*/
public class CassandraRepositoryConfigurationExtensionUnitTests {
StandardAnnotationMetadata metadata = new StandardAnnotationMetadata(Config.class, true);
ResourceLoader loader = new PathMatchingResourcePatternResolver();
Environment environment = new StandardEnvironment();
RepositoryConfigurationSource configurationSource = new AnnotationRepositoryConfigurationSource(metadata,
EnableCassandraRepositories.class, loader, environment);
CassandraRepositoryConfigurationExtension extension;
@Before
public void setUp() {
extension = new CassandraRepositoryConfigurationExtension();
}
/**
* @see DATACASS-257
*/
@Test
public void isStrictMatchIfDomainTypeIsAnnotatedWithDocument() {
assertHasRepo(SampleRepository.class, extension.getRepositoryConfigurations(configurationSource, loader, true));
}
/**
* @see DATACASS-257
*/
@Test
public void isStrictMatchIfRepositoryExtendsStoreSpecificBase() {
assertHasRepo(StoreRepository.class, extension.getRepositoryConfigurations(configurationSource, loader, true));
}
/**
* @see DATACASS-257
*/
@Test
public void isNotStrictMatchIfDomainTypeIsNotAnnotatedWithDocument() {
assertDoesNotHaveRepo(UnannotatedRepository.class,
extension.getRepositoryConfigurations(configurationSource, loader, true));
}
private static void assertDoesNotHaveRepo(Class<?> repositoryInterface,
Collection<RepositoryConfiguration<RepositoryConfigurationSource>> configs) {
try {
assertHasRepo(repositoryInterface, configs);
fail("Expected not to find config for repository interface ".concat(repositoryInterface.getName()));
} catch (AssertionError error) {
// repo not there. we're fine.
}
}
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()));
}
@EnableCassandraRepositories(considerNestedRepositories = true)
static class Config {
}
@Table
static class Sample {}
interface SampleRepository extends Repository<Sample, Long> {}
interface UnannotatedRepository extends Repository<Object, Long> {}
interface StoreRepository extends CassandraRepository<Object> {}
}