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.jibx.runtime.impl.UnmarshallingContext; 42 import org.xmlpull.v1.XmlPullParserException; 43 44 54 55 public class TestSimple { 56 57 protected static boolean runTest(String mname, String fin) 58 throws IOException , JiBXException, XmlPullParserException { 59 60 Class mclas; 62 try { 63 mclas = TestSimple.class.getClassLoader().loadClass(mname); 64 } catch (ClassNotFoundException ex) { 65 System.err.println("Class " + mname + " not found"); 66 return false; 67 } 68 IBindingFactory bfact = BindingDirectory.getFactory(mclas); 69 70 IUnmarshallingContext uctx = bfact.createUnmarshallingContext(); 72 Object obj = uctx.unmarshalDocument(new FileInputStream (fin), null); 73 if (!mclas.isInstance(obj)) { 74 System.err.println("Unmarshalled result not expected type"); 75 return false; 76 } 77 78 String enc = ((UnmarshallingContext)uctx).getInputEncoding(); 80 if (enc == null) { 81 enc = "UTF-8"; 82 } 83 84 IMarshallingContext mctx = bfact.createMarshallingContext(); 86 ByteArrayOutputStream bos = new ByteArrayOutputStream (); 87 mctx.setIndent(2); 88 mctx.marshalDocument(obj, enc, null, bos); 89 90 InputStreamReader brdr = new InputStreamReader 92 (new ByteArrayInputStream (bos.toByteArray()), enc); 93 InputStreamReader frdr = new InputStreamReader 94 (new FileInputStream (fin), enc); 95 DocumentComparator comp = new DocumentComparator(System.err); 96 if (comp.compare(frdr, brdr)) { 97 return true; 98 } else { 99 100 try { 102 File fout = new File ("temp.xml"); 103 fout.delete(); 104 FileOutputStream fos = new FileOutputStream (fout); 105 fos.write(bos.toByteArray()); 106 fos.close(); 107 } catch (IOException ex) { 108 System.err.println("Error writing to temp.xml: " + 109 ex.getMessage()); 110 } 111 return false; 112 } 113 } 114 115 public static void main(String [] args) { 116 if (args.length >= 2 && args.length % 2 == 0) { 117 118 File temp = new File ("temp.xml"); 120 if (temp.exists()) { 121 temp.delete(); 122 } 123 124 boolean err = false; 126 int base = 0; 127 for (; base < args.length; base += 2) { 128 try { 129 if (!runTest(args[base], args[base+1])) { 130 err = true; 131 break; 132 } 133 } catch (Exception ex) { 134 ex.printStackTrace(); 135 err = true; 137 break; 138 } 139 } 140 141 if (err) { 143 System.err.println("Error on argument set: " + 144 args[base] + ", " + args[base+1]); 145 System.err.println("File path " + temp.getAbsolutePath()); 146 System.exit(1); 147 } 148 149 } else { 150 System.err.println("Requires arguments in sets of two:\n" + 151 " mapped-class in-file\n" + 152 "Leaves output as temp.xml in case of error"); 153 System.exit(1); 154 } 155 } 156 } 157 | Popular Tags |