1 30 package org.objectweb.asm; 31 32 import java.io.File ; 33 import java.io.FileInputStream ; 34 import java.io.InputStream ; 35 import java.io.PrintWriter ; 36 import java.io.StringWriter ; 37 import java.util.Arrays ; 38 import java.util.Enumeration ; 39 import java.util.zip.ZipEntry ; 40 import java.util.zip.ZipFile ; 41 42 import org.objectweb.asm.util.TraceClassVisitor; 43 44 import junit.framework.TestCase; 45 import junit.framework.TestSuite; 46 47 53 public abstract class AbstractTest extends TestCase { 54 55 protected String n; 56 57 protected InputStream is; 58 59 public AbstractTest() { 60 super("test"); 61 } 62 63 protected void init(final String n, final InputStream is) { 64 this.n = n; 65 this.is = is; 66 } 67 68 protected TestSuite getSuite() throws Exception { 69 TestSuite suite = new TestSuite(); 70 String files = System.getProperty("asm.test") + ","; 71 String clazz = System.getProperty("asm.test.class"); 72 while (files.indexOf(',') != -1) { 73 String file = files.substring(0, files.indexOf(',')); 74 files = files.substring(files.indexOf(',') + 1); 75 File f = new File (file); 76 if (f.isDirectory()) { 77 File [] fs = f.listFiles(); 78 for (int i = 0; i < fs.length; ++i) { 79 String n = fs[i].getName(); 80 if (n.endsWith(".class")) { 81 n = n.substring(0, n.length() - 6).replace('/', '.'); 82 if (clazz == null || n.indexOf(clazz) != -1) { 83 InputStream is = new FileInputStream (fs[i]); 84 AbstractTest t = (AbstractTest) getClass().newInstance(); 85 t.init(n, is); 86 suite.addTest(t); 87 } 88 } 89 } 90 } else { 91 ZipFile zip = new ZipFile (file); 92 Enumeration entries = zip.entries(); 93 while (entries.hasMoreElements()) { 94 ZipEntry e = (ZipEntry ) entries.nextElement(); 95 String n = e.getName(); 96 if (n.endsWith(".class")) { 97 n = n.substring(0, n.length() - 6).replace('/', '.'); 98 if (clazz == null || n.indexOf(clazz) != -1) { 99 InputStream is = zip.getInputStream(e); 100 AbstractTest t = (AbstractTest) getClass().newInstance(); 101 t.init(n, is); 102 suite.addTest(t); 103 } 104 } 105 } 106 } 107 } 108 return suite; 109 } 110 111 public abstract void test() throws Exception ; 112 113 public void assertEquals(final ClassReader cr1, final ClassReader cr2) 114 throws Exception 115 { 116 if (!Arrays.equals(cr1.b, cr2.b)) { 117 StringWriter sw1 = new StringWriter (); 118 StringWriter sw2 = new StringWriter (); 119 ClassVisitor cv1 = new TraceClassVisitor(new PrintWriter (sw1)); 120 ClassVisitor cv2 = new TraceClassVisitor(new PrintWriter (sw2)); 121 cr1.accept(new ClassFilter(cv1), false); 122 cr2.accept(new ClassFilter(cv2), false); 123 String s1 = sw1.toString(); 124 String s2 = sw2.toString(); 125 assertEquals("different data", s1, s2); 126 } 127 } 128 129 public String getName() { 130 return super.getName() + ": " + n; 131 } 132 133 135 static class ClassFilter extends ClassAdapter { 136 137 public ClassFilter(final ClassVisitor cv) { 138 super(cv); 139 } 140 141 public void visitAttribute(final Attribute attr) { 142 } 144 145 public FieldVisitor visitField( 146 final int access, 147 final String name, 148 final String desc, 149 final String signature, 150 final Object value) 151 { 152 return new FieldFilter(cv.visitField(access, 153 name, 154 desc, 155 signature, 156 value)); 157 } 158 159 public MethodVisitor visitMethod( 160 final int access, 161 final String name, 162 final String desc, 163 final String signature, 164 final String [] exceptions) 165 { 166 return new MethodFilter(cv.visitMethod(access, 167 name, 168 desc, 169 signature, 170 exceptions)); 171 } 172 } 173 174 static class MethodFilter extends MethodAdapter { 175 176 public MethodFilter(final MethodVisitor mv) { 177 super(mv); 178 } 179 180 public void visitAttribute(final Attribute attr) { 181 } 183 } 184 185 static class FieldFilter implements FieldVisitor { 186 187 FieldVisitor fv; 188 189 public FieldFilter(final FieldVisitor fv) { 190 this.fv = fv; 191 } 192 193 public AnnotationVisitor visitAnnotation( 194 final String desc, 195 final boolean visible) 196 { 197 return fv.visitAnnotation(desc, visible); 198 } 199 200 public void visitAttribute(final Attribute attr) { 201 } 203 204 public void visitEnd() { 205 fv.visitEnd(); 206 } 207 } 208 } 209 | Popular Tags |