📄 xmlbeanfactorytestsuite.java
字号:
HasMap hasMap = (HasMap) xbf.getBean("integerArray");
assertTrue(hasMap.getIntegerArray().length == 3);
assertTrue(hasMap.getIntegerArray()[0].intValue() == 0);
assertTrue(hasMap.getIntegerArray()[1].intValue() == 1);
assertTrue(hasMap.getIntegerArray()[2].intValue() == 2);
}
*/
public void testInitMethodIsInvoked() throws Exception {
InputStream is = getClass().getResourceAsStream("initializers.xml");
XmlBeanFactory xbf = new XmlBeanFactory(is);
DoubleInitializer in = (DoubleInitializer) xbf.getBean("init-method1");
// Initializer should have doubled value
assertEquals(14, in.getNum());
}
/**
* Test that if a custom initializer throws an exception, it's handled correctly
*/
public void testInitMethodThrowsException() {
InputStream is = getClass().getResourceAsStream("initializers.xml");
XmlBeanFactory xbf = new XmlBeanFactory(is);
try {
xbf.getBean("init-method2");
fail();
}
catch (BeanCreationException ex) {
assertTrue(ex.getCause() instanceof ServletException);
}
}
public void testNoSuchInitMethod() throws Exception {
XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("initializers.xml", getClass()));
try {
xbf.getBean("init-method3");
fail();
}
catch (FatalBeanException ex) {
// check message is helpful
assertTrue(ex.getMessage().indexOf("initializers.xml") != -1);
assertTrue(ex.getMessage().indexOf("init-method3") != -1);
assertTrue(ex.getMessage().indexOf("init") != -1);
}
}
/**
* Check that InitializingBean method is called first.
*/
public void testInitializingBeanAndInitMethod() throws Exception {
InitAndIB.constructed = false;
InputStream is = getClass().getResourceAsStream("initializers.xml");
XmlBeanFactory xbf = new XmlBeanFactory(is);
assertFalse(InitAndIB.constructed);
xbf.preInstantiateSingletons();
assertFalse(InitAndIB.constructed);
InitAndIB iib = (InitAndIB) xbf.getBean("init-and-ib");
assertTrue(InitAndIB.constructed);
assertTrue(iib.afterPropertiesSetInvoked && iib.initMethodInvoked);
assertTrue(!iib.destroyed && !iib.customDestroyed);
xbf.destroySingletons();
assertTrue(iib.destroyed && iib.customDestroyed);
xbf.destroySingletons();
assertTrue(iib.destroyed && iib.customDestroyed);
}
/**
* Check that InitializingBean method is called first.
*/
public void testDefaultLazyInit() throws Exception {
InitAndIB.constructed = false;
InputStream is = getClass().getResourceAsStream("default-lazy-init.xml");
XmlBeanFactory xbf = new XmlBeanFactory(is);
assertFalse(InitAndIB.constructed);
xbf.preInstantiateSingletons();
assertTrue(InitAndIB.constructed);
try {
xbf.getBean("lazy-and-bad");
}
catch (BeanCreationException ex) {
assertTrue(ex.getCause() instanceof ServletException);
}
}
public void testNoSuchXmlFile() throws Exception {
try {
XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("missing.xml", getClass()));
fail("Shouldn't create factory from missing XML");
}
catch (BeanDefinitionStoreException ex) {
// Ok
// TODO Check that the error message includes filename
}
}
public void testInvalidXmlFile() throws Exception {
try {
XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("invalid.xml", getClass()));
fail("Shouldn't create factory from invalid XML");
}
catch (BeanDefinitionStoreException ex) {
// Ok
// TODO Check that the error message includes filename
}
}
public void testUnsatisfiedObjectDependencyCheck() throws Exception {
try {
XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("unsatisfiedObjectDependencyCheck.xml", getClass()));
DependenciesBean a = (DependenciesBean) xbf.getBean("a");
fail();
}
catch (UnsatisfiedDependencyException ex) {
// Ok
// What if many dependencies are unsatisfied?
//assertTrue(ex.getMessage().indexOf("spouse"))
}
}
public void testUnsatisfiedSimpleDependencyCheck() throws Exception {
try {
XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("unsatisfiedSimpleDependencyCheck.xml", getClass()));
DependenciesBean a = (DependenciesBean) xbf.getBean("a");
fail();
}
catch (UnsatisfiedDependencyException ex) {
// Ok
// What if many dependencies are unsatisfied?
//assertTrue(ex.getMessage().indexOf("spouse"))
}
}
public void testSatisfiedObjectDependencyCheck() throws Exception {
XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("satisfiedObjectDependencyCheck.xml", getClass()));
DependenciesBean a = (DependenciesBean) xbf.getBean("a");
assertNotNull(a.getSpouse());
}
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()));
DependenciesBean a = (DependenciesBean) xbf.getBean("a");
fail();
}
catch (UnsatisfiedDependencyException ex) {
// Ok
// What if many dependencies are unsatisfied?
//assertTrue(ex.getMessage().indexOf("spouse"))
}
}
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 {
ConstructorDependenciesBean rod4 = (ConstructorDependenciesBean) xbf.getBean("rod4");
fail("Should not have thrown FatalBeanException");
}
catch (FatalBeanException ex) {
// 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 {
DependenciesBean rod3 = (DependenciesBean) xbf.getBean("rod3");
fail("Should have thrown UnsatisfiedDependencyException");
}
catch (UnsatisfiedDependencyException ex) {
// 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());
ConstructorDependenciesBean rod4 = (ConstructorDependenciesBean) xbf.getBean("rod4");
// 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();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -