KickJava   Java API By Example, From Geeks To Geeks.

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


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.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 three input
39  * parameters: binding-resource, mapped-class, in-file. Unmarshals documents
40  * from files using the binding defined for the mapped class, then marshals them
41  * back out using the same bindings and compares the results. In case of a
42  * comparison error the output file is left as <i>temp.xml</i>.
43  *
44  * @author Dennis M. Sosnoski
45  * @version 1.0
46  */

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