KickJava   Java API By Example, From Geeks To Geeks.

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


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 runner for the JiBX framework. This has a single static method, which
46  * runs a test using a mapped class to unmarshal an input file and then
47  * compares the result to an match file. In case of a comparison error the
48  * output file is left as <i>temp.xml</i>. This is a separate class to allow
49  * easy use with a custom classloader.
50  *
51  * @author Dennis M. Sosnoski
52  * @version 1.0
53  */

54
55 public class TestRunner
56 {
57     private TestRunner() {}
58     
59     public static Boolean JavaDoc runTest(String JavaDoc mname, String JavaDoc fin, String JavaDoc fcomp,
60         boolean save)
61         throws IOException JavaDoc, JiBXException, XmlPullParserException {
62         
63         // look up the mapped class and associated binding factory
64
Class JavaDoc mclas;
65         try {
66             mclas = TestSimple.class.getClassLoader().loadClass(mname);
67         } catch (ClassNotFoundException JavaDoc ex) {
68             System.err.println("Class " + mname + " not found");
69             return Boolean.FALSE;
70         }
71         IBindingFactory bfact = BindingDirectory.getFactory(mclas);
72         
73         // unmarshal document to construct objects
74
IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
75         Object JavaDoc obj = uctx.unmarshalDocument(new FileInputStream JavaDoc(fin), fin, null);
76         if (!mclas.isInstance(obj)) {
77             System.err.println("Unmarshalled result not expected type");
78             return Boolean.FALSE;
79         }
80         
81         // determine encoding of input document
82
String JavaDoc enc = ((UnmarshallingContext)uctx).getInputEncoding();
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 match 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(fcomp), enc);
95         DocumentComparator comp = new DocumentComparator(System.err);
96         boolean match = comp.compare(frdr, brdr);
97         if (!match || save) {
98             
99             // save file before returning
100
try {
101                 File JavaDoc fout = new File JavaDoc("temp.xml");
102                 fout.delete();
103                 FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(fout);
104                 fos.write(bos.toByteArray());
105                 fos.close();
106             } catch (IOException JavaDoc ex) {
107                 System.err.println("Error writing to temp.xml: " +
108                     ex.getMessage());
109             }
110         }
111         return match ? Boolean.TRUE : Boolean.FALSE;
112     }
113     
114     public static Boolean JavaDoc runTest(String JavaDoc mname, String JavaDoc fin, String JavaDoc fcomp)
115         throws IOException JavaDoc, JiBXException, XmlPullParserException {
116         return runTest(mname, fin, fcomp, false);
117     }
118 }
119
Popular Tags