use of io.micronaut.test.annotation.MockBean in project micronaut-test by micronaut-projects.
the class TestActiveCondition method matches.
@Override
public boolean matches(ConditionContext context) {
if (context.getComponent() instanceof BeanDefinition) {
BeanDefinition<?> definition = (BeanDefinition<?>) context.getComponent();
final BeanContext beanContext = context.getBeanContext();
final Optional<Class<?>> declaringType = definition.getDeclaringType();
if (beanContext instanceof ApplicationContext) {
ApplicationContext applicationContext = (ApplicationContext) beanContext;
final Class activeSpecClazz = applicationContext.get(ACTIVE_SPEC_CLAZZ, Class.class).orElse(null);
final String activeSpecName = Optional.ofNullable(activeSpecClazz).map(clazz -> clazz.getPackage().getName() + "." + clazz.getSimpleName()).orElse(null);
if (definition.isAnnotationPresent(MockBean.class) && declaringType.isPresent()) {
final Class<?> declaringTypeClass = declaringType.get();
String declaringTypeName = declaringTypeClass.getName();
if (activeSpecClazz != null) {
final String packageName = NameUtils.getPackageName(activeSpecName);
final String simpleName = NameUtils.getSimpleName(activeSpecName);
final String rootName = packageName + ".$" + simpleName;
if (definition.isProxy()) {
return declaringTypeClass.isAssignableFrom(activeSpecClazz) || declaringTypeName.equals(rootName) || declaringTypeName.startsWith(rootName + "$");
} else {
return declaringTypeClass.isAssignableFrom(activeSpecClazz) && // Check if super class def is part of the current spec
definition.getClass().getName().startsWith(rootName + "$") || activeSpecName.equals(declaringTypeName) || declaringTypeName.startsWith(activeSpecName + "$");
}
} else {
context.fail("@MockBean of type " + definition.getBeanType() + " not within scope of parent test.");
return false;
}
} else {
if (activeSpecName != null) {
boolean beanTypeMatches = activeSpecName.equals(definition.getBeanType().getName());
return beanTypeMatches || (declaringType.isPresent() && activeSpecClazz == declaringType.get());
} else {
return false;
}
}
} else {
return false;
}
} else {
return true;
}
}
Aggregations