Skip to content
Permalink
Browse files

Disabling interceptor bindings on mocks

(cherry picked from commit 45a34a6)

# Conflicts:
#	changelog.txt
  • Loading branch information...
Alex Stockinger
Alex Stockinger committed Sep 3, 2017
1 parent 0f5e824 commit 9f1cbb38f0f9810bd91022c60cc8372065fd07d7
@@ -1,6 +1,9 @@
Version 0.6.0
- Hibernate support

Version 0.5.3
- Fixed issue with globally enabled interceptors and mocks

Version 0.5.2
- Fixed classpath entry merging issues with Gradle 4 (found and fixed with 4.1)
- Using Gradle 4.1 for build
@@ -152,6 +152,7 @@ public NoImplementation getPureMockInEjbViaEjb() {
}

@Singleton
@UseInterceptor
public static class ExampleSessionBean2 {
public String doIt() {
return "I am IronMan";
@@ -0,0 +1,37 @@
/*
* Copyright (C) 2017 Alex Stockinger
*
* 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 fi.testee.mockito;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Priority;
import javax.interceptor.AroundInvoke;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;

@Interceptor
@UseInterceptor
@Priority(Interceptor.Priority.APPLICATION)
public class TestInterceptor {
private static final Logger LOG = LoggerFactory.getLogger(TestInterceptor.class);

@AroundInvoke
public Object logMethodEntry(final InvocationContext invocationContext) throws Exception {
LOG.info("AroundInvoke: {} {}", invocationContext.getTarget(), invocationContext.getMethod());
return invocationContext.proceed();
}
}
@@ -0,0 +1,27 @@
/*
* Copyright (C) 2017 Alex Stockinger
*
* 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 fi.testee.mockito;

import javax.interceptor.InterceptorBinding;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Inherited
@InterceptorBinding
@Retention(RetentionPolicy.RUNTIME)
public @interface UseInterceptor {
}
@@ -0,0 +1,70 @@
/*
* Copyright (C) 2017 Alex Stockinger
*
* 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 fi.testee.mocking;

import org.jboss.weld.util.annotated.ForwardingAnnotatedType;

import javax.enterprise.inject.spi.AnnotatedType;
import java.lang.annotation.Annotation;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class FilteringAnnotatedTypeWrapper<T> extends ForwardingAnnotatedType<T> {
private final AnnotatedType<T> delegate;
private final Predicate<Class<? extends Annotation>> filter;

public FilteringAnnotatedTypeWrapper(
final AnnotatedType<T> delegate,
final Predicate<Class<? extends Annotation>> filter
) {
this.delegate = delegate;
this.filter = filter;
}

@Override
public AnnotatedType<T> delegate() {
return delegate;
}


@Override
public <A extends Annotation> A getAnnotation(final Class<A> annotationType) {
final A ret = delegate.getAnnotation(annotationType);
if (ret == null) {
return null;
}
if (!filter.test(ret.annotationType())) {
return null;
}
return ret;
}

@Override
public Set<Annotation> getAnnotations() {
return super.getAnnotations().stream()
.filter(it -> filter.test(it.annotationType()))
.collect(Collectors.toSet());
}

@Override
public boolean isAnnotationPresent(Class<? extends Annotation> annotationType) {
if (!filter.test(annotationType)) {
return false;
}
return super.isAnnotationPresent(annotationType);
}
}
@@ -20,6 +20,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Priority;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.event.Observes;
import javax.enterprise.inject.Default;
@@ -30,9 +31,11 @@
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.Extension;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.enterprise.inject.spi.ProcessAnnotatedType;
import javax.enterprise.inject.spi.ProcessInjectionPoint;
import javax.enterprise.inject.spi.Producer;
import javax.enterprise.inject.spi.ProducerFactory;
import javax.interceptor.Interceptor;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.Collection;
@@ -63,6 +66,19 @@ public MockingExtension(
this.mockStore = mockStore;
}

public <X> void beans(
final @Observes ProcessAnnotatedType<X> processBean
) {
if (!processBean.getAnnotatedType().isAnnotationPresent(Interceptor.class)) {
return;
}
final FilteringAnnotatedTypeWrapper<X> filtered = new FilteringAnnotatedTypeWrapper<>(
processBean.getAnnotatedType(),
it -> it != Priority.class
);
processBean.setAnnotatedType(filtered);
}

public <T, X> void injectionPoints(
final @Observes ProcessInjectionPoint<T, X> processInjectionPoint
) {
@@ -102,7 +118,7 @@ public void afterBeanDiscovery(
mockStore.forEachType(types, (field, mock) -> {
final Class<?> beanType = mock.getClass();
final AnnotatedType<?> annotatedType = beanManager.createAnnotatedType(beanType);
final AnnotatedTypeWrapper<?> wrapped = new AnnotatedTypeWrapper<>(annotatedType, MOCKED);
final AnnotatedType<?> wrapped = new AnnotatedTypeWrapper<>(annotatedType, MOCKED);
final BeanAttributes<?> attributes = beanManager.createBeanAttributes(wrapped);
final Bean<?> bean = beanManager.createBean(attributes, beanType, factory(mock));
LOG.trace("Creating CDI mock bean for {}", annotatedType);
@@ -16,7 +16,6 @@
package fi.testee.mocking.spi;

import fi.testee.exceptions.TestEEfiException;
import fi.testee.utils.ReflectionUtils;
import org.apache.commons.lang3.reflect.FieldUtils;

import javax.annotation.Resource;
@@ -30,22 +29,22 @@
@Resource(mappedName = "testeefi/instance/instance")
private Object testInstance;

@Override
public Map<Field, Object> contributeMocks() {
injectMocks(testInstance);
return stream(FieldUtils.getAllFields(testInstance.getClass()))
.filter(this::isMockField)
.collect(Collectors.toMap(
it -> it,
it -> {
try {
it.setAccessible(true);
return it.get(testInstance);
} catch (IllegalAccessException e) {
throw new TestEEfiException("Failed to retrieve mock from test instance", e);
}
@Override
public Map<Field, Object> contributeMocks() {
injectMocks(testInstance);
return stream(FieldUtils.getAllFields(testInstance.getClass()))
.filter(this::isMockField)
.collect(Collectors.toMap(
it -> it,
it -> {
try {
it.setAccessible(true);
return it.get(testInstance);
} catch (IllegalAccessException e) {
throw new TestEEfiException("Failed to retrieve mock from test instance", e);
}
));
}
));
}

protected abstract boolean isMockField(Field field);
@@ -28,7 +28,7 @@
private static final Logger LOG = LoggerFactory.getLogger(NoPostConstructInterceptor.class);

@PostConstruct
public Object logPostConstruct(final InvocationContext invocationContext) throws Exception {
public Object logPostConstruct(final InvocationContext invocationContext) {
LOG.trace("Skipping @PostConstruct for " + invocationContext.getTarget());
return null;
}
@@ -57,6 +57,9 @@ public String getValue() {

@Override
public String getLocation() {
if (beansXml == null || beansXml.getUrl() == null) {
return "null";
}
return beansXml.getUrl().toString();
}
});

0 comments on commit 9f1cbb3

Please sign in to comment.
You can’t perform that action at this time.