1 26 27 package net.sourceforge.groboutils.codecoverage.v2.compiler; 28 29 import junit.framework.Test; 30 import junit.framework.TestCase; 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.codecoverage.v2.IAnalysisModule; 35 import net.sourceforge.groboutils.codecoverage.v2.IMethodCodeUTestI; 36 import net.sourceforge.groboutils.codecoverage.v2.datastore.AnalysisModuleSet; 37 import net.sourceforge.groboutils.codecoverage.v2.datastore.ClassRecord; 38 import net.sourceforge.groboutils.junit.v1.iftc.CxFactory; 39 import net.sourceforge.groboutils.junit.v1.iftc.InterfaceTestSuite; 40 41 import org.apache.bcel.classfile.JavaClass; 42 import org.apache.bcel.classfile.Method; 43 import org.apache.bcel.generic.MethodGen; 44 45 46 53 public class DefaultMethodCodeUTest extends TestCase 54 { 55 58 private static final Class THIS_CLASS = DefaultMethodCodeUTest.class; 59 private static final AutoDoc DOC = new AutoDoc( THIS_CLASS ); 60 61 public DefaultMethodCodeUTest( String name ) 62 { 63 super( name ); 64 } 65 66 67 70 71 public void testConstructor1() 72 { 73 try 74 { 75 new DefaultMethodCode( (short)0, null, null ); 76 fail( "Did not throw IllegalArgumentException" ); 77 } 78 catch (IllegalArgumentException e) 79 { 80 } 82 } 83 84 85 public void testConstructor2() 86 throws Exception 87 { 88 try 89 { 90 new DefaultMethodCode( (short)0, createModifiedMethod(), null ); 91 fail( "Did not throw IllegalArgumentException" ); 92 } 93 catch (IllegalArgumentException e) 94 { 95 } 97 } 98 99 100 public void testConstructor3() 101 throws Exception 102 { 103 try 104 { 105 new DefaultMethodCode( (short)0, null, createClassRecord( 106 createModifiedMethod() ) ); 107 fail( "Did not throw IllegalArgumentException" ); 108 } 109 catch (IllegalArgumentException e) 110 { 111 } 113 } 114 115 116 public void testConstructor4() 117 throws Exception 118 { 119 ModifiedMethod mm = createModifiedMethod(); 120 new DefaultMethodCode( (short)0, mm, createClassRecord( mm ) ); 121 } 122 123 124 public void testConstructor5() throws Exception 125 { 126 ModifiedMethod mm = createModifiedMethod( GCI1.class, "m" ); 127 try 128 { 129 new DefaultMethodCode( (short)0, mm, 130 createClassRecord( mm ) ); 131 fail( "Did not throw an IllegalStateException." ); 132 } 133 catch (IllegalStateException ise) 134 { 135 assertTrue( 136 "Did not mention the abstract method.", 137 ise.getMessage().indexOf( "abstract method" ) >= 0 ); 138 } 139 } 140 141 142 public void testGetOriginalMethod1() 143 throws Exception 144 { 145 ModifiedMethod mm = createModifiedMethod(); 146 DefaultMethodCode dmc = new DefaultMethodCode( (short)0, mm, 147 createClassRecord( mm ) ); 148 assertNotNull( 149 "Returned null original method.", 150 dmc.getOriginalMethod() ); 151 assertSame( 152 "Not the same original method.", 153 mm.getOriginalMethod(), 154 dmc.getOriginalMethod() ); 155 } 156 157 158 public void testGetMethodName1() 159 throws Exception 160 { 161 ModifiedMethod mm = createModifiedMethod(); 162 Method m = mm.getOriginalMethod(); 163 DefaultMethodCode dmc = new DefaultMethodCode( (short)0, mm, 164 createClassRecord( mm ) ); 165 assertNotNull( 166 "Returned null method name.", 167 dmc.getMethodName() ); 168 assertEquals( 169 "Not the same original method.", 170 m.getName()+m.getSignature(), 171 dmc.getMethodName() ); 172 } 173 174 175 public void testGetClassName1() 176 throws Exception 177 { 178 ModifiedMethod mm = createModifiedMethod(); 179 DefaultMethodCode dmc = new DefaultMethodCode( (short)0, mm, 180 createClassRecord( mm ) ); 181 assertNotNull( 182 "Returned null class name.", 183 dmc.getClassName() ); 184 assertSame( 185 "Not the same original method.", 186 mm.getOriginalClass().getClassName(), 187 dmc.getClassName() ); 188 } 189 190 191 193 194 197 198 protected static ModifiedMethod createModifiedMethod() 199 throws Exception 200 { 201 return CCCreatorUtil.createModifiedMethod( THIS_CLASS, 0 ); 202 } 203 204 205 protected static ModifiedMethod getModifiedMethod( Class c, 206 String methodName ) throws Exception 207 { 208 210 ModifiedClass mc = CCCreatorUtil.createModifiedClass( c ); 211 ModifiedMethod mmL[] = mc.getMethods(); 212 213 for (int i = 0; i < mmL.length; ++i) 214 { 215 if (methodName.equals( mmL[i].getOriginalMethod().getName() )) 216 { 217 return mmL[i]; 218 } 219 } 220 fail( "Could not find method named '"+methodName+"' in class "+c+"." ); 221 throw new Exception (); 223 } 224 225 226 protected static ModifiedMethod createModifiedMethod( Class c, 227 String methodName ) throws Exception 228 { 229 JavaClass jc = BCELCreatorUtil.createJavaClass( c ); 232 Method mL[] = jc.getMethods(); 233 for (int i = 0; i < mL.length; ++i) 234 { 235 if (methodName.equals( mL[i].getName())) 236 { 237 Method m = mL[i]; 238 MethodGen mg = BCELCreatorUtil.createMethodGen( jc, m ); 239 ModifiedMethod mm = CCCreatorUtil.createModifiedMethod( jc, 240 i, m, mg ); 241 return mm; 242 } 243 } 244 fail( "Could not find method named '"+methodName+"' in class "+c+"." ); 245 throw new Exception (); 247 } 248 249 250 protected static AnalysisModuleSet createAnalysisModuleSet() 251 { 252 IAnalysisModule amL[] = new IAnalysisModule[] { 253 CCCreatorUtil.createIAnalysisModule( 0 ), 254 CCCreatorUtil.createIAnalysisModule( 1 ), 255 CCCreatorUtil.createIAnalysisModule( 2 ), 256 }; 257 return CCCreatorUtil.createAnalysisModuleSet( amL ); 258 } 259 260 261 protected static ClassRecord createClassRecord( ModifiedMethod mm ) 262 { 263 return CCCreatorUtil.createClassRecord( THIS_CLASS, mm, 264 createAnalysisModuleSet() ); 265 } 266 267 private static abstract class GCI1 268 { 269 public abstract void m(); 270 } 271 272 273 276 public static Test suite() 277 { 278 InterfaceTestSuite suite = IMethodCodeUTestI.suite(); 279 suite.addTestSuite( THIS_CLASS ); 280 suite.addFactory( new CxFactory( "A" ) { 281 public Object createImplObject() throws Exception { 282 ModifiedMethod mm = createModifiedMethod(); 283 return new DefaultMethodCode( (short)0, mm, 284 createClassRecord( mm ) ); 285 } 286 } ); 287 288 return suite; 289 } 290 291 public static void main( String [] args ) 292 { 293 String [] name = { THIS_CLASS.getName() }; 294 295 298 junit.textui.TestRunner.main( name ); 299 } 300 301 302 306 protected void setUp() throws Exception 307 { 308 super.setUp(); 309 310 } 312 313 314 318 protected void tearDown() throws Exception 319 { 320 322 323 super.tearDown(); 324 } 325 } 326 327 | Popular Tags |