1 9 package org.nanocontainer.script.xml; 10 11 import junit.framework.TestCase; 12 import org.w3c.dom.Document ; 13 import org.xml.sax.InputSource ; 14 import org.xml.sax.SAXException ; 15 16 import javax.xml.parsers.DocumentBuilder ; 17 import javax.xml.parsers.DocumentBuilderFactory ; 18 import javax.xml.parsers.ParserConfigurationException ; 19 import java.io.IOException ; 20 import java.io.StringReader ; 21 22 26 public class BeanComponentInstanceFactoryTestCase extends TestCase { 27 28 public void testDeserialization() throws ParserConfigurationException , IOException , SAXException , ClassNotFoundException { 29 BeanComponentInstanceFactory factory = new BeanComponentInstanceFactory(); 30 31 StringReader sr = new StringReader ("" + 32 "<org.nanocontainer.script.xml.TestBean>" + 33 "<foo>10</foo>" + 34 "<bar>hello</bar>" + 35 "</org.nanocontainer.script.xml.TestBean>"); 36 InputSource is = new InputSource (sr); 37 DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 38 Document doc = db.parse(is); 39 40 Object o = factory.makeInstance(null, doc.getDocumentElement(), Thread.currentThread().getContextClassLoader()); 41 TestBean bean = (TestBean) o; 42 assertEquals("hello", bean.getBar()); 43 assertEquals(10, bean.getFoo()); 44 } 45 46 public void testDeserializationWithMappedName() throws ParserConfigurationException , IOException , SAXException , ClassNotFoundException { 47 BeanComponentInstanceFactory factory = new BeanComponentInstanceFactory(); 48 49 StringReader sr = new StringReader ("" + 50 "<org.nanocontainer.script.xml.TestBean>" + 51 "<any name='foo'>10</any>" + 52 "<bar>hello</bar>" + 53 "</org.nanocontainer.script.xml.TestBean>"); 54 InputSource is = new InputSource (sr); 55 DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 56 Document doc = db.parse(is); 57 58 Object o = factory.makeInstance(null, doc.getDocumentElement(), Thread.currentThread().getContextClassLoader()); 59 TestBean bean = (TestBean) o; 60 assertEquals("hello", bean.getBar()); 61 assertEquals(10, bean.getFoo()); 62 } 63 } 64 | Popular Tags |