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 import com.thoughtworks.xstream.XStream; 026 import com.thoughtworks.xstream.converters.reflection.Sun14ReflectionProvider; 027 028 /** 029 * @author Paul Hammant 030 * @author Marcos Tarruella 031 */ 032 public class XStreamComponentInstanceFactoryTestCase { 033 034 @Test public void testDeserializationWithDefaultMode() throws ParserConfigurationException, IOException, SAXException { 035 runDeserializationTest(new XStreamComponentInstanceFactory()); 036 } 037 038 @Test public void testDeserializationInEncancedMode() throws ParserConfigurationException, IOException, SAXException { 039 runDeserializationTest(new XStreamComponentInstanceFactory(new XStream(new Sun14ReflectionProvider()))); 040 } 041 042 @Test public void testDeserializationInPureJavaMode() throws ParserConfigurationException, IOException, SAXException { 043 runDeserializationTest(new PureJavaXStreamComponentInstanceFactory()); 044 } 045 046 public void runDeserializationTest(XMLComponentInstanceFactory factory) throws ParserConfigurationException, IOException, SAXException { 047 StringReader sr = new StringReader("" + 048 "<org.picocontainer.script.xml.TestBean>" + 049 "<foo>10</foo>" + 050 "<bar>hello</bar>" + 051 "</org.picocontainer.script.xml.TestBean>"); 052 InputSource is = new InputSource(sr); 053 DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 054 Document doc = db.parse(is); 055 056 Object o = factory.makeInstance(null, doc.getDocumentElement(), Thread.currentThread().getContextClassLoader()); 057 TestBean bean = (TestBean) o; 058 assertEquals("hello", bean.getBar()); 059 assertEquals(10, bean.getFoo()); 060 } 061 062 }