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;
023
024import org.mockito.Mock;
025import org.mockito.Mockito;
026import org.mockito.internal.configuration.FieldAnnotationProcessor;
027import org.nuxeo.runtime.api.DefaultServiceProvider;
028
029/**
030 * @since 5.7.8
031 */
032public class NuxeoServiceMockAnnotationProcessor implements FieldAnnotationProcessor<Mock> {
033
034    @Override
035    public Object process(Mock annotation, final Field field) {
036
037        Object mock = Mockito.mock(field.getType(), field.getName());
038
039        for (Annotation ann : field.getAnnotations()) {
040            if (ann.annotationType().equals(RuntimeService.class)) {
041                bindMockAsNuxeoService(field, mock);
042            }
043        }
044
045        return mock;
046    }
047
048    protected void bindMockAsNuxeoService(final Field field, Object mock) {
049        MockProvider provider = (MockProvider) DefaultServiceProvider.getProvider();
050        provider.bind(field.getType(), mock);
051    }
052
053}