1 17 18 package org.netbeans.lib.uihandlerserver; 19 20 import java.io.ByteArrayInputStream ; 21 import java.io.ByteArrayOutputStream ; 22 import java.io.DataInputStream ; 23 import java.io.DataOutputStream ; 24 import java.io.EOFException ; 25 import java.io.File ; 26 import java.io.FileInputStream ; 27 import java.io.FileOutputStream ; 28 import java.io.InputStream ; 29 import java.io.UnsupportedEncodingException ; 30 import java.lang.reflect.Method ; 31 import java.util.Arrays ; 32 import java.util.Random ; 33 import java.util.ResourceBundle ; 34 import java.util.logging.Handler ; 35 import java.util.logging.Level ; 36 import java.util.logging.LogRecord ; 37 import java.util.logging.Logger ; 38 import junit.framework.Test; 39 import org.netbeans.junit.Log; 40 import org.netbeans.junit.NbTestCase; 41 import org.netbeans.junit.NbTestSuite; 42 import org.netbeans.lib.uihandler.LogRecords; 43 44 48 public class LogRecordsTest extends NbTestCase { 49 private Logger LOG; 50 51 public LogRecordsTest(String testName) { 52 super(testName); 53 } 54 55 56 public static Test suite() { 57 return new NbTestSuite(LogRecordsTest.class); 58 } 60 61 protected Level logLevel() { 62 return Level.FINEST; 63 } 64 65 protected void setUp() throws Exception { 66 LOG = Logger.getLogger("TEST-" + getName()); 67 } 68 69 protected void tearDown() throws Exception { 70 } 71 72 public void testParamsGetCleared() throws Exception { 73 String r = 74 "<record>" + 75 "<date>2006-11-17T10:16:14</date>" + 76 "<millis>1163729774285</millis>" + 77 "<sequence>20</sequence>" + 78 "<level>INFO</level>" + 79 "<thread>12</thread>" + 80 "<message>MSG</message>" + 81 "<key>MSG</key>" + 82 "<catalog>a.bundle.somewhere</catalog>" + 83 "<param>1</param>" + 84 "</record>" + 85 86 "<record>" + 87 "<date>2006-11-17T10:16:14</date>" + 88 "<millis>1163729774285</millis>" + 89 "<sequence>20</sequence>" + 90 "<level>INFO</level>" + 91 "<thread>12</thread>" + 92 "<message>MSG</message>" + 93 "<key>MSG</key>" + 94 "<catalog>a.bundle.somewhere</catalog>" + 95 "<param>2</param>" + 96 "</record>"; 97 98 class H extends Handler { 99 int cnt; 100 101 public void publish(LogRecord arg0) { 102 cnt++; 103 assertNotNull("We have params " + cnt, arg0.getParameters()); 104 assertEquals("One argument for " + cnt + "th record", 1, arg0.getParameters().length); 105 } 106 107 public void flush() { 108 } 109 110 public void close() throws SecurityException { 111 } 112 } 113 H h = new H(); 114 115 LogRecords.scan(new ByteArrayInputStream (r.getBytes()), h); 116 117 assertEquals("Two records", 2, h.cnt); 118 } 119 120 public void testWriteAndRead() throws Exception { 121 doWriteAndReadTest(System.currentTimeMillis()); 122 } 123 124 public void testNewFailureOn1165572711706() throws Exception { 125 doWriteAndReadTest(1165572711706L); 126 } 127 128 public void testFailureOn1159804485342() throws Exception { 129 doWriteAndReadTest(1159804485342L); 130 } 131 132 public void testMakeSureItIsReadable() throws Exception { 133 InputStream is = getClass().getResourceAsStream("NB1216449736.xml"); 134 int cnt = 0; 135 for (;;) { 136 LOG.log(Level.INFO, "Reading {0}th record", cnt); 137 LogRecord r = LogRecords.read(is); 138 if (r == null) { 139 break; 140 } 141 LOG.log(Level.INFO, "Read {0}th record", cnt); 142 cnt++; 143 } 144 is.close(); 145 } 146 147 public void testReadException() throws Exception { 148 String what = "NB1216449736.xml"; 149 150 InputStream is = getClass().getResourceAsStream(what); 151 int cnt = 0; 152 153 class H extends Handler { 154 int cnt; 155 LogRecord first; 156 157 public void publish(LogRecord record) { 158 if (cnt == 0) { 159 first = record; 160 } 161 cnt++; 162 } 163 164 public void flush() { 165 } 166 167 public void close() throws SecurityException { 168 } 169 } 170 171 LogRecord first = null; 172 for (;;) { 173 LOG.log(Level.INFO, "Reading {0}th record", cnt); 174 LogRecord r = LogRecords.read(is); 175 if (r == null) { 176 break; 177 } 178 if (first == null) { 179 first = r; 180 } 181 LOG.log(Level.INFO, "Read {0}th record", cnt); 182 cnt++; 183 } 184 is.close(); 185 186 H h = new H(); 187 is = getClass().getResourceAsStream(what); 188 LogRecords.scan(is, h); 189 is.close(); 190 191 assertNotNull(first); 192 assertNotNull(h.first); 193 assertEquals("Same message", first.getMessage(), h.first.getMessage()); 194 assertNotNull("exception from read", first.getThrown()); 195 assertNotNull("exception from scan", h.first.getThrown()); 196 assertEquals("Same exception message", first.getThrown().getMessage(), h.first.getThrown().getMessage()); 197 198 StackTraceElement [] arr1 = first.getThrown().getStackTrace(); 199 StackTraceElement [] arr2 = h.first.getThrown().getStackTrace(); 200 201 assertEquals("Same length", arr1.length, arr2.length); 202 203 for (int i = 0; i < arr1.length; i++) { 204 if (!arr1[i].equals(arr2[i])) { 205 fail(i + " th stack differ: " + arr1[i] + " != " + arr2[i] + "\nline: " + arr1[i].getLineNumber() + " and " + arr2[i].getLineNumber()); 206 } 207 } 208 209 210 assertEquals("The same amount of records", cnt, h.cnt); 211 212 } 213 214 public void testCanReadEmpty() throws Exception { 215 InputStream is = getClass().getResourceAsStream("Empty.xml"); 216 int cnt = 0; 217 for (;;) { 218 LOG.log(Level.INFO, "Reading {0}th record", cnt); 219 LogRecord r = LogRecords.read(is); 220 if (r == null) { 221 break; 222 } 223 LOG.log(Level.INFO, "Read {0}th record", cnt); 224 cnt++; 225 } 226 is.close(); 227 228 assertEquals("No records", 0, cnt); 229 } 230 public void testMakeSureItIsScannable() throws Exception { 231 InputStream is = getClass().getResourceAsStream("NB1216449736.xml"); 232 int cnt = 0; 233 234 class H extends Handler { 235 int cnt; 236 237 public void publish(LogRecord record) { 238 cnt++; 239 } 240 241 public void flush() { 242 } 243 244 public void close() throws SecurityException { 245 } 246 } 247 248 for (;;) { 249 LOG.log(Level.INFO, "Reading {0}th record", cnt); 250 LogRecord r = LogRecords.read(is); 251 if (r == null) { 252 break; 253 } 254 LOG.log(Level.INFO, "Read {0}th record", cnt); 255 cnt++; 256 } 257 is.close(); 258 259 H h = new H(); 260 is = getClass().getResourceAsStream("NB1216449736.xml"); 261 LogRecords.scan(is, h); 262 is.close(); 263 264 assertEquals("The same amount of records", cnt, h.cnt); 265 } 266 public void testBadUserIsBad() throws Exception { 267 String what = "baduser.xml"; 268 InputStream is = getClass().getResourceAsStream(what); 269 int cnt = 0; 270 271 class H extends Handler { 272 int cnt; 273 274 public void publish(LogRecord record) { 275 cnt++; 276 } 277 278 public void flush() { 279 } 280 281 public void close() throws SecurityException { 282 } 283 } 284 285 for (;;) { 286 LOG.log(Level.INFO, "Reading {0}th record", cnt); 287 LogRecord r = LogRecords.read(is); 288 if (r == null) { 289 break; 290 } 291 LOG.log(Level.INFO, "Read {0}th record", cnt); 292 cnt++; 293 } 294 is.close(); 295 296 H h = new H(); 297 is = getClass().getResourceAsStream(what); 298 LogRecords.scan(is, h); 299 is.close(); 300 301 assertEquals("The same amount of records", cnt, h.cnt); 302 } 303 304 305 306 public void testDoesNotAskForWrongBunles() throws Exception { 307 LogRecord rec = new LogRecord (Level.FINER, "UI_ACTION_BUTTON_PRESS"); rec.setParameters(new Object [] { "0", "1" }); 309 rec.setResourceBundle(ResourceBundle.getBundle(LogRecordsTest.class.getPackage().getName() + ".Props")); 310 311 ByteArrayOutputStream os = new ByteArrayOutputStream (); 312 LogRecords.write(os, rec); 313 os.close(); 314 315 316 class H extends Handler { 317 int cnt; 318 319 public void publish(LogRecord arg0) { 320 cnt++; 321 assertNotNull("We have params " + cnt, arg0.getParameters()); 322 assertEquals("Two argument for " + cnt + "th record", 2, arg0.getParameters().length); 323 } 324 325 public void flush() { 326 } 327 328 public void close() throws SecurityException { 329 } 330 } 331 H h = new H(); 332 333 CharSequence log = Log.enable("", Level.INFO); 334 LogRecords.scan(new ByteArrayInputStream (os.toByteArray()), h); 335 336 assertEquals("One record", 1, h.cnt); 337 338 if (log.toString().indexOf("Cannot find resource") < 0) { 339 fail(log.toString()); 340 } 341 } 342 343 public void testScanEmpty91974() throws Exception { 344 String what = "uigestures-iz91974.xml"; 345 InputStream is = getClass().getResourceAsStream(what); 346 int cnt = 0; 347 348 class H extends Handler { 349 int cnt; 350 351 public void publish(LogRecord record) { 352 cnt++; 353 } 354 355 public void flush() { 356 } 357 358 public void close() throws SecurityException { 359 } 360 } 361 362 for (;;) { 363 LOG.log(Level.INFO, "Reading {0}th record", cnt); 364 LogRecord r = LogRecords.read(is); 365 if (r == null) { 366 break; 367 } 368 LOG.log(Level.INFO, "Read {0}th record", cnt); 369 cnt++; 370 } 371 is.close(); 372 373 H h = new H(); 374 is = getClass().getResourceAsStream(what); 375 LogRecords.scan(is, h); 376 is.close(); 377 378 assertEquals("The same amount of records", cnt, h.cnt); 379 } 380 public void testNotFinishedFiles() throws Exception { 381 String what = "eof.xml"; 382 InputStream is = getClass().getResourceAsStream(what); 383 int cnt = 0; 384 385 class H extends Handler { 386 int cnt; 387 388 public void publish(LogRecord record) { 389 cnt++; 390 } 391 392 public void flush() { 393 } 394 395 public void close() throws SecurityException { 396 } 397 } 398 399 for (;;) { 400 LOG.log(Level.INFO, "Reading {0}th record", cnt); 401 LogRecord r; 402 try { 403 r = LogRecords.read(is); 404 } catch (EOFException ex) { 405 assertNull("Next read is null", LogRecords.read(is)); 406 break; 407 } 408 if (r == null) { 409 break; 410 } 411 LOG.log(Level.INFO, "Read {0}th record", cnt); 412 cnt++; 413 } 414 is.close(); 415 416 H h = new H(); 417 is = getClass().getResourceAsStream(what); 418 LogRecords.scan(is, h); 419 is.close(); 420 421 assertEquals("The same amount of records", cnt, h.cnt); 422 } 423 public void testScanFileThatClaimsTohaveWrongUTF8Char() throws Exception { 424 InputStream is = getClass().getResourceAsStream("wrongutfchar.xml"); 425 int cnt = 0; 426 427 class H extends Handler { 428 int cnt; 429 430 public void publish(LogRecord record) { 431 cnt++; 432 } 433 434 public void flush() { 435 } 436 437 public void close() throws SecurityException { 438 } 439 } 440 441 for (;;) { 442 LOG.log(Level.INFO, "Reading {0}th record", cnt); 443 LogRecord r = LogRecords.read(is); 444 if (r == null) { 445 break; 446 } 447 LOG.log(Level.INFO, "Read {0}th record", cnt); 448 cnt++; 449 } 450 is.close(); 451 452 H h = new H(); 453 is = getClass().getResourceAsStream("wrongutfchar.xml"); 454 LogRecords.scan(is, h); 455 is.close(); 456 457 assertEquals("The same amount of records", cnt, h.cnt); 458 } 459 460 private void doWriteAndReadTest(long seed) throws Exception { 461 Logger.getAnonymousLogger().info("seed is: " + seed); 462 463 File file = new File (getWorkDir(), "feed.txt"); 464 Random r = new Random (seed); 465 DataOutputStream out = new DataOutputStream (new FileOutputStream (file)); 466 467 468 int cnt = r.nextInt(500); 469 final LogRecord [] arr = new LogRecord [cnt]; 470 for (int i = 0; i < cnt; i++) { 471 LogRecord rec = generateLogRecord(r); 472 arr[i] = rec; 473 LogRecords.write(out, rec); 474 } 475 out.close(); 476 477 478 { 479 DataInputStream in = new DataInputStream (new FileInputStream (file)); 480 for (int i = 0; i < cnt; i++) { 481 LogRecord rec = LogRecords.read(in); 482 assertLog(i + "-th record is the same", rec, arr[i]); 483 } 484 in.close(); 485 } 486 487 class H extends Handler { 488 int cnt; 489 490 public void publish(LogRecord rec) { 491 try { 492 assertLog(cnt + "-th record is the same", rec, arr[cnt]); 493 } catch(Exception ex) { 494 throw (RuntimeException )new RuntimeException ().initCause(ex); 495 } 496 cnt++; 497 } 498 499 public void flush() { 500 assertEquals("All read", cnt, arr.length); 501 cnt = -1; 502 } 503 504 public void close() throws SecurityException { 505 } 506 } 507 508 H h = new H(); 509 { 510 LOG.info("Scanning " + file); 511 DataInputStream in = new DataInputStream (new FileInputStream (file)); 512 LogRecords.scan(in, h); 513 in.close(); 514 } 515 assertEquals("Cleared", -1, h.cnt); 516 } 517 518 private LogRecord generateLogRecord(Random r) throws UnsupportedEncodingException { 519 LogRecord rec = new LogRecord (randomLevel(r), randomString(r)); 520 return rec; 521 } 522 523 private void assertLog(String string, LogRecord r1, LogRecord r2) throws Exception { 524 if (r1 == null && r2 != null) { 525 fail("r1: null r2 not: " + r(r2)); 526 } 527 if (r1 != null && r2 == null) { 528 fail("r2: null r1 not: " + r(r2)); 529 } 530 531 for (Method m : LogRecord .class.getMethods()) { 532 if (m.getName().startsWith("get") && m.getParameterTypes().length == 0) { 533 Object o1 = m.invoke(r1); 534 Object o2 = m.invoke(r2); 535 536 if (o1 == null && o2 == null) { 537 continue; 538 } 539 if (o1 == null || o2 == null || !o1.equals(o2)) { 540 assertEquals( 541 "Logs differ in result of " + m.getName() + "\nrec1: " + r(r1) + "\nrec2: " + r(r2), 542 o1, o2 543 ); 544 } 545 } 546 } 547 } 548 549 private static String r(LogRecord r) { 550 return r.getMessage(); 551 } 552 553 private static Level randomLevel(Random r) { 554 int lev = r.nextInt(1100); 555 if (lev >= Level.SEVERE.intValue()) return Level.SEVERE; 556 if (lev >= Level.WARNING.intValue()) return Level.WARNING; 557 if (lev >= Level.INFO.intValue()) return Level.INFO; 558 if (lev >= Level.CONFIG.intValue()) return Level.CONFIG; 559 if (lev >= Level.FINE.intValue()) return Level.FINE; 560 if (lev >= Level.FINER.intValue()) return Level.FINER; 561 if (lev >= Level.FINEST.intValue()) return Level.FINEST; 562 return Level.OFF; 563 } 564 565 private static String randomString(Random r) throws UnsupportedEncodingException { 566 int len = r.nextInt(50); 567 byte[] arr = new byte[len]; 568 for (int i = 0; i < arr.length; i++) { 569 int ch = r.nextInt(256); 570 if (ch < 32) { 571 ch = 32; 572 } 573 if (ch > 'z') { 574 ch = 'z'; 575 } 576 arr[i] = (byte)ch; 577 } 578 return new String (new String (arr, "utf-8").getBytes(), "utf-8"); 579 } 580 } 581 | Popular Tags |