DATACMNS-110 - Added support for CDI integration for repositories.
Added custom CDI extension to use repositories in a CDI environment. Base class picks up all Spring Data repository interfaces and allows working with qualifiers to bind beans to be created to qualifiers.
This commit is contained in:
committed by
Oliver Gierke
parent
0334a2dd78
commit
eebc822dff
@@ -13,6 +13,8 @@
|
||||
|
||||
<properties>
|
||||
<querydsl.version>2.2.0</querydsl.version>
|
||||
<cdi.version>1.0</cdi.version>
|
||||
<webbeans.version>1.1.3</webbeans.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
@@ -96,6 +98,29 @@
|
||||
<version>3.0</version>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<!-- CDI -->
|
||||
<dependency>
|
||||
<groupId>javax.enterprise</groupId>
|
||||
<artifactId>cdi-api</artifactId>
|
||||
<version>${cdi.version}</version>
|
||||
<scope>provided</scope>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>javax.el</groupId>
|
||||
<artifactId>el-api</artifactId>
|
||||
<version>${cdi.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.openwebbeans.test</groupId>
|
||||
<artifactId>cditest-owb</artifactId>
|
||||
<version>${webbeans.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
<build>
|
||||
|
||||
@@ -0,0 +1,208 @@
|
||||
/*
|
||||
* Copyright 2011 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.repository.cdi;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.enterprise.context.spi.CreationalContext;
|
||||
import javax.enterprise.inject.Alternative;
|
||||
import javax.enterprise.inject.Stereotype;
|
||||
import javax.enterprise.inject.spi.Bean;
|
||||
import javax.enterprise.inject.spi.BeanManager;
|
||||
import javax.enterprise.inject.spi.InjectionPoint;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Base class for {@link Bean} wrappers.
|
||||
*
|
||||
* @author Dirk Mahler
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public abstract class CdiRepositoryBean<T> implements Bean<T> {
|
||||
|
||||
private static final Log LOG = LogFactory.getLog(CdiRepositoryBean.class);
|
||||
|
||||
private final Set<Annotation> qualifiers;
|
||||
private final Class<T> repositoryType;
|
||||
private final BeanManager beanManager;
|
||||
|
||||
/**
|
||||
* Creates a new {@link CdiRepositoryBean}.
|
||||
*
|
||||
* @param qualifiers must not be {@literal null}.
|
||||
* @param repositoryType has to be an interface must not be {@literal null}.
|
||||
*/
|
||||
public CdiRepositoryBean(Set<Annotation> qualifiers, Class<T> repositoryType, BeanManager beanManager) {
|
||||
|
||||
Assert.notNull(qualifiers);
|
||||
Assert.notNull(beanManager);
|
||||
Assert.notNull(repositoryType);
|
||||
Assert.isTrue(repositoryType.isInterface());
|
||||
|
||||
this.qualifiers = qualifiers;
|
||||
this.repositoryType = repositoryType;
|
||||
this.beanManager = beanManager;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see javax.enterprise.inject.spi.Bean#getTypes()
|
||||
*/
|
||||
@SuppressWarnings("rawtypes")
|
||||
public Set<Type> getTypes() {
|
||||
|
||||
Set<Class> interfaces = new HashSet<Class>();
|
||||
interfaces.add(repositoryType);
|
||||
interfaces.addAll(Arrays.asList(repositoryType.getInterfaces()));
|
||||
|
||||
if (LOG.isDebugEnabled()) {
|
||||
LOG.debug(String.format("Declaring types '%s' for repository '%s'.", interfaces.toString(),
|
||||
repositoryType.getName()));
|
||||
}
|
||||
|
||||
return new HashSet<Type>(interfaces);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an instance of an {@link EntityManager}.
|
||||
*
|
||||
* @param beanManager The BeanManager.
|
||||
* @param bean The bean representing an EntityManager.
|
||||
* @return The EntityManager instance.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
protected <S> S getDependencyInstance(Bean<S> bean, Class<S> type) {
|
||||
CreationalContext<S> creationalContext = beanManager.createCreationalContext(bean);
|
||||
return (S) beanManager.getReference(bean, type, creationalContext);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see javax.enterprise.context.spi.Contextual#create(javax.enterprise.context.spi.CreationalContext)
|
||||
*/
|
||||
public final T create(CreationalContext<T> creationalContext) {
|
||||
|
||||
if (LOG.isDebugEnabled()) {
|
||||
LOG.debug(String.format("Creating bean instance for repository type '%s'.", repositoryType.getName()));
|
||||
}
|
||||
return create(creationalContext, repositoryType);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see javax.enterprise.context.spi.Contextual#destroy(java.lang.Object, javax.enterprise.context.spi.CreationalContext)
|
||||
*/
|
||||
public void destroy(T instance, CreationalContext<T> creationalContext) {
|
||||
|
||||
if (LOG.isDebugEnabled()) {
|
||||
LOG.debug(String.format("Destroying bean instance %s for repository type '%s'.", instance.toString(),
|
||||
repositoryType.getName()));
|
||||
}
|
||||
|
||||
creationalContext.release();
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see javax.enterprise.inject.spi.Bean#getQualifiers()
|
||||
*/
|
||||
public Set<Annotation> getQualifiers() {
|
||||
return qualifiers;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see javax.enterprise.inject.spi.Bean#getName()
|
||||
*/
|
||||
public String getName() {
|
||||
return repositoryType.getName();
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see javax.enterprise.inject.spi.Bean#getStereotypes()
|
||||
*/
|
||||
public Set<Class<? extends Annotation>> getStereotypes() {
|
||||
|
||||
Set<Class<? extends Annotation>> stereotypes = new HashSet<Class<? extends Annotation>>();
|
||||
|
||||
for (Annotation annotation : repositoryType.getAnnotations()) {
|
||||
Class<? extends Annotation> annotationType = annotation.annotationType();
|
||||
if (annotationType.isAnnotationPresent(Stereotype.class)) {
|
||||
stereotypes.add(annotationType);
|
||||
}
|
||||
}
|
||||
|
||||
return stereotypes;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see javax.enterprise.inject.spi.Bean#getBeanClass()
|
||||
*/
|
||||
public Class<?> getBeanClass() {
|
||||
return repositoryType;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see javax.enterprise.inject.spi.Bean#isAlternative()
|
||||
*/
|
||||
public boolean isAlternative() {
|
||||
return repositoryType.isAnnotationPresent(Alternative.class);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see javax.enterprise.inject.spi.Bean#isNullable()
|
||||
*/
|
||||
public boolean isNullable() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see javax.enterprise.inject.spi.Bean#getInjectionPoints()
|
||||
*/
|
||||
public Set<InjectionPoint> getInjectionPoints() {
|
||||
return Collections.emptySet();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param creationalContext
|
||||
* @param repositoryType
|
||||
* @return
|
||||
*/
|
||||
protected abstract T create(CreationalContext<T> creationalContext, Class<T> repositoryType);
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see java.lang.Object#toString()
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return String
|
||||
.format("JpaRepositoryBean: type='%s', qualifiers=%s", repositoryType.getName(), qualifiers.toString());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
/*
|
||||
* Copyright 2011 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.repository.cdi;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.enterprise.event.Observes;
|
||||
import javax.enterprise.inject.Any;
|
||||
import javax.enterprise.inject.Default;
|
||||
import javax.enterprise.inject.spi.AnnotatedType;
|
||||
import javax.enterprise.inject.spi.Extension;
|
||||
import javax.enterprise.inject.spi.ProcessAnnotatedType;
|
||||
import javax.enterprise.util.AnnotationLiteral;
|
||||
import javax.inject.Qualifier;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.data.repository.NoRepositoryBean;
|
||||
import org.springframework.data.repository.Repository;
|
||||
import org.springframework.data.repository.RepositoryDefinition;
|
||||
|
||||
/**
|
||||
* Base class for {@link Extension} implementations that create instances for Spring Data repositories.
|
||||
*
|
||||
* @author Dirk Mahler
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public abstract class CdiRepositoryExtensionSupport implements Extension {
|
||||
|
||||
private static final Log LOGGER = LogFactory.getLog(CdiRepositoryExtensionSupport.class);
|
||||
|
||||
private final Map<Class<?>, Set<Annotation>> repositoryTypes = new HashMap<Class<?>, Set<Annotation>>();
|
||||
|
||||
/**
|
||||
* Implementation of a an observer which checks for Spring Data repository types and stores them in
|
||||
* {@link #repositoryTypes} for later registration as bean type.
|
||||
*
|
||||
* @param <X> The type.
|
||||
* @param processAnnotatedType The annotated type as defined by CDI.
|
||||
*/
|
||||
protected <X> void processAnnotatedType(@Observes ProcessAnnotatedType<X> processAnnotatedType) {
|
||||
|
||||
AnnotatedType<X> annotatedType = processAnnotatedType.getAnnotatedType();
|
||||
Class<X> repositoryType = annotatedType.getJavaClass();
|
||||
|
||||
if (isRepository(repositoryType)) {
|
||||
|
||||
// Determine the qualifiers of the repository type.
|
||||
Set<Annotation> qualifiers = getQualifiers(repositoryType);
|
||||
|
||||
if (LOGGER.isDebugEnabled()) {
|
||||
LOGGER.debug(String.format("Discovered repository type '%s' with qualifiers %s.", repositoryType.getName(),
|
||||
qualifiers));
|
||||
}
|
||||
// Store the repository type using its qualifiers.
|
||||
repositoryTypes.put(repositoryType, qualifiers);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the given type is a repository type.
|
||||
*
|
||||
* @param type must not be {@literal null}.
|
||||
* @return
|
||||
*/
|
||||
private boolean isRepository(Class<?> type) {
|
||||
|
||||
boolean isInterface = type.isInterface();
|
||||
boolean extendsRepository = Repository.class.isAssignableFrom(type);
|
||||
boolean isAnnotated = type.isAnnotationPresent(RepositoryDefinition.class);
|
||||
boolean excludedByAnnotation = type.isAnnotationPresent(NoRepositoryBean.class);
|
||||
|
||||
return isInterface && (extendsRepository || isAnnotated) && !excludedByAnnotation;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines the qualifiers of the given type.
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
private Set<Annotation> getQualifiers(final Class<?> type) {
|
||||
|
||||
Set<Annotation> qualifiers = new HashSet<Annotation>();
|
||||
Annotation[] annotations = type.getAnnotations();
|
||||
for (Annotation annotation : annotations) {
|
||||
Class<? extends Annotation> annotationType = annotation.annotationType();
|
||||
if (annotationType.isAnnotationPresent(Qualifier.class)) {
|
||||
qualifiers.add(annotation);
|
||||
}
|
||||
}
|
||||
// Add @Default qualifier if no qualifier is specified.
|
||||
if (qualifiers.isEmpty()) {
|
||||
qualifiers.add(new AnnotationLiteral<Default>() {
|
||||
});
|
||||
}
|
||||
// Add @Any qualifier.
|
||||
qualifiers.add(new AnnotationLiteral<Any>() {
|
||||
});
|
||||
return qualifiers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides access to all repository types as well as their qualifiers.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
protected Iterable<Entry<Class<?>, Set<Annotation>>> getRepositoryTypes() {
|
||||
return repositoryTypes.entrySet();
|
||||
}
|
||||
}
|
||||
@@ -105,7 +105,7 @@ public abstract class RepositoryFactorySupport {
|
||||
* @param repositoryInterface
|
||||
* @return
|
||||
*/
|
||||
public <T extends Repository<?, ?>> T getRepository(Class<T> repositoryInterface) {
|
||||
public <T> T getRepository(Class<T> repositoryInterface) {
|
||||
|
||||
return getRepository(repositoryInterface, null);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,123 @@
|
||||
/*
|
||||
* Copyright 2011 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.repository.cdi;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.enterprise.context.spi.CreationalContext;
|
||||
import javax.enterprise.inject.spi.BeanManager;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
import org.springframework.data.repository.Repository;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link CdiRepositoryBean}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class CdiRepositoryBeanUnitTests {
|
||||
|
||||
static final Set<Annotation> NO_ANNOTATIONS = Collections.emptySet();
|
||||
|
||||
@Mock
|
||||
BeanManager beanManager;
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void voidRejectsNullQualifiers() {
|
||||
new DummyCdiRepositoryBean<SampleRepository>(null, SampleRepository.class, beanManager);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void voidRejectsNullRepositoryType() {
|
||||
new DummyCdiRepositoryBean<SampleRepository>(NO_ANNOTATIONS, null, beanManager);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void voidRejectsNullBeanManager() {
|
||||
new DummyCdiRepositoryBean<SampleRepository>(NO_ANNOTATIONS, SampleRepository.class, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void returnsBasicMetadata() {
|
||||
|
||||
DummyCdiRepositoryBean<SampleRepository> bean = new DummyCdiRepositoryBean<SampleRepository>(
|
||||
NO_ANNOTATIONS, SampleRepository.class, beanManager);
|
||||
|
||||
assertThat(bean.getBeanClass(), is(typeCompatibleWith(SampleRepository.class)));
|
||||
assertThat(bean.getName(), is(SampleRepository.class.getName()));
|
||||
assertThat(bean.isNullable(), is(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void returnsAllImplementedTypes() {
|
||||
|
||||
DummyCdiRepositoryBean<SampleRepository> bean = new DummyCdiRepositoryBean<SampleRepository>(
|
||||
NO_ANNOTATIONS, SampleRepository.class, beanManager);
|
||||
|
||||
Set<Type> types = bean.getTypes();
|
||||
assertThat(types.size(), is(2));
|
||||
assertThat(types.containsAll(Arrays.asList(SampleRepository.class, Repository.class)), is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void detectsStereotypes() {
|
||||
|
||||
DummyCdiRepositoryBean<StereotypedSampleRepository> bean = new DummyCdiRepositoryBean<StereotypedSampleRepository>(
|
||||
NO_ANNOTATIONS, StereotypedSampleRepository.class, beanManager);
|
||||
|
||||
Set<Class<? extends Annotation>> stereotypes = bean.getStereotypes();
|
||||
assertThat(stereotypes.size(), is(1));
|
||||
assertThat(stereotypes, hasItem(StereotypeAnnotation.class));
|
||||
}
|
||||
|
||||
static class DummyCdiRepositoryBean<T> extends CdiRepositoryBean<T> {
|
||||
|
||||
public DummyCdiRepositoryBean(Set<Annotation> qualifiers, Class<T> repositoryType, BeanManager beanManager) {
|
||||
super(qualifiers, repositoryType, beanManager);
|
||||
}
|
||||
|
||||
public Class<? extends Annotation> getScope() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected T create(CreationalContext<T> creationalContext, Class<T> repositoryType) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
static interface SampleRepository extends Repository<Object, Serializable> {
|
||||
|
||||
}
|
||||
|
||||
@StereotypeAnnotation
|
||||
static interface StereotypedSampleRepository {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Copyright 2011 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.repository.cdi;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Common integration tests for Spring Data repository CDI extension.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public abstract class CdiRepositoryExtensionSupportIntegrationTests {
|
||||
|
||||
@Test
|
||||
public void createsSpringDataRepositoryBean() {
|
||||
|
||||
assertThat(getBean(SampleRepository.class), is(notNullValue()));
|
||||
|
||||
RepositoryClient client = getBean(RepositoryClient.class);
|
||||
assertThat(client.repository, is(notNullValue()));
|
||||
}
|
||||
|
||||
protected abstract <T> T getBean(Class<T> type);
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* Copyright 2011 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.repository.cdi;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.enterprise.context.NormalScope;
|
||||
import javax.enterprise.context.spi.CreationalContext;
|
||||
import javax.enterprise.event.Observes;
|
||||
import javax.enterprise.inject.spi.AfterBeanDiscovery;
|
||||
import javax.enterprise.inject.spi.BeanManager;
|
||||
|
||||
import org.mockito.Mockito;
|
||||
|
||||
/**
|
||||
* Dummy extension of {@link CdiRepositoryExtensionSupport} to allow integration tests. Will create mocks for repository
|
||||
* interfaces being found.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public class DummyCdiExtension extends CdiRepositoryExtensionSupport {
|
||||
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
void afterBeanDiscovery(@Observes AfterBeanDiscovery afterBeanDiscovery, BeanManager beanManager) {
|
||||
for (Entry<Class<?>, Set<Annotation>> type : getRepositoryTypes()) {
|
||||
afterBeanDiscovery.addBean(new DummyCdiRepositoryBean(type.getValue(), type.getKey(), beanManager));
|
||||
}
|
||||
}
|
||||
|
||||
static class DummyCdiRepositoryBean<T> extends CdiRepositoryBean<T> {
|
||||
|
||||
public DummyCdiRepositoryBean(Set<Annotation> qualifiers, Class<T> repositoryType, BeanManager beanManager) {
|
||||
super(qualifiers, repositoryType, beanManager);
|
||||
}
|
||||
|
||||
public Class<? extends Annotation> getScope() {
|
||||
return MyScope.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected T create(CreationalContext<T> creationalContext, Class<T> repositoryType) {
|
||||
return Mockito.mock(repositoryType);
|
||||
}
|
||||
}
|
||||
|
||||
@NormalScope
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@interface MyScope {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Copyright 2011 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.repository.cdi;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
class RepositoryClient {
|
||||
|
||||
@Inject
|
||||
SampleRepository repository;
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Copyright 2011 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.repository.cdi;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import org.springframework.data.repository.Repository;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public interface SampleRepository extends Repository<Object, Serializable> {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* Copyright 2011 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.repository.cdi;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
|
||||
import javax.enterprise.inject.Stereotype;
|
||||
|
||||
@Stereotype
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@interface StereotypeAnnotation {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Copyright 2011 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.repository.cdi;
|
||||
|
||||
import org.apache.webbeans.cditest.CdiTestContainer;
|
||||
import org.apache.webbeans.cditest.CdiTestContainerLoader;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
|
||||
/**
|
||||
* CDI extension integration test using OpenWebbeans.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public class WebbeansCdiRepositoryExtensionSupportIntegrationTests extends CdiRepositoryExtensionSupportIntegrationTests {
|
||||
|
||||
static CdiTestContainer container;
|
||||
|
||||
@BeforeClass
|
||||
public static void setUp() throws Exception {
|
||||
|
||||
container = CdiTestContainerLoader.getCdiContainer();
|
||||
container.bootContainer();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected <T> T getBean(Class<T> type) {
|
||||
return container.getInstance(type);
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void tearDown() throws Exception {
|
||||
container.shutdownContainer();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://java.sun.com/xml/ns/javaee"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
|
||||
</beans>
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
org.springframework.data.repository.cdi.DummyCdiExtension
|
||||
@@ -6,6 +6,8 @@ Import-Package:
|
||||
sun.reflect;version="0";resolution:=optional
|
||||
Import-Template:
|
||||
com.mysema.query.*;version="[2.2.0,3.0.0)";resolution:=optional,
|
||||
javax.enterprise.*;version="${cdi.version:[=.=.=,+1.0.0)}";resolution:=optional,
|
||||
javax.inject.*;version="[1.0.0,2.0.0)";resolution:=optional,
|
||||
org.springframework.aop.*;version="${org.springframework.version.30:[=.=.=,+1.0.0)}",
|
||||
org.springframework.beans.*;version="${org.springframework.version.30:[=.=.=,+1.0.0)}",
|
||||
org.springframework.core.*;version="${org.springframework.version.30:[=.=.=,+1.0.0)}",
|
||||
|
||||
Reference in New Issue
Block a user