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