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;
19  import java.io.StringWriter;
20  
21  import javax.xml.parsers.DocumentBuilder;
22  import javax.xml.parsers.DocumentBuilderFactory;
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;
31  import org.w3c.dom.Element;
32  import org.w3c.dom.Node;
33  import org.w3c.dom.NodeList;
34  import org.xml.sax.Attributes;
35  import org.xml.sax.InputSource;
36  import org.xml.sax.helpers.DefaultHandler;
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 XML = "<?xml version='1.0'?><PersonBean id='1'><age>35</age><name>John Smith</name></PersonBean>";
48  
49      public TestSAXBeanWriter(String name) {
50          super(name);
51      }
52  
53      public void testWrite() throws Exception {
54          PersonBean bean = new PersonBean(35, "John Smith");
55  
56          // writer bean into string
57          StringWriter out = new StringWriter();
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 beanString = out.getBuffer().toString();
67          String 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 factory = DocumentBuilderFactory.newInstance();
78          DocumentBuilder builder = factory.newDocumentBuilder();
79          factory.setIgnoringElementContentWhitespace(true);
80          InputSource in = new InputSource();
81          StringReader reader = new StringReader(beanString);
82          in.setCharacterStream(reader);
83          Document doc = builder.parse(in);
84          assertNotNull("Document missing", doc);        
85          Element root = doc.getDocumentElement();
86          assertNotNull("Document root missing", root);
87          assertEquals("Document root name wrong", "PersonBean", root.getNodeName());
88          NodeList children = root.getChildNodes();       
89          for (int i = 0; i < children.getLength(); i++) {
90              Node 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 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 {
116         
117         class TestDocHandler extends DefaultHandler {
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 {
153     
154         class TestNames extends DefaultHandler {
155             boolean namesMatch = true;
156             
157             public void startElement(String uri, String localName, String qName, Attributes 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 uri, String localName, String 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[] args) {
190         TestRunner.run(suite());
191     }
192 }