1 26 27 package net.sourceforge.groboutils.codecoverage.v2.util; 28 29 import net.sourceforge.groboutils.codecoverage.v2.IChannelLogger; 30 import java.io.Reader ; 31 import java.io.BufferedReader ; 32 import java.io.IOException ; 33 import java.util.*; 34 35 import junit.framework.Test; 36 import junit.framework.TestCase; 37 import junit.framework.TestSuite; 38 import net.sourceforge.groboutils.autodoc.v1.AutoDoc; 39 40 41 48 public class ConvertSingleLogUTest extends TestCase 49 { 50 53 private static final Class THIS_CLASS = ConvertSingleLogUTest.class; 54 private static final AutoDoc DOC = new AutoDoc( THIS_CLASS ); 55 56 public ConvertSingleLogUTest( String name ) 57 { 58 super( name ); 59 } 60 61 62 63 66 67 public void testConstructor1() 68 { 69 try 70 { 71 new ConvertSingleLog( null ); 72 fail( "Didn't throw IllegalArgumentException" ); 73 } 74 catch (IllegalArgumentException e) 75 { 76 } 78 } 79 80 81 public void testConstructor2() 82 { 83 try 84 { 85 new ConvertSingleLog( new IChannelLogger[0] ); 86 fail( "Didn't throw IllegalArgumentException" ); 87 } 88 catch (IllegalArgumentException e) 89 { 90 } 92 } 93 94 95 public void testConstructor3() 96 { 97 try 98 { 99 new ConvertSingleLog( new IChannelLogger[1] ); 100 fail( "Didn't throw IllegalArgumentException" ); 101 } 102 catch (IllegalArgumentException e) 103 { 104 } 106 } 107 108 109 public void testParseShort1() 110 { 111 MyChannelLogger mcl = new MyChannelLogger(); 112 ConvertSingleLog csl = new ConvertSingleLog( new IChannelLogger[] { mcl } ); 113 114 try 115 { 116 csl.parseShort( "1,", "0,1,2" ); 117 fail( "Did not throw IOE" ); 118 } 119 catch (IOException e) 120 { 121 } 123 } 124 125 126 public void testParseShort2() 127 { 128 MyChannelLogger mcl = new MyChannelLogger(); 129 ConvertSingleLog csl = new ConvertSingleLog( new IChannelLogger[] { mcl } ); 130 131 try 132 { 133 csl.parseShort( "asdfasdf", "0,1,2" ); 134 fail( "Did not throw IOE" ); 135 } 136 catch (IOException e) 137 { 138 } 140 } 141 142 143 public void testParseShort3() throws IOException 144 { 145 MyChannelLogger mcl = new MyChannelLogger(); 146 ConvertSingleLog csl = new ConvertSingleLog( new IChannelLogger[] { mcl } ); 147 148 assertEquals( 149 "Didn't convert right", 150 12, 151 csl.parseShort( "12", "0,1,2" ) ); 152 } 153 154 155 public void testNextElement1() 156 { 157 MyChannelLogger mcl = new MyChannelLogger(); 158 ConvertSingleLog csl = new ConvertSingleLog( new IChannelLogger[] { mcl } ); 159 160 try 161 { 162 int pos[] = { 0, 0 }; 163 csl.nextElement( pos, "asdf" ); 164 fail("Did not throw IOE."); 165 } 166 catch (IOException e) 167 { 168 } 170 } 171 172 173 public void testProcessLine1() 174 { 175 MyChannelLogger mcl = new MyChannelLogger(); 176 ConvertSingleLog csl = new ConvertSingleLog( new IChannelLogger[] { mcl } ); 177 178 try 179 { 180 csl.processLine( null, true ); 181 fail("Did not throw IOE"); 182 } 183 catch (IOException e) 184 { 185 assertTrue( 186 "Bad exception text", 187 e.getMessage().indexOf("End of stream: line is null") >= 0 ); 188 } 189 } 190 191 192 public void testProcessLine2() 193 { 194 MyChannelLogger mcl = new MyChannelLogger(); 195 ConvertSingleLog csl = new ConvertSingleLog( new IChannelLogger[] { mcl } ); 196 197 try 198 { 199 csl.processLine( "1,asdf,1,1", false ); 200 fail("Did not throw IOE"); 201 } 202 catch (IOException e) 203 { 204 assertTrue( 205 "Bad exception text", 206 e.getMessage().indexOf("Invalid channel:") >= 0 ); 207 } 208 } 209 210 211 public void testProcessLine3() throws IOException 212 { 213 MyChannelLogger mclL[] = { 214 new MyChannelLogger(), 215 new MyChannelLogger(), 216 new MyChannelLogger() 217 }; 218 ConvertSingleLog csl = new ConvertSingleLog( mclL ); 219 220 csl.processLine( "1,asdf1,1 2", false ); 221 csl.processLine( "0,asdf2,0006 000C", false ); 222 csl.processLine( "2,asdf3,9 3", false ); 223 224 assertEquals( "did not store all class IDs for c0", 225 new String [] { "asdf2" }, 226 mclL[0].getClassIDs() ); 227 assertEquals( "did not store all marks for c0", 228 new String [] { "6,12" }, 229 mclL[0].getMarks("asdf2") ); 230 231 assertEquals( "did not store all class IDs for c1", 232 new String [] { "asdf1" }, 233 mclL[1].getClassIDs() ); 234 assertEquals( "did not store all marks for c1", 235 new String [] { "1,2" }, 236 mclL[1].getMarks("asdf1") ); 237 238 assertEquals( "did not store all class IDs for c2", 239 new String [] { "asdf3" }, 240 mclL[2].getClassIDs() ); 241 assertEquals( "did not store all marks for c2", 242 new String [] { "9,3" }, 243 mclL[2].getMarks("asdf3") ); 244 } 245 246 247 public void testProcessLine4() throws IOException 248 { 249 MyChannelLogger mcl = new MyChannelLogger(); 250 ConvertSingleLog csl = new ConvertSingleLog( new IChannelLogger[] { mcl } ); 251 252 csl.processLine( "0,A,0001 0002", false ); 253 csl.processLine( "0,B,0005 000C", false ); 254 csl.processLine( "0,C,03E7 0006", false ); 255 csl.processLine( "0,D,0012 0004", false ); 256 csl.processLine( "0,D,0017 004E", false ); 257 csl.processLine( "0,D,0017 0000", false ); 258 259 assertEquals( "did not store all class IDs", 260 new String [] { "A", "B", "C", "D" }, 261 mcl.getClassIDs() ); 262 assertEquals( "did not store all marks for A", 263 new String [] { "1,2" }, 264 mcl.getMarks("A") ); 265 assertEquals( "did not store all marks for B", 266 new String [] { "5,12" }, 267 mcl.getMarks("B") ); 268 assertEquals( "did not store all marks for C", 269 new String [] { "999,6" }, 270 mcl.getMarks("C") ); 271 assertEquals( "did not store all marks", 272 new String [] { "23,78", "23,0", "18,4" }, 273 mcl.getMarks("D") ); 274 } 275 276 277 278 281 private static class MyChannelLogger implements IChannelLogger 282 { 283 private Map classToMark = new HashMap(); 284 285 public void cover( String classID, short methodIndex, short markIndex ) 286 { 287 List s = (List)this.classToMark.get( classID ); 288 if (s == null) 289 { 290 s = new ArrayList(); 291 this.classToMark.put( classID, s ); 292 } 293 s.add( ""+methodIndex+','+markIndex ); 294 } 295 296 297 public String [] getClassIDs() 298 { 299 Set s = this.classToMark.keySet(); 300 return (String [])s.toArray( new String [ s.size() ] ); 301 } 302 303 public String [] getMarks( String classID ) 304 { 305 List s = (List)this.classToMark.get( classID ); 306 if (s == null) 307 { 308 return new String [0]; 309 } 310 return (String [])s.toArray( new String [ s.size() ] ); 311 } 312 } 313 314 315 private static void assertEquals( String msg, String [] a, String [] b ) 316 { 317 if (a == null) 318 { 319 assertNull( 320 msg, b ); 321 return; 322 } 323 if (b == null) 324 { 325 fail(msg+": actual array is null, but expected isn't."); 326 return; 327 } 328 329 assertEquals( 330 msg+": lengths don't match;", 331 a.length, 332 b.length ); 333 Arrays.sort( a ); 334 Arrays.sort( b ); 335 for (int i = 0; i < a.length; ++i) 336 { 337 assertEquals( 338 msg+": contents aren't equal", 339 a[i], b[i] ); 340 } 341 } 342 343 344 347 348 public static Test suite() 349 { 350 TestSuite suite = new TestSuite( THIS_CLASS ); 351 352 return suite; 353 } 354 355 public static void main( String [] args ) 356 { 357 String [] name = { THIS_CLASS.getName() }; 358 359 362 junit.textui.TestRunner.main( name ); 363 } 364 365 366 370 protected void setUp() throws Exception 371 { 372 super.setUp(); 373 374 } 376 377 378 382 protected void tearDown() throws Exception 383 { 384 386 387 super.tearDown(); 388 } 389 } 390 391 | Popular Tags |