org.apache.openejb.mockito.MockitoExtension Maven / Gradle / Ivy
The newest version!
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.apache.openejb.mockito;
import org.apache.webbeans.annotation.AnyLiteral;
import org.apache.webbeans.annotation.DefaultLiteral;
import org.apache.webbeans.annotation.NamedLiteral;
import org.mockito.cglib.proxy.Factory;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.Dependent;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.event.Observes;
import javax.enterprise.inject.spi.AfterBeanDiscovery;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.Extension;
import javax.enterprise.inject.spi.InjectionPoint;
import java.lang.annotation.Annotation;
import java.lang.reflect.Proxy;
import java.lang.reflect.Type;
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
/**
* This class is responsible to add mocks as CDI beans.
*/
public class MockitoExtension implements Extension {
private static final Annotation DEFAULT_ANNOTATION = new DefaultLiteral();
private static final Annotation ANY_ANNOTATION = new AnyLiteral();
public void addMocks(@Observes final AfterBeanDiscovery abd) {
for (Map.Entry, Object> instance : MockRegistry.mocksByType().entrySet()) {
abd.addBean(new MockBean(instance.getKey(), instance.getValue()));
}
for (Map.Entry instance : MockRegistry.mocksByName().entrySet()) {
abd.addBean(new NamedMockBean(instance.getKey(), instance.getValue()));
}
}
private static class MockBean implements Bean {
protected static final Set QUALIFIERS = new HashSet(2) {{
add(DEFAULT_ANNOTATION);
add(ANY_ANNOTATION);
}};
protected final Class clazz;
protected final Object instance;
protected final HashSet types;
public MockBean(final Class key, final Object value) {
clazz = key;
instance = value;
types = new HashSet();
Class> current = clazz;
if (clazz != null) {
if (!Proxy.isProxyClass(current)) {
while (!Object.class.equals(current) && current != null) {
types.add(current);
current = current.getSuperclass();
}
}
for (Class> itf : clazz.getInterfaces()) {
if (Factory.class.isAssignableFrom(itf)) {
continue;
}
types.add(itf);
}
}
}
public Set getTypes() {
return types;
}
public Set getQualifiers() {
return QUALIFIERS;
}
public Class extends Annotation> getScope() {
return Dependent.class;
}
public String getName() {
return null;
}
public boolean isNullable() {
return false;
}
public Set getInjectionPoints() {
return Collections.emptySet();
}
public Class> getBeanClass() {
return clazz;
}
public Set> getStereotypes() {
return Collections.emptySet();
}
public boolean isAlternative() {
return true;
}
public T create(final CreationalContext context) {
return clazz.cast(instance);
}
public void destroy(final T instance, final CreationalContext context) {
// no-op
}
}
private static class NamedMockBean extends MockBean {
private final String name;
private final Set qualifiers;
public NamedMockBean(final String named, final Object value) {
super((Class) value.getClass(), value);
name = named;
// we need to pass value.getClass() to get interfaces
// but we don't want the proxy to be injectable
if (!clazz.isInterface()) {
types.remove(clazz);
}
qualifiers = new HashSet(2);
qualifiers.add(ANY_ANNOTATION);
qualifiers.add(new NamedLiteral(name));
}
@Override
public String getName() {
return name;
}
@Override
public Set getQualifiers() {
return qualifiers;
}
@Override
public T create(final CreationalContext context) {
return clazz.cast(instance);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy