View Javadoc

1   /*-------------------------------------------------------------------------
2    Copyright 2012 Olivier Berlanger
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 net.sf.xolite.utils;
17  
18  
19  import java.io.ByteArrayInputStream;
20  import java.io.ByteArrayOutputStream;
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.io.UnsupportedEncodingException;
24  
25  import net.sf.xolite.XMLObjectFactory;
26  import net.sf.xolite.XMLSerializable;
27  import net.sf.xolite.dom.DomUtils;
28  import net.sf.xolite.dom.DomXMLEventParser;
29  import net.sf.xolite.dom.DomXMLSerializer;
30  import net.sf.xolite.sax.SaxXMLEventParser;
31  
32  import org.w3c.dom.Document;
33  import org.xml.sax.InputSource;
34  
35  
36  /**
37   * Helper class to test your XML serialization. <br>
38   * Give it an input stream containing a serialized XML and a XMLSerializable representing the same XML. When you call
39   * <code>testXMLSerialization()</code>, this tester class will try several transformations (using X-O lite APIs and standard DOM
40   * APIs) from both sources and check that the result are matching.
41   * <p>
42   * Note: for the purpose of the test this class will create several copies of the initial document in memory (as objects, DOM
43   * and byte[]). So it's not suited for very big documents.
44   * </p>
45   * 
46   * @author Olivier Berlanger
47   */
48  public class XMLSerializationTester {
49  
50  
51      private byte[] xmlSource;
52      private XMLSerializable root;
53      private String schemaLocations;
54      private XMLObjectFactory factory;
55      private int comparisonFlags;
56  
57  
58      public XMLSerializationTester() {
59      }
60  
61  
62      public XMLSerializationTester(InputStream xmlSourceStream, XMLSerializable rootObject) {
63          setXmlSource(xmlSourceStream);
64          setRootObject(rootObject);
65      }
66  
67  
68      public void setRootObject(XMLSerializable rootObject) {
69          root = rootObject;
70      }
71  
72  
73      public void setXmlSource(InputStream xmlSourceStream) {
74          try {
75              ByteArrayOutputStream baos = new ByteArrayOutputStream();
76              byte[] buffer = new byte[1024];
77              int readCount;
78              while ((readCount = xmlSourceStream.read(buffer)) > 0) {
79                  baos.write(buffer, 0, readCount);
80              }
81              xmlSourceStream.close();
82              baos.close();
83              xmlSource = baos.toByteArray();
84          } catch (IOException ioe) {
85              throw new IllegalArgumentException("Unable to read xml source stream", ioe);
86          }
87      }
88  
89  
90      public void setXmlSource(String xmlSourceString) {
91          setXmlSource(xmlSourceString, "UTF-8");
92      }
93  
94  
95      public void setXmlSource(String xmlSourceString, String encoding) {
96          try {
97              xmlSource = xmlSourceString.getBytes(encoding);
98          } catch (UnsupportedEncodingException ex) {
99              throw new IllegalArgumentException("Unsupported encoding", ex);
100         }
101     }
102 
103 
104     public void setSchemaLocations(String newSchemaLocations) {
105         schemaLocations = newSchemaLocations;
106     }
107 
108 
109     public void setXMLObjectFactory(XMLObjectFactory newFactory) {
110         factory = newFactory;
111     }
112 
113 
114     public void setComparisonFlags(int newComparisonFlags) {
115         comparisonFlags = newComparisonFlags;
116     }
117 
118 
119     public void testXMLSerialization() throws Exception {
120         // get a DOM document for comparison
121         Document referenceDomDocument = DomUtils.streamToDom(new ByteArrayInputStream(xmlSource));
122         checkWithSaxRoundtrip(referenceDomDocument);
123         checkWithDomRoundtrip(referenceDomDocument);
124     }
125 
126 
127     private void checkWithSaxRoundtrip(Document referenceDomDocument) throws Exception {
128         // parse the XML source using SAX
129         if (root == null) root = new RootHolder();
130         InputSource src = new InputSource(new ByteArrayInputStream(xmlSource));
131         SaxXMLEventParser saxParser;
132         if (schemaLocations == null) saxParser = new SaxXMLEventParser();
133         else saxParser = new SaxXMLEventParser(schemaLocations);
134         if (factory != null) saxParser.setFactory(factory);
135         saxParser.parse(src, root);
136         // serialize using StreamXMLSerializer
137         ByteArrayOutputStream baos = new ByteArrayOutputStream();
138         StreamXMLSerializer streamSerializer = new StreamXMLSerializer(baos);
139         streamSerializer.serializeObject(root);
140         baos.close();
141         byte[] saxRoundtripResult = baos.toByteArray();
142         // transform the result to DOM and compare with the reference DOM
143         Document saxRoundtripDocument = DomUtils.streamToDom(new ByteArrayInputStream(saxRoundtripResult));
144         DomUtils.documentsAreEquals(saxRoundtripDocument, referenceDomDocument, comparisonFlags);
145     }
146 
147 
148     private void checkWithDomRoundtrip(Document referenceDomDocument) throws Exception {
149         // parse the referenceDomDocument to object
150         if (root == null) root = new RootHolder();
151         DomXMLEventParser domParser = new DomXMLEventParser();
152         if (factory != null) domParser.setFactory(factory);
153         domParser.parse(referenceDomDocument, root);
154         // serialize back to DOM using DomXMLSerializer
155         DomXMLSerializer serializer = new DomXMLSerializer();
156         Document domRoundtripDocument = serializer.serializeToDOM(root);
157         // compare the two DOMs
158         DomUtils.documentsAreEquals(domRoundtripDocument, referenceDomDocument, comparisonFlags);
159     }
160 
161 
162 }