DATACASS-249 - Use Set to store qualifiers in CDI extension.

Qualifiers are now stored in their original set instead of using the toString representation.
This commit is contained in:
Mark Paluch
2016-06-06 14:09:06 +02:00
parent f6aef61d51
commit 98297f939d
7 changed files with 154 additions and 7 deletions

View File

@@ -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<String, Bean<CassandraOperations>> cassandraOperationsMap = new HashMap<String, Bean<CassandraOperations>>();
private final Map<Set<Annotation>, Bean<CassandraOperations>> cassandraOperationsMap = new HashMap<Set<Annotation>, Bean<CassandraOperations>>();
/**
* Implementation of a an observer which checks for CassandraOperations beans and stores them in
@@ -53,7 +52,7 @@ public class CassandraRepositoryExtension extends CdiRepositoryExtensionSupport
Bean<T> bean = processBean.getBean();
for (Type type : bean.getTypes()) {
if (type instanceof Class<?> && CassandraOperations.class.isAssignableFrom((Class<?>) type)) {
cassandraOperationsMap.put(bean.getQualifiers().toString(), ((Bean<CassandraOperations>) bean));
cassandraOperationsMap.put(bean.getQualifiers(), ((Bean<CassandraOperations>) bean));
}
}
}
@@ -89,7 +88,7 @@ public class CassandraRepositoryExtension extends CdiRepositoryExtensionSupport
private <T> CdiRepositoryBean<T> createRepositoryBean(Class<T> repositoryType, Set<Annotation> qualifiers,
BeanManager beanManager) {
Bean<CassandraOperations> cassandraOperationsBean = this.cassandraOperationsMap.get(qualifiers.toString());
Bean<CassandraOperations> cassandraOperationsBean = this.cassandraOperationsMap.get(qualifiers);
if (cassandraOperationsBean == null) {
throw new UnsatisfiedResolutionException(String.format("Unable to resolve a bean for '%s' with qualifiers %s.",

View File

@@ -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));

View File

@@ -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;
}
}

View File

@@ -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
*/

View File

@@ -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 {
}

View File

@@ -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<User, String> {
}

View File

@@ -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 {
}