/*
 * Copyright (c) 2010-2016. Axon Framework
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.axonframework.test.utils;

import org.axonframework.common.ReflectionUtils;
import org.axonframework.eventhandling.saga.ResourceInjector;
import org.axonframework.test.FixtureExecutionException;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import static org.axonframework.common.ReflectionUtils.methodsOf;

/**
 * Resource injector that uses setter methods to inject resources. All methods starting with "set" are evaluated. If
 * that method has a single parameter, a Resource of that type is injected into it, if present.
 *
 * @author Allard Buijze
 * @since 1.1
 */
public class AutowiredResourceInjector implements ResourceInjector {

    private Iterable<?> resources;

    /**
     * Initializes the resource injector to inject to given <code>resources</code>.
     *
     * @param resources The resources to inject
     */
    public AutowiredResourceInjector(Iterable<?> resources) {
        this.resources = resources;
    }

    @Override
    public void injectResources(Object saga) {
        for (Method method : methodsOf(saga.getClass())) {
            if (isSetter(method)) {
                Class<?> requiredType = method.getParameterTypes()[0];
                for (Object resource : resources) {
                    if (requiredType.isInstance(resource)) {
                        injectResource(saga, method, resource);
                    }
                }
            }
        }
    }

    private void injectResource(Object saga, Method setterMethod, Object resource) {
        try {
            ReflectionUtils.ensureAccessible(setterMethod);
            setterMethod.invoke(saga, resource);
        } catch (IllegalAccessException | InvocationTargetException e) {
            throw new FixtureExecutionException("An exception occurred while trying to inject a resource", e);
        }
    }

    private boolean isSetter(Method method) {
        return method.getParameterTypes().length == 1 && method.getName().startsWith("set");
    }
}

@@ -645,57 +645,8 @@
 s;%0A%0A
-import org.axonframework.common.ReflectionUtils;%0A
 impo
@@ -685,16 +685,24 @@
 ng.saga.
+Abstract
 Resource
@@ -715,216 +715,34 @@
 or;%0A
-import org.axonframework.test.FixtureExecutionException;%0A%0Aimport java.lang.reflect.InvocationTargetException;%0Aimport java.lang.reflect.Method;%0A%0Aimport static org.axonframework.common.ReflectionUtils.methodsOf
+%0Aimport java.util.Optional
 ;%0A%0A/
@@ -1046,19 +1046,24 @@
 tor 
-implement
+extend
 s 
+Abstract
 Reso
@@ -1397,168 +1397,53 @@
    p
-ublic void injectResources(Object saga) %7B%0A        for (Method method : methodsOf(saga.getClass())) %7B%0A            if (isSetter(method)) %7B%0A                
+rotected %3CR%3E Optional%3CR%3E findResource(
 Class%3C
-?
+R
 %3E re
@@ -1456,50 +1456,12 @@
 Type
- = method.getParameterTypes()%5B0%5D;%0A        
+) %7B%0A
     
@@ -1512,24 +1512,16 @@
         
-        
 if (requ
@@ -1573,512 +1573,53 @@
     
-        injectResource(saga, method, resource);%0A                    %7D%0A                %7D%0A            %7D%0A        %7D%0A    %7D%0A%0A    private void injectResource(Object saga, Method setterMethod, Object resource) %7B%0A        try %7B%0A            ReflectionUtils.ensureAccessible(setterMethod);%0A            setterMethod.invoke(saga, resource);%0A        %7D catch (IllegalAccessException %7C InvocationTargetException e) %7B%0A            throw new FixtureExecutionException(%22An exception occurred while trying to inject a 
+return Optional.of(requiredType.cast(
 resource
 %22, e
@@ -1618,12 +1618,9 @@
 urce
-%22, e
+)
 );%0A 
@@ -1630,153 +1630,54 @@
     
-%7D%0A
     %7D%0A
-%0A
     
-private boolean isSetter(Method method) %7B%0A        return method.getParameterTypes().length == 1 && method.getName().startsWith(%22set%22
+    %7D%0A        return Optional.empty(
 );%0A 
@@ -1681,10 +1681,11 @@
 ;%0A    %7D%0A
+%0A
 %7D%0A

Test fixtures should use same resourceInjector mechanism as runtime