KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nanocontainer > script > xml > XStreamComponentInstanceFactoryTestCase


1 /*****************************************************************************
2  * Copyright (C) NanoContainer Organization. All rights reserved. *
3  * ------------------------------------------------------------------------- *
4  * The software in this package is published under the terms of the BSD *
5  * style license a copy of which has been included with this distribution in *
6  * the LICENSE.txt file. *
7  * *
8  *****************************************************************************/

9 package org.nanocontainer.script.xml;
10
11 import java.io.IOException JavaDoc;
12 import java.io.StringReader JavaDoc;
13
14 import javax.xml.parsers.DocumentBuilder JavaDoc;
15 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
16 import javax.xml.parsers.ParserConfigurationException JavaDoc;
17
18 import junit.framework.TestCase;
19
20 import org.w3c.dom.Document JavaDoc;
21 import org.xml.sax.InputSource JavaDoc;
22 import org.xml.sax.SAXException JavaDoc;
23
24 import com.thoughtworks.xstream.XStream;
25 import com.thoughtworks.xstream.converters.reflection.Sun14ReflectionProvider;
26
27 /**
28  * @author Paul Hammant
29  * @author Marcos Tarruella
30  */

31 public class XStreamComponentInstanceFactoryTestCase extends TestCase {
32
33     public void testDeserializationWithDefaultMode() throws ParserConfigurationException JavaDoc, IOException JavaDoc, SAXException JavaDoc, ClassNotFoundException JavaDoc {
34         runDeserializationTest(new XStreamComponentInstanceFactory());
35     }
36
37     public void testDeserializationInEncancedMode() throws ParserConfigurationException JavaDoc, IOException JavaDoc, SAXException JavaDoc, ClassNotFoundException JavaDoc {
38         runDeserializationTest(new XStreamComponentInstanceFactory(new XStream(new Sun14ReflectionProvider())));
39     }
40
41     public void testDeserializationInPureJavaMode() throws ParserConfigurationException JavaDoc, IOException JavaDoc, SAXException JavaDoc, ClassNotFoundException JavaDoc {
42         runDeserializationTest(new PureJavaXStreamComponentInstanceFactory());
43     }
44
45     public void runDeserializationTest(XMLComponentInstanceFactory factory) throws ParserConfigurationException JavaDoc, IOException JavaDoc, SAXException JavaDoc, ClassNotFoundException JavaDoc {
46         StringReader JavaDoc sr = new StringReader JavaDoc("" +
47                 "<org.nanocontainer.script.xml.TestBean>" +
48                 "<foo>10</foo>" +
49                 "<bar>hello</bar>" +
50                 "</org.nanocontainer.script.xml.TestBean>");
51         InputSource JavaDoc is = new InputSource JavaDoc(sr);
52         DocumentBuilder JavaDoc db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
53         Document JavaDoc doc = db.parse(is);
54
55         Object JavaDoc o = factory.makeInstance(null, doc.getDocumentElement(), Thread.currentThread().getContextClassLoader());
56         TestBean bean = (TestBean) o;
57         assertEquals("hello", bean.getBar());
58         assertEquals(10, bean.getFoo());
59     }
60
61 }
62
Popular Tags