Skip to content

Commit 3e2491c

Browse files
committed
Polishing
1 parent e51da28 commit 3e2491c

File tree

10 files changed

+118
-157
lines changed

10 files changed

+118
-157
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/BeanFactoryUtils.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,7 +16,6 @@
1616

1717
package org.springframework.beans.factory;
1818

19-
2019
import java.util.ArrayList;
2120
import java.util.Arrays;
2221
import java.util.LinkedHashMap;

spring-beans/src/test/java/org/springframework/beans/factory/BeanFactoryUtilsTests.java

Lines changed: 32 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,18 +16,13 @@
1616

1717
package org.springframework.beans.factory;
1818

19-
import static org.junit.Assert.assertEquals;
20-
import static org.junit.Assert.assertTrue;
21-
import static org.springframework.tests.TestResourceUtils.qualifiedResource;
22-
2319
import java.util.Arrays;
2420
import java.util.List;
2521
import java.util.Map;
2622

2723
import org.junit.Before;
2824
import org.junit.Test;
29-
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
30-
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
25+
3126
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
3227
import org.springframework.beans.factory.support.StaticListableBeanFactory;
3328
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
@@ -39,6 +34,8 @@
3934
import org.springframework.tests.sample.beans.factory.DummyFactory;
4035
import org.springframework.util.ObjectUtils;
4136

37+
import static org.junit.Assert.*;
38+
import static org.springframework.tests.TestResourceUtils.*;
4239

4340
/**
4441
* @author Rod Johnson
@@ -54,29 +51,30 @@ public final class BeanFactoryUtilsTests {
5451
private static final Resource LEAF_CONTEXT = qualifiedResource(CLASS, "leaf.xml");
5552
private static final Resource DEPENDENT_BEANS_CONTEXT = qualifiedResource(CLASS, "dependentBeans.xml");
5653

57-
private ConfigurableListableBeanFactory listableBeanFactory;
54+
private DefaultListableBeanFactory listableBeanFactory;
55+
56+
private DefaultListableBeanFactory dependentBeansFactory;
5857

59-
private ConfigurableListableBeanFactory dependentBeansBF;
6058

6159
@Before
6260
public void setUp() {
6361
// Interesting hierarchical factory to test counts.
6462
// Slow to read so we cache it.
6563

66-
6764
DefaultListableBeanFactory grandParent = new DefaultListableBeanFactory();
6865
new XmlBeanDefinitionReader(grandParent).loadBeanDefinitions(ROOT_CONTEXT);
6966
DefaultListableBeanFactory parent = new DefaultListableBeanFactory(grandParent);
7067
new XmlBeanDefinitionReader(parent).loadBeanDefinitions(MIDDLE_CONTEXT);
7168
DefaultListableBeanFactory child = new DefaultListableBeanFactory(parent);
7269
new XmlBeanDefinitionReader(child).loadBeanDefinitions(LEAF_CONTEXT);
7370

74-
this.dependentBeansBF = new DefaultListableBeanFactory();
75-
new XmlBeanDefinitionReader((BeanDefinitionRegistry) this.dependentBeansBF).loadBeanDefinitions(DEPENDENT_BEANS_CONTEXT);
76-
dependentBeansBF.preInstantiateSingletons();
71+
this.dependentBeansFactory = new DefaultListableBeanFactory();
72+
new XmlBeanDefinitionReader(this.dependentBeansFactory).loadBeanDefinitions(DEPENDENT_BEANS_CONTEXT);
73+
dependentBeansFactory.preInstantiateSingletons();
7774
this.listableBeanFactory = child;
7875
}
7976

77+
8078
@Test
8179
public void testHierarchicalCountBeansWithNonHierarchicalFactory() {
8280
StaticListableBeanFactory lbf = new StaticListableBeanFactory();
@@ -93,22 +91,21 @@ public void testHierarchicalCountBeansWithOverride() throws Exception {
9391
// Leaf count
9492
assertTrue(this.listableBeanFactory.getBeanDefinitionCount() == 1);
9593
// Count minus duplicate
96-
assertTrue("Should count 7 beans, not "
97-
+ BeanFactoryUtils.countBeansIncludingAncestors(this.listableBeanFactory),
98-
BeanFactoryUtils.countBeansIncludingAncestors(this.listableBeanFactory) == 7);
94+
assertTrue("Should count 7 beans, not " + BeanFactoryUtils.countBeansIncludingAncestors(this.listableBeanFactory),
95+
BeanFactoryUtils.countBeansIncludingAncestors(this.listableBeanFactory) == 7);
9996
}
10097

10198
@Test
10299
public void testHierarchicalNamesWithNoMatch() throws Exception {
103-
List<String> names = Arrays.asList(BeanFactoryUtils.beanNamesForTypeIncludingAncestors(this.listableBeanFactory,
104-
NoOp.class));
100+
List<String> names = Arrays.asList(
101+
BeanFactoryUtils.beanNamesForTypeIncludingAncestors(this.listableBeanFactory, NoOp.class));
105102
assertEquals(0, names.size());
106103
}
107104

108105
@Test
109106
public void testHierarchicalNamesWithMatchOnlyInRoot() throws Exception {
110-
List<String> names = Arrays.asList(BeanFactoryUtils.beanNamesForTypeIncludingAncestors(this.listableBeanFactory,
111-
IndexedTestBean.class));
107+
List<String> names = Arrays.asList(
108+
BeanFactoryUtils.beanNamesForTypeIncludingAncestors(this.listableBeanFactory, IndexedTestBean.class));
112109
assertEquals(1, names.size());
113110
assertTrue(names.contains("indexedBean"));
114111
// Distinguish from default ListableBeanFactory behavior
@@ -117,8 +114,8 @@ public void testHierarchicalNamesWithMatchOnlyInRoot() throws Exception {
117114

118115
@Test
119116
public void testGetBeanNamesForTypeWithOverride() throws Exception {
120-
List<String> names = Arrays.asList(BeanFactoryUtils.beanNamesForTypeIncludingAncestors(this.listableBeanFactory,
121-
ITestBean.class));
117+
List<String> names = Arrays.asList(
118+
BeanFactoryUtils.beanNamesForTypeIncludingAncestors(this.listableBeanFactory, ITestBean.class));
122119
// includes 2 TestBeans from FactoryBeans (DummyFactory definitions)
123120
assertEquals(4, names.size());
124121
assertTrue(names.contains("test"));
@@ -131,7 +128,7 @@ public void testGetBeanNamesForTypeWithOverride() throws Exception {
131128
public void testNoBeansOfType() {
132129
StaticListableBeanFactory lbf = new StaticListableBeanFactory();
133130
lbf.addBean("foo", new Object());
134-
Map<?, ?> beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf, ITestBean.class, true, false);
131+
Map<String, ?> beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf, ITestBean.class, true, false);
135132
assertTrue(beans.isEmpty());
136133
}
137134

@@ -148,7 +145,7 @@ public void testFindsBeansOfTypeWithStaticFactory() {
148145
lbf.addBean("t3", t3);
149146
lbf.addBean("t4", t4);
150147

151-
Map<?, ?> beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf, ITestBean.class, true, false);
148+
Map<String, ?> beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf, ITestBean.class, true, false);
152149
assertEquals(2, beans.size());
153150
assertEquals(t1, beans.get("t1"));
154151
assertEquals(t2, beans.get("t2"));
@@ -192,21 +189,18 @@ public void testFindsBeansOfTypeWithDefaultFactory() {
192189
this.listableBeanFactory.registerSingleton("t3", t3);
193190
this.listableBeanFactory.registerSingleton("t4", t4);
194191

195-
Map<?, ?> beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, ITestBean.class, true,
196-
false);
192+
Map<String, ?> beans =
193+
BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, ITestBean.class, true, false);
197194
assertEquals(6, beans.size());
198195
assertEquals(test3, beans.get("test3"));
199196
assertEquals(test, beans.get("test"));
200197
assertEquals(t1, beans.get("t1"));
201198
assertEquals(t2, beans.get("t2"));
202199
assertEquals(t3.getObject(), beans.get("t3"));
203200
assertTrue(beans.get("t4") instanceof TestBean);
204-
// t3 and t4 are found here as of Spring 2.0, since they are
205-
// pre-registered
206-
// singleton instances, while testFactory1 and testFactory are *not*
207-
// found
208-
// because they are FactoryBean definitions that haven't been
209-
// initialized yet.
201+
// t3 and t4 are found here as of Spring 2.0, since they are pre-registered
202+
// singleton instances, while testFactory1 and testFactory are *not* found
203+
// because they are FactoryBean definitions that haven't been initialized yet.
210204

211205
beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, ITestBean.class, false, true);
212206
Object testFactory1 = this.listableBeanFactory.getBean("testFactory1");
@@ -248,8 +242,8 @@ public void testHierarchicalResolutionWithOverride() throws Exception {
248242
Object test3 = this.listableBeanFactory.getBean("test3");
249243
Object test = this.listableBeanFactory.getBean("test");
250244

251-
Map<?, ?> beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, ITestBean.class, true,
252-
false);
245+
Map<String, ?> beans =
246+
BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, ITestBean.class, true, false);
253247
assertEquals(2, beans.size());
254248
assertEquals(test3, beans.get("test3"));
255249
assertEquals(test, beans.get("test"));
@@ -284,25 +278,25 @@ public void testHierarchicalResolutionWithOverride() throws Exception {
284278

285279
@Test
286280
public void testADependencies() {
287-
String[] deps = this.dependentBeansBF.getDependentBeans("a");
281+
String[] deps = this.dependentBeansFactory.getDependentBeans("a");
288282
assertTrue(ObjectUtils.isEmpty(deps));
289283
}
290284

291285
@Test
292286
public void testBDependencies() {
293-
String[] deps = this.dependentBeansBF.getDependentBeans("b");
287+
String[] deps = this.dependentBeansFactory.getDependentBeans("b");
294288
assertTrue(Arrays.equals(new String[] { "c" }, deps));
295289
}
296290

297291
@Test
298292
public void testCDependencies() {
299-
String[] deps = this.dependentBeansBF.getDependentBeans("c");
293+
String[] deps = this.dependentBeansFactory.getDependentBeans("c");
300294
assertTrue(Arrays.equals(new String[] { "int", "long" }, deps));
301295
}
302296

303297
@Test
304298
public void testIntDependencies() {
305-
String[] deps = this.dependentBeansBF.getDependentBeans("int");
299+
String[] deps = this.dependentBeansFactory.getDependentBeans("int");
306300
assertTrue(Arrays.equals(new String[] { "buffer" }, deps));
307301
}
308302

spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,7 +32,6 @@
3232
import java.util.Map;
3333
import java.util.Properties;
3434
import java.util.Set;
35-
3635
import javax.security.auth.Subject;
3736

3837
import org.apache.commons.logging.Log;
@@ -41,6 +40,7 @@
4140
import org.junit.Rule;
4241
import org.junit.Test;
4342
import org.junit.rules.ExpectedException;
43+
4444
import org.springframework.beans.BeansException;
4545
import org.springframework.beans.MutablePropertyValues;
4646
import org.springframework.beans.NotWritablePropertyException;
@@ -190,7 +190,7 @@ public void testPrototypeFactoryBeanIgnoredByNonEagerTypeMatching() {
190190
}
191191

192192
@Test
193-
public void testPrototypeSingletonFactoryBeanIgnoredByNonEagerTypeMatching() {
193+
public void testSingletonFactoryBeanIgnoredByNonEagerTypeMatching() {
194194
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
195195
Properties p = new Properties();
196196
p.setProperty("x1.(class)", DummyFactory.class.getName());
@@ -1212,8 +1212,8 @@ public void testAutowireWithTwoMatchesForConstructorDependency() {
12121212
}
12131213
catch (UnsatisfiedDependencyException ex) {
12141214
// expected
1215-
assertTrue(ex.getMessage().indexOf("rod") != -1);
1216-
assertTrue(ex.getMessage().indexOf("rod2") != -1);
1215+
assertTrue(ex.getMessage().contains("rod"));
1216+
assertTrue(ex.getMessage().contains("rod2"));
12171217
}
12181218
}
12191219

@@ -1283,13 +1283,13 @@ public void testAutowireBeanByNameWithNoDependencyCheck() {
12831283
assertNull(bean.getSpouse());
12841284
}
12851285

1286-
@Test(expected=NoSuchBeanDefinitionException.class)
1286+
@Test(expected = NoSuchBeanDefinitionException.class)
12871287
public void testGetBeanByTypeWithNoneFound() {
12881288
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
12891289
lbf.getBean(TestBean.class);
12901290
}
12911291

1292-
@Test(expected=NoUniqueBeanDefinitionException.class)
1292+
@Test(expected = NoUniqueBeanDefinitionException.class)
12931293
public void testGetBeanByTypeWithAmbiguity() {
12941294
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
12951295
RootBeanDefinition bd1 = new RootBeanDefinition(TestBean.class);
@@ -1393,7 +1393,7 @@ public void testGetTypeForAbstractFactoryBean() {
13931393
* Java method names. In other words, you can't name a method
13941394
* {@code set&amp;FactoryBean(...)}.
13951395
*/
1396-
@Test(expected=TypeMismatchException.class)
1396+
@Test(expected = TypeMismatchException.class)
13971397
public void testAutowireBeanWithFactoryBeanByName() {
13981398
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
13991399
RootBeanDefinition bd = new RootBeanDefinition(LazyInitFactory.class);
@@ -1416,8 +1416,8 @@ public void testAutowireBeanByTypeWithTwoMatches() {
14161416
}
14171417
catch (UnsatisfiedDependencyException ex) {
14181418
// expected
1419-
assertTrue(ex.getMessage().indexOf("test") != -1);
1420-
assertTrue(ex.getMessage().indexOf("spouse") != -1);
1419+
assertTrue(ex.getMessage().contains("test"));
1420+
assertTrue(ex.getMessage().contains("spouse"));
14211421
}
14221422
}
14231423

@@ -1434,8 +1434,8 @@ public void testAutowireBeanByTypeWithTwoMatchesAndParameterNameDiscovery() {
14341434
}
14351435
catch (UnsatisfiedDependencyException ex) {
14361436
// expected
1437-
assertTrue(ex.getMessage().indexOf("test") != -1);
1438-
assertTrue(ex.getMessage().indexOf("spouse") != -1);
1437+
assertTrue(ex.getMessage().contains("test"));
1438+
assertTrue(ex.getMessage().contains("spouse"));
14391439
}
14401440
}
14411441

@@ -1683,7 +1683,7 @@ public void testBeanDefinitionWithInterface() {
16831683
}
16841684
catch (BeanCreationException ex) {
16851685
assertEquals("test", ex.getBeanName());
1686-
assertTrue(ex.getMessage().toLowerCase().indexOf("interface") != -1);
1686+
assertTrue(ex.getMessage().toLowerCase().contains("interface"));
16871687
}
16881688
}
16891689

@@ -1697,7 +1697,7 @@ public void testBeanDefinitionWithAbstractClass() {
16971697
}
16981698
catch (BeanCreationException ex) {
16991699
assertEquals("test", ex.getBeanName());
1700-
assertTrue(ex.getMessage().toLowerCase().indexOf("abstract") != -1);
1700+
assertTrue(ex.getMessage().toLowerCase().contains("abstract"));
17011701
}
17021702
}
17031703

@@ -2144,13 +2144,13 @@ private void findTypeOfPrototypeFactoryMethodOnBeanInstance(boolean singleton) {
21442144
assertEquals(expectedNameFromArgs, tb2.getName());
21452145
}
21462146

2147-
@Test(expected=IllegalStateException.class)
2147+
@Test(expected = IllegalStateException.class)
21482148
public void testScopingBeanToUnregisteredScopeResultsInAnException() throws Exception {
21492149
BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(TestBean.class);
21502150
AbstractBeanDefinition beanDefinition = builder.getBeanDefinition();
21512151
beanDefinition.setScope("he put himself so low could hardly look me in the face");
21522152

2153-
final DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
2153+
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
21542154
factory.registerBeanDefinition("testBean", beanDefinition);
21552155
factory.getBean("testBean");
21562156
}
@@ -2162,8 +2162,7 @@ public void testExplicitScopeInheritanceForChildBeanDefinitions() throws Excepti
21622162
RootBeanDefinition parent = new RootBeanDefinition();
21632163
parent.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
21642164

2165-
AbstractBeanDefinition child = BeanDefinitionBuilder
2166-
.childBeanDefinition("parent").getBeanDefinition();
2165+
AbstractBeanDefinition child = BeanDefinitionBuilder.childBeanDefinition("parent").getBeanDefinition();
21672166
child.setBeanClass(TestBean.class);
21682167
child.setScope(theChildScope);
21692168

0 commit comments

Comments
 (0)