1 28 29 package org.jibx.match; 30 31 import java.io.ByteArrayInputStream ; 32 import java.io.ByteArrayOutputStream ; 33 import java.io.File ; 34 import java.io.FileInputStream ; 35 import java.io.FileOutputStream ; 36 import java.io.IOException ; 37 import java.io.InputStreamReader ; 38 39 import org.jibx.extras.*; 40 import org.jibx.runtime.*; 41 import org.xmlpull.v1.XmlPullParserException; 42 43 import multiple.*; 44 45 56 57 public class TestTables { 58 59 protected static boolean runTest(String bin, String fin, String bout, 60 String cout) throws IOException , JiBXException, XmlPullParserException { 61 62 IBindingFactory ibf = null; 64 IBindingFactory obf = null; 65 Class 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 IUnmarshallingContext uctx = ibf.createUnmarshallingContext(); 82 Object obj = uctx.unmarshalDocument(new FileInputStream (fin), null); 83 if (!target.isInstance(obj)) { 84 System.err.println("Unmarshalled result not expected type"); 85 return false; 86 } 87 88 IMarshallingContext mctx = obf.createMarshallingContext(); 90 ByteArrayOutputStream bos = new ByteArrayOutputStream (); 91 mctx.setIndent(1, "\n", ' '); 92 mctx.marshalDocument(obj, "UTF-8", null, bos); 93 94 InputStreamReader brdr = new InputStreamReader 96 (new ByteArrayInputStream (bos.toByteArray()), "UTF-8"); 97 InputStreamReader frdr = new InputStreamReader 98 (new FileInputStream (cout), "UTF-8"); 99 DocumentComparator comp = new DocumentComparator(System.err); 100 if (comp.compare(frdr, brdr)) { 101 return true; 102 } else { 103 104 try { 106 File fout = new File ("temp.xml"); 107 fout.delete(); 108 FileOutputStream fos = new FileOutputStream (fout); 109 fos.write(bos.toByteArray()); 110 fos.close(); 111 } catch (IOException 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 [] args) { 120 if (args.length >= 4 && args.length % 4 == 0) { 121 122 File temp = new File ("temp.xml"); 124 if (temp.exists()) { 125 temp.delete(); 126 } 127 128 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 ex) { 139 System.err.println("Exception: " + ex.getMessage()); 140 ex.printStackTrace(); 141 err = true; 142 break; 143 } 144 } 145 146 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 |