1 26 27 package net.sourceforge.groboutils.codecoverage.v2.compiler; 28 29 import java.io.ByteArrayOutputStream ; 30 import java.io.IOException ; 31 import java.util.HashMap ; 32 import java.util.LinkedList ; 33 import java.util.List ; 34 import java.util.Map ; 35 36 import junit.framework.Test; 37 import junit.framework.TestCase; 38 import junit.framework.TestSuite; 39 import net.sourceforge.groboutils.autodoc.v1.AutoDoc; 40 import net.sourceforge.groboutils.codecoverage.v2.BytecodeLoaderUtil; 41 import net.sourceforge.groboutils.codecoverage.v2.CCCreatorUtil; 42 import net.sourceforge.groboutils.codecoverage.v2.IAnalysisModule; 43 import net.sourceforge.groboutils.codecoverage.v2.datastore.ClassRecord; 44 import net.sourceforge.groboutils.codecoverage.v2.datastore.IClassMetaDataWriter; 45 import net.sourceforge.groboutils.codecoverage.v2.datastore.IMetaDataWriter; 46 import net.sourceforge.groboutils.codecoverage.v2.logger.TestLogger; 47 48 49 56 public class PostCompileClassUTest extends TestCase 57 { 58 61 private static final Class THIS_CLASS = PostCompileClassUTest.class; 62 private static final AutoDoc DOC = new AutoDoc( THIS_CLASS ); 63 64 public PostCompileClassUTest( String name ) 65 { 66 super( name ); 67 } 68 69 70 73 public void testConstructor1() 74 { 75 try 76 { 77 new PostCompileClass( null, null ); 78 fail( "Did not throw IllegalArgumentException." ); 79 } 80 catch (IllegalArgumentException ex) 81 { 82 } 84 } 85 86 public void testConstructor2() 87 { 88 try 89 { 90 new PostCompileClass( createIMetaDataWriter(), null ); 91 fail( "Did not throw IllegalArgumentException." ); 92 } 93 catch (IllegalArgumentException ex) 94 { 95 } 97 } 98 99 public void testConstructor3() 100 { 101 try 102 { 103 new PostCompileClass( null, createAnalysisModules() ); 104 fail( "Did not throw IllegalArgumentException." ); 105 } 106 catch (IllegalArgumentException ex) 107 { 108 } 110 } 111 112 public void testConstructor4() 113 { 114 try 115 { 116 new PostCompileClass( null, new IAnalysisModule[1] ); 117 fail( "Did not throw IllegalArgumentException." ); 118 } 119 catch (IllegalArgumentException ex) 120 { 121 } 123 } 124 125 public void testConstructor5() 126 { 127 try 128 { 129 new PostCompileClass( createIMetaDataWriter(), 130 new IAnalysisModule[1] ); 131 fail( "Did not throw IllegalArgumentException." ); 132 } 133 catch (IllegalArgumentException ex) 134 { 135 } 137 } 138 139 public void testConstructor6() 140 { 141 new PostCompileClass( createIMetaDataWriter(), 142 createAnalysisModules() ); 143 } 144 145 147 public void testConstructor7() 148 { 149 try 150 { 151 new PostCompileClass( null, null, null ); 152 fail( "Did not throw IllegalArgumentException." ); 153 } 154 catch (IllegalArgumentException ex) 155 { 156 } 158 } 159 160 public void testConstructor8() 161 { 162 try 163 { 164 new PostCompileClass( null, createIMetaDataWriter(), null ); 165 fail( "Did not throw IllegalArgumentException." ); 166 } 167 catch (IllegalArgumentException ex) 168 { 169 } 171 } 172 173 public void testConstructor9() 174 { 175 try 176 { 177 new PostCompileClass( null, null, createAnalysisModules() ); 178 fail( "Did not throw IllegalArgumentException." ); 179 } 180 catch (IllegalArgumentException ex) 181 { 182 } 184 } 185 186 public void testConstructor10() 187 { 188 try 189 { 190 new PostCompileClass( null, null, new IAnalysisModule[1] ); 191 fail( "Did not throw IllegalArgumentException." ); 192 } 193 catch (IllegalArgumentException ex) 194 { 195 } 197 } 198 199 public void testConstructor11() 200 { 201 try 202 { 203 new PostCompileClass( null, createIMetaDataWriter(), 204 new IAnalysisModule[1] ); 205 fail( "Did not throw IllegalArgumentException." ); 206 } 207 catch (IllegalArgumentException ex) 208 { 209 } 211 } 212 213 public void testConstructor12() 214 { 215 try 216 { 217 new PostCompileClass( null, createIMetaDataWriter(), 218 createAnalysisModules() ); 219 fail( "Did not throw IllegalArgumentException." ); 220 } 221 catch (IllegalArgumentException ex) 222 { 223 } 225 } 226 227 public void testConstructor13() 228 { 229 try 230 { 231 new PostCompileClass( createParseCoverageLogger(), 232 createIMetaDataWriter(), null ); 233 fail( "Did not throw IllegalArgumentException." ); 234 } 235 catch (IllegalArgumentException ex) 236 { 237 } 239 } 240 241 public void testConstructor14() 242 { 243 try 244 { 245 new PostCompileClass( createParseCoverageLogger(), 246 null, createAnalysisModules() ); 247 fail( "Did not throw IllegalArgumentException." ); 248 } 249 catch (IllegalArgumentException ex) 250 { 251 } 253 } 254 255 public void testConstructor15() 256 { 257 try 258 { 259 new PostCompileClass( createParseCoverageLogger(), 260 null, new IAnalysisModule[1] ); 261 fail( "Did not throw IllegalArgumentException." ); 262 } 263 catch (IllegalArgumentException ex) 264 { 265 } 267 } 268 269 public void testConstructor16() 270 { 271 try 272 { 273 new PostCompileClass( createParseCoverageLogger(), 274 createIMetaDataWriter(), new IAnalysisModule[1] ); 275 fail( "Did not throw IllegalArgumentException." ); 276 } 277 catch (IllegalArgumentException ex) 278 { 279 } 281 } 282 283 public void testConstructor17() 284 { 285 new PostCompileClass( createParseCoverageLogger(), 286 createIMetaDataWriter(), createAnalysisModules() ); 287 } 288 289 291 private static class Sample1 {} 292 public void testPostCompile1() 293 throws Exception 294 { 295 PostCompileClass pcc = new PostCompileClass( 296 createParseCoverageLogger(), createIMetaDataWriter(), 297 createAnalysisModules() ); 298 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 299 String filename = getClassFilename( Sample1.class ); 300 byte[] bytecode = loadBytecode( filename ); 301 302 pcc.postCompile( filename, bytecode, baos ); 303 assertTrue( 304 "Output bytecode is empty.", 305 baos.toByteArray().length > 0 ); 306 } 307 308 309 public void testPostCompile2() 310 throws Exception 311 { 312 PostCompileClass pcc = new PostCompileClass( 313 createIMetaDataWriter(), createAnalysisModules() ); 314 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 315 String filename = getClassFilename( Sample1.class ); 316 byte[] bytecode = loadBytecode( filename ); 317 318 pcc.postCompile( filename, bytecode, baos ); 319 assertTrue( 320 "Output bytecode is empty.", 321 baos.toByteArray().length > 0 ); 322 } 323 324 325 private static abstract class Sample3 326 { 327 public abstract void a(); 328 } 329 public void testPostCompile3() 330 throws Exception 331 { 332 PostCompileClass pcc = new PostCompileClass( 333 createIMetaDataWriter(), createAnalysisModules() ); 334 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 335 String filename = getClassFilename( Sample3.class ); 336 byte[] bytecode = loadBytecode( filename ); 337 338 pcc.postCompile( filename, bytecode, baos ); 339 assertTrue( 340 "Output bytecode is empty.", 341 baos.toByteArray().length > 0 ); 342 } 343 344 345 346 349 350 public static class MyClassMetaDataWriter implements IClassMetaDataWriter 351 { 352 private List records; 353 public MyClassMetaDataWriter( List l ) 354 { 355 this.records = l; 356 } 357 public void writeClassRecord( ClassRecord cr ) 358 { 359 this.records.add( cr ); 360 } 361 public void close() 362 { 363 this.records = null; 364 } 365 } 366 367 368 public static class MyMetaDataWriter implements IMetaDataWriter 369 { 370 public Map modulesToClassRecordList = new HashMap (); 371 372 public IClassMetaDataWriter getClassWriter( IAnalysisModule module ) 373 { 374 List list = (List )this.modulesToClassRecordList.get( module ); 375 if (list == null) 376 { 377 list = new LinkedList (); 378 this.modulesToClassRecordList.put( module, list ); 379 } 380 return new MyClassMetaDataWriter( list ); 381 } 382 public void close() {} 383 } 384 385 387 protected ParseCoverageLogger createParseCoverageLogger() 388 { 389 return TestLogger.createPCL(); 390 } 391 392 protected MyMetaDataWriter createIMetaDataWriter() 393 { 394 return new MyMetaDataWriter(); 395 } 396 397 398 protected IAnalysisModule[] createAnalysisModules() 399 { 400 return new IAnalysisModule[] { 401 CCCreatorUtil.createIAnalysisModule( 2 ), 402 CCCreatorUtil.createIAnalysisModule( 3 ), 403 CCCreatorUtil.createIAnalysisModule( 4 ), 404 }; 405 } 406 407 protected static String getClassFilename( Class c ) 408 { 409 return BytecodeLoaderUtil.getClassFilename( c ); 410 } 411 412 protected static byte[] loadBytecode( String filename ) throws IOException 413 { 414 return BytecodeLoaderUtil.loadBytecode( filename ); 415 } 416 417 418 421 422 public static Test suite() 423 { 424 TestSuite suite = new TestSuite( THIS_CLASS ); 425 426 return suite; 427 } 428 429 public static void main( String [] args ) 430 { 431 String [] name = { THIS_CLASS.getName() }; 432 433 436 junit.textui.TestRunner.main( name ); 437 } 438 439 440 444 protected void setUp() throws Exception 445 { 446 super.setUp(); 447 448 } 450 451 452 456 protected void tearDown() throws Exception 457 { 458 460 461 super.tearDown(); 462 } 463 } 464 465 | Popular Tags |