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 junit.framework.TestCase;
012    import org.w3c.dom.Document;
013    import org.xml.sax.InputSource;
014    import org.xml.sax.SAXException;
015    
016    import javax.xml.parsers.DocumentBuilder;
017    import javax.xml.parsers.DocumentBuilderFactory;
018    import javax.xml.parsers.ParserConfigurationException;
019    import java.io.IOException;
020    import java.io.StringReader;
021    
022    /**
023     * @author Paul Hammant
024     * @author Marcos Tarruella
025     */
026    public class BeanComponentInstanceFactoryTestCase extends TestCase {
027    
028        public void testDeserialization() throws ParserConfigurationException, IOException, SAXException, ClassNotFoundException {
029            BeanComponentInstanceFactory factory = new BeanComponentInstanceFactory();
030    
031            StringReader sr = new StringReader("" +
032                    "<org.nanocontainer.script.xml.TestBean>" +
033                    "<foo>10</foo>" +
034                    "<bar>hello</bar>" +
035                    "</org.nanocontainer.script.xml.TestBean>");
036            InputSource is = new InputSource(sr);
037            DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
038            Document doc = db.parse(is);
039    
040            Object o = factory.makeInstance(null, doc.getDocumentElement(), Thread.currentThread().getContextClassLoader());
041            TestBean bean = (TestBean) o;
042            assertEquals("hello", bean.getBar());
043            assertEquals(10, bean.getFoo());
044        }
045    
046        public void testDeserializationWithMappedName() throws ParserConfigurationException, IOException, SAXException, ClassNotFoundException {
047            BeanComponentInstanceFactory factory = new BeanComponentInstanceFactory();
048    
049            StringReader sr = new StringReader("" +
050                    "<org.nanocontainer.script.xml.TestBean>" +
051                    "<any name='foo'>10</any>" +
052                    "<bar>hello</bar>" +
053                    "</org.nanocontainer.script.xml.TestBean>");
054            InputSource is = new InputSource(sr);
055            DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
056            Document doc = db.parse(is);
057    
058            Object o = factory.makeInstance(null, doc.getDocumentElement(), Thread.currentThread().getContextClassLoader());
059            TestBean bean = (TestBean) o;
060            assertEquals("hello", bean.getBar());
061            assertEquals(10, bean.getFoo());
062        }
063    }