Java tutorial
/* * Copyright 2015 Sharmarke Aden <saden1 at gmail.org>. * * 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 com.fitbur.jestify.junit.spring.injector; import com.fitbur.jestify.api.Mock; import com.fitbur.jestify.core.TestContext; import com.fitbur.jestify.core.TestInjector; import com.fitbur.jestify.core.TestReifier; import com.fitbur.jestify.core.descriptor.DescriptorKey; import com.fitbur.jestify.core.descriptor.FieldDescriptor; import com.fitbur.jestify.core.descriptor.ParameterDescriptor; import java.lang.reflect.Field; import java.lang.reflect.Parameter; import java.lang.reflect.Type; import java.util.Collection; import java.util.Map; import java.util.Optional; import javax.inject.Provider; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.core.ResolvableType; /** * * @author saden1 */ public class TypeMockInjector implements TestInjector { private final TestContext context; private final TestReifier testReifier; private final AnnotationConfigApplicationContext applicationContext; private final FieldDescriptor fieldDescriptor; private final Object[] arguments; public TypeMockInjector(TestContext context, TestReifier testReifier, AnnotationConfigApplicationContext applicationContext, FieldDescriptor fieldDescriptor, Object[] arguments) { this.context = context; this.testReifier = testReifier; this.applicationContext = applicationContext; this.fieldDescriptor = fieldDescriptor; this.arguments = arguments; } @Override public void inject() { Map<DescriptorKey, ParameterDescriptor> parameterDescriptors = context.getParamaterDescriptors(); Field field = fieldDescriptor.getField(); Mock mock = fieldDescriptor.getMock().get(); Type fieldType = field.getGenericType(); String fieldName = field.getName(); DescriptorKey descriptorKey = new DescriptorKey(fieldType, fieldName); //if there is a parameter descriptor that matches the field then lets use that if (parameterDescriptors.containsKey(descriptorKey)) { ParameterDescriptor descriptor = parameterDescriptors.get(descriptorKey); Parameter parameter = descriptor.getParameter(); Integer index = descriptor.getIndex(); ResolvableType resolver = ResolvableType.forType(fieldType); Class rawType; if (resolver.hasGenerics()) { if (resolver.isAssignableFrom(Provider.class) || resolver.isAssignableFrom(Optional.class)) { rawType = resolver.getRawClass(); } else { rawType = resolver.resolve(); } } else { rawType = (Class) fieldType; } Object instance = testReifier.reifyField(fieldDescriptor, descriptor); arguments[index] = instance; } else { //otherwise find the right parameter based on the type of the field Collection<ParameterDescriptor> descriptors = parameterDescriptors.values(); for (ParameterDescriptor descriptor : descriptors) { Parameter parameter = descriptor.getParameter(); Type parameterType = parameter.getParameterizedType(); Integer index = descriptor.getIndex(); ResolvableType resolver = ResolvableType.forType(parameterType); Class rawType; if (resolver.hasGenerics()) { if (resolver.isAssignableFrom(Provider.class) || resolver.isAssignableFrom(Optional.class)) { rawType = resolver.getRawClass(); } else { rawType = resolver.resolve(); } } else { rawType = resolver.resolve(); } if (arguments[index] != null) { continue; } if (parameterType.equals(fieldType)) { Object instance = testReifier.reifyField(fieldDescriptor, descriptor); arguments[index] = instance; break; } } } } }