KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jibx > extras > TestRoundtrip


1 /*
2 Copyright (c) 2003, Dennis M. Sosnoski
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
7
8  * Redistributions of source code must retain the above copyright notice, this
9    list of conditions and the following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright notice,
11    this list of conditions and the following disclaimer in the documentation
12    and/or other materials provided with the distribution.
13  * Neither the name of JiBX nor the names of its contributors may be used
14    to endorse or promote products derived from this software without specific
15    prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */

28
29 package org.jibx.extras;
30
31 import java.io.ByteArrayInputStream JavaDoc;
32 import java.io.ByteArrayOutputStream JavaDoc;
33 import java.io.File JavaDoc;
34 import java.io.FileInputStream JavaDoc;
35 import java.io.FileOutputStream JavaDoc;
36 import java.io.FileReader JavaDoc;
37 import java.io.IOException JavaDoc;
38 import java.io.InputStreamReader JavaDoc;
39 import java.net.URL JavaDoc;
40 import java.net.URLClassLoader JavaDoc;
41
42 import org.jibx.runtime.*;
43 import org.xmlpull.v1.XmlPullParserException;
44
45 /**
46  * Test program for the JiBX framework. Works with two or three command line
47  * arguments: mapped-class, in-file, and out-file (optional, only needed if
48  * different from in-file). You can also supply a multiple of three input
49  * arguments, in which case each set of three is processed in turn (in this case
50  * the out-file is required). Unmarshals documents from files using the binding
51  * defined for the mapped class, then marshals them back out using the same
52  * bindings and compares the results. In case of a comparison error the output
53  * file is left as <i>temp.xml</i>.
54  *
55  * @author Dennis M. Sosnoski
56  * @version 1.0
57  */

58
59 public class TestRoundtrip {
60     
61     protected static boolean runTest(String JavaDoc mname, String JavaDoc fin, String JavaDoc fout)
62         throws IOException JavaDoc, JiBXException, XmlPullParserException {
63         
64         // look up the mapped class and associated binding factory
65
Class JavaDoc mclas;
66         try {
67             URL JavaDoc[] urls = new URL JavaDoc[] { new File JavaDoc(".").toURL() };
68             ClassLoader JavaDoc parent = Thread.currentThread().getContextClassLoader();
69             if (parent == null) {
70                 parent = TestRoundtrip.class.getClassLoader();
71             }
72             ClassLoader JavaDoc loader = new URLClassLoader JavaDoc(urls, parent);
73             Thread.currentThread().setContextClassLoader(loader);
74             mclas = loader.loadClass(mname);
75         } catch (ClassNotFoundException JavaDoc ex) {
76             System.err.println("Class " + mname + " not found");
77             return false;
78         }
79         IBindingFactory bfact = BindingDirectory.getFactory(mclas);
80         
81         // unmarshal document to construct objects
82
IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
83         Object JavaDoc obj = uctx.unmarshalDocument(new FileInputStream JavaDoc(fin), null);
84         if (!mclas.isInstance(obj)) {
85             System.err.println("Unmarshalled result not expected type");
86             return false;
87         }
88         
89         // marshal root object back out to document in memory
90
IMarshallingContext mctx = bfact.createMarshallingContext();
91         mctx.setIndent(2);
92         ByteArrayOutputStream JavaDoc bos = new ByteArrayOutputStream JavaDoc();
93         mctx.marshalDocument(obj, "UTF-8", null, bos);
94         
95         // compare with output document to be matched
96
InputStreamReader JavaDoc brdr = new InputStreamReader JavaDoc
97             (new ByteArrayInputStream JavaDoc(bos.toByteArray()), "UTF-8");
98         FileReader JavaDoc frdr = new FileReader JavaDoc(fout);
99         DocumentComparator comp = new DocumentComparator(System.err);
100         if (comp.compare(frdr, brdr)) {
101             return true;
102         } else {
103             
104             // save file before returning failure
105
try {
106                 FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc("temp.xml");
107                 fos.write(bos.toByteArray());
108                 fos.close();
109             } catch (IOException JavaDoc ex) {
110                 System.err.println("Error writing to temp.xml: " +
111                     ex.getMessage());
112             }
113             return false;
114         }
115     }
116
117     public static void main(String JavaDoc[] args) {
118         if (args.length == 2 || (args.length > 0 && args.length % 3 == 0)) {
119             
120             // delete generated output file if present
121
File JavaDoc temp = new File JavaDoc("temp.xml");
122             if (temp.exists()) {
123                 temp.delete();
124             }
125             
126             // process input arguments
127
int base = 0;
128             boolean err = false;
129             String JavaDoc fin = null;
130             String JavaDoc fout = null;
131             while (base < args.length) {
132                 
133                 // run test with one argument set
134
fin = args[base+1];
135                 fout = (args.length < 3) ? fin : args[base+2];
136                 try {
137                     if (!runTest(args[base], fin, fout)) {
138                         err = true;
139                     }
140                 } catch (Exception JavaDoc ex) {
141                     ex.printStackTrace();
142 // System.err.println(ex.getMessage());
143
err = true;
144                 }
145             
146                 // take error exit if difference found
147
if (err) {
148                     System.err.println("Error round-tripping class: " + args[base] +
149                         "\n with input file " + fin + " and output compared to " +
150                         fout);
151                     System.err.println("Saved output document file path " +
152                         temp.getAbsolutePath());
153                     System.exit(1);
154                 }
155                 
156                 // advance to next argument set
157
base += 3;
158             }
159             
160         } else {
161             System.err.println("Usage: java TestRoundtrip mapped-class" +
162                 " in-file [out-file]\n where out-file is only required if the" +
163                 " output document is different from\nthe input document. " +
164                 "Leaves output as temp.xml in case of error");
165             System.exit(1);
166         }
167     }
168 }
169
170
Popular Tags