From 71e24b2fdce0f1b914c8b79ef367f27281db53f5 Mon Sep 17 00:00:00 2001 From: Thomas Darimont Date: Wed, 6 May 2015 21:46:00 +0200 Subject: [PATCH] DATACASS-211 - Adapt to changes in Spring Data Commons. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Tweaked method signatures in CassandraRepositoryFactory after some signature changes in Spring Data Commons. Use newly introduced getTragetRepositoryViaReflection(…) to obtain the repository instance via the super class. Added repositoryBaseClass() attribute to @EnableCassandraRepositories. Related tickets: DATACMNS-542. Original pull request: #35. --- .../config/EnableCassandraRepositories.java | 12 +++- .../support/CassandraRepositoryFactory.java | 23 +++---- ...omRepositoryBaseClassIntegrationTests.java | 67 +++++++++++++++++++ 3 files changed, 89 insertions(+), 13 deletions(-) create mode 100644 spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/repository/UserRepositoryJavaConfigCustomRepositoryBaseClassIntegrationTests.java diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/repository/config/EnableCassandraRepositories.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/repository/config/EnableCassandraRepositories.java index 53834766c..9583539cd 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/repository/config/EnableCassandraRepositories.java +++ b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/repository/config/EnableCassandraRepositories.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2014 the original author or authors + * Copyright 2013-2015 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. @@ -27,6 +27,7 @@ import org.springframework.context.annotation.ComponentScan.Filter; import org.springframework.context.annotation.Import; import org.springframework.data.cassandra.core.CassandraTemplate; import org.springframework.data.cassandra.repository.support.CassandraRepositoryFactoryBean; +import org.springframework.data.repository.config.DefaultRepositoryBaseClass; import org.springframework.data.repository.query.QueryLookupStrategy; import org.springframework.data.repository.query.QueryLookupStrategy.Key; @@ -35,6 +36,7 @@ import org.springframework.data.repository.query.QueryLookupStrategy.Key; * * @author Alex Shvid * @author Matthew T. Adams + * @author Thomas Darimont */ @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @@ -107,6 +109,14 @@ public @interface EnableCassandraRepositories { */ Class repositoryFactoryBeanClass() default CassandraRepositoryFactoryBean.class; + /** + * Configure the repository base class to be used to create repository proxies for this particular configuration. + * + * @return + * @since 1.3 + */ + Class repositoryBaseClass() default DefaultRepositoryBaseClass.class; + /** * Configures the name of the {@link CassandraTemplate} bean to be used with the repositories detected. * diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/repository/support/CassandraRepositoryFactory.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/repository/support/CassandraRepositoryFactory.java index 01f453f28..24048932d 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/repository/support/CassandraRepositoryFactory.java +++ b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/repository/support/CassandraRepositoryFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2014 the original author or authors + * Copyright 2013-2015 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. @@ -28,6 +28,7 @@ import org.springframework.data.cassandra.repository.query.CassandraQueryMethod; import org.springframework.data.cassandra.repository.query.StringBasedCassandraQuery; import org.springframework.data.mapping.model.MappingException; import org.springframework.data.repository.core.NamedQueries; +import org.springframework.data.repository.core.RepositoryInformation; import org.springframework.data.repository.core.RepositoryMetadata; import org.springframework.data.repository.core.support.RepositoryFactorySupport; import org.springframework.data.repository.query.QueryLookupStrategy; @@ -40,11 +41,12 @@ import org.springframework.util.Assert; * * @author Alex Shvid * @author Matthew T. Adams + * @author Thomas Darimont */ public class CassandraRepositoryFactory extends RepositoryFactorySupport { - private final CassandraOperations cassandraTemplate; + private final CassandraOperations cassandraOperations; private final CassandraMappingContext mappingContext; /** @@ -56,7 +58,7 @@ public class CassandraRepositoryFactory extends RepositoryFactorySupport { Assert.notNull(cassandraOperations); - this.cassandraTemplate = cassandraOperations; + this.cassandraOperations = cassandraOperations; this.mappingContext = cassandraOperations.getConverter().getMappingContext(); // TODO: remove when supporting declarative query methods @@ -69,13 +71,10 @@ public class CassandraRepositoryFactory extends RepositoryFactorySupport { } @Override - @SuppressWarnings({ "rawtypes", "unchecked" }) - protected Object getTargetRepository(RepositoryMetadata metadata) { - - CassandraEntityInformation entityInformation = getEntityInformation(metadata.getDomainType()); - - return new SimpleCassandraRepository(entityInformation, cassandraTemplate); + protected Object getTargetRepository(RepositoryInformation information) { + CassandraEntityInformation entityInformation = getEntityInformation(information.getDomainType()); + return getTargetRepositoryViaReflection(information, entityInformation, cassandraOperations); } @Override @@ -90,7 +89,7 @@ public class CassandraRepositoryFactory extends RepositoryFactorySupport { } return new MappingCassandraEntityInformation((CassandraPersistentEntity) entity, - cassandraTemplate.getConverter()); + cassandraOperations.getConverter()); } @Override @@ -108,9 +107,9 @@ public class CassandraRepositoryFactory extends RepositoryFactorySupport { if (namedQueries.hasQuery(namedQueryName)) { String namedQuery = namedQueries.getQuery(namedQueryName); - return new StringBasedCassandraQuery(namedQuery, queryMethod, cassandraTemplate); + return new StringBasedCassandraQuery(namedQuery, queryMethod, cassandraOperations); } else if (queryMethod.hasAnnotatedQuery()) { - return new StringBasedCassandraQuery(queryMethod, cassandraTemplate); + return new StringBasedCassandraQuery(queryMethod, cassandraOperations); } else { throw new InvalidDataAccessApiUsageException("declarative query methods are a todo"); } diff --git a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/repository/UserRepositoryJavaConfigCustomRepositoryBaseClassIntegrationTests.java b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/repository/UserRepositoryJavaConfigCustomRepositoryBaseClassIntegrationTests.java new file mode 100644 index 000000000..7848cdd62 --- /dev/null +++ b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/repository/UserRepositoryJavaConfigCustomRepositoryBaseClassIntegrationTests.java @@ -0,0 +1,67 @@ +/* + * Copyright 2015 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.integration.repository; + +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; + +import java.io.Serializable; + +import org.junit.Test; +import org.springframework.aop.framework.Advised; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.cassandra.core.CassandraOperations; +import org.springframework.data.cassandra.repository.config.EnableCassandraRepositories; +import org.springframework.data.cassandra.repository.query.CassandraEntityInformation; +import org.springframework.data.cassandra.repository.support.SimpleCassandraRepository; +import org.springframework.data.cassandra.test.integration.support.IntegrationTestConfig; +import org.springframework.test.context.ContextConfiguration; + +/** + * Java config tests for {@link UserRepository}. + * + * @author Thomas Darimont + */ +@ContextConfiguration +public class UserRepositoryJavaConfigCustomRepositoryBaseClassIntegrationTests extends + UserRepositoryIntegrationTestsDelegator { + + @Configuration + @EnableCassandraRepositories(basePackageClasses = UserRepository.class, + repositoryBaseClass = CustomCassandraRepository.class) + public static class Config extends IntegrationTestConfig { + + @Override + public String[] getEntityBasePackages() { + return new String[] { User.class.getPackage().getName() }; + } + } + + /** + * @see DATACASS-211 + */ + @Test + public void targetRepositoryClassShouldBeConfiguredCustomBaseRepositoryClass() throws Exception { + assertThat(((Advised) repository).getTargetSource().getTarget(), is(instanceOf(CustomCassandraRepository.class))); + } + + public static class CustomCassandraRepository extends SimpleCassandraRepository { + + public CustomCassandraRepository(CassandraEntityInformation metadata, CassandraOperations operations) { + super(metadata, operations); + } + } +}