KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jibx > match > TestSimple


1 /*
2 Copyright (c) 2003-2004, 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.match;
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.IOException JavaDoc;
37 import java.io.InputStreamReader JavaDoc;
38
39 import org.jibx.extras.*;
40 import org.jibx.runtime.*;
41 import org.jibx.runtime.impl.UnmarshallingContext;
42 import org.xmlpull.v1.XmlPullParserException;
43
44 /**
45  * Test program for the JiBX framework. Works with sets of two input
46  * parameters: mapped-class, in-file. Unmarshals documents from files using the
47  * binding defined for the mapped class, then marshals them back out using the
48  * same bindings and compares the results. In case of a comparison error the
49  * output file is left as <i>temp.xml</i>.
50  *
51  * @author Dennis M. Sosnoski
52  * @version 1.0
53  */

54
55 public class TestSimple {
56     
57     protected static boolean runTest(String JavaDoc mname, String JavaDoc fin)
58         throws IOException JavaDoc, JiBXException, XmlPullParserException {
59         
60         // look up the mapped class and associated binding factory
61
Class JavaDoc mclas;
62         try {
63             mclas = TestSimple.class.getClassLoader().loadClass(mname);
64         } catch (ClassNotFoundException JavaDoc ex) {
65             System.err.println("Class " + mname + " not found");
66             return false;
67         }
68         IBindingFactory bfact = BindingDirectory.getFactory(mclas);
69         
70         // unmarshal document to construct objects
71
IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
72         Object JavaDoc obj = uctx.unmarshalDocument(new FileInputStream JavaDoc(fin), null);
73         if (!mclas.isInstance(obj)) {
74             System.err.println("Unmarshalled result not expected type");
75             return false;
76         }
77         
78         // determine encoding of input document
79
String JavaDoc enc = ((UnmarshallingContext)uctx).getInputEncoding();
80         if (enc == null) {
81             enc = "UTF-8";
82         }
83         
84         // marshal root object back out to document in memory
85
IMarshallingContext mctx = bfact.createMarshallingContext();
86         ByteArrayOutputStream JavaDoc bos = new ByteArrayOutputStream JavaDoc();
87         mctx.setIndent(2);
88         mctx.marshalDocument(obj, enc, null, bos);
89         
90         // compare with original input document
91
InputStreamReader JavaDoc brdr = new InputStreamReader JavaDoc
92             (new ByteArrayInputStream JavaDoc(bos.toByteArray()), enc);
93         InputStreamReader JavaDoc frdr = new InputStreamReader JavaDoc
94             (new FileInputStream JavaDoc(fin), enc);
95         DocumentComparator comp = new DocumentComparator(System.err);
96         if (comp.compare(frdr, brdr)) {
97             return true;
98         } else {
99             
100             // save file before returning failure
101
try {
102                 File JavaDoc fout = new File JavaDoc("temp.xml");
103                 fout.delete();
104                 FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(fout);
105                 fos.write(bos.toByteArray());
106                 fos.close();
107             } catch (IOException JavaDoc ex) {
108                 System.err.println("Error writing to temp.xml: " +
109                     ex.getMessage());
110             }
111             return false;
112         }
113     }
114
115     public static void main(String JavaDoc[] args) {
116         if (args.length >= 2 && args.length % 2 == 0) {
117             
118             // delete generated output file if present
119
File JavaDoc temp = new File JavaDoc("temp.xml");
120             if (temp.exists()) {
121                 temp.delete();
122             }
123             
124             // process each set of two arguments
125
boolean err = false;
126             int base = 0;
127             for (; base < args.length; base += 2) {
128                 try {
129                     if (!runTest(args[base], args[base+1])) {
130                         err = true;
131                         break;
132                     }
133                 } catch (Exception JavaDoc ex) {
134                     ex.printStackTrace();
135 // System.err.println(ex.getMessage());
136
err = true;
137                     break;
138                 }
139             }
140             
141             // take error exit if difference found
142
if (err) {
143                 System.err.println("Error on argument set: " +
144                     args[base] + ", " + args[base+1]);
145                 System.err.println("File path " + temp.getAbsolutePath());
146                 System.exit(1);
147             }
148             
149         } else {
150             System.err.println("Requires arguments in sets of two:\n" +
151                 " mapped-class in-file\n" +
152                 "Leaves output as temp.xml in case of error");
153             System.exit(1);
154         }
155     }
156 }
157
Popular Tags