|
17 | 17 | package org.springframework.beans.factory;
|
18 | 18 |
|
19 | 19 | import static org.hamcrest.CoreMatchers.is;
|
| 20 | +import static org.hamcrest.Matchers.containsString; |
| 21 | +import static org.hamcrest.Matchers.equalTo; |
20 | 22 | import static org.junit.Assert.assertEquals;
|
21 | 23 | import static org.junit.Assert.assertFalse;
|
22 | 24 | import static org.junit.Assert.assertNotNull;
|
|
49 | 51 | import org.apache.commons.logging.Log;
|
50 | 52 | import org.apache.commons.logging.LogFactory;
|
51 | 53 | import org.junit.Ignore;
|
| 54 | +import org.junit.Rule; |
52 | 55 | import org.junit.Test;
|
| 56 | +import org.junit.rules.ExpectedException; |
53 | 57 | import org.springframework.beans.BeansException;
|
54 | 58 | import org.springframework.beans.MutablePropertyValues;
|
55 | 59 | import org.springframework.beans.NotWritablePropertyException;
|
|
102 | 106 | * @author Rick Evans
|
103 | 107 | * @author Sam Brannen
|
104 | 108 | * @author Chris Beams
|
| 109 | + * @author Phillip Webb |
105 | 110 | */
|
106 | 111 | public class DefaultListableBeanFactoryTests {
|
107 | 112 |
|
108 | 113 | private static final Log factoryLog = LogFactory.getLog(DefaultListableBeanFactory.class);
|
109 | 114 |
|
| 115 | + @Rule |
| 116 | + public ExpectedException thrown = ExpectedException.none(); |
110 | 117 |
|
111 | 118 | @Test
|
112 | 119 | public void testUnreferencedSingletonWasInstantiated() {
|
@@ -1285,6 +1292,32 @@ public void testGetBeanByTypeWithAmbiguity() {
|
1285 | 1292 | lbf.getBean(TestBean.class);
|
1286 | 1293 | }
|
1287 | 1294 |
|
| 1295 | + @Test |
| 1296 | + public void testGetBeanByTypeWithPrimary() throws Exception { |
| 1297 | + DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); |
| 1298 | + RootBeanDefinition bd1 = new RootBeanDefinition(TestBean.class); |
| 1299 | + RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class); |
| 1300 | + bd2.setPrimary(true); |
| 1301 | + lbf.registerBeanDefinition("bd1", bd1); |
| 1302 | + lbf.registerBeanDefinition("bd2", bd2); |
| 1303 | + TestBean bean = lbf.getBean(TestBean.class); |
| 1304 | + assertThat(bean.getBeanName(), equalTo("bd2")); |
| 1305 | + } |
| 1306 | + |
| 1307 | + @Test |
| 1308 | + public void testGetBeanByTypeWithMultiplePrimary() throws Exception { |
| 1309 | + DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); |
| 1310 | + RootBeanDefinition bd1 = new RootBeanDefinition(TestBean.class); |
| 1311 | + bd1.setPrimary(true); |
| 1312 | + RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class); |
| 1313 | + bd2.setPrimary(true); |
| 1314 | + lbf.registerBeanDefinition("bd1", bd1); |
| 1315 | + lbf.registerBeanDefinition("bd2", bd2); |
| 1316 | + thrown.expect(NoUniqueBeanDefinitionException.class); |
| 1317 | + thrown.expectMessage(containsString("more than one 'primary'")); |
| 1318 | + lbf.getBean(TestBean.class); |
| 1319 | + } |
| 1320 | + |
1288 | 1321 | @Test
|
1289 | 1322 | public void testGetBeanByTypeFiltersOutNonAutowireCandidates() {
|
1290 | 1323 | DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
|
|
0 commit comments