All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.kubek2k.springockito.annotations.DesiredMockitoBeansFinder Maven / Gradle / Ivy

There is a newer version: 1.0.9
Show newest version
package org.kubek2k.springockito.annotations;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.*;

class DesiredMockitoBeansFinder {
    public static class MockProperties {
        private AnnotationType annotationInstance;

        private Class mockClass;
        private AnnotationClasspathRepresentationCreator annotationClasspathRepresentationCreator;

        public MockProperties(AnnotationType annotationInstance, Class mockClass, AnnotationClasspathRepresentationCreator annotationClasspathRepresentationCreator) {
            this.annotationInstance = annotationInstance;
            this.mockClass = mockClass;
            this.annotationClasspathRepresentationCreator = annotationClasspathRepresentationCreator;
        }

        public AnnotationType getAnnotationInstance() {
            return annotationInstance;
        }

        public Class getMockClass() {
            return mockClass;
        }

        public String getClasspathRepresentation() {
            return mockClass + ":" + annotationClasspathRepresentationCreator.toClasspathRepresentation(annotationInstance);
        }

    }

    private interface AnnotationClasspathRepresentationCreator {

        public String toClasspathRepresentation(AnnotationType annotationType);

    }

    public Map> findMockedBeans(Class clazz) {
        return findAnnotatedFieldsTypes(clazz.getDeclaredFields(), ReplaceWithMock.class, new AnnotationClasspathRepresentationCreator() {
            public String toClasspathRepresentation(ReplaceWithMock replaceWithMock) {
                String defaultAnswer = replaceWithMock.defaultAnswer() != null ? replaceWithMock.defaultAnswer().toString() : "";
                String extraInterfaces = replaceWithMock.extraInterfaces() != null ? Arrays.toString(replaceWithMock.extraInterfaces()) : "";
                return replaceWithMock.name() + "," + defaultAnswer + "," + extraInterfaces;
            }
        });
    }

    public Set findSpiedBeans(Class clazz) {
        return findAnnotatedFieldsTypes(clazz.getDeclaredFields(), WrapWithSpy.class, new AnnotationClasspathRepresentationCreator() {
            public String toClasspathRepresentation(WrapWithSpy wrapWithSpy) {
                return "";
            }
        }).keySet();
    }

    private  Map> findAnnotatedFieldsTypes(Field[] fieldsToScan, Class annotationClass, AnnotationClasspathRepresentationCreator annotationClasspathRepresentationCreator) {
        Map> mockedBeans = new LinkedHashMap>();
        for (Field field : fieldsToScan) {
            Annotation replaceWithMockAnnotation = field.getAnnotation(annotationClass);
            if (replaceWithMockAnnotation != null) {
                mockedBeans.put(field.getName(), new MockProperties((AnnotationType)field.getAnnotation(annotationClass), field.getType(), annotationClasspathRepresentationCreator));
            }
        }
        return mockedBeans;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy