KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2 Copyright (c) 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.File JavaDoc;
32 import java.lang.reflect.InvocationTargetException JavaDoc;
33 import java.lang.reflect.Method JavaDoc;
34
35 import org.jibx.binding.Loader;
36
37 /**
38  * Test program for the JiBX framework. Works with sets of four input
39  * parameters: binding-resource, mapped-class, in-file, comp-file. Unmarshals
40  * documents from files using the binding defined for the mapped class, then
41  * marshals them back out using the same bindings and compares the results to
42  * the comparison file. This form of test is intended for asymmetric bindings,
43  * where the output differs from the input. In case of a comparison error the
44  * output file is left as <i>temp.xml</i>.
45  *
46  * @author Dennis M. Sosnoski
47  * @version 1.0
48  */

49
50 public class TestLoaderDiff
51 {
52     private static final Class JavaDoc[] RUNNER_PARAM_TYPES =
53     {
54         String JavaDoc.class, String JavaDoc.class, String JavaDoc.class
55     };
56     
57     private TestLoaderDiff() {}
58     
59     public static void main(String JavaDoc[] args)
60         throws ClassNotFoundException JavaDoc, NoSuchMethodException JavaDoc {
61         if (args.length >= 4 && args.length % 4 == 0) {
62             
63             // delete generated output file if present
64
File JavaDoc temp = new File JavaDoc("temp.xml");
65             if (temp.exists()) {
66                 temp.delete();
67             }
68             
69             // process each set of three arguments
70
boolean err = false;
71             int offset = 0;
72             String JavaDoc[] pargs = new String JavaDoc[3];
73             ClassLoader JavaDoc base = Thread.currentThread().getContextClassLoader();
74             for (; offset < args.length; offset += 4) {
75                 try {
76                 
77                     // load the test runner class using new custom class loader
78
Thread.currentThread().setContextClassLoader(base);
79                     Loader loader = new Loader();
80                     loader.loadResourceBinding(args[offset]);
81                     
82                     // invoke the "runTest" method of the runner class
83
Thread.currentThread().setContextClassLoader(loader);
84                     Class JavaDoc clas = loader.loadClass("org.jibx.match.TestRunner");
85                     Method JavaDoc test = clas.getDeclaredMethod("runTest",
86                         RUNNER_PARAM_TYPES);
87                     pargs[0] = args[offset+1];
88                     pargs[1] = args[offset+2];
89                     pargs[2] = args[offset+3];
90                     Boolean JavaDoc result = (Boolean JavaDoc)test.invoke(null, pargs);
91                     if (!result.booleanValue()) {
92                         err = true;
93                         break;
94                     }
95                 } catch (InvocationTargetException JavaDoc ex) {
96                     ex.getTargetException().printStackTrace();
97                     err = true;
98                     break;
99                 } catch (Exception JavaDoc ex) {
100                     ex.printStackTrace();
101                     err = true;
102                     break;
103                 }
104             }
105             
106             // take error exit if difference found
107
if (err) {
108                 System.err.println("Error on argument set: " +
109                     args[offset] + ", " + args[offset+1] + ", " + args[offset+2] +
110                     ", " + args[offset+3]);
111                 System.err.println("File path " + temp.getAbsolutePath());
112                 System.exit(1);
113             }
114             
115         } else {
116             System.err.println("Requires arguments in sets of four:\n" +
117                 " binding-resource mapped-class in-file comp-file\n" +
118                 "Leaves output as temp.xml in case of error");
119             System.exit(1);
120         }
121     }
122 }
Popular Tags