All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.picocontainer.script.xml.XStreamComponentInstanceFactoryTestCase Maven / Gradle / Ivy

/*****************************************************************************
 * Copyright (C) PicoContainer Organization. All rights reserved.            *
 * ------------------------------------------------------------------------- *
 * The software in this package is published under the terms of the BSD      *
 * style license a copy of which has been included with this distribution in *
 * the LICENSE.txt file.                                                     *
 *                                                                           *
 *****************************************************************************/
package org.picocontainer.script.xml;

import static org.junit.Assert.assertEquals;

import java.io.IOException;
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.junit.Test;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.reflection.Sun14ReflectionProvider;

/**
 * @author Paul Hammant
 * @author Marcos Tarruella
 */
public class XStreamComponentInstanceFactoryTestCase {

    @Test public void testDeserializationWithDefaultMode() throws ParserConfigurationException, IOException, SAXException {
        runDeserializationTest(new XStreamComponentInstanceFactory());
    }

    @Test public void testDeserializationInEncancedMode() throws ParserConfigurationException, IOException, SAXException {
        runDeserializationTest(new XStreamComponentInstanceFactory(new XStream(new Sun14ReflectionProvider())));
    }

    @Test public void testDeserializationInPureJavaMode() throws ParserConfigurationException, IOException, SAXException {
        runDeserializationTest(new PureJavaXStreamComponentInstanceFactory());
    }

    public void runDeserializationTest(XMLComponentInstanceFactory factory) throws ParserConfigurationException, IOException, SAXException {
        StringReader sr = new StringReader("" +
                "" +
                "10" +
                "hello" +
                "");
        InputSource is = new InputSource(sr);
        DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document doc = db.parse(is);

        Object o = factory.makeInstance(null, doc.getDocumentElement(), Thread.currentThread().getContextClassLoader());
        TestBean bean = (TestBean) o;
        assertEquals("hello", bean.getBar());
        assertEquals(10, bean.getFoo());
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy