001/* 002 * (C) Copyright 2013 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * dmetzler 018 */ 019package org.nuxeo.runtime.mockito; 020 021import java.lang.annotation.Annotation; 022import java.lang.reflect.Field; 023import java.util.HashMap; 024import java.util.Map; 025 026import org.mockito.Captor; 027import org.mockito.Mock; 028import org.mockito.MockitoAnnotations; 029import org.mockito.exceptions.Reporter; 030import org.mockito.internal.configuration.CaptorAnnotationProcessor; 031import org.mockito.internal.configuration.DefaultAnnotationEngine; 032import org.mockito.internal.configuration.FieldAnnotationProcessor; 033import org.mockito.internal.configuration.MockitoAnnotationsMockAnnotationProcessor; 034 035/** 036 * Unfortunately, since there are some private methos in the Mockito DefaultAnnotationEngine we have to copy/paste some 037 * original code to insert our own logic. 038 * 039 * @since 5.7.8 040 */ 041public class NuxeoDefaultAnnotationEngine extends DefaultAnnotationEngine { 042 043 private final Map<Class<? extends Annotation>, FieldAnnotationProcessor<?>> annotationProcessorMap = new HashMap<>(); 044 045 public NuxeoDefaultAnnotationEngine() { 046 registerAnnotationProcessor(Mock.class, new NuxeoServiceMockAnnotationProcessor()); 047 registerAnnotationProcessor(MockitoAnnotations.Mock.class, new MockitoAnnotationsMockAnnotationProcessor()); 048 registerAnnotationProcessor(Captor.class, new CaptorAnnotationProcessor()); 049 050 } 051 052 /* 053 * (non-Javadoc) 054 * @see org.mockito.AnnotationEngine#createMockFor(java.lang.annotation.Annotation , java.lang.reflect.Field) 055 */ 056 @Override 057 public Object createMockFor(Annotation annotation, Field field) { 058 return forAnnotation(annotation).process(annotation, field); 059 } 060 061 private <A extends Annotation> FieldAnnotationProcessor<A> forAnnotation(A annotation) { // NOSONAR 062 if (annotationProcessorMap.containsKey(annotation.annotationType())) { 063 return (FieldAnnotationProcessor<A>) annotationProcessorMap.get(annotation.annotationType()); 064 } 065 return new FieldAnnotationProcessor<>() { 066 @Override 067 public Object process(A annotation, Field field) { 068 return null; 069 } 070 }; 071 } 072 073 private <A extends Annotation> void registerAnnotationProcessor(Class<A> annotationClass, // NOSONAR 074 FieldAnnotationProcessor<A> fieldAnnotationProcessor) { 075 annotationProcessorMap.put(annotationClass, fieldAnnotationProcessor); 076 } 077 078 void throwIfAlreadyAssigned(Field field, boolean alreadyAssigned) { // NOSONAR 079 if (alreadyAssigned) { 080 new Reporter().moreThanOneAnnotationNotAllowed(field.getName()); 081 } 082 } 083}