001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.betwixt.io;
018:
019: import java.io.StringReader;
020: import java.io.StringWriter;
021:
022: import javax.xml.parsers.DocumentBuilder;
023: import javax.xml.parsers.DocumentBuilderFactory;
024:
025: import junit.framework.Test;
026: import junit.framework.TestSuite;
027: import junit.textui.TestRunner;
028:
029: import org.apache.commons.betwixt.AbstractTestCase;
030: import org.apache.commons.betwixt.PersonBean;
031: import org.w3c.dom.Document;
032: import org.w3c.dom.Element;
033: import org.w3c.dom.Node;
034: import org.w3c.dom.NodeList;
035: import org.xml.sax.Attributes;
036: import org.xml.sax.InputSource;
037: import org.xml.sax.helpers.DefaultHandler;
038:
039: /**
040: * Test harness for SAXBeanWriter.
041: *
042: * @author <a href="mailto:contact@hdietrich.net">Harald Dietrich</a>
043: * @author <a href="mailto:martin@mvdb.net">Martin van den Bemt</a>
044: * @version $Id: TestSAXBeanWriter.java 438373 2006-08-30 05:17:21Z bayard $
045: */
046: public class TestSAXBeanWriter extends AbstractTestCase {
047:
048: public static final String XML = "<?xml version='1.0'?><PersonBean id='1'><age>35</age><name>John Smith</name></PersonBean>";
049:
050: public TestSAXBeanWriter(String name) {
051: super (name);
052: }
053:
054: public void testWrite() throws Exception {
055: PersonBean bean = new PersonBean(35, "John Smith");
056:
057: // writer bean into string
058: StringWriter out = new StringWriter();
059:
060: //SimpleLog log = new SimpleLog("[TestWrite:SAXBeanWriter]");
061: //log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
062:
063: SAXBeanWriter writer = new SAXBeanWriter(new SAXContentHandler(
064: out));
065: //writer.setLog(log);
066: writer.getBindingConfiguration().setMapIDs(false);
067: writer.write(bean);
068: String beanString = out.getBuffer().toString();
069: String xml = "<?xml version='1.0'?><PersonBean><age>35</age>"
070: + "<name>John Smith</name></PersonBean>";
071:
072: xmlAssertIsomorphicContent(parseString(xml),
073: parseString(beanString), true);
074:
075: // test the result
076: DocumentBuilderFactory factory = DocumentBuilderFactory
077: .newInstance();
078: DocumentBuilder builder = factory.newDocumentBuilder();
079: factory.setIgnoringElementContentWhitespace(true);
080: InputSource in = new InputSource();
081: StringReader reader = new StringReader(beanString);
082: in.setCharacterStream(reader);
083: Document doc = builder.parse(in);
084: assertNotNull("Document missing", doc);
085: Element root = doc.getDocumentElement();
086: assertNotNull("Document root missing", root);
087: assertEquals("Document root name wrong", "PersonBean", root
088: .getNodeName());
089: NodeList children = root.getChildNodes();
090: for (int i = 0; i < children.getLength(); i++) {
091: Node child = children.item(i);
092: if (child.getNodeName().equals("age")) {
093: assertNotNull("Person age missing", child
094: .getFirstChild());
095: assertEquals("Person age wrong", "35", child
096: .getFirstChild().getNodeValue().trim());
097: } else if (child.getNodeName().equals("name")) {
098: assertNotNull("Person name missing", child
099: .getFirstChild());
100: assertEquals("Person name wrong", "John Smith", child
101: .getFirstChild().getNodeValue().trim());
102: } else {
103: if (child.getNodeName().equals("#text")) {
104: // now check if the textNode is empty after a trim.
105: String value = child.getNodeValue();
106: if (value != null) {
107: value = value.trim();
108: }
109: if (value.length() != 0) {
110: fail("Text should not contain content in node "
111: + child.getNodeName());
112: }
113: } else {
114: fail("Invalid node " + child.getNodeName());
115: }
116:
117: }
118: }
119: }
120:
121: public void testDocumentElements() throws Exception {
122:
123: class TestDocHandler extends DefaultHandler {
124:
125: boolean startCalled = false;
126: boolean endCalled = false;
127:
128: public void startDocument() {
129: startCalled = true;
130: }
131:
132: public void endDocument() {
133: endCalled = true;
134: }
135:
136: }
137:
138: PersonBean bean = new PersonBean(35, "John Smith");
139:
140: TestDocHandler handler = new TestDocHandler();
141: SAXBeanWriter writer = new SAXBeanWriter(handler);
142: writer.setCallDocumentEvents(true);
143: writer.write(bean);
144:
145: assertEquals("Start not called", handler.startCalled, true);
146: assertEquals("End not called", handler.endCalled, true);
147:
148: handler = new TestDocHandler();
149: writer = new SAXBeanWriter(handler);
150: writer.setCallDocumentEvents(false);
151: writer.write(bean);
152:
153: assertEquals("Start called", handler.startCalled, false);
154: assertEquals("End called", handler.endCalled, false);
155: }
156:
157: /** This tests whether local names and qNames match */
158: public void testLocalNames() throws Exception {
159:
160: class TestNames extends DefaultHandler {
161: boolean namesMatch = true;
162:
163: public void startElement(String uri, String localName,
164: String qName, Attributes attributes) {
165: if (!localName.equals(qName)) {
166: namesMatch = false;
167: }
168:
169: for (int i = 0, size = attributes.getLength(); i < size; i++) {
170: if (!attributes.getLocalName(i).equals(
171: attributes.getQName(i))) {
172: namesMatch = false;
173: }
174: }
175: }
176:
177: public void endElement(String uri, String localName,
178: String qName) {
179: if (!localName.equals(qName)) {
180: namesMatch = false;
181: }
182: }
183: }
184:
185: PersonBean bean = new PersonBean(24, "vikki");
186: TestNames testHandler = new TestNames();
187: SAXBeanWriter writer = new SAXBeanWriter(testHandler);
188: writer.write(bean);
189:
190: assertEquals("Local names match QNames",
191: testHandler.namesMatch, true);
192: }
193:
194: public static Test suite() {
195: return new TestSuite(TestSAXBeanWriter.class);
196: }
197:
198: public static void main(String[] args) {
199: TestRunner.run(suite());
200: }
201: }
|