001 /*****************************************************************************
002 * Copyright (C) NanoContainer Organization. All rights reserved. *
003 * ------------------------------------------------------------------------- *
004 * The software in this package is published under the terms of the BSD *
005 * style license a copy of which has been included with this distribution in *
006 * the LICENSE.txt file. *
007 * *
008 *****************************************************************************/
009 package org.nanocontainer.script.xml;
010
011 import java.io.IOException;
012 import java.io.StringReader;
013
014 import javax.xml.parsers.DocumentBuilder;
015 import javax.xml.parsers.DocumentBuilderFactory;
016 import javax.xml.parsers.ParserConfigurationException;
017
018 import junit.framework.TestCase;
019
020 import org.w3c.dom.Document;
021 import org.xml.sax.InputSource;
022 import org.xml.sax.SAXException;
023
024 import com.thoughtworks.xstream.XStream;
025 import com.thoughtworks.xstream.converters.reflection.Sun14ReflectionProvider;
026
027 /**
028 * @author Paul Hammant
029 * @author Marcos Tarruella
030 */
031 public class XStreamComponentInstanceFactoryTestCase extends TestCase {
032
033 public void testDeserializationWithDefaultMode() throws ParserConfigurationException, IOException, SAXException, ClassNotFoundException {
034 runDeserializationTest(new XStreamComponentInstanceFactory());
035 }
036
037 public void testDeserializationInEncancedMode() throws ParserConfigurationException, IOException, SAXException, ClassNotFoundException {
038 runDeserializationTest(new XStreamComponentInstanceFactory(new XStream(new Sun14ReflectionProvider())));
039 }
040
041 public void testDeserializationInPureJavaMode() throws ParserConfigurationException, IOException, SAXException, ClassNotFoundException {
042 runDeserializationTest(new PureJavaXStreamComponentInstanceFactory());
043 }
044
045 public void runDeserializationTest(XMLComponentInstanceFactory factory) throws ParserConfigurationException, IOException, SAXException, ClassNotFoundException {
046 StringReader sr = new StringReader("" +
047 "<org.nanocontainer.script.xml.TestBean>" +
048 "<foo>10</foo>" +
049 "<bar>hello</bar>" +
050 "</org.nanocontainer.script.xml.TestBean>");
051 InputSource is = new InputSource(sr);
052 DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
053 Document doc = db.parse(is);
054
055 Object o = factory.makeInstance(null, doc.getDocumentElement(), Thread.currentThread().getContextClassLoader());
056 TestBean bean = (TestBean) o;
057 assertEquals("hello", bean.getBar());
058 assertEquals(10, bean.getFoo());
059 }
060
061 }