KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2 Copyright (c) 2003-2005, 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.xmlpull.v1.XmlPullParserException;
42
43 import multiple.*;
44
45 /**
46  * Test program for the JiBX framework. Works with sets of four input
47  * parameters: in-binding, in-file, out-binding, compare-file. Unmarshals
48  * documents from files using the specified bindings, then marshals them back
49  * out using the same or different bindings and compares the results with match
50  * files. In case of a comparison error the output file is left as
51  * <i>temp.xml</i>.
52  *
53  * @author Dennis M. Sosnoski
54  * @version 1.0
55  */

56
57 public class TestTables {
58     
59     protected static boolean runTest(String JavaDoc bin, String JavaDoc fin, String JavaDoc bout,
60         String JavaDoc cout) throws IOException JavaDoc, JiBXException, XmlPullParserException {
61         
62         // get binding factories for the named bindings
63
IBindingFactory ibf = null;
64         IBindingFactory obf = null;
65         Class JavaDoc target = TimeTableBean.class;
66         try {
67             ibf = BindingDirectory.getFactory(bin, target);
68             obf = BindingDirectory.getFactory(bout, target);
69         } catch (JiBXException ex1) {
70             target = SplitTableBean.class;
71             try {
72                 ibf = BindingDirectory.getFactory(bin, target);
73                 obf = BindingDirectory.getFactory(bout, target);
74             } catch (JiBXException ex2) {
75                 ex2.printStackTrace();
76                 return false;
77             }
78         }
79         
80         // unmarshal document to construct bean
81
IUnmarshallingContext uctx = ibf.createUnmarshallingContext();
82         Object JavaDoc obj = uctx.unmarshalDocument(new FileInputStream JavaDoc(fin), null);
83         if (!target.isInstance(obj)) {
84             System.err.println("Unmarshalled result not expected type");
85             return false;
86         }
87         
88         // marshal bean back out to document in memory
89
IMarshallingContext mctx = obf.createMarshallingContext();
90         ByteArrayOutputStream JavaDoc bos = new ByteArrayOutputStream JavaDoc();
91         mctx.setIndent(1, "\n", ' ');
92         mctx.marshalDocument(obj, "UTF-8", null, bos);
93         
94         // compare with expected result document
95
InputStreamReader JavaDoc brdr = new InputStreamReader JavaDoc
96             (new ByteArrayInputStream JavaDoc(bos.toByteArray()), "UTF-8");
97         InputStreamReader JavaDoc frdr = new InputStreamReader JavaDoc
98             (new FileInputStream JavaDoc(cout), "UTF-8");
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                 File JavaDoc fout = new File JavaDoc("temp.xml");
107                 fout.delete();
108                 FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(fout);
109                 fos.write(bos.toByteArray());
110                 fos.close();
111             } catch (IOException JavaDoc ex) {
112                 System.err.println("Error writing to temp.xml: " +
113                     ex.getMessage());
114             }
115             return false;
116         }
117     }
118
119     public static void main(String JavaDoc[] args) {
120         if (args.length >= 4 && args.length % 4 == 0) {
121             
122             // delete generated output file if present
123
File JavaDoc temp = new File JavaDoc("temp.xml");
124             if (temp.exists()) {
125                 temp.delete();
126             }
127             
128             // process each set of four arguments
129
boolean err = false;
130             int base = 0;
131             for (; base < args.length; base += 4) {
132                 try {
133                     if (!runTest(args[base], args[base+1], args[base+2],
134                         args[base+3])) {
135                         err = true;
136                         break;
137                     }
138                 } catch (Exception JavaDoc ex) {
139                     System.err.println("Exception: " + ex.getMessage());
140                     ex.printStackTrace();
141                     err = true;
142                     break;
143                 }
144             }
145             
146             // take error exit if difference found
147
if (err) {
148                 System.err.println("Error on argument set: " +
149                     args[base] + ", " + args[base+1] + ", " +
150                     args[base+2] + ", " + args[base+3]);
151                 System.exit(1);
152             }
153             
154         } else {
155             System.err.println("Requires arguments in sets of four:\n" +
156                 " in-binding in-file out-binding compare-file\n" +
157                 "Leaves output as temp.xml in case of error");
158             System.exit(1);
159         }
160     }
161 }
Popular Tags