1 19 20 package org.netbeans.core; 21 22 import java.awt.Dialog ; 23 import java.io.File ; 24 import java.io.FileInputStream ; 25 import java.io.FileOutputStream ; 26 import java.io.IOException ; 27 import java.lang.reflect.InvocationTargetException ; 28 import java.util.Iterator ; 29 import java.util.MissingResourceException ; 30 import java.util.logging.Level ; 31 import java.util.logging.LogManager ; 32 import java.util.logging.Logger ; 33 import javax.swing.JDialog ; 34 import javax.swing.SwingUtilities ; 35 import junit.framework.Test; 36 import org.netbeans.core.startup.CLIOptions; 37 import org.netbeans.junit.MockServices; 38 import org.netbeans.junit.NbTestCase; 39 import org.netbeans.junit.NbTestSuite; 40 import org.openide.DialogDescriptor; 41 import org.openide.DialogDisplayer; 42 import org.openide.ErrorManager; 43 import org.openide.NotifyDescriptor; 44 import org.openide.util.Exceptions; 45 import org.openide.util.Lookup; 46 import org.xml.sax.SAXParseException ; 47 48 49 54 public final class NbErrorManagerTest extends NbTestCase { 55 public NbErrorManagerTest(String s) { 56 super(s); 57 } 58 59 public static Test suite() { 60 return new NbTestSuite(NbErrorManagerTest.class); 62 } 63 64 private ErrorManager err; 65 protected void setUp() throws Exception { 66 clearWorkDir(); 67 68 MockServices.setServices(MockDD.class); 69 70 System.setProperty("netbeans.user", getWorkDirPath()); 71 CLIOptions.initialize(); 73 74 75 err = ErrorManager.getDefault(); 76 assertNotNull("One Error manager found", err); 77 } 78 79 public void testIsLoggable() { 80 assertFalse(ErrorManager.getDefault ().isLoggable(ErrorManager.INFORMATIONAL)); 81 assertFalse(ErrorManager.getDefault ().isLoggable(ErrorManager.INFORMATIONAL + 1)); 82 assertTrue(ErrorManager.getDefault ().isLoggable(ErrorManager.WARNING + 1)); 83 } 84 85 public void testBasicNotify() throws Exception { 86 assertTrue(err.isNotifiable(ErrorManager.EXCEPTION)); 87 NullPointerException npe = new NullPointerException ("unloc msg"); 88 err.notify(ErrorManager.INFORMATIONAL, npe); 89 String s = readLog(); 90 assertTrue(s.indexOf("java.lang.NullPointerException: unloc msg") != -1); 91 assertTrue(s.indexOf("testBasicNotify") != -1); 92 } 93 94 public void testLog() throws Exception { 95 assertFalse(err.isLoggable(ErrorManager.INFORMATIONAL)); 96 err.log("some msg"); 97 String s = readLog(); 98 assertTrue(s.indexOf("some msg") == -1); 99 assertTrue(err.isLoggable(ErrorManager.WARNING)); 100 err.log(ErrorManager.WARNING, "another msg"); 101 s = readLog(); 102 assertTrue(s.indexOf("another msg") != -1); 103 ErrorManager err2 = err.getInstance("foo.bar.baz"); 104 assertFalse(err2.isLoggable(ErrorManager.INFORMATIONAL)); 105 err2.log("sub msg #1"); 106 s = readLog(); 107 assertTrue(s.indexOf("sub msg #1") == -1); 108 System.setProperty("quux.hoho.level", "0"); 109 110 LogManager.getLogManager().readConfiguration(); 111 112 err2 = err.getInstance("quux.hoho.yaya"); 113 assertTrue(err2.isLoggable(ErrorManager.INFORMATIONAL)); 114 err2.log("sub msg #2"); 115 s = readLog(); 116 assertTrue(s, s.indexOf("sub msg #2") != -1); 117 assertTrue(s, s.indexOf("quux.hoho.yaya") != -1); 118 } 119 120 121 public void testNestedThrowables() throws Exception { 122 NullPointerException npe = new NullPointerException ("unloc msg"); 123 ClassNotFoundException cnfe = new ClassNotFoundException ("other msg", npe); 124 err.notify(ErrorManager.INFORMATIONAL, cnfe); 125 String s = readLog(); 126 assertTrue(s.indexOf("java.lang.NullPointerException: unloc msg") != -1); 127 assertTrue(s.indexOf("java.lang.ClassNotFoundException") != -1); 128 npe = new NullPointerException ("msg1"); 129 IOException ioe = new IOException ("msg2"); 130 err.annotate(ioe, npe); 131 InvocationTargetException ite = new InvocationTargetException (ioe, "msg3"); 132 IllegalStateException ise = new IllegalStateException ("msg4"); 133 err.annotate(ise, ite); 134 err.notify(ErrorManager.INFORMATIONAL, ise); 135 s = readLog(); 136 assertTrue(s, s.indexOf("java.lang.NullPointerException: msg1") != -1); 137 assertTrue(s, s.indexOf("java.io.IOException: msg2") != -1); 138 assertTrue(s.indexOf("msg3") != -1); 139 assertTrue(s, s.indexOf("java.lang.IllegalStateException: msg4") != -1); 140 } 141 142 public void testNotifyWithAnnotations() throws Exception { 143 NullPointerException npe = new NullPointerException ("unloc msg"); 144 err.annotate(npe, "loc msg #1"); 145 err.notify(ErrorManager.INFORMATIONAL, npe); 146 String s = readLog(); 147 assertTrue(s.indexOf("java.lang.NullPointerException: unloc msg") != -1); 148 assertTrue(s.indexOf("loc msg #1") != -1); 149 npe = new NullPointerException ("unloc msg"); 150 err.annotate(npe, ErrorManager.UNKNOWN, "extra unloc msg", null, null, null); 151 err.notify(ErrorManager.INFORMATIONAL, npe); 152 s = readLog(); 153 assertTrue(s.indexOf("extra unloc msg") != -1); 154 npe = new NullPointerException ("new unloc msg"); 155 IOException ioe = new IOException ("something bad"); 156 err.annotate(ioe, npe); 157 err.notify(ErrorManager.INFORMATIONAL, ioe); 158 s = readLog(); 159 assertTrue(s.indexOf("java.lang.NullPointerException: new unloc msg") != -1); 160 assertTrue(s.indexOf("java.io.IOException: something bad") != -1); 161 } 162 163 public void testDeepAnnotations() throws Exception { 164 Exception e1 = new Exception ("msg1"); 165 err.annotate(e1, "some loc msg"); 167 Exception e2 = new Exception ("msg2"); 168 err.annotate(e2, e1); 169 Exception e3 = new Exception ("msg3"); 170 err.annotate(e3, e2); 171 Exception e4 = new Exception ("msg4"); 172 err.annotate(e3, e4); 173 err.notify(ErrorManager.INFORMATIONAL, e3); 174 String s = readLog(); 175 assertTrue(s.indexOf("java.lang.Exception: msg1") != -1); 176 assertTrue(s.indexOf("java.lang.Exception: msg2") != -1); 177 assertTrue(s.indexOf("java.lang.Exception: msg3") != -1); 178 assertTrue(s.indexOf("java.lang.Exception: msg4") != -1); 179 assertTrue(s.indexOf("some loc msg") != -1); 180 } 181 182 183 public void testLoops() throws Exception { 184 Exception e1 = new Exception ("msg1"); 185 Exception e2 = new Exception ("msg2"); 186 err.annotate(e2, e1); 187 Exception e3 = new Exception ("msg3"); 188 err.annotate(e3, e2); 189 err.annotate(e1, e3); 190 err.notify(ErrorManager.INFORMATIONAL, e1); 191 String s = readLog(); 192 assertTrue(s.indexOf("java.lang.Exception: msg1") != -1); 193 assertTrue(s.indexOf("java.lang.Exception: msg2") != -1); 194 assertTrue(s.indexOf("java.lang.Exception: msg3") != -1); 195 assertTrue(s.indexOf("cyclic") != -1); 197 } 198 199 public void testAddedInfo() throws Exception { 200 MissingResourceException mre = new MissingResourceException ("msg1", "the.class.Name", "the-key"); 201 err.notify(ErrorManager.INFORMATIONAL, mre); 202 String s = readLog(); 203 assertTrue(s.indexOf("java.util.MissingResourceException: msg1") != -1); 204 assertTrue(s.indexOf("the.class.Name") != -1); 205 assertTrue(s.indexOf("the-key") != -1); 206 SAXParseException saxpe = new SAXParseException ("msg2", "pub-id", "sys-id", 313, 424); 207 err.notify(ErrorManager.INFORMATIONAL, saxpe); 208 s = readLog(); 209 assertTrue(s.indexOf("org.xml.sax.SAXParseException: msg2") != -1); 210 assertTrue(s.indexOf("pub-id") != -1); 211 assertTrue(s.indexOf("sys-id") != -1); 212 assertTrue(s.indexOf("313") != -1); 213 assertTrue(s.indexOf("424") != -1); 214 } 215 216 219 public void testNotifyException() throws Exception { 220 IOException ioe = new IOException ("unloc msg"); 221 err.annotate(ioe, "loc msg"); 222 NbErrorManager.Exc x = NbErrorManager.createExc(ioe, Level.INFO, null); 223 assertEquals(Level.INFO, x.getSeverity()); 224 assertEquals("loc msg", x.getLocalizedMessage()); 225 assertTrue(x.isLocalized()); 226 } 228 229 233 public void testUnknownSeverity() throws Exception { 234 235 Throwable t = new IOException ("unloc msg"); 237 NbErrorManager.Exc x = NbErrorManager.createExc(t, null, null); 238 assertEquals(Level.WARNING, x.getSeverity()); 239 assertEquals("unloc msg", x.getMessage()); 240 assertEquals("unloc msg", x.getLocalizedMessage()); 241 assertFalse(x.isLocalized()); 242 243 t = new IOException ("unloc msg"); 245 err.annotate(t, ErrorManager.UNKNOWN, "some debug info", null, null, null); 246 x = NbErrorManager.createExc(t, null, null); 247 assertEquals(Level.WARNING, x.getSeverity()); 248 assertEquals("unloc msg", x.getMessage()); 249 assertEquals("unloc msg", x.getLocalizedMessage()); 250 assertFalse(x.isLocalized()); 251 252 t = new IOException ("unloc msg"); 254 Throwable t2 = new IOException ("unloc msg #2"); 255 err.annotate(t, ErrorManager.UNKNOWN, null, null, t2, null); 256 x = NbErrorManager.createExc(t, null, null); 257 assertEquals(Level.WARNING, x.getSeverity()); 258 assertEquals("unloc msg", x.getMessage()); 259 assertEquals("unloc msg", x.getLocalizedMessage()); 260 assertFalse(x.isLocalized()); 261 262 t = new IOException ("unloc msg"); 265 err.annotate(t, ErrorManager.USER, null, "loc msg", null, null); 266 x = NbErrorManager.createExc(t, null, null); 267 assertEquals(1973, x.getSeverity().intValue()); 268 assertEquals("unloc msg", x.getMessage()); 269 assertEquals("loc msg", x.getLocalizedMessage()); 270 assertTrue(x.isLocalized()); 271 272 t = new IOException ("unloc msg"); 274 t2 = new IOException ("unloc msg #2"); 275 err.annotate(t2, ErrorManager.USER, null, "loc msg", null, null); 276 err.annotate(t, ErrorManager.UNKNOWN, null, null, t2, null); 277 x = NbErrorManager.createExc(t, null, null); 278 assertEquals(1973, x.getSeverity().intValue()); 279 assertEquals("unloc msg", x.getMessage()); 280 assertEquals("loc msg", x.getLocalizedMessage()); 281 assertTrue(x.isLocalized()); 282 283 t2 = new IOException ("loc msg"); 285 err.annotate(t2, ErrorManager.USER, null, "loc msg", null, null); 286 t = new IOException ("loc msg"); 287 err.annotate(t, ErrorManager.USER, null, null, t2, null); 288 x = NbErrorManager.createExc(t, null, null); 289 assertEquals(1973, x.getSeverity().intValue()); 290 assertEquals("loc msg", x.getMessage()); 291 assertEquals("loc msg", x.getLocalizedMessage()); 292 assertTrue(x.isLocalized()); 295 296 } 297 298 public void testPerPetrKuzelsRequestInIssue62836() throws Exception { 299 class My extends Exception { 300 public My() { 301 super("Ahoj"); 302 } 303 } 304 305 My my = new My(); 306 307 err.notify(err.INFORMATIONAL, my); 308 err.notify(err.USER, my); 309 310 String output = readLog(); 311 waitEQ(); 313 314 int report = output.indexOf("My: Ahoj"); 315 assertTrue("There is one exception reported: " + output, report > 0); 316 int next = output.indexOf("My: Ahoj", report + 1); 317 assertEquals("No next exceptions there (after " + report + "):\n" + output, -1, next); 318 } 319 320 private void waitEQ() throws InterruptedException , InvocationTargetException { 321 SwingUtilities.invokeAndWait(new Runnable () { 322 public void run() { 323 } 324 }); 325 } 326 327 public void testErrorManagerCompatibilityAsDescribedInIssue79227() throws Exception { 328 MockDD.lastDescriptor = null; 329 330 Exception ex = new ClassNotFoundException (); 331 ErrorManager em = ErrorManager.getDefault(); 332 String msg = "LocMsg"; 333 em.annotate(ex, msg); 334 em.notify(ErrorManager.USER, ex); 336 waitEQ(); 337 assertNotNull("Mock descriptor called", MockDD.lastDescriptor); 338 assertEquals("Info msg", NotifyDescriptor.INFORMATION_MESSAGE, MockDD.lastDescriptor.getMessageType()); 339 } 340 341 public void testUIExceptionsTriggersTheDialog() throws Exception { 342 MockDD.lastDescriptor = null; 343 344 Exception ex = new IOException (); 345 ErrorManager em = ErrorManager.getDefault(); 346 em.annotate(ex, ErrorManager.USER, "bla", "blaLoc", null, null); 347 Exceptions.printStackTrace(ex); 348 349 waitEQ(); 350 assertNotNull("Mock descriptor called", MockDD.lastDescriptor); 351 assertEquals("Info msg", NotifyDescriptor.INFORMATION_MESSAGE, MockDD.lastDescriptor.getMessageType()); 352 } 353 public void testUIExceptionsTriggersTheDialogWithWarningPlus1() throws Exception { 354 MockDD.lastDescriptor = null; 355 356 Exception ex = new IOException (); 357 ErrorManager em = ErrorManager.getDefault(); 358 em.annotate(ex, ErrorManager.USER, "bla", "blaLoc", null, null); 359 Logger.global.log(OwnLevel.UNKNOWN, "someerror", ex); 360 361 waitEQ(); 362 assertNotNull("Mock descriptor called", MockDD.lastDescriptor); 363 assertEquals("Info msg", NotifyDescriptor.INFORMATION_MESSAGE, MockDD.lastDescriptor.getMessageType()); 364 } 365 366 public void testCatchMarker() throws Exception { 368 try { 369 m1(); 370 fail(); 371 } catch (IOException e) { 372 err.notify(ErrorManager.INFORMATIONAL, e); 373 String s = readLog(); 374 assertTrue("added [catch] marker in simple cases", s.indexOf("[catch] at " + NbErrorManagerTest.class.getName() + ".testCatchMarker") != -1); 375 } 376 try { 377 m3(); 378 fail(); 379 } catch (IOException e) { 380 err.notify(ErrorManager.INFORMATIONAL, e); 381 String s = readLog(); 382 assertTrue("added [catch] marker in compound exception", s.indexOf("[catch] at " + NbErrorManagerTest.class.getName() + ".testCatchMarker") != -1); 383 } 384 try { 385 m5(); 386 fail(); 387 } catch (InterruptedException e) { 388 err.notify(ErrorManager.INFORMATIONAL, e); 389 String s = readLog(); 390 assertTrue("added [catch] marker in multiply compound exception", s.indexOf("[catch] at " + NbErrorManagerTest.class.getName() + ".testCatchMarker") != -1); 391 } 392 try { 393 throw new IOException ("main line\ndata 1\ndata 2\ndata 3\ndata 4"); 394 } catch (IOException e) { 395 err.notify(ErrorManager.INFORMATIONAL, e); 396 String s = readLog(); 397 assertTrue("added [catch] marker in an actual stack trace line: " + s, s.indexOf("[catch] at " + NbErrorManagerTest.class.getName() + ".testCatchMarker") != -1); 398 } 399 } 400 private static void m1() throws IOException { 401 m2(); 402 } 403 private static void m2() throws IOException { 404 throw new IOException (); 405 } 406 private static void m3() throws IOException { 407 try { 408 m4(); 409 } catch (ClassNotFoundException e) { 410 throw (IOException ) new IOException ().initCause(e); 411 } 412 } 413 private static void m4() throws ClassNotFoundException { 414 throw new ClassNotFoundException (); 415 } 416 private static void m5() throws InterruptedException { 417 try { 418 m3(); 419 } catch (IOException e) { 420 throw (InterruptedException ) new InterruptedException ().initCause(e); 421 } 422 } 423 424 private String readLog() throws IOException { 425 LogManager.getLogManager().readConfiguration(); 426 427 File log = new File (new File (new File (getWorkDir(), "var"), "log"), "messages.log"); 428 assertTrue("Log file exists: " + log, log.canRead()); 429 430 FileInputStream is = new FileInputStream (log); 431 432 byte[] arr = new byte[(int)log.length()]; 433 int r = is.read(arr); 434 assertEquals("all read", arr.length, r); 435 is.close(); 436 437 new FileOutputStream (log).close(); 439 return new String (arr); 440 } 441 442 public static final class MockDD extends DialogDisplayer { 443 static NotifyDescriptor lastDescriptor; 444 445 public Object notify(NotifyDescriptor descriptor) { 446 lastDescriptor = descriptor; 447 return null; 448 } 449 450 public Dialog createDialog(DialogDescriptor descriptor) { 451 lastDescriptor = descriptor; 452 return new JDialog () { 453 @SuppressWarnings ("deprecation") 454 public void show() {} 455 }; 456 } 457 458 } 459 private static final class OwnLevel extends Level { 460 public static final Level UNKNOWN = new OwnLevel("UNKNOWN", Level.WARNING.intValue() + 1); 462 private OwnLevel(String s, int i) { 463 super(s, i); 464 } 465 } } 467 | Popular Tags |