|
| 1 | +package com.github.exabrial.junit5.injectmap; |
| 2 | + |
| 3 | +import java.lang.reflect.Field; |
| 4 | +import java.lang.reflect.InvocationTargetException; |
| 5 | +import java.lang.reflect.Method; |
| 6 | +import java.lang.reflect.Modifier; |
| 7 | +import java.util.HashMap; |
| 8 | +import java.util.LinkedList; |
| 9 | +import java.util.List; |
| 10 | +import java.util.Map; |
| 11 | + |
| 12 | +import javax.annotation.PostConstruct; |
| 13 | + |
| 14 | +import org.junit.jupiter.api.extension.BeforeTestExecutionCallback; |
| 15 | +import org.junit.jupiter.api.extension.ExtensionContext; |
| 16 | +import org.mockito.InjectMocks; |
| 17 | + |
| 18 | +import javassist.util.proxy.MethodFilter; |
| 19 | +import javassist.util.proxy.MethodHandler; |
| 20 | +import javassist.util.proxy.Proxy; |
| 21 | +import javassist.util.proxy.ProxyFactory; |
| 22 | + |
| 23 | +public class InjectExtension implements BeforeTestExecutionCallback { |
| 24 | + @Override |
| 25 | + public void beforeTestExecution(ExtensionContext context) throws Exception { |
| 26 | + Object testInstance = context.getTestInstance().get(); |
| 27 | + if (testInstance != null) { |
| 28 | + final Map<String, Field> injectMap = new HashMap<>(); |
| 29 | + for (Field testClassField : testInstance.getClass().getDeclaredFields()) { |
| 30 | + if (testClassField.getAnnotation(InjectMocks.class) != null) { |
| 31 | + testClassField.setAccessible(true); |
| 32 | + final Object injectionTarget = testClassField.get(testInstance); |
| 33 | + final ProxyFactory proxyFactory = new ProxyFactory(); |
| 34 | + proxyFactory.setSuperclass(injectionTarget.getClass()); |
| 35 | + proxyFactory.setFilter(createMethodFilter()); |
| 36 | + final Class<?> proxyClass = proxyFactory.createClass(); |
| 37 | + final Object proxy = proxyClass.newInstance(); |
| 38 | + final Map<String, List<Field>> fieldMap = createFieldMap(injectionTarget.getClass()); |
| 39 | + Method postConstructMethod; |
| 40 | + if (testClassField.getAnnotation(InvokePostConstruct.class) != null) { |
| 41 | + postConstructMethod = findPostConstructMethod(injectionTarget); |
| 42 | + } else { |
| 43 | + postConstructMethod = null; |
| 44 | + } |
| 45 | + final MethodHandler handler = createMethodHandler(injectMap, injectionTarget, fieldMap, testInstance, postConstructMethod); |
| 46 | + ((Proxy) proxy).setHandler(handler); |
| 47 | + testClassField.set(testInstance, proxy); |
| 48 | + } else if (testClassField.getAnnotation(InjectionSource.class) != null) { |
| 49 | + injectMap.put(testClassField.getName(), testClassField); |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + private Method findPostConstructMethod(Object injectionTarget) { |
| 56 | + for (Method method : injectionTarget.getClass().getDeclaredMethods()) { |
| 57 | + if (method.isAnnotationPresent(PostConstruct.class)) { |
| 58 | + return method; |
| 59 | + } |
| 60 | + } |
| 61 | + throw new RuntimeException( |
| 62 | + "@InvokePostConstruct is delcared on:" + injectionTarget + " however no method annotated with @PostConstruct found"); |
| 63 | + } |
| 64 | + |
| 65 | + private Map<String, List<Field>> createFieldMap(Class<? extends Object> targetClass) { |
| 66 | + if (targetClass == Object.class) { |
| 67 | + return new HashMap<>(); |
| 68 | + } else { |
| 69 | + Map<String, List<Field>> fieldMap = createFieldMap(targetClass.getSuperclass()); |
| 70 | + for (Field field : targetClass.getDeclaredFields()) { |
| 71 | + List<Field> fieldList = fieldMap.get(field.getName()); |
| 72 | + if (fieldList == null) { |
| 73 | + fieldList = new LinkedList<>(); |
| 74 | + fieldMap.put(field.getName(), fieldList); |
| 75 | + } |
| 76 | + fieldList.add(field); |
| 77 | + } |
| 78 | + return fieldMap; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + private MethodHandler createMethodHandler(final Map<String, Field> injectMap, final Object injectionTarget, |
| 83 | + final Map<String, List<Field>> fieldMap, final Object testInstance, final Method postConstructMethod) { |
| 84 | + return (proxy, invokedMethod, proceedMethod, args) -> { |
| 85 | + invokedMethod.setAccessible(true); |
| 86 | + for (String fieldName : injectMap.keySet()) { |
| 87 | + for (Field field : fieldMap.get(fieldName)) { |
| 88 | + field.setAccessible(true); |
| 89 | + field.set(injectionTarget, injectMap.get(fieldName).get(testInstance)); |
| 90 | + } |
| 91 | + } |
| 92 | + if (postConstructMethod != null) { |
| 93 | + postConstructMethod.setAccessible(true); |
| 94 | + postConstructMethod.invoke(injectionTarget); |
| 95 | + } |
| 96 | + try { |
| 97 | + return invokedMethod.invoke(injectionTarget, args); |
| 98 | + } catch (InvocationTargetException itEx) { |
| 99 | + if (null != itEx.getCause()) { |
| 100 | + throw itEx.getCause(); |
| 101 | + } else { |
| 102 | + throw itEx; |
| 103 | + } |
| 104 | + } |
| 105 | + }; |
| 106 | + } |
| 107 | + |
| 108 | + private MethodFilter createMethodFilter() { |
| 109 | + return method -> !Modifier.isPrivate(method.getModifiers()); |
| 110 | + } |
| 111 | +} |
0 commit comments