Skip to content

Commit b305f00

Browse files
committed
Add test to assess claims in SPR-10411
This commit verifies that the return type of a parameterized instance factory method is properly resolved. Issue: SPR-10411
1 parent b3a693e commit b305f00

File tree

1 file changed

+70
-16
lines changed

1 file changed

+70
-16
lines changed

spring-beans/src/test/java/org/springframework/beans/factory/support/BeanFactoryGenericsTests.java

Lines changed: 70 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,9 @@
1616

1717
package org.springframework.beans.factory.support;
1818

19-
import static org.junit.Assert.assertEquals;
20-
import static org.junit.Assert.assertNotSame;
21-
import static org.junit.Assert.assertNull;
22-
import static org.junit.Assert.assertSame;
23-
import static org.junit.Assert.assertTrue;
24-
import static org.junit.Assert.fail;
25-
19+
import java.lang.reflect.InvocationHandler;
20+
import java.lang.reflect.Method;
21+
import java.lang.reflect.Proxy;
2622
import java.net.MalformedURLException;
2723
import java.net.URI;
2824
import java.net.URL;
@@ -46,12 +42,12 @@
4642
import org.springframework.core.io.UrlResource;
4743
import org.springframework.tests.Assume;
4844
import org.springframework.tests.TestGroup;
49-
5045
import org.springframework.tests.sample.beans.GenericBean;
5146
import org.springframework.tests.sample.beans.GenericIntegerBean;
5247
import org.springframework.tests.sample.beans.GenericSetOfIntegerBean;
5348
import org.springframework.tests.sample.beans.TestBean;
5449

50+
import static org.junit.Assert.*;
5551

5652
/**
5753
* @author Juergen Hoeller
@@ -115,7 +111,7 @@ public void testGenericListPropertyWithInvalidElementType() {
115111
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
116112
RootBeanDefinition rbd = new RootBeanDefinition(GenericIntegerBean.class);
117113

118-
List input = new ArrayList();
114+
List<Integer> input = new ArrayList<Integer>();
119115
input.add(1);
120116
rbd.getPropertyValues().add("testBeanList", input);
121117

@@ -655,18 +651,22 @@ public void testSetBean() throws Exception {
655651
}
656652

657653
/**
658-
* Tests support for parameterized {@code factory-method} declarations such
659-
* as Mockito {@code mock()} method which has the following signature.
660-
*
661-
* <pre>{@code
654+
* Tests support for parameterized static {@code factory-method} declarations such as
655+
* Mockito's {@code mock()} method which has the following signature.
656+
*
657+
* <pre>
658+
* {@code
662659
* public static <T> T mock(Class<T> classToMock)
663-
* }</pre>
664-
*
660+
* }
661+
* </pre>
662+
*
663+
* <p>
665664
* See SPR-9493
665+
*
666666
* @since 3.2
667667
*/
668668
@Test
669-
public void parameterizedFactoryMethod() {
669+
public void parameterizedStaticFactoryMethod() {
670670
RootBeanDefinition rbd = new RootBeanDefinition(Mockito.class);
671671
rbd.setFactoryMethodName("mock");
672672
rbd.getConstructorArgumentValues().addGenericArgumentValue(Runnable.class);
@@ -678,6 +678,39 @@ public void parameterizedFactoryMethod() {
678678
assertEquals(1, beans.size());
679679
}
680680

681+
/**
682+
* Tests support for parameterized instance {@code factory-method} declarations such
683+
* as EasyMock's {@code IMocksControl.createMock()} method which has the following
684+
* signature.
685+
*
686+
* <pre>
687+
* {@code
688+
* public <T> T createMock(Class<T> toMock)
689+
* }
690+
* </pre>
691+
*
692+
* <p>
693+
* See SPR-10411
694+
*
695+
* @since 4.0
696+
*/
697+
@Test
698+
public void parameterizedInstanceFactoryMethod() {
699+
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
700+
701+
RootBeanDefinition rbd = new RootBeanDefinition(MocksControl.class);
702+
bf.registerBeanDefinition("mocksControl", rbd);
703+
704+
rbd = new RootBeanDefinition();
705+
rbd.setFactoryBeanName("mocksControl");
706+
rbd.setFactoryMethodName("createMock");
707+
rbd.getConstructorArgumentValues().addGenericArgumentValue(Runnable.class);
708+
709+
bf.registerBeanDefinition("mock", rbd);
710+
711+
Map<String, Runnable> beans = bf.getBeansOfType(Runnable.class);
712+
assertEquals(1, beans.size());
713+
}
681714

682715
@SuppressWarnings("serial")
683716
public static class NamedUrlList extends LinkedList<URL> {
@@ -722,4 +755,25 @@ public void setUrlNames(Set<URI> urlNames) throws MalformedURLException {
722755
}
723756
}
724757

758+
/**
759+
* Pseudo-implementation of EasyMock's {@code MocksControl} class.
760+
*/
761+
public static class MocksControl {
762+
763+
@SuppressWarnings("unchecked")
764+
public <T> T createMock(Class<T> toMock) {
765+
766+
return (T) Proxy.newProxyInstance(
767+
BeanFactoryGenericsTests.class.getClassLoader(),
768+
new Class[] { toMock }, new InvocationHandler() {
769+
770+
@Override
771+
public Object invoke(Object proxy, Method method, Object[] args)
772+
throws Throwable {
773+
throw new UnsupportedOperationException("mocked!");
774+
}
775+
});
776+
}
777+
}
778+
725779
}

0 commit comments

Comments
 (0)