diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/repository/cdi/CassandraRepositoryExtension.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/repository/cdi/CassandraRepositoryExtension.java index c6d5067f6..a9cd0c9d5 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/repository/cdi/CassandraRepositoryExtension.java +++ b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/repository/cdi/CassandraRepositoryExtension.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. @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.springframework.data.cassandra.repository.cdi; import javax.enterprise.event.Observes; @@ -39,7 +38,7 @@ import org.springframework.data.repository.cdi.CdiRepositoryExtensionSupport; */ public class CassandraRepositoryExtension extends CdiRepositoryExtensionSupport { - private final Map> cassandraOperationsMap = new HashMap>(); + private final Map, Bean> cassandraOperationsMap = new HashMap, Bean>(); /** * Implementation of a an observer which checks for CassandraOperations beans and stores them in @@ -53,7 +52,7 @@ public class CassandraRepositoryExtension extends CdiRepositoryExtensionSupport Bean bean = processBean.getBean(); for (Type type : bean.getTypes()) { if (type instanceof Class && CassandraOperations.class.isAssignableFrom((Class) type)) { - cassandraOperationsMap.put(bean.getQualifiers().toString(), ((Bean) bean)); + cassandraOperationsMap.put(bean.getQualifiers(), ((Bean) bean)); } } } @@ -89,7 +88,7 @@ public class CassandraRepositoryExtension extends CdiRepositoryExtensionSupport private CdiRepositoryBean createRepositoryBean(Class repositoryType, Set qualifiers, BeanManager beanManager) { - Bean cassandraOperationsBean = this.cassandraOperationsMap.get(qualifiers.toString()); + Bean cassandraOperationsBean = this.cassandraOperationsMap.get(qualifiers); if (cassandraOperationsBean == null) { throw new UnsatisfiedResolutionException(String.format("Unable to resolve a bean for '%s' with qualifiers %s.", diff --git a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/repository/cdi/CassandraOperationsProducer.java b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/repository/cdi/CassandraOperationsProducer.java index 0592f1f71..8513652c7 100644 --- a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/repository/cdi/CassandraOperationsProducer.java +++ b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/repository/cdi/CassandraOperationsProducer.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. @@ -78,6 +78,14 @@ class CassandraOperationsProducer { return cassandraTemplate; } + @OtherQualifier + @UserDB + @Produces + @ApplicationScoped + public CassandraOperations createQualifiedCassandraOperations(CassandraOperations cassandraOperations){ + return cassandraOperations; + } + public void close(@Disposes CassandraOperations cassandraOperations) { cassandraOperations.execute(DropKeyspaceSpecification.dropKeyspace(KEYSPACE_NAME)); diff --git a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/repository/cdi/CdiRepositoryClient.java b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/repository/cdi/CdiRepositoryClient.java index fef23a0a7..9bab22403 100644 --- a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/repository/cdi/CdiRepositoryClient.java +++ b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/repository/cdi/CdiRepositoryClient.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. @@ -20,10 +20,12 @@ import javax.inject.Inject; /** * @author Mohsin Husen * @author Oliver Gierke + * @author Mark Paluch */ class CdiRepositoryClient { private CdiUserRepository repository; + private QualifiedUserRepository qualifiedUserRepository; private SamplePersonRepository samplePersonRepository; public CdiUserRepository getRepository() { @@ -43,4 +45,13 @@ class CdiRepositoryClient { public void setSamplePersonRepository(SamplePersonRepository samplePersonRepository) { this.samplePersonRepository = samplePersonRepository; } + + public QualifiedUserRepository getQualifiedUserRepository() { + return qualifiedUserRepository; + } + + @Inject + public void setQualifiedUserRepository(@UserDB @OtherQualifier QualifiedUserRepository qualifiedUserRepository) { + this.qualifiedUserRepository = qualifiedUserRepository; + } } diff --git a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/repository/cdi/CdiRepositoryTests.java b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/repository/cdi/CdiRepositoryTests.java index ec526d7fb..68e9d49c5 100644 --- a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/repository/cdi/CdiRepositoryTests.java +++ b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/repository/cdi/CdiRepositoryTests.java @@ -36,6 +36,7 @@ public class CdiRepositoryTests extends AbstractEmbeddedCassandraIntegrationTest private static CdiTestContainer cdiContainer; private CdiUserRepository repository; private SamplePersonRepository personRepository; + private QualifiedUserRepository qualifiedUserRepository; @BeforeClass public static void init() throws Exception { @@ -51,20 +52,29 @@ public class CdiRepositoryTests extends AbstractEmbeddedCassandraIntegrationTest @AfterClass public static void shutdown() throws Exception { + cdiContainer.stopContexts(); cdiContainer.shutdownContainer(); } @Before public void setUp() { + CdiRepositoryClient client = cdiContainer.getInstance(CdiRepositoryClient.class); repository = client.getRepository(); personRepository = client.getSamplePersonRepository(); + qualifiedUserRepository = client.getQualifiedUserRepository(); } + /** + * @see DATACASS-149 + */ @Test public void testCdiRepository() { + assertNotNull(repository); + + repository.deleteAll(); User bean = new User(); bean.setUsername("username"); @@ -92,6 +102,25 @@ public class CdiRepositoryTests extends AbstractEmbeddedCassandraIntegrationTest assertNull(retrieved); } + /** + * @see DATACASS-249 + */ + @Test + public void testQualifiedCdiRepository() { + + assertNotNull(qualifiedUserRepository); + qualifiedUserRepository.deleteAll(); + + User bean = new User(); + bean.setUsername("username"); + bean.setFirstName("first"); + bean.setLastName("last"); + + qualifiedUserRepository.save(bean); + + assertTrue(qualifiedUserRepository.exists(bean.getUsername())); + } + /** * @see DATACASS-149 */ diff --git a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/repository/cdi/OtherQualifier.java b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/repository/cdi/OtherQualifier.java new file mode 100644 index 000000000..c6434becb --- /dev/null +++ b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/repository/cdi/OtherQualifier.java @@ -0,0 +1,35 @@ +/* + * 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.integration.repository.cdi; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import javax.inject.Qualifier; + +/** + * @author Mark Paluch + * @see DATACASS-249 + */ +@Qualifier +@Retention(RetentionPolicy.RUNTIME) +@Target({ ElementType.TYPE, ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER }) +@interface OtherQualifier { + +} diff --git a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/repository/cdi/QualifiedUserRepository.java b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/repository/cdi/QualifiedUserRepository.java new file mode 100644 index 000000000..d2e50256a --- /dev/null +++ b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/repository/cdi/QualifiedUserRepository.java @@ -0,0 +1,30 @@ +/* + * 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.integration.repository.cdi; + +import org.springframework.data.cassandra.test.integration.repository.simple.User; +import org.springframework.data.repository.CrudRepository; + +/** + * @author Mark Paluch + * @see DATACASS-249 + */ +@OtherQualifier +@UserDB +public interface QualifiedUserRepository extends CrudRepository { + +} diff --git a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/repository/cdi/UserDB.java b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/repository/cdi/UserDB.java new file mode 100644 index 000000000..ad3a71b5c --- /dev/null +++ b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/repository/cdi/UserDB.java @@ -0,0 +1,35 @@ +/* + * 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.integration.repository.cdi; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import javax.inject.Qualifier; + +/** + * @author Mark Paluch + * @see DATACASS-249 + */ +@Qualifier +@Retention(RetentionPolicy.RUNTIME) +@Target({ ElementType.TYPE, ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER }) +@interface UserDB { + +}