1 18 package org.apache.activemq.openwire; 19 20 import java.io.ByteArrayInputStream ; 21 import java.io.ByteArrayOutputStream ; 22 import java.io.DataOutputStream ; 23 import java.io.File ; 24 import java.io.FileInputStream ; 25 import java.io.FileOutputStream ; 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.util.ArrayList ; 29 import java.util.Iterator ; 30 31 import junit.framework.Assert; 32 33 abstract public class DataFileGenerator extends Assert { 34 35 static final File moduleBaseDir; 36 static final File controlDir; 37 static final File classFileDir; 38 39 static { 40 moduleBaseDir = new File (System.getProperty("basedir", ".")); 41 controlDir = new File (moduleBaseDir, "src/test/resources/openwire-control"); 42 classFileDir = new File (moduleBaseDir, "src/test/java/org/apache/activemq/openwire"); 43 } 44 45 public static void main(String [] args) throws Exception { 46 generateControlFiles(); 47 } 48 49 56 public static ArrayList getAllDataFileGenerators() throws Exception { 57 ArrayList l = new ArrayList (); 59 File [] files = classFileDir.listFiles(); 60 for (int i = 0; files!=null && i < files.length; i++) { 61 File file = files[i]; 62 if( file.getName().endsWith("Data.java") ) { 63 String cn = file.getName(); 64 cn = cn.substring(0, cn.length() - ".java".length()); 65 Class clazz = DataFileGenerator.class.getClassLoader().loadClass("org.apache.activemq.openwire."+cn); 66 l.add((DataFileGenerator) clazz.newInstance()); 67 } 68 } 69 return l; 70 } 71 72 private static void generateControlFiles() throws Exception { 73 ArrayList generators = getAllDataFileGenerators(); 74 for (Iterator iter = generators.iterator(); iter.hasNext();) { 75 DataFileGenerator object = (DataFileGenerator) iter.next(); 76 try { 77 object.generateControlFile(); 79 } catch (Exception e) { 80 } 82 } 83 } 84 85 public void generateControlFile() throws Exception { 86 controlDir.mkdirs(); 87 File dataFile = new File (controlDir, getClass().getName()+".bin"); 88 89 OpenWireFormat wf = new OpenWireFormat(); 90 wf.setCacheEnabled(false); 91 wf.setStackTraceEnabled(false); 92 wf.setVersion(1); 93 94 FileOutputStream os = new FileOutputStream (dataFile); 95 DataOutputStream ds = new DataOutputStream (os); 96 wf.marshal(createObject(), ds); 97 ds.close(); 98 } 99 100 public InputStream generateInputStream() throws Exception { 101 OpenWireFormat wf = new OpenWireFormat(); 102 wf.setCacheEnabled(false); 103 wf.setStackTraceEnabled(false); 104 wf.setVersion(1); 105 106 ByteArrayOutputStream os = new ByteArrayOutputStream (); 107 DataOutputStream ds = new DataOutputStream (os); 108 wf.marshal(createObject(), ds); 109 ds.close(); 110 111 return new ByteArrayInputStream (os.toByteArray()); 112 } 113 114 public static void assertAllControlFileAreEqual() throws Exception { 115 ArrayList generators = getAllDataFileGenerators(); 116 for (Iterator iter = generators.iterator(); iter.hasNext();) { 117 DataFileGenerator object = (DataFileGenerator) iter.next(); 118 object.assertControlFileIsEqual(); 120 } 121 } 122 123 public void assertControlFileIsEqual() throws Exception { 124 File dataFile = new File (controlDir, getClass().getName()+".bin"); 125 FileInputStream is1 = new FileInputStream (dataFile); 126 int pos=0; 127 try { 128 InputStream is2 = generateInputStream(); 129 int a = is1.read(); 130 int b = is2.read(); 131 pos++; 132 assertEquals("Data does not match control file: "+dataFile+" at byte position "+pos,a,b); 133 while(a >= 0 && b>= 0) { 134 a = is1.read(); 135 b = is2.read(); 136 pos++; 137 assertEquals(a,b); 138 } 139 is2.close(); 140 } finally { 141 is1.close(); 142 } 143 } 144 145 abstract protected Object createObject() throws IOException ; 146 } 147 | Popular Tags |