1 26 27 package net.sourceforge.groboutils.codecoverage.v2.compiler; 28 29 import java.io.ByteArrayOutputStream ; 30 import java.io.File ; 31 import java.io.IOException ; 32 33 import junit.framework.Test; 34 import junit.framework.TestCase; 35 import junit.framework.TestSuite; 36 import net.sourceforge.groboutils.autodoc.v1.AutoDoc; 37 import net.sourceforge.groboutils.codecoverage.v2.BytecodeLoaderUtil; 38 import net.sourceforge.groboutils.codecoverage.v2.CCCreatorUtil; 39 import net.sourceforge.groboutils.codecoverage.v2.IAnalysisModule; 40 import net.sourceforge.groboutils.codecoverage.v2.datastore.DirMetaDataWriter; 41 import net.sourceforge.groboutils.codecoverage.v2.module.BranchCountMeasure; 42 import net.sourceforge.groboutils.codecoverage.v2.module.BytecodeCountMeasure; 43 import net.sourceforge.groboutils.codecoverage.v2.module.CallPairMeasure; 44 import net.sourceforge.groboutils.codecoverage.v2.module.FunctionMeasure; 45 import net.sourceforge.groboutils.codecoverage.v2.module.LineCountMeasure; 46 import net.sourceforge.groboutils.junit.v1.SubTestTestCase; 47 48 49 57 public class MultipleAnalyzersIUTest extends SubTestTestCase 58 { 59 62 private static final Class THIS_CLASS = MultipleAnalyzersIUTest.class; 63 private static final AutoDoc DOC = new AutoDoc( THIS_CLASS ); 64 65 public MultipleAnalyzersIUTest( String name ) 66 { 67 super( name ); 68 } 69 70 71 72 75 public static class MyLogger2 76 { 77 public static void cover( String classSig, 78 short methodIndex, short channel, short markIndex ) 79 { 80 System.out.println( "MyLogger2.cover" ); 81 DOC.getLog().info( getMeasureName( channel ) ); 82 } 83 } 84 85 86 private static final String COVER_METHOD_NAME = "cover"; 87 private static final String CLASSFILE_PATH = 88 "net/sourceforge/groboutils/codecoverage/v2/compiler/testcode/"; 89 private static final String CLASSNAME_PACKAGE = 90 "net.sourceforge.groboutils.codecoverage.v2.compiler.testcode."; 91 private static final String MAIN_SIG = "main([Ljava/lang/String;)V"; 92 93 94 95 public static final String [] TESTCLASSNAMES = { 96 "Main1", 97 "If1", 98 "If2", 99 "If3", 100 "Case1", 101 "Case2", 102 "Case3", 103 "Case4", 104 "Case5", 105 "Except1", 106 "Except2", 107 "Finally1", 108 "Finally2", 109 "Finally3", 110 "Finally4", 111 "Finally5", 112 "Synchronized1", 113 "Synchronized2", 114 "Synchronized3", 115 }; 116 public void testRebuildMain() 117 throws Exception 118 { 119 File metadir = CCCreatorUtil.createNewDirectory(); 120 for (int i = 0; i < TESTCLASSNAMES.length; ++i) 121 { 122 addSubTest( new Recompile( TESTCLASSNAMES[i], metadir ) ); 123 } 124 } 125 126 127 public static class Recompile extends TestCase 128 { 129 public String TESTNAME; 130 public File dir; 131 public Recompile( String testname, File dir ) 132 { 133 super( "testRecompile" ); 134 this.TESTNAME = testname; 135 this.dir = dir; 136 DOC.getLog().info("===== Adding test for "+testname); 137 } 138 public String getName() 139 { 140 return "testRecompile:"+TESTNAME; 141 } 142 public void testRecompile() throws Exception 143 { 144 File f = new File ( TESTNAME+".passed" ); 145 if (f.exists()) f.delete(); 146 147 String filename = CLASSFILE_PATH+TESTNAME+".class"; 148 String classname = CLASSNAME_PACKAGE+TESTNAME; 149 byte[] bytecode = compileClass( dir, filename ); 150 DOC.getLog().info( "Recompiled Classfile:" ); 153 RebuildClassIUTest.debugClass( bytecode, filename ); 154 155 BytecodeLoaderUtil.verifyClass( classname, bytecode ); 156 Class clazz = BytecodeLoaderUtil.loadClassFromBytecode( 157 classname, bytecode ); 158 BytecodeLoaderUtil.runMain( clazz ); 159 160 assertTrue( 162 TESTNAME+" failed: recompilation didn't work.", 163 f.exists() ); 164 } 165 } 166 167 168 169 170 173 174 protected static byte[] compileClass( File metadir, String filename ) 175 throws IOException 176 { 177 byte[] bytecode = null; 178 DirMetaDataWriter dmdw = new DirMetaDataWriter( metadir ); 179 try 180 { 181 PostCompileClass pcc = new PostCompileClass( 182 new ParseCoverageLogger( MyLogger2.class, 183 COVER_METHOD_NAME ), 184 dmdw, getAnalysisModules() ); 185 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 186 byte[] origBytecode = BytecodeLoaderUtil.loadBytecode( 187 filename ); 188 pcc.postCompile( filename, origBytecode, baos ); 189 bytecode = baos.toByteArray(); 190 } 191 finally 192 { 193 dmdw.close(); 194 } 195 return bytecode; 196 } 197 198 199 200 201 204 private static IAnalysisModule[] getAnalysisModules() 205 { 206 final IAnalysisModule[] amL = new IAnalysisModule[] { 207 new BranchCountMeasure(), 208 new BytecodeCountMeasure(), 209 new CallPairMeasure(), 210 new FunctionMeasure(), 211 new LineCountMeasure() 212 }; 213 return amL; 214 } 215 216 217 private static String getMeasureName( short i ) 218 { 219 if (i < 0) return null; 220 String s[] = getMeasureNames(); 221 if (s.length <= i) return null; 222 return s[i]; 223 } 224 225 226 private static String [] getMeasureNames() 227 { 228 IAnalysisModule[] am = getAnalysisModules(); 229 String s[] = new String [ am.length ]; 230 for (int i = 0; i < am.length; ++i) 231 { 232 s[i] = am[i].getMeasureName(); 233 } 234 return s; 235 } 236 237 238 239 240 243 244 public static Test suite() 245 { 246 TestSuite suite = new TestSuite( THIS_CLASS ); 247 248 return suite; 249 } 250 251 public static void main( String [] args ) 252 { 253 String [] name = { THIS_CLASS.getName() }; 254 255 258 junit.textui.TestRunner.main( name ); 259 } 260 261 262 266 protected void setUp() throws Exception 267 { 268 super.setUp(); 269 270 } 272 273 274 278 protected void tearDown() throws Exception 279 { 280 282 283 super.tearDown(); 284 } 285 } 286 287 | Popular Tags |