|
| 1 | +/* |
| 2 | + * Copyright 2002-2013 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.beans.factory.support; |
| 18 | + |
| 19 | +import java.lang.reflect.Method; |
| 20 | +import java.util.HashMap; |
| 21 | +import java.util.Map; |
| 22 | + |
| 23 | +import org.junit.Test; |
| 24 | + |
| 25 | +import org.springframework.util.ReflectionUtils; |
| 26 | + |
| 27 | +import static org.junit.Assert.*; |
| 28 | + |
| 29 | +/** |
| 30 | + * @author Juergen Hoeller |
| 31 | + * @author Sam Brannen |
| 32 | + */ |
| 33 | +public class AutowireUtilsTests { |
| 34 | + |
| 35 | + @Test |
| 36 | + public void genericMethodReturnTypes() { |
| 37 | + Method notParameterized = ReflectionUtils.findMethod(MyTypeWithMethods.class, "notParameterized", new Class[]{}); |
| 38 | + assertEquals(String.class, |
| 39 | + AutowireUtils.resolveReturnTypeForFactoryMethod(notParameterized, new Object[]{}, getClass().getClassLoader())); |
| 40 | + |
| 41 | + Method notParameterizedWithArguments = ReflectionUtils.findMethod(MyTypeWithMethods.class, "notParameterizedWithArguments", |
| 42 | + new Class[] { Integer.class, Boolean.class }); |
| 43 | + assertEquals(String.class, |
| 44 | + AutowireUtils.resolveReturnTypeForFactoryMethod(notParameterizedWithArguments, new Object[] { 99, true }, getClass().getClassLoader())); |
| 45 | + |
| 46 | + Method createProxy = ReflectionUtils.findMethod(MyTypeWithMethods.class, "createProxy", new Class[] { Object.class }); |
| 47 | + assertEquals(String.class, |
| 48 | + AutowireUtils.resolveReturnTypeForFactoryMethod(createProxy, new Object[] { "foo" }, getClass().getClassLoader())); |
| 49 | + |
| 50 | + Method createNamedProxyWithDifferentTypes = ReflectionUtils.findMethod(MyTypeWithMethods.class, "createNamedProxy", |
| 51 | + new Class[] { String.class, Object.class }); |
| 52 | + // one argument to few |
| 53 | + assertNull( |
| 54 | + AutowireUtils.resolveReturnTypeForFactoryMethod(createNamedProxyWithDifferentTypes, new Object[]{"enigma"}, getClass().getClassLoader())); |
| 55 | + assertEquals(Long.class, |
| 56 | + AutowireUtils.resolveReturnTypeForFactoryMethod(createNamedProxyWithDifferentTypes, new Object[] { "enigma", 99L }, getClass().getClassLoader())); |
| 57 | + |
| 58 | + Method createNamedProxyWithDuplicateTypes = ReflectionUtils.findMethod(MyTypeWithMethods.class, "createNamedProxy", |
| 59 | + new Class[] { String.class, Object.class }); |
| 60 | + assertEquals(String.class, |
| 61 | + AutowireUtils.resolveReturnTypeForFactoryMethod(createNamedProxyWithDuplicateTypes, new Object[] { "enigma", "foo" }, getClass().getClassLoader())); |
| 62 | + |
| 63 | + Method createMock = ReflectionUtils.findMethod(MyTypeWithMethods.class, "createMock", new Class[] { Class.class }); |
| 64 | + assertEquals(Runnable.class, |
| 65 | + AutowireUtils.resolveReturnTypeForFactoryMethod(createMock, new Object[] { Runnable.class }, getClass().getClassLoader())); |
| 66 | + assertEquals(Runnable.class, |
| 67 | + AutowireUtils.resolveReturnTypeForFactoryMethod(createMock, new Object[] { Runnable.class.getName() }, getClass().getClassLoader())); |
| 68 | + |
| 69 | + Method createNamedMock = ReflectionUtils.findMethod(MyTypeWithMethods.class, "createNamedMock", new Class[] { String.class, |
| 70 | + Class.class }); |
| 71 | + assertEquals(Runnable.class, |
| 72 | + AutowireUtils.resolveReturnTypeForFactoryMethod(createNamedMock, new Object[] { "foo", Runnable.class }, getClass().getClassLoader())); |
| 73 | + |
| 74 | + Method createVMock = ReflectionUtils.findMethod(MyTypeWithMethods.class, "createVMock", |
| 75 | + new Class[] { Object.class, Class.class }); |
| 76 | + assertEquals(Runnable.class, |
| 77 | + AutowireUtils.resolveReturnTypeForFactoryMethod(createVMock, new Object[] { "foo", Runnable.class }, getClass().getClassLoader())); |
| 78 | + |
| 79 | + // Ideally we would expect String.class instead of Object.class, but |
| 80 | + // resolveReturnTypeForFactoryMethod() does not currently support this form of |
| 81 | + // look-up. |
| 82 | + Method extractValueFrom = ReflectionUtils.findMethod(MyTypeWithMethods.class, "extractValueFrom", |
| 83 | + new Class[] { MyInterfaceType.class }); |
| 84 | + assertEquals(Object.class, |
| 85 | + AutowireUtils.resolveReturnTypeForFactoryMethod(extractValueFrom, new Object[] { new MySimpleInterfaceType() }, getClass().getClassLoader())); |
| 86 | + |
| 87 | + // Ideally we would expect Boolean.class instead of Object.class, but this |
| 88 | + // information is not available at run-time due to type erasure. |
| 89 | + Map<Integer, Boolean> map = new HashMap<Integer, Boolean>(); |
| 90 | + map.put(0, false); |
| 91 | + map.put(1, true); |
| 92 | + Method extractMagicValue = ReflectionUtils.findMethod(MyTypeWithMethods.class, "extractMagicValue", new Class[] { Map.class }); |
| 93 | + assertEquals(Object.class, AutowireUtils.resolveReturnTypeForFactoryMethod(extractMagicValue, new Object[] { map }, getClass().getClassLoader())); |
| 94 | + } |
| 95 | + |
| 96 | + |
| 97 | + public interface MyInterfaceType<T> { |
| 98 | + } |
| 99 | + |
| 100 | + public class MySimpleInterfaceType implements MyInterfaceType<String> { |
| 101 | + } |
| 102 | + |
| 103 | + public static class MyTypeWithMethods<T> { |
| 104 | + |
| 105 | + public MyInterfaceType<Integer> integer() { |
| 106 | + return null; |
| 107 | + } |
| 108 | + |
| 109 | + public MySimpleInterfaceType string() { |
| 110 | + return null; |
| 111 | + } |
| 112 | + |
| 113 | + public Object object() { |
| 114 | + return null; |
| 115 | + } |
| 116 | + |
| 117 | + @SuppressWarnings("rawtypes") |
| 118 | + public MyInterfaceType raw() { |
| 119 | + return null; |
| 120 | + } |
| 121 | + |
| 122 | + public String notParameterized() { |
| 123 | + return null; |
| 124 | + } |
| 125 | + |
| 126 | + public String notParameterizedWithArguments(Integer x, Boolean b) { |
| 127 | + return null; |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Simulates a factory method that wraps the supplied object in a proxy of the |
| 132 | + * same type. |
| 133 | + */ |
| 134 | + public static <T> T createProxy(T object) { |
| 135 | + return null; |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Similar to {@link #createProxy(Object)} but adds an additional argument before |
| 140 | + * the argument of type {@code T}. Note that they may potentially be of the same |
| 141 | + * time when invoked! |
| 142 | + */ |
| 143 | + public static <T> T createNamedProxy(String name, T object) { |
| 144 | + return null; |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Simulates factory methods found in libraries such as Mockito and EasyMock. |
| 149 | + */ |
| 150 | + public static <MOCK> MOCK createMock(Class<MOCK> toMock) { |
| 151 | + return null; |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Similar to {@link #createMock(Class)} but adds an additional method argument |
| 156 | + * before the parameterized argument. |
| 157 | + */ |
| 158 | + public static <T> T createNamedMock(String name, Class<T> toMock) { |
| 159 | + return null; |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Similar to {@link #createNamedMock(String, Class)} but adds an additional |
| 164 | + * parameterized type. |
| 165 | + */ |
| 166 | + public static <V extends Object, T> T createVMock(V name, Class<T> toMock) { |
| 167 | + return null; |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * Extract some value of the type supported by the interface (i.e., by a concrete, |
| 172 | + * non-generic implementation of the interface). |
| 173 | + */ |
| 174 | + public static <T> T extractValueFrom(MyInterfaceType<T> myInterfaceType) { |
| 175 | + return null; |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + * Extract some magic value from the supplied map. |
| 180 | + */ |
| 181 | + public static <K, V> V extractMagicValue(Map<K, V> map) { |
| 182 | + return null; |
| 183 | + } |
| 184 | + |
| 185 | + public void readIntegerInputMessage(MyInterfaceType<Integer> message) { |
| 186 | + } |
| 187 | + |
| 188 | + public void readIntegerArrayInputMessage(MyInterfaceType<Integer>[] message) { |
| 189 | + } |
| 190 | + |
| 191 | + public void readGenericArrayInputMessage(T[] message) { |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | +} |
0 commit comments