1 26 27 package net.sourceforge.groboutils.codecoverage.v2.compiler; 28 29 import junit.framework.Test; 30 import junit.framework.TestSuite; 31 import net.sourceforge.groboutils.autodoc.v1.AutoDoc; 32 import net.sourceforge.groboutils.codecoverage.v2.BCELCreatorUtil; 33 import net.sourceforge.groboutils.codecoverage.v2.CCCreatorUtil; 34 import net.sourceforge.groboutils.junit.v1.SubTestTestCase; 35 36 import org.apache.bcel.classfile.JavaClass; 37 import org.apache.bcel.classfile.Method; 38 import org.apache.bcel.generic.MethodGen; 39 40 41 48 public class ModifiedMethodUTest extends SubTestTestCase 49 { 50 53 private static final Class THIS_CLASS = ModifiedMethodUTest.class; 54 private static final AutoDoc DOC = new AutoDoc( THIS_CLASS ); 55 56 public ModifiedMethodUTest( String name ) 57 { 58 super( name ); 59 } 60 61 62 65 public void testConstructor1() throws Exception 66 { 67 try 68 { 69 new ModifiedMethod( (short)0, 0, 0, null, null, null ); 70 fail( "Did not throw IllegalArgumentException." ); 71 } 72 catch (IllegalArgumentException e) 73 { 74 } 76 } 77 78 79 public void testConstructor2() throws Exception 80 { 81 try 82 { 83 new ModifiedMethod( (short)0, 0, 0, 84 createJavaClass( THIS_CLASS ), null, null ); 85 fail( "Did not throw IllegalArgumentException." ); 86 } 87 catch (IllegalArgumentException e) 88 { 89 } 91 } 92 93 94 public void testConstructor3() throws Exception 95 { 96 try 97 { 98 new ModifiedMethod( (short)0, 0, 0, 99 null, getMethod( createJavaClass( THIS_CLASS ), 0 ), null ); 100 fail( "Did not throw IllegalArgumentException." ); 101 } 102 catch (IllegalArgumentException e) 103 { 104 } 106 } 107 108 109 public void testConstructor4() throws Exception 110 { 111 try 112 { 113 new ModifiedMethod( (short)0, 0, 0, 114 null, null, 115 createMethodGen( createJavaClass( THIS_CLASS ), 0 ) ); 116 fail( "Did not throw IllegalArgumentException." ); 117 } 118 catch (IllegalArgumentException e) 119 { 120 } 122 } 123 124 125 public void testConstructor5() throws Exception 126 { 127 JavaClass jc = createJavaClass( THIS_CLASS ); 128 try 129 { 130 new ModifiedMethod( (short)0, 0, 0, 131 null, getMethod( jc, 0 ), createMethodGen( jc, 0 ) ); 132 fail( "Did not throw IllegalArgumentException." ); 133 } 134 catch (IllegalArgumentException e) 135 { 136 } 138 } 139 140 141 public void testConstructor6() throws Exception 142 { 143 JavaClass jc = createJavaClass( THIS_CLASS ); 144 try 145 { 146 new ModifiedMethod( (short)0, 0, 0, 147 jc, null, createMethodGen( jc, 0 ) ); 148 fail( "Did not throw IllegalArgumentException." ); 149 } 150 catch (IllegalArgumentException e) 151 { 152 } 154 } 155 156 157 public void testConstructor7() throws Exception 158 { 159 JavaClass jc = createJavaClass( THIS_CLASS ); 160 try 161 { 162 new ModifiedMethod( (short)0, 0, 0, 163 jc, getMethod( jc, 0 ), null ); 164 fail( "Did not throw IllegalArgumentException." ); 165 } 166 catch (IllegalArgumentException e) 167 { 168 } 170 } 171 172 173 public void testGetMethodName1() throws Exception 174 { 175 JavaClass jc = createJavaClass( THIS_CLASS ); 176 Method m = getMethod( jc, 2 ); 177 MethodGen mg = createMethodGen( jc, m ); 178 ModifiedMethod mm = createModifiedMethod( jc, 2, m, mg ); 179 assertEquals( 180 "Did not return correct method name.", 181 m.getName() + m.getSignature(), 182 mm.getMethodName() ); 183 } 184 185 public void testGetInstructionList1() throws Exception 186 { 187 JavaClass jc = createJavaClass( THIS_CLASS ); 188 Method m = getMethod( jc, 4 ); 189 MethodGen mg = createMethodGen( jc, m ); 190 ModifiedMethod mm = createModifiedMethod( jc, 4, m, mg ); 191 ModifiedInstructionList mil = mm.getInstructionList(); 192 assertNotNull( 193 "Returned null instruction list.", 194 mil ); 195 assertEquals( 196 "Instruction list size not correct.", 197 mg.getInstructionList().size(), 198 mil.getInstructionCount() ); 199 } 200 201 202 public void testGetOriginalClass1() throws Exception 203 { 204 JavaClass jc = createJavaClass( THIS_CLASS ); 205 Method m = getMethod( jc, 4 ); 206 MethodGen mg = createMethodGen( jc, m ); 207 ModifiedMethod mm = createModifiedMethod( jc, 4, m, mg ); 208 assertSame( 209 "Did not return the original java class.", 210 jc, 211 mm.getOriginalClass() ); 212 } 213 214 215 public void testGetOriginalMethod1() throws Exception 216 { 217 JavaClass jc = createJavaClass( THIS_CLASS ); 218 Method m = getMethod( jc, 4 ); 219 MethodGen mg = createMethodGen( jc, m ); 220 ModifiedMethod mm = createModifiedMethod( jc, 4, m, mg ); 221 assertSame( 222 "Did not return the original method.", 223 m, 224 mm.getOriginalMethod() ); 225 } 226 227 228 public void testCanAddMarks1() throws Exception 229 { 230 ModifiedMethod mm = createModifiedMethod( THIS_CLASS, 14 ); 231 assertTrue( mm.canAddMarks() ); 233 } 234 235 236 public void testGetModifiedMethodGen1() throws Exception 237 { 238 JavaClass jc = createJavaClass( THIS_CLASS ); 239 Method m = getMethod( jc, 4 ); 240 MethodGen mg = createMethodGen( jc, m ); 241 ModifiedMethod mm = createModifiedMethod( jc, 4, m, mg ); 242 assertSame( 243 "Did not return the original MethodGen.", 244 mg, 245 mm.getModifiedMethodGen() ); 246 } 247 248 249 public void testGetNewMethod1() throws Exception 250 { 251 ModifiedMethod mm = createModifiedMethod( THIS_CLASS, 7 ); 252 try 253 { 254 mm.getNewMethod(); 255 fail( "Did not throw IllegalStateException." ); 256 } 257 catch (IllegalStateException e) 258 { 259 } 261 } 262 263 264 public void testGetNewMethod2() throws Exception 265 { 266 JavaClass jc = createJavaClass( THIS_CLASS ); 267 Method m = getMethod( jc, 7 ); 268 MethodGen mg = createMethodGen( jc, m ); 269 ModifiedMethod mm = createModifiedMethod( jc, 7, m, mg ); 270 mm.close(); 271 Method m2 = mm.getNewMethod(); 272 assertNotSame( 273 "Returned the original object.", 274 m, m2 ); 275 assertEquals( 276 "Method name was changed.", 277 m.getName(), m2.getName() ); 278 assertEquals( 279 "Method signature was changed.", 280 m.getSignature(), m2.getSignature() ); 281 } 282 283 284 public void testClose1() throws Exception 285 { 286 ModifiedMethod mm = createModifiedMethod( THIS_CLASS, 1 ); 287 mm.close(); 288 try 289 { 290 mm.close(); 291 fail( "Did not throw IllegalStateException." ); 292 } 293 catch (IllegalStateException e) 294 { 295 } 297 } 298 299 300 public void testClose2() throws Exception 301 { 302 ModifiedMethod mm = createModifiedMethod( THIS_CLASS, 1 ); 303 mm.close(); 304 try 305 { 306 mm.getInstructionList(); 307 fail( "Did not throw IllegalStateException." ); 308 } 309 catch (IllegalStateException e) 310 { 311 } 313 } 314 315 316 public void testClose3() throws Exception 317 { 318 ModifiedMethod mm = createModifiedMethod( THIS_CLASS, 1 ); 319 mm.close(); 320 try 321 { 322 mm.getModifiedMethodGen(); 323 fail( "Did not throw IllegalStateException." ); 324 } 325 catch (IllegalStateException e) 326 { 327 } 329 } 330 331 332 335 336 protected ModifiedMethod createModifiedMethod( Class c, int methodIndex ) 337 throws Exception 338 { 339 return CCCreatorUtil.createModifiedMethod( c, methodIndex ); 340 } 341 342 protected ModifiedMethod createModifiedMethod( JavaClass jc, 343 int methodIndex, Method m, MethodGen mg ) 344 { 345 return CCCreatorUtil.createModifiedMethod( jc, methodIndex, m, mg ); 346 } 347 348 349 protected JavaClass createJavaClass( Class c ) throws Exception 350 { 351 return BCELCreatorUtil.createJavaClass( c ); 352 } 353 354 355 protected Method getMethod( JavaClass jc, int methodIndex ) 356 { 357 return BCELCreatorUtil.getMethod( jc, methodIndex ); 358 } 359 360 361 protected MethodGen createMethodGen( JavaClass jc, Method m ) 362 { 363 return BCELCreatorUtil.createMethodGen( jc, m ); 364 } 365 366 367 protected MethodGen createMethodGen( JavaClass jc, int methodIndex ) 368 { 369 return BCELCreatorUtil.createMethodGen( jc, methodIndex ); 370 } 371 372 373 376 377 public static Test suite() 378 { 379 TestSuite suite = new TestSuite( THIS_CLASS ); 380 381 return suite; 382 } 383 384 public static void main( String [] args ) 385 { 386 String [] name = { THIS_CLASS.getName() }; 387 388 391 junit.textui.TestRunner.main( name ); 392 } 393 394 395 399 protected void setUp() throws Exception 400 { 401 super.setUp(); 402 403 } 405 406 407 411 protected void tearDown() throws Exception 412 { 413 415 416 super.tearDown(); 417 } 418 } 419 420 | Popular Tags |