org.kubek2k.springockito.annotations.internal.DesiredMockitoBeansFinder Maven / Gradle / Ivy
package org.kubek2k.springockito.annotations.internal;
import org.kubek2k.springockito.annotations.ReplaceWithMock;
import org.kubek2k.springockito.annotations.WrapWithSpy;
import org.kubek2k.springockito.annotations.internal.naming.BeanNameResolver;
import org.kubek2k.springockito.annotations.internal.naming.BeanNameResolverChainOfResponsibility;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
public class DesiredMockitoBeansFinder {
private final BeanNameResolver beanNameResolver = new BeanNameResolverChainOfResponsibility();
public static class MockProperties {
private T annotationInstance;
private Class> mockClass;
public MockProperties(T annotationInstance, Class> mockClass) {
this.annotationInstance = annotationInstance;
this.mockClass = mockClass;
}
public T getAnnotationInstance() {
return annotationInstance;
}
public Class> getMockClass() {
return mockClass;
}
}
public Map> findMockedBeans(Class> clazz) {
Map> mockedBeans = new LinkedHashMap>();
for (Field field : clazz.getDeclaredFields()) {
ReplaceWithMock annotationInstance = field.getAnnotation(ReplaceWithMock.class);
if (annotationInstance != null) {
String beanName = getBeanName(field);
MockProperties mockProperties = new MockProperties(annotationInstance, field.getType());
mockedBeans.put(beanName, mockProperties);
}
}
return mockedBeans;
}
public Set findSpiedBeans(Class> clazz) {
Set mockedBeans = new HashSet();
for (Field field : clazz.getDeclaredFields()) {
Annotation replaceWithMockAnnotation = field.getAnnotation(WrapWithSpy.class);
if (replaceWithMockAnnotation != null) {
String beanName = getBeanName(field);
mockedBeans.add(beanName);
}
}
return mockedBeans;
}
private String getBeanName(Field field) {
return beanNameResolver.retrieveBeanName(field);
}
}