📄 xmlbeanfactorytests.java
字号:
}
}
public void testUnsatisfiedSimpleDependencyCheck() throws Exception {
try {
XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("unsatisfiedSimpleDependencyCheck.xml", getClass()));
xbf.getBean("a", DependenciesBean.class);
fail("Must have thrown an UnsatisfiedDependencyException");
}
catch (UnsatisfiedDependencyException expected) {
}
}
public void testSatisfiedObjectDependencyCheck() throws Exception {
XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("satisfiedObjectDependencyCheck.xml", getClass()));
DependenciesBean a = (DependenciesBean) xbf.getBean("a");
assertNotNull(a.getSpouse());
assertEquals(xbf, a.getBeanFactory());
}
public void testSatisfiedSimpleDependencyCheck() throws Exception {
XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("satisfiedSimpleDependencyCheck.xml", getClass()));
DependenciesBean a = (DependenciesBean) xbf.getBean("a");
assertEquals(a.getAge(), 33);
}
public void testUnsatisfiedAllDependencyCheck() throws Exception {
try {
XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("unsatisfiedAllDependencyCheckMissingObjects.xml", getClass()));
xbf.getBean("a", DependenciesBean.class);
fail("Must have thrown an UnsatisfiedDependencyException");
}
catch (UnsatisfiedDependencyException expected) {
}
}
public void testSatisfiedAllDependencyCheck() throws Exception {
XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("satisfiedAllDependencyCheck.xml", getClass()));
DependenciesBean a = (DependenciesBean) xbf.getBean("a");
assertEquals(a.getAge(), 33);
assertNotNull(a.getName());
assertNotNull(a.getSpouse());
}
public void testAutowire() throws Exception {
XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("autowire.xml", getClass()));
TestBean spouse = new TestBean("kerry", 0);
xbf.registerSingleton("spouse", spouse);
doTestAutowire(xbf);
}
public void testAutowireWithParent() throws Exception {
XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("autowire.xml", getClass()));
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue("name", "kerry");
lbf.registerBeanDefinition("spouse", new RootBeanDefinition(TestBean.class, pvs));
xbf.setParentBeanFactory(lbf);
doTestAutowire(xbf);
}
private void doTestAutowire(XmlBeanFactory xbf) throws Exception {
DependenciesBean rod1 = (DependenciesBean) xbf.getBean("rod1");
TestBean kerry = (TestBean) xbf.getBean("spouse");
// should have been autowired
assertEquals(kerry, rod1.getSpouse());
DependenciesBean rod1a = (DependenciesBean) xbf.getBean("rod1a");
// should have been autowired
assertEquals(kerry, rod1a.getSpouse());
DependenciesBean rod2 = (DependenciesBean) xbf.getBean("rod2");
// should have been autowired
assertEquals(kerry, rod2.getSpouse());
ConstructorDependenciesBean rod3 = (ConstructorDependenciesBean) xbf.getBean("rod3");
IndexedTestBean other = (IndexedTestBean) xbf.getBean("other");
// should have been autowired
assertEquals(kerry, rod3.getSpouse1());
assertEquals(kerry, rod3.getSpouse2());
assertEquals(other, rod3.getOther());
ConstructorDependenciesBean rod3a = (ConstructorDependenciesBean) xbf.getBean("rod3a");
// should have been autowired
assertEquals(kerry, rod3a.getSpouse1());
assertEquals(kerry, rod3a.getSpouse2());
assertEquals(other, rod3a.getOther());
try {
xbf.getBean("rod4", ConstructorDependenciesBean.class);
fail("Must have thrown a FatalBeanException");
}
catch (FatalBeanException expected) {
}
DependenciesBean rod5 = (DependenciesBean) xbf.getBean("rod5");
// Should not have been autowired
assertNull(rod5.getSpouse());
BeanFactory appCtx = (BeanFactory) xbf.getBean("childAppCtx");
assertTrue(appCtx.containsBean("rod1"));
assertTrue(appCtx.containsBean("jenny"));
}
public void testAutowireWithDefault() throws Exception {
XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("default-autowire.xml", getClass()));
DependenciesBean rod1 = (DependenciesBean) xbf.getBean("rod1");
// should have been autowired
assertNotNull(rod1.getSpouse());
assertTrue(rod1.getSpouse().getName().equals("Kerry"));
DependenciesBean rod2 = (DependenciesBean) xbf.getBean("rod2");
// should have been autowired
assertNotNull(rod2.getSpouse());
assertTrue(rod2.getSpouse().getName().equals("Kerry"));
try {
xbf.getBean("rod3", DependenciesBean.class);
fail("Must have thrown UnsatisfiedDependencyException");
}
catch (UnsatisfiedDependencyException expected) {
}
}
public void testAutowireByConstructor() throws Exception {
XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("constructor-arg.xml", getClass()));
ConstructorDependenciesBean rod1 = (ConstructorDependenciesBean) xbf.getBean("rod1");
TestBean kerry = (TestBean) xbf.getBean("kerry2");
// should have been autowired
assertEquals(kerry, rod1.getSpouse1());
assertEquals(0, rod1.getAge());
assertEquals(null, rod1.getName());
ConstructorDependenciesBean rod2 = (ConstructorDependenciesBean) xbf.getBean("rod2");
TestBean kerry1 = (TestBean) xbf.getBean("kerry1");
TestBean kerry2 = (TestBean) xbf.getBean("kerry2");
// should have been autowired
assertEquals(kerry2, rod2.getSpouse1());
assertEquals(kerry1, rod2.getSpouse2());
assertEquals(0, rod2.getAge());
assertEquals(null, rod2.getName());
ConstructorDependenciesBean rod = (ConstructorDependenciesBean) xbf.getBean("rod3");
IndexedTestBean other = (IndexedTestBean) xbf.getBean("other");
// should have been autowired
assertEquals(kerry, rod.getSpouse1());
assertEquals(kerry, rod.getSpouse2());
assertEquals(other, rod.getOther());
assertEquals(0, rod.getAge());
assertEquals(null, rod.getName());
xbf.getBean("rod4", ConstructorDependenciesBean.class);
// should have been autowired
assertEquals(kerry, rod.getSpouse1());
assertEquals(kerry, rod.getSpouse2());
assertEquals(other, rod.getOther());
assertEquals(0, rod.getAge());
assertEquals(null, rod.getName());
}
public void testAutowireByConstructorWithSimpleValues() throws Exception {
XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("constructor-arg.xml", getClass()));
ConstructorDependenciesBean rod5 = (ConstructorDependenciesBean) xbf.getBean("rod5");
TestBean kerry1 = (TestBean) xbf.getBean("kerry1");
TestBean kerry2 = (TestBean) xbf.getBean("kerry2");
IndexedTestBean other = (IndexedTestBean) xbf.getBean("other");
// should have been autowired
assertEquals(kerry2, rod5.getSpouse1());
assertEquals(kerry1, rod5.getSpouse2());
assertEquals(other, rod5.getOther());
assertEquals(99, rod5.getAge());
assertEquals("myname", rod5.getName());
DerivedConstructorDependenciesBean rod6 = (DerivedConstructorDependenciesBean) xbf.getBean("rod6");
// should have been autowired
assertTrue(rod6.initialized);
assertTrue(!rod6.destroyed);
assertEquals(kerry2, rod6.getSpouse1());
assertEquals(kerry1, rod6.getSpouse2());
assertEquals(other, rod6.getOther());
assertEquals(0, rod6.getAge());
assertEquals(null, rod6.getName());
xbf.destroySingletons();
assertTrue(rod6.destroyed);
}
public void testConstructorArgResolution() {
XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("constructor-arg.xml", getClass()));
TestBean kerry1 = (TestBean) xbf.getBean("kerry1");
TestBean kerry2 = (TestBean) xbf.getBean("kerry2");
//ConstructorDependenciesBean rod9 = (ConstructorDependenciesBean) xbf.getBean("rod9");
//assertEquals(99, rod9.getAge());
ConstructorDependenciesBean rod10 = (ConstructorDependenciesBean) xbf.getBean("rod10");
assertEquals(null, rod10.getName());
ConstructorDependenciesBean rod11 = (ConstructorDependenciesBean) xbf.getBean("rod11");
assertEquals(kerry2, rod11.getSpouse1());
ConstructorDependenciesBean rod12 = (ConstructorDependenciesBean) xbf.getBean("rod12");
assertEquals(kerry1, rod12.getSpouse1());
assertNull(rod12.getSpouse2());
ConstructorDependenciesBean rod13 = (ConstructorDependenciesBean) xbf.getBean("rod13");
assertEquals(kerry1, rod13.getSpouse1());
assertEquals(kerry2, rod13.getSpouse2());
ConstructorDependenciesBean rod14 = (ConstructorDependenciesBean) xbf.getBean("rod14");
assertEquals(kerry1, rod14.getSpouse1());
assertEquals(kerry2, rod14.getSpouse2());
ConstructorDependenciesBean rod15 = (ConstructorDependenciesBean) xbf.getBean("rod15");
assertEquals(kerry2, rod15.getSpouse1());
assertEquals(kerry1, rod15.getSpouse2());
ConstructorDependenciesBean rod16 = (ConstructorDependenciesBean) xbf.getBean("rod16");
assertEquals(kerry2, rod16.getSpouse1());
assertEquals(kerry1, rod16.getSpouse2());
assertEquals(29, rod16.getAge());
}
public void testConstructorArgWithSingleMatch() {
XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("constructor-arg.xml", getClass()));
File file = (File) xbf.getBean("file");
assertEquals(File.separator + "test", file.getPath());
}
public void testThrowsExceptionOnTooManyArguments() throws Exception {
XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("constructor-arg.xml", getClass()));
try {
xbf.getBean("rod7", ConstructorDependenciesBean.class);
fail("Should have thrown BeanCreationException");
}
catch (BeanCreationException expected) {
}
}
public void testThrowsExceptionOnAmbiguousResolution() throws Exception {
XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("constructor-arg.xml", getClass()));
try {
xbf.getBean("rod8", ConstructorDependenciesBean.class);
fail("Must have thrown UnsatisfiedDependencyException");
}
catch (UnsatisfiedDependencyException expected) {
}
}
public void testDependsOn() {
doTestDependencies("dependencies-dependsOn.xml", 1);
}
public void testDependsOnInInnerBean() {
doTestDependencies("dependencies-dependsOn-inner.xml", 4);
}
public void testDependenciesThroughConstructorArguments() {
doTestDependencies("dependencies-carg.xml", 1);
}
public void testDependenciesThroughConstructorArgumentAutowiring() {
doTestDependencies("dependencies-carg-autowire.xml", 1);
}
public void testDependenciesThroughConstructorArgumentsInInnerBean() {
doTestDependencies("dependencies-carg-inner.xml", 1);
}
public void testDependenciesThroughProperties() {
doTestDependencies("dependencies-prop.xml", 1);
}
public void testDependenciesThroughPropertiesWithInTheMiddle() {
doTestDependencies("dependencies-prop-inTheMiddle.xml", 1);
}
public void testDependenciesThroughPropertyAutowiringByName() {
doTestDependencies("dependencies-prop-autowireByName.xml", 1);
}
public void testDependenciesThroughPropertyAutowiringByType() {
doTestDependencies("dependencies-prop-autowireByType.xml", 1);
}
public void testDependenciesThroughPropertiesInInnerBean() {
doTestDependencies("dependencies-prop-inner.xml", 1);
}
private void doTestDependencies(String filename, int nrOfHoldingBeans) {
PreparingBean1.prepared = false;
PreparingBean1.destroyed = false;
PreparingBean2.prepared = false;
PreparingBean2.destroyed = false;
DependingBean.destroyCount = 0;
HoldingBean.destroyCount = 0;
XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource(filename, getClass()));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -