KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > example15 > Test


1 /*
2 Copyright (c) 2003, 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 example15;
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.FileReader JavaDoc;
37 import java.io.InputStreamReader JavaDoc;
38
39 import org.jibx.extras.DocumentComparator;
40 import org.jibx.runtime.*;
41
42
43 /**
44  * Test program for the JiBX framework. Works with two or three command line
45  * arguments: mapped-class, in-file, and out-file (optional, only needed if
46  * different from in-file). You can also supply a multiple of three input
47  * arguments, in which case each set of three is processed in turn (in this case
48  * the out-file is required). Unmarshals documents from files using the binding
49  * defined for the mapped class, then marshals them back out using the same
50  * bindings and compares the results. In case of a comparison error the output
51  * file is left as <i>temp.xml</i>.
52  *
53  * @author Dennis M. Sosnoski
54  * @version 1.0
55  */

56
57 public class Test {
58     
59     // definitions for version attribute on document root element
60
private static final String JavaDoc VERSION_URI = null;
61     private static final String JavaDoc VERSION_NAME = "version";
62     
63     // attribute text strings used for different document versions
64
private static String JavaDoc[] VERSION_TEXTS = {
65         "1.0", "1.1", "1.2"
66     };
67     
68     // binding names corresponding to text strings
69
private static String JavaDoc[] VERSION_BINDINGS = {
70         "binding0", "binding1", "binding2"
71     };
72
73     public static void main(String JavaDoc[] args) {
74         if (args.length == 1) {
75             
76             // delete generated output file if present
77
File JavaDoc temp = new File JavaDoc("temp.xml");
78             if (temp.exists()) {
79                 temp.delete();
80             }
81             try {
82                 
83                 // process input file according to declared version
84
BindingSelector select = new BindingSelector(VERSION_URI,
85                     VERSION_NAME, VERSION_TEXTS, VERSION_BINDINGS);
86                 IUnmarshallingContext context = select.getContext();
87                 context.setDocument(new FileInputStream JavaDoc(args[0]), null);
88                 Customer customer = (Customer)select.
89                     unmarshalVersioned(Customer.class);
90                 
91                 // now marshal to in-memory array with same document version
92
ByteArrayOutputStream JavaDoc bos = new ByteArrayOutputStream JavaDoc();
93                 select.setOutput(bos, "UTF-8");
94                 select.marshalVersioned(customer, customer.version);
95                 
96                 // run comparison of output with original document
97
InputStreamReader JavaDoc brdr = new InputStreamReader JavaDoc
98                     (new ByteArrayInputStream JavaDoc(bos.toByteArray()), "UTF-8");
99                 FileReader JavaDoc frdr = new FileReader JavaDoc(args[0]);
100                 FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc("temp.xml");
101                 fos.write(bos.toByteArray());
102                 fos.close();
103                 DocumentComparator comp = new DocumentComparator(System.err);
104                 if (!comp.compare(frdr, brdr)) {
105                     
106                     // report mismatch with output saved to file
107
// FileOutputStream fos = new FileOutputStream("temp.xml");
108
// fos.write(bos.toByteArray());
109
// fos.close();
110
System.err.println("Error testing on input file " + args[0]);
111                     System.err.println("Saved output document file path " +
112                         temp.getAbsolutePath());
113                     System.exit(1);
114                 }
115                 
116             } catch (Exception JavaDoc e) {
117                 e.printStackTrace();
118             }
119             
120         } else {
121             System.err.println("Usage: java exampl15.Test in-file\n" +
122                 "Leaves output as temp.xml in case of error");
123             System.exit(1);
124         }
125     }
126 }
127
128
Popular Tags