KickJava   Java API By Example, From Geeks To Geeks.

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


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 junit.framework.TestCase;
12 import org.w3c.dom.Document JavaDoc;
13 import org.xml.sax.InputSource JavaDoc;
14 import org.xml.sax.SAXException JavaDoc;
15
16 import javax.xml.parsers.DocumentBuilder JavaDoc;
17 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
18 import javax.xml.parsers.ParserConfigurationException JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.io.StringReader JavaDoc;
21
22 /**
23  * @author Paul Hammant
24  * @author Marcos Tarruella
25  */

26 public class BeanComponentInstanceFactoryTestCase extends TestCase {
27
28     public void testDeserialization() throws ParserConfigurationException JavaDoc, IOException JavaDoc, SAXException JavaDoc, ClassNotFoundException JavaDoc {
29         BeanComponentInstanceFactory factory = new BeanComponentInstanceFactory();
30
31         StringReader JavaDoc sr = new StringReader JavaDoc("" +
32                 "<org.nanocontainer.script.xml.TestBean>" +
33                 "<foo>10</foo>" +
34                 "<bar>hello</bar>" +
35                 "</org.nanocontainer.script.xml.TestBean>");
36         InputSource JavaDoc is = new InputSource JavaDoc(sr);
37         DocumentBuilder JavaDoc db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
38         Document JavaDoc doc = db.parse(is);
39
40         Object JavaDoc o = factory.makeInstance(null, doc.getDocumentElement(), Thread.currentThread().getContextClassLoader());
41         TestBean bean = (TestBean) o;
42         assertEquals("hello", bean.getBar());
43         assertEquals(10, bean.getFoo());
44     }
45
46     public void testDeserializationWithMappedName() throws ParserConfigurationException JavaDoc, IOException JavaDoc, SAXException JavaDoc, ClassNotFoundException JavaDoc {
47         BeanComponentInstanceFactory factory = new BeanComponentInstanceFactory();
48
49         StringReader JavaDoc sr = new StringReader JavaDoc("" +
50                 "<org.nanocontainer.script.xml.TestBean>" +
51                 "<any name='foo'>10</any>" +
52                 "<bar>hello</bar>" +
53                 "</org.nanocontainer.script.xml.TestBean>");
54         InputSource JavaDoc is = new InputSource JavaDoc(sr);
55         DocumentBuilder JavaDoc db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
56         Document JavaDoc doc = db.parse(is);
57
58         Object JavaDoc o = factory.makeInstance(null, doc.getDocumentElement(), Thread.currentThread().getContextClassLoader());
59         TestBean bean = (TestBean) o;
60         assertEquals("hello", bean.getBar());
61         assertEquals(10, bean.getFoo());
62     }
63 }
64
Popular Tags