1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
38
39
40
41
42
43
44
45
46
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
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
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
137 ByteArrayOutputStream baos = new ByteArrayOutputStream();
138 StreamXMLSerializer streamSerializer = new StreamXMLSerializer(baos);
139 streamSerializer.serializeObject(root);
140 baos.close();
141 byte[] saxRoundtripResult = baos.toByteArray();
142
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
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
155 DomXMLSerializer serializer = new DomXMLSerializer();
156 Document domRoundtripDocument = serializer.serializeToDOM(root);
157
158 DomUtils.documentsAreEquals(domRoundtripDocument, referenceDomDocument, comparisonFlags);
159 }
160
161
162 }