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