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 TestRunner 56 { 57 private TestRunner() {} 58 59 public static Boolean runTest(String mname, String fin, String fcomp, 60 boolean save) 61 throws IOException , JiBXException, XmlPullParserException { 62 63 Class mclas; 65 try { 66 mclas = TestSimple.class.getClassLoader().loadClass(mname); 67 } catch (ClassNotFoundException ex) { 68 System.err.println("Class " + mname + " not found"); 69 return Boolean.FALSE; 70 } 71 IBindingFactory bfact = BindingDirectory.getFactory(mclas); 72 73 IUnmarshallingContext uctx = bfact.createUnmarshallingContext(); 75 Object obj = uctx.unmarshalDocument(new FileInputStream (fin), fin, null); 76 if (!mclas.isInstance(obj)) { 77 System.err.println("Unmarshalled result not expected type"); 78 return Boolean.FALSE; 79 } 80 81 String enc = ((UnmarshallingContext)uctx).getInputEncoding(); 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 (fcomp), enc); 95 DocumentComparator comp = new DocumentComparator(System.err); 96 boolean match = comp.compare(frdr, brdr); 97 if (!match || save) { 98 99 try { 101 File fout = new File ("temp.xml"); 102 fout.delete(); 103 FileOutputStream fos = new FileOutputStream (fout); 104 fos.write(bos.toByteArray()); 105 fos.close(); 106 } catch (IOException ex) { 107 System.err.println("Error writing to temp.xml: " + 108 ex.getMessage()); 109 } 110 } 111 return match ? Boolean.TRUE : Boolean.FALSE; 112 } 113 114 public static Boolean runTest(String mname, String fin, String fcomp) 115 throws IOException , JiBXException, XmlPullParserException { 116 return runTest(mname, fin, fcomp, false); 117 } 118 } 119 | Popular Tags |