KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > betwixt > io > TestSAXBeanWriter


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.betwixt.io;
17
18 import java.io.StringReader JavaDoc;
19 import java.io.StringWriter JavaDoc;
20
21 import javax.xml.parsers.DocumentBuilder JavaDoc;
22 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
23
24 import junit.framework.Test;
25 import junit.framework.TestSuite;
26 import junit.textui.TestRunner;
27
28 import org.apache.commons.betwixt.AbstractTestCase;
29 import org.apache.commons.betwixt.PersonBean;
30 import org.w3c.dom.Document JavaDoc;
31 import org.w3c.dom.Element JavaDoc;
32 import org.w3c.dom.Node JavaDoc;
33 import org.w3c.dom.NodeList JavaDoc;
34 import org.xml.sax.Attributes JavaDoc;
35 import org.xml.sax.InputSource JavaDoc;
36 import org.xml.sax.helpers.DefaultHandler JavaDoc;
37
38 /**
39  * Test harness for SAXBeanWriter.
40  *
41  * @author <a HREF="mailto:contact@hdietrich.net">Harald Dietrich</a>
42  * @author <a HREF="mailto:martin@mvdb.net">Martin van den Bemt</a>
43  * @version $Id: TestSAXBeanWriter.java,v 1.11 2004/06/13 21:32:48 rdonkin Exp $
44  */

45 public class TestSAXBeanWriter extends AbstractTestCase {
46     
47     public static final String JavaDoc XML = "<?xml version='1.0'?><PersonBean id='1'><age>35</age><name>John Smith</name></PersonBean>";
48
49     public TestSAXBeanWriter(String JavaDoc name) {
50         super(name);
51     }
52
53     public void testWrite() throws Exception JavaDoc {
54         PersonBean bean = new PersonBean(35, "John Smith");
55
56         // writer bean into string
57
StringWriter JavaDoc out = new StringWriter JavaDoc();
58         
59         //SimpleLog log = new SimpleLog("[TestWrite:SAXBeanWriter]");
60
//log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
61

62         SAXBeanWriter writer = new SAXBeanWriter(new SAXContentHandler(out));
63         //writer.setLog(log);
64
writer.getBindingConfiguration().setMapIDs(false);
65         writer.write(bean);
66         String JavaDoc beanString = out.getBuffer().toString();
67         String JavaDoc xml = "<?xml version='1.0'?><PersonBean><age>35</age>"
68                 + "<name>John Smith</name></PersonBean>";
69                 
70                         
71         xmlAssertIsomorphicContent(
72                     parseString(xml),
73                     parseString(beanString),
74                     true);
75      
76         // test the result
77
DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
78         DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
79         factory.setIgnoringElementContentWhitespace(true);
80         InputSource JavaDoc in = new InputSource JavaDoc();
81         StringReader JavaDoc reader = new StringReader JavaDoc(beanString);
82         in.setCharacterStream(reader);
83         Document JavaDoc doc = builder.parse(in);
84         assertNotNull("Document missing", doc);
85         Element JavaDoc root = doc.getDocumentElement();
86         assertNotNull("Document root missing", root);
87         assertEquals("Document root name wrong", "PersonBean", root.getNodeName());
88         NodeList JavaDoc children = root.getChildNodes();
89         for (int i = 0; i < children.getLength(); i++) {
90             Node JavaDoc child = children.item(i);
91             if (child.getNodeName().equals("age")) {
92                 assertNotNull("Person age missing", child.getFirstChild());
93                 assertEquals("Person age wrong", "35", child.getFirstChild().getNodeValue().trim());
94             } else if (child.getNodeName().equals("name")) {
95                 assertNotNull("Person name missing", child.getFirstChild());
96                 assertEquals("Person name wrong", "John Smith", child.getFirstChild().getNodeValue().trim());
97             } else {
98                 if (child.getNodeName().equals("#text")) {
99                     // now check if the textNode is empty after a trim.
100
String JavaDoc value = child.getNodeValue();
101                     if (value != null) {
102                         value = value.trim();
103                     }
104                     if (value.length() != 0) {
105                         fail("Text should not contain content in node " + child.getNodeName());
106                     }
107                 }else{
108                     fail("Invalid node " + child.getNodeName());
109                 }
110                 
111             }
112         }
113     }
114         
115     public void testDocumentElements() throws Exception JavaDoc {
116         
117         class TestDocHandler extends DefaultHandler JavaDoc {
118             
119             boolean startCalled = false;
120             boolean endCalled = false;
121             
122             public void startDocument() {
123                 startCalled = true;
124             }
125             
126             public void endDocument() {
127                 endCalled = true;
128             }
129             
130         }
131         
132         PersonBean bean = new PersonBean(35, "John Smith");
133         
134         TestDocHandler handler = new TestDocHandler();
135         SAXBeanWriter writer = new SAXBeanWriter(handler);
136         writer.setCallDocumentEvents(true);
137         writer.write(bean);
138         
139         assertEquals("Start not called", handler.startCalled , true);
140         assertEquals("End not called", handler.endCalled , true);
141         
142         handler = new TestDocHandler();
143         writer = new SAXBeanWriter(handler);
144         writer.setCallDocumentEvents(false);
145         writer.write(bean);
146         
147         assertEquals("Start called", handler.startCalled , false);
148         assertEquals("End called", handler.endCalled , false);
149     }
150     
151     /** This tests whether local names and qNames match */
152     public void testLocalNames() throws Exception JavaDoc {
153     
154         class TestNames extends DefaultHandler JavaDoc {
155             boolean namesMatch = true;
156             
157             public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc attributes) {
158                 if (!localName.equals(qName)) {
159                     namesMatch = false;
160                 }
161                 
162                 for (int i=0, size=attributes.getLength(); i<size; i++) {
163                     if (!attributes.getLocalName(i).equals(attributes.getQName(i))) {
164                         namesMatch = false;
165                     }
166                 }
167             }
168             
169             public void endElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName) {
170                 if (!localName.equals(qName)) {
171                     namesMatch = false;
172                 }
173             }
174         }
175         
176         PersonBean bean = new PersonBean(24, "vikki");
177         TestNames testHandler = new TestNames();
178         SAXBeanWriter writer = new SAXBeanWriter(testHandler);
179         writer.write(bean);
180         
181         assertEquals("Local names match QNames", testHandler.namesMatch, true);
182     }
183     
184         
185     public static Test suite() {
186         return new TestSuite(TestSAXBeanWriter.class);
187     }
188     
189     public static void main(String JavaDoc[] args) {
190         TestRunner.run(suite());
191     }
192 }
193
Popular Tags