1 22 23 package net.sourceforge.groboutils.codecoverage.v2; 24 25 import java.io.IOException ; 26 27 import net.sourceforge.groboutils.autodoc.v1.AutoDoc; 28 import net.sourceforge.groboutils.junit.v1.iftc.ImplFactory; 29 import net.sourceforge.groboutils.junit.v1.iftc.InterfaceTestCase; 30 import net.sourceforge.groboutils.junit.v1.iftc.InterfaceTestSuite; 31 32 import org.apache.bcel.classfile.JavaClass; 33 import org.apache.bcel.classfile.LineNumberTable; 34 import org.apache.bcel.classfile.Method; 35 import org.apache.bcel.generic.ConstantPoolGen; 36 import org.apache.bcel.generic.Instruction; 37 import org.apache.bcel.generic.MethodGen; 38 39 40 47 public class IAnalysisModuleUTestI extends InterfaceTestCase 48 { 49 52 private static final Class THIS_CLASS = IAnalysisModuleUTestI.class; 53 private static final AutoDoc DOC = new AutoDoc( THIS_CLASS ); 54 55 public IAnalysisModuleUTestI( String name, ImplFactory f ) 56 { 57 super( name, IAnalysisModule.class, f ); 58 } 59 60 61 public IAnalysisModule createIAnalysisModule() 62 { 63 return (IAnalysisModule)createImplObject(); 64 } 65 66 67 70 public void testGetMeasureName1() 71 { 72 IAnalysisModule am = createIAnalysisModule(); 73 String s = am.getMeasureName(); 74 assertNotNull( 75 "Null measure name.", 76 s ); 77 assertTrue( 78 "Empty measure name.", 79 s.length() > 0 ); 80 } 81 82 83 public void testGetMeasureUnit1() 84 { 85 IAnalysisModule am = createIAnalysisModule(); 86 String s = am.getMeasureUnit(); 87 assertNotNull( 88 "Null measure Unit.", 89 s ); 90 assertTrue( 91 "Empty measure Unit.", 92 s.length() > 0 ); 93 } 94 95 96 public void testGetMimeEncoding1() 97 { 98 IAnalysisModule am = createIAnalysisModule(); 99 String s = am.getMimeEncoding(); 100 assertNotNull( 101 "Null mime encoding.", 102 s ); 103 assertTrue( 104 "Empty mime encoding.", 105 s.length() > 0 ); 106 int pos = s.indexOf( '/' ); 107 assertTrue( 108 "No '/' in mime encoding: invalid encoding type.", 109 pos > 0 ); 110 assertTrue( 111 "Multiple mime encoding types are not allowed.", 112 s.indexOf( ';' ) < 0 ); 113 } 114 115 116 private static class MockMethodCode implements IMethodCode 117 { 118 private Method meth; 119 private Instruction[] instrL; 120 private String className; 121 public MockMethodCode( Class c, int methodIndex ) 122 throws IOException 123 { 124 if (c == null) 125 { 126 c = this.getClass(); 127 } 128 JavaClass jc = loadBytecode( c.getName() ); 129 this.className = jc.getClassName(); 130 this.meth = jc.getMethods()[ methodIndex ]; 131 MethodGen mg = new MethodGen( this.meth, this.className, 132 new ConstantPoolGen( jc.getConstantPool() ) ); 133 this.instrL = mg.getInstructionList().getInstructions(); 134 } 135 136 public Method getOriginalMethod() { return this.meth; } 137 public String getMethodName() { return this.meth.getName(); } 138 public String getClassName() { return this.className; } 139 public int getInstructionCount() { return this.instrL.length; } 140 public Instruction getInstructionAt( int index ) 141 { 142 if (index < 0 || index >= getInstructionCount()) 143 { 144 throw new IndexOutOfBoundsException ( ""+index ); 145 } 146 return this.instrL[ index ]; 147 } 148 public void markInstruction( int index, IAnalysisMetaData meta ) 149 { 150 if (index < 0 || index >= getInstructionCount()) 151 { 152 throw new IndexOutOfBoundsException ( ""+index ); 153 } 154 } 156 public LineNumberTable getLineNumberTable() 157 { return this.meth.getLineNumberTable(); } 158 } 159 160 161 162 public void testAnalyze1() throws Exception 163 { 164 IAnalysisModule am = createIAnalysisModule(); 165 am.analyze( new MockMethodCode( null, 0 ) ); 166 } 167 168 169 171 protected static JavaClass loadBytecode( String className ) 172 throws IOException 173 { 174 return BytecodeLoaderUtil.loadJavaClass( className ); 175 } 176 177 178 179 182 183 public static InterfaceTestSuite suite() 184 { 185 InterfaceTestSuite suite = new InterfaceTestSuite( THIS_CLASS ); 186 187 return suite; 188 } 189 190 public static void main( String [] args ) 191 { 192 String [] name = { THIS_CLASS.getName() }; 193 194 197 junit.textui.TestRunner.main( name ); 198 } 199 200 201 205 protected void setUp() throws Exception 206 { 207 super.setUp(); 208 209 } 211 212 213 217 protected void tearDown() throws Exception 218 { 219 221 222 super.tearDown(); 223 } 224 } 225 226 | Popular Tags |