1 26 27 package net.sourceforge.groboutils.codecoverage.v2.datastore; 28 29 import java.io.StringReader ; 30 import java.io.StringWriter ; 31 32 import junit.framework.Test; 33 import junit.framework.TestCase; 34 import junit.framework.TestSuite; 35 import net.sourceforge.groboutils.autodoc.v1.AutoDoc; 36 import net.sourceforge.groboutils.codecoverage.v2.CCCreatorUtil; 37 import net.sourceforge.groboutils.codecoverage.v2.compiler.ModifiedMethod; 38 39 40 47 public class ClassRecordIOUTest extends TestCase 48 { 49 52 private static final Class THIS_CLASS = ClassRecordIOUTest.class; 53 private static final AutoDoc DOC = new AutoDoc( THIS_CLASS ); 54 55 public ClassRecordIOUTest( String name ) 56 { 57 super( name ); 58 } 59 60 61 64 65 public void testWriteClass1() throws Exception 66 { 67 StringWriter sw = new StringWriter (); 68 ModifiedMethod mm = createModifiedMethod( THIS_CLASS, 0 ); 69 ClassRecord cr = createClassRecord( THIS_CLASS, mm ); 70 ClassRecordIO crio = new ClassRecordIO(); 71 72 crio.writeClass( cr, sw ); 73 String out = sw.toString(); 74 75 assertTrue( 76 "Did not write any data.", 77 out.length() > 0 ); 78 assertEquals( 79 "Did not write the correct data.", 80 THIS_CLASS.getName().length()+":"+THIS_CLASS.getName()+ 81 ","+cr.getClassCRC()+","+cr.getSourceFileName().length()+ 82 ":"+cr.getSourceFileName()+",1[("+mm.getMethodName().length()+":"+ 83 mm.getMethodName()+")]", 84 out ); 85 } 86 87 88 public void testWriteClass2() throws Exception 89 { 90 ClassRecord cr = new ClassRecord( "a.MyClass", 100, "MyClass.java", 91 new String [ 0 ], createAnalysisModuleSet() ); 92 StringWriter sw = new StringWriter (); 93 ClassRecordIO crio = new ClassRecordIO(); 94 95 crio.writeClass( cr, sw ); 96 String out = sw.toString(); 97 98 assertEquals( 99 "Did not write the correct data.", 100 "9:a.MyClass,100,12:MyClass.java,0[]", 101 out ); 102 } 103 104 105 public void testReadClass1() throws Exception 106 { 107 StringReader sr = new StringReader ( 108 "9:a.MyClass,100,12:MyClass.java,2[(4:a()V)(6:b([S)V)]" 109 ); 110 ClassRecordIO crio = new ClassRecordIO(); 111 112 ClassRecord cr = crio.readClass( createAnalysisModuleSet(), sr ); 113 assertNotNull( 114 "Returned null class record.", 115 cr ); 116 assertEquals( 117 "Did not return correct class name.", 118 "a.MyClass", 119 cr.getClassName() ); 120 assertEquals( 121 "Did not return correct class check-sum.", 122 100L, 123 cr.getClassCRC() ); 124 assertEquals( 125 "Did not return correct source file name.", 126 "MyClass.java", 127 cr.getSourceFileName() ); 128 String methods[] = cr.getMethods(); 129 assertEquals( 130 "Did not find correct number of methods.", 131 2, 132 methods.length ); 133 assertEquals( 134 "First method isn't correct signature.", 135 "a()V", 136 methods[0] ); 137 assertEquals( 138 "Second method isn't correct signature.", 139 "b([S)V", 140 methods[1] ); 141 } 142 143 144 public void testReadClass2() throws Exception 145 { 146 StringReader sr = new StringReader ( 147 "9:b.MyClass,100,13:MyClass2.java,0[]" 148 ); 149 ClassRecordIO crio = new ClassRecordIO(); 150 151 ClassRecord cr = crio.readClass( createAnalysisModuleSet(), sr ); 152 assertNotNull( 153 "Returned null class record.", 154 cr ); 155 assertEquals( 156 "Did not return correct class name.", 157 "b.MyClass", 158 cr.getClassName() ); 159 assertEquals( 160 "Did not return correct class check-sum.", 161 100L, 162 cr.getClassCRC() ); 163 assertEquals( 164 "Did not return correct source file name.", 165 "MyClass2.java", 166 cr.getSourceFileName() ); 167 assertEquals( 168 "Created methods out of nothing.", 169 0, 170 cr.getMethods().length ); 171 } 172 173 174 public void testReadWriteClass1() throws Exception 175 { 176 StringWriter sw = new StringWriter (); 177 ModifiedMethod mm = createModifiedMethod( THIS_CLASS, 0 ); 178 ClassRecord crIn = createClassRecord( THIS_CLASS, mm ); 179 ClassRecordIO crio = new ClassRecordIO(); 180 181 crio.writeClass( crIn, sw ); 182 183 String out = sw.toString(); 184 StringReader sr = new StringReader ( out ); 185 ClassRecord crOut = crio.readClass( createAnalysisModuleSet(), sr ); 186 187 assertNotNull( 188 "Returned null class record.", 189 crOut ); 190 assertEquals( 191 "Did not return correct class name.", 192 crIn.getClassName(), 193 crOut.getClassName() ); 194 assertEquals( 195 "Did not return correct class check-sum.", 196 crIn.getClassCRC(), 197 crOut.getClassCRC() ); 198 assertEquals( 199 "Did not return correct method count.", 200 crIn.getMethodCount(), 201 crOut.getMethodCount() ); 202 for (int i = 0; i < crIn.getMethodCount(); ++i) 203 { 204 assertEquals( 205 "Did not return same method for index "+i+".", 206 crIn.getMethodAt( (short)i ), 207 crOut.getMethodAt( (short)i ) ); 208 } 209 } 210 211 212 215 216 protected ModifiedMethod createModifiedMethod( Class c, int index ) 217 throws Exception 218 { 219 return CCCreatorUtil.createModifiedMethod( c, 0 ); 220 } 221 222 223 protected AnalysisModuleSet createAnalysisModuleSet() 224 { 225 return CCCreatorUtil.createAnalysisModuleSet( 3 ); 226 } 227 228 229 protected ClassRecord createClassRecord( Class c, ModifiedMethod mm ) 230 throws Exception 231 { 232 return CCCreatorUtil.createClassRecord( c, mm, 233 createAnalysisModuleSet() ); 234 } 235 236 237 240 241 public static Test suite() 242 { 243 TestSuite suite = new TestSuite( THIS_CLASS ); 244 245 return suite; 246 } 247 248 public static void main( String [] args ) 249 { 250 String [] name = { THIS_CLASS.getName() }; 251 252 255 junit.textui.TestRunner.main( name ); 256 } 257 258 259 263 protected void setUp() throws Exception 264 { 265 super.setUp(); 266 267 268 } 270 271 272 276 protected void tearDown() throws Exception 277 { 278 280 281 super.tearDown(); 282 } 283 } 284 285 | Popular Tags |