View Javadoc
1   /*
2    * Copyright 2002-2014 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 org.junit.Before;
20  import org.junit.Test;
21  
22  import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
23  import org.springframework.core.io.ClassPathResource;
24  import org.springframework.tests.sample.beans.TestBean;
25  
26  import static org.junit.Assert.*;
27  
28  /**
29   * @author Karl Pietrzak
30   * @author Juergen Hoeller
31   */
32  public class LookupMethodTests {
33  
34  	private DefaultListableBeanFactory beanFactory;
35  
36  
37  	@Before
38  	public void setUp() {
39  		beanFactory = new DefaultListableBeanFactory();
40  		XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);
41  		reader.loadBeanDefinitions(new ClassPathResource("lookupMethodTests.xml", getClass()));
42  	}
43  
44  
45  	@Test
46  	public void testWithoutConstructorArg() {
47  		AbstractBean bean = (AbstractBean) beanFactory.getBean("abstractBean");
48  		assertNotNull(bean);
49  		Object expected = bean.get();
50  		assertEquals(TestBean.class, expected.getClass());
51  	}
52  
53  	@Test
54  	public void testWithOverloadedArg() {
55  		AbstractBean bean = (AbstractBean) beanFactory.getBean("abstractBean");
56  		assertNotNull(bean);
57  		TestBean expected = bean.get("haha");
58  		assertEquals(TestBean.class, expected.getClass());
59  		assertEquals("haha", expected.getName());
60  	}
61  
62  	@Test
63  	public void testWithOneConstructorArg() {
64  		AbstractBean bean = (AbstractBean) beanFactory.getBean("abstractBean");
65  		assertNotNull(bean);
66  		TestBean expected = bean.getOneArgument("haha");
67  		assertEquals(TestBean.class, expected.getClass());
68  		assertEquals("haha", expected.getName());
69  	}
70  	
71  	@Test
72  	public void testWithTwoConstructorArg() {
73  		AbstractBean bean = (AbstractBean) beanFactory.getBean("abstractBean");
74  		assertNotNull(bean);
75  		TestBean expected = bean.getTwoArguments("haha", 72);
76  		assertEquals(TestBean.class, expected.getClass());
77  		assertEquals("haha", expected.getName());
78  		assertEquals(72, expected.getAge());
79  	}
80  	
81  	@Test
82  	public void testWithThreeArgsShouldFail() {
83  		AbstractBean bean = (AbstractBean) beanFactory.getBean("abstractBean");
84  		assertNotNull(bean);
85  		try {
86  			bean.getThreeArguments("name", 1, 2);
87  			fail("TestBean does not have a three arg constructor so this should not have worked");
88  		}
89  		catch (AbstractMethodError ex) {
90  		}
91  	}
92  
93  
94  	public static abstract class AbstractBean {
95  
96  		public abstract TestBean get();
97  
98  		public abstract TestBean get(String name);  // overloaded
99  
100 		public abstract TestBean getOneArgument(String name);
101 
102 		public abstract TestBean getTwoArguments(String name, int age);
103 
104 		public abstract TestBean getThreeArguments(String name, int age, int anotherArg);
105 	}
106 
107 }