1 26 27 package net.sourceforge.groboutils.codecoverage.v2.module; 28 29 import java.io.IOException ; 30 31 import junit.framework.Test; 32 import junit.framework.TestCase; 33 import net.sourceforge.groboutils.autodoc.v1.AutoDoc; 34 import net.sourceforge.groboutils.codecoverage.v2.BytecodeLoaderUtil; 35 import net.sourceforge.groboutils.codecoverage.v2.CCCreatorUtil; 36 import net.sourceforge.groboutils.codecoverage.v2.CreateMainClassHelper; 37 import net.sourceforge.groboutils.codecoverage.v2.IAnalysisModule; 38 import net.sourceforge.groboutils.codecoverage.v2.IAnalysisModuleUTestI; 39 import net.sourceforge.groboutils.codecoverage.v2.IMethodCode; 40 import net.sourceforge.groboutils.codecoverage.v2.compiler.ModifiedClass; 41 import net.sourceforge.groboutils.codecoverage.v2.compiler.ModifiedMethod; 42 import net.sourceforge.groboutils.codecoverage.v2.logger.TestLogger; 43 import net.sourceforge.groboutils.junit.v1.iftc.CxFactory; 44 import net.sourceforge.groboutils.junit.v1.iftc.InterfaceTestSuite; 45 46 import org.apache.bcel.Constants; 47 import org.apache.bcel.generic.InstructionConstants; 48 import org.apache.bcel.generic.ObjectType; 49 import org.apache.bcel.generic.PUSH; 50 import org.apache.bcel.generic.Type; 51 52 53 60 public class LineCountMeasureUTest extends TestCase 61 { 62 65 private static final Class THIS_CLASS = LineCountMeasureUTest.class; 66 private static final AutoDoc DOC = new AutoDoc( THIS_CLASS ); 67 68 public LineCountMeasureUTest( String name ) 69 { 70 super( name ); 71 } 72 73 74 77 public static class MyTestClass 78 { 79 public static void main( String [] args ) 80 { 81 int i = 0; i += 10; 82 System.out.println( ""+i ); 83 } 84 } 85 86 87 public void testAnalyze1() throws Exception 88 { 89 LineCountMeasure bcm = new LineCountMeasure(); 90 ModifiedClass mc = CCCreatorUtil.createModifiedClass( 91 TestLogger.createPCL(), MyTestClass.class ); 92 ModifiedMethod mm = CCCreatorUtil.getMainModifiedMethod( mc ); 93 IMethodCode imc = createMethodCode( MyTestClass.class, mm, bcm ); 94 95 bcm.analyze( imc ); 97 98 Class c = BytecodeLoaderUtil.loadClassFromBytecode( 100 mc.getClassName(), mc.getModifiedClass() ); 101 TestLogger.reset(); 102 BytecodeLoaderUtil.runMain( c ); 103 assertEquals( 104 "Did not mark every bytecode in method.", 105 2, 106 TestLogger.size() ); 107 } 108 109 110 public void testAnalyze2() throws Exception 111 { 112 LineCountMeasure bcm = new LineCountMeasure(); 113 CreateMainClassHelper cmch = createHelloWorld(); 114 byte bytecode[] = cmch.getBytecode(); 115 Class cc = cmch.getGenClass(); 116 ModifiedClass mc = CCCreatorUtil.createModifiedClass( 117 TestLogger.createPCL(), "test/HelloWorld.class", 118 bytecode ); 119 ModifiedMethod mm = CCCreatorUtil.getMainModifiedMethod( mc ); 120 IMethodCode imc = createMethodCode( cc, mm, bcm ); 121 122 bcm.analyze( imc ); 124 125 Class c = BytecodeLoaderUtil.loadClassFromBytecode( 127 mc.getClassName(), mc.getModifiedClass() ); 128 TestLogger.reset(); 129 BytecodeLoaderUtil.runMain( c ); 130 assertEquals( 131 "Somehow we found a line number.", 132 0, 133 TestLogger.size() ); 134 } 135 136 137 138 141 142 protected IMethodCode createMethodCode( Class c, ModifiedMethod mm, 143 IAnalysisModule am ) 144 { 145 return CCCreatorUtil.createIMethodCode( c, mm, 146 CCCreatorUtil.createAnalysisModuleSet( 147 new IAnalysisModule[] { am } ), 148 0 ); 149 } 150 151 152 protected CreateMainClassHelper createHelloWorld() 153 throws IOException 154 { 155 CreateMainClassHelper cmch = new CreateMainClassHelper( 156 "test.HelloWorld" ); 157 ObjectType p_stream = new ObjectType("java.io.PrintStream"); 158 cmch.il.append( 159 cmch.factory.createFieldAccess("java.lang.System", "out", p_stream, 160 Constants.GETSTATIC ) ); 161 cmch.il.append( new PUSH(cmch.cp, "Hello, world.")) ; 162 cmch.il.append( cmch.factory.createInvoke("java.io.PrintStream", 163 "println", Type.VOID, new Type[] { Type.STRING }, 164 Constants.INVOKEVIRTUAL ) ); 165 cmch.il.append(InstructionConstants.RETURN); 166 167 cmch.close(); 168 return cmch; 169 } 170 171 172 175 176 public static Test suite() 177 { 178 InterfaceTestSuite suite = IAnalysisModuleUTestI.suite(); 179 suite.addTestSuite( THIS_CLASS ); 180 suite.addFactory( new CxFactory( "A" ) { 181 public Object createImplObject() throws IOException { 182 return new LineCountMeasure(); 183 } 184 } ); 185 186 return suite; 187 } 188 189 public static void main( String [] args ) 190 { 191 String [] name = { THIS_CLASS.getName() }; 192 193 196 junit.textui.TestRunner.main( name ); 197 } 198 199 200 204 protected void setUp() throws Exception 205 { 206 super.setUp(); 207 208 209 } 211 212 213 217 protected void tearDown() throws Exception 218 { 219 221 222 super.tearDown(); 223 } 224 } 225 226 | Popular Tags |