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<Class<? extends Annotation>, FieldAnnotationProcessor<?>>(); 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 @SuppressWarnings("deprecation") 058 public Object createMockFor(Annotation annotation, Field field) { 059 return forAnnotation(annotation).process(annotation, field); 060 } 061 062 private <A extends Annotation> FieldAnnotationProcessor<A> forAnnotation(A annotation) { 063 if (annotationProcessorMap.containsKey(annotation.annotationType())) { 064 return (FieldAnnotationProcessor<A>) annotationProcessorMap.get(annotation.annotationType()); 065 } 066 return new FieldAnnotationProcessor<A>() { 067 @Override 068 public Object process(A annotation, Field field) { 069 return null; 070 } 071 }; 072 } 073 074 private <A extends Annotation> void registerAnnotationProcessor(Class<A> annotationClass, 075 FieldAnnotationProcessor<A> fieldAnnotationProcessor) { 076 annotationProcessorMap.put(annotationClass, fieldAnnotationProcessor); 077 } 078 079 void throwIfAlreadyAssigned(Field field, boolean alreadyAssigned) { 080 if (alreadyAssigned) { 081 new Reporter().moreThanOneAnnotationNotAllowed(field.getName()); 082 } 083 } 084}