1 26 27 package net.sourceforge.groboutils.codecoverage.v2.compiler; 28 29 import java.io.IOException ; 30 31 import junit.framework.Test; 32 import junit.framework.TestCase; 33 import junit.framework.TestSuite; 34 import net.sourceforge.groboutils.autodoc.v1.AutoDoc; 35 import net.sourceforge.groboutils.codecoverage.v2.BytecodeLoaderUtil; 36 import net.sourceforge.groboutils.codecoverage.v2.CCCreatorUtil; 37 import net.sourceforge.groboutils.codecoverage.v2.datastore.AnalysisModuleSet; 38 import net.sourceforge.groboutils.codecoverage.v2.datastore.ClassRecord; 39 40 41 48 public class ModifiedClassUTest extends TestCase 49 { 50 53 private static final Class THIS_CLASS = ModifiedClassUTest.class; 54 private static final AutoDoc DOC = new AutoDoc( THIS_CLASS ); 55 56 public ModifiedClassUTest( String name ) 57 { 58 super( name ); 59 } 60 61 62 65 66 public void testConstructor1() throws Exception 67 { 68 try 69 { 70 new ModifiedClass( null, null ); 71 fail( "Did not throw IllegalArgumentException." ); 72 } 73 catch (IllegalArgumentException iae) 74 { 75 } 77 } 78 79 80 public void testConstructor2() throws Exception 81 { 82 try 83 { 84 new ModifiedClass( "", null ); 85 fail( "Did not throw IllegalArgumentException." ); 86 } 87 catch (IllegalArgumentException iae) 88 { 89 } 91 } 92 93 94 public void testConstructor3() throws Exception 95 { 96 try 97 { 98 new ModifiedClass( null, new byte[0] ); 99 fail( "Did not throw IllegalArgumentException." ); 100 } 101 catch (IllegalArgumentException iae) 102 { 103 } 105 } 106 107 108 public void testConstructor4() throws Exception 109 { 110 try 111 { 112 new ModifiedClass( null, null, null ); 113 fail( "Did not throw IllegalArgumentException." ); 114 } 115 catch (IllegalArgumentException iae) 116 { 117 } 119 } 120 121 122 public void testConstructor5() throws Exception 123 { 124 try 125 { 126 new ModifiedClass( null, "", null ); 127 fail( "Did not throw IllegalArgumentException." ); 128 } 129 catch (IllegalArgumentException iae) 130 { 131 } 133 } 134 135 136 public void testConstructor6() throws Exception 137 { 138 try 139 { 140 new ModifiedClass( null, null, new byte[0] ); 141 fail( "Did not throw IllegalArgumentException." ); 142 } 143 catch (IllegalArgumentException iae) 144 { 145 } 147 } 148 149 150 public void testConstructor7() throws Exception 151 { 152 try 153 { 154 new ModifiedClass( new ParseCoverageLogger(), null, null ); 155 fail( "Did not throw IllegalArgumentException." ); 156 } 157 catch (IllegalArgumentException iae) 158 { 159 } 161 } 162 163 164 public void testConstructor8() throws Exception 165 { 166 try 167 { 168 new ModifiedClass( new ParseCoverageLogger(), "", null ); 169 fail( "Did not throw IllegalArgumentException." ); 170 } 171 catch (IllegalArgumentException iae) 172 { 173 } 175 } 176 177 178 public void testConstructor9() throws Exception 179 { 180 try 181 { 182 new ModifiedClass( new ParseCoverageLogger(), null, new byte[0] ); 183 fail( "Did not throw IllegalArgumentException." ); 184 } 185 catch (IllegalArgumentException iae) 186 { 187 } 189 } 190 191 192 public void testConstructor10() throws Exception 193 { 194 try 195 { 196 new ModifiedClass( new ParseCoverageLogger(), "", new byte[0] ); 197 fail( "Did not throw an exception." ); 198 } 199 catch (IOException io) 200 { 201 } 203 } 204 205 206 public void testConstructor11() throws Exception 207 { 208 String filename = BytecodeLoaderUtil.getClassFilename( THIS_CLASS ); 209 new ModifiedClass( filename, 210 BytecodeLoaderUtil.loadBytecode( filename ) ); 211 } 212 213 214 public void testConstructor12() throws Exception 215 { 216 String filename = BytecodeLoaderUtil.getClassFilename( THIS_CLASS ); 217 new ModifiedClass( new ParseCoverageLogger(), filename, 218 BytecodeLoaderUtil.loadBytecode( filename ) ); 219 } 220 221 222 public void testGetClassName1() throws Exception 223 { 224 ModifiedClass mc = createModifiedClass( THIS_CLASS ); 225 assertEquals( 226 "Created class and returned classname don't match.", 227 THIS_CLASS.getName(), 228 mc.getClassName() ); 229 } 230 231 232 public void testGetClassCRC1() throws Exception 233 { 234 ModifiedClass mc = createModifiedClass( THIS_CLASS ); 235 assertTrue( 236 "Checksum is zero or -1.", 237 mc.getClassCRC() != 0L && mc.getClassCRC() != -1L ); 238 } 239 240 241 public void testGetClassSignature1() throws Exception 242 { 243 ModifiedClass mc = createModifiedClass( THIS_CLASS ); 244 long crc = mc.getClassCRC(); 245 String name = THIS_CLASS.getName(); 246 assertTrue( 247 "Does not contain the class name.", 248 mc.getClassSignature().indexOf( name ) >= 0 ); 249 assertTrue( 250 "Does not contain the class CRC.", 251 mc.getClassSignature().indexOf( Long.toString( crc ) ) >= 0 ); 252 } 253 254 255 public void testCreateClassRecord1() throws Exception 256 { 257 ModifiedClass mc = createModifiedClass( THIS_CLASS ); 258 ClassRecord cr = mc.createClassRecord( createAnalysisModuleSet() ); 259 assertNotNull( 260 "Returned null class record.", 261 cr ); 262 assertEquals( 263 "Mismatch class name.", 264 THIS_CLASS.getName(), 265 cr.getClassName() ); 266 assertEquals( 267 "Mismatch class CRC.", 268 mc.getClassCRC(), 269 cr.getClassCRC() ); 270 assertEquals( 271 "Mismatch signature.", 272 mc.getClassSignature(), 273 cr.getClassSignature() ); 274 assertEquals( 275 "Mismatch method count.", 276 mc.getMethods().length, 277 cr.getMethodCount() ); 278 } 279 280 281 public void testGetMethods1() throws Exception 282 { 283 ModifiedClass mc = createModifiedClass( THIS_CLASS ); 284 ModifiedMethod[] mmL = mc.getMethods(); 285 assertNotNull( 286 "Returned null method list.", 287 mmL ); 288 295 } 296 297 298 public void testGetModifiedClass1() throws Exception 299 { 300 ModifiedClass mc = createModifiedClass( THIS_CLASS ); 301 byte[] orig = BytecodeLoaderUtil.loadBytecode( 302 BytecodeLoaderUtil.getClassFilename( THIS_CLASS ) ); 303 byte[] c = mc.getModifiedClass(); 304 assertNotNull( 305 "Returned null class bytecode.", 306 c ); 307 assertTrue( 308 "Returned smaller length bytecode than the original.", 309 c.length >= orig.length ); 310 } 311 312 313 314 315 316 319 320 protected ModifiedClass createModifiedClass( Class c ) 321 throws IOException 322 { 323 return CCCreatorUtil.createModifiedClass( c ); 324 } 325 326 protected AnalysisModuleSet createAnalysisModuleSet() 327 { 328 return CCCreatorUtil.createAnalysisModuleSet( 2 ); 329 } 330 331 334 335 public static Test suite() 336 { 337 TestSuite suite = new TestSuite( THIS_CLASS ); 338 339 return suite; 340 } 341 342 public static void main( String [] args ) 343 { 344 String [] name = { THIS_CLASS.getName() }; 345 346 349 junit.textui.TestRunner.main( name ); 350 } 351 352 353 357 protected void setUp() throws Exception 358 { 359 super.setUp(); 360 361 } 363 364 365 369 protected void tearDown() throws Exception 370 { 371 373 374 super.tearDown(); 375 } 376 } 377 378 | Popular Tags |