1 28 29 package org.jibx.extras; 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.FileReader ; 37 import java.io.IOException ; 38 import java.io.InputStreamReader ; 39 import java.net.URL ; 40 import java.net.URLClassLoader ; 41 42 import org.jibx.runtime.*; 43 import org.xmlpull.v1.XmlPullParserException; 44 45 58 59 public class TestRoundtrip { 60 61 protected static boolean runTest(String mname, String fin, String fout) 62 throws IOException , JiBXException, XmlPullParserException { 63 64 Class mclas; 66 try { 67 URL [] urls = new URL [] { new File (".").toURL() }; 68 ClassLoader parent = Thread.currentThread().getContextClassLoader(); 69 if (parent == null) { 70 parent = TestRoundtrip.class.getClassLoader(); 71 } 72 ClassLoader loader = new URLClassLoader (urls, parent); 73 Thread.currentThread().setContextClassLoader(loader); 74 mclas = loader.loadClass(mname); 75 } catch (ClassNotFoundException ex) { 76 System.err.println("Class " + mname + " not found"); 77 return false; 78 } 79 IBindingFactory bfact = BindingDirectory.getFactory(mclas); 80 81 IUnmarshallingContext uctx = bfact.createUnmarshallingContext(); 83 Object obj = uctx.unmarshalDocument(new FileInputStream (fin), null); 84 if (!mclas.isInstance(obj)) { 85 System.err.println("Unmarshalled result not expected type"); 86 return false; 87 } 88 89 IMarshallingContext mctx = bfact.createMarshallingContext(); 91 mctx.setIndent(2); 92 ByteArrayOutputStream bos = new ByteArrayOutputStream (); 93 mctx.marshalDocument(obj, "UTF-8", null, bos); 94 95 InputStreamReader brdr = new InputStreamReader 97 (new ByteArrayInputStream (bos.toByteArray()), "UTF-8"); 98 FileReader frdr = new FileReader (fout); 99 DocumentComparator comp = new DocumentComparator(System.err); 100 if (comp.compare(frdr, brdr)) { 101 return true; 102 } else { 103 104 try { 106 FileOutputStream fos = new FileOutputStream ("temp.xml"); 107 fos.write(bos.toByteArray()); 108 fos.close(); 109 } catch (IOException ex) { 110 System.err.println("Error writing to temp.xml: " + 111 ex.getMessage()); 112 } 113 return false; 114 } 115 } 116 117 public static void main(String [] args) { 118 if (args.length == 2 || (args.length > 0 && args.length % 3 == 0)) { 119 120 File temp = new File ("temp.xml"); 122 if (temp.exists()) { 123 temp.delete(); 124 } 125 126 int base = 0; 128 boolean err = false; 129 String fin = null; 130 String fout = null; 131 while (base < args.length) { 132 133 fin = args[base+1]; 135 fout = (args.length < 3) ? fin : args[base+2]; 136 try { 137 if (!runTest(args[base], fin, fout)) { 138 err = true; 139 } 140 } catch (Exception ex) { 141 ex.printStackTrace(); 142 err = true; 144 } 145 146 if (err) { 148 System.err.println("Error round-tripping class: " + args[base] + 149 "\n with input file " + fin + " and output compared to " + 150 fout); 151 System.err.println("Saved output document file path " + 152 temp.getAbsolutePath()); 153 System.exit(1); 154 } 155 156 base += 3; 158 } 159 160 } else { 161 System.err.println("Usage: java TestRoundtrip mapped-class" + 162 " in-file [out-file]\n where out-file is only required if the" + 163 " output document is different from\nthe input document. " + 164 "Leaves output as temp.xml in case of error"); 165 System.exit(1); 166 } 167 } 168 } 169 170 | Popular Tags |