KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > NbErrorManagerTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.core;
21
22 import java.awt.Dialog JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.FileInputStream JavaDoc;
25 import java.io.FileOutputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.lang.reflect.InvocationTargetException JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.MissingResourceException JavaDoc;
30 import java.util.logging.Level JavaDoc;
31 import java.util.logging.LogManager JavaDoc;
32 import java.util.logging.Logger JavaDoc;
33 import javax.swing.JDialog JavaDoc;
34 import javax.swing.SwingUtilities JavaDoc;
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 JavaDoc;
47
48
49 /**
50  * Test the core error manager impl.
51  * @author Jesse Glick
52  * @see "#18141"
53  */

54 public final class NbErrorManagerTest extends NbTestCase {
55     public NbErrorManagerTest(String JavaDoc s) {
56         super(s);
57     }
58
59     public static Test suite() {
60         //return new NbErrorManagerTest("testNestedThrowables");
61
return new NbTestSuite(NbErrorManagerTest.class);
62     }
63     
64     private ErrorManager err;
65     protected void setUp() throws Exception JavaDoc {
66         clearWorkDir();
67
68         MockServices.setServices(MockDD.class);
69         
70         System.setProperty("netbeans.user", getWorkDirPath());
71         // init the whole system
72
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 JavaDoc {
86         assertTrue(err.isNotifiable(ErrorManager.EXCEPTION));
87         NullPointerException JavaDoc npe = new NullPointerException JavaDoc("unloc msg");
88         err.notify(ErrorManager.INFORMATIONAL, npe);
89         String JavaDoc 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 JavaDoc {
95         assertFalse(err.isLoggable(ErrorManager.INFORMATIONAL));
96         err.log("some msg");
97         String JavaDoc 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     /** @see "#15611" */
121     public void testNestedThrowables() throws Exception JavaDoc {
122         NullPointerException JavaDoc npe = new NullPointerException JavaDoc("unloc msg");
123         ClassNotFoundException JavaDoc cnfe = new ClassNotFoundException JavaDoc("other msg", npe);
124         err.notify(ErrorManager.INFORMATIONAL, cnfe);
125         String JavaDoc s = readLog();
126         assertTrue(s.indexOf("java.lang.NullPointerException: unloc msg") != -1);
127         assertTrue(s.indexOf("java.lang.ClassNotFoundException") != -1);
128         npe = new NullPointerException JavaDoc("msg1");
129         IOException JavaDoc ioe = new IOException JavaDoc("msg2");
130         err.annotate(ioe, npe);
131         InvocationTargetException JavaDoc ite = new InvocationTargetException JavaDoc(ioe, "msg3");
132         IllegalStateException JavaDoc ise = new IllegalStateException JavaDoc("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 JavaDoc {
143         NullPointerException JavaDoc npe = new NullPointerException JavaDoc("unloc msg");
144         err.annotate(npe, "loc msg #1");
145         err.notify(ErrorManager.INFORMATIONAL, npe);
146         String JavaDoc s = readLog();
147         assertTrue(s.indexOf("java.lang.NullPointerException: unloc msg") != -1);
148         assertTrue(s.indexOf("loc msg #1") != -1);
149         npe = new NullPointerException JavaDoc("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 JavaDoc("new unloc msg");
155         IOException JavaDoc ioe = new IOException JavaDoc("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 JavaDoc {
164         Exception JavaDoc e1 = new Exception JavaDoc("msg1");
165         // #19114: deeply nested loc msgs should be used
166
err.annotate(e1, "some loc msg");
167         Exception JavaDoc e2 = new Exception JavaDoc("msg2");
168         err.annotate(e2, e1);
169         Exception JavaDoc e3 = new Exception JavaDoc("msg3");
170         err.annotate(e3, e2);
171         Exception JavaDoc e4 = new Exception JavaDoc("msg4");
172         err.annotate(e3, e4);
173         err.notify(ErrorManager.INFORMATIONAL, e3);
174         String JavaDoc 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     /** @see "#19487" */
183     public void testLoops() throws Exception JavaDoc {
184         Exception JavaDoc e1 = new Exception JavaDoc("msg1");
185         Exception JavaDoc e2 = new Exception JavaDoc("msg2");
186         err.annotate(e2, e1);
187         Exception JavaDoc e3 = new Exception JavaDoc("msg3");
188         err.annotate(e3, e2);
189         err.annotate(e1, e3);
190         err.notify(ErrorManager.INFORMATIONAL, e1);
191         String JavaDoc 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         // warning from NBEM itself:
196
assertTrue(s.indexOf("cyclic") != -1);
197     }
198     
199     public void testAddedInfo() throws Exception JavaDoc {
200         MissingResourceException JavaDoc mre = new MissingResourceException JavaDoc("msg1", "the.class.Name", "the-key");
201         err.notify(ErrorManager.INFORMATIONAL, mre);
202         String JavaDoc 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 JavaDoc saxpe = new SAXParseException JavaDoc("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     /**
217      * Actually just tests the same code used when running NE.
218      */

219     public void testNotifyException() throws Exception JavaDoc {
220         IOException JavaDoc ioe = new IOException JavaDoc("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         // could do more here...
227
}
228     
229     /**
230      * Check that UNKNOWN works.
231      * @see "#30947"
232      */

233     public void testUnknownSeverity() throws Exception JavaDoc {
234         
235         // Simple exception is EXCEPTION.
236
Throwable JavaDoc t = new IOException JavaDoc("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         // Same when there is unloc debug info attached.
244
t = new IOException JavaDoc("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         // Nested exceptions don't necessarily change anything severity-wise.
253
t = new IOException JavaDoc("unloc msg");
254         Throwable JavaDoc t2 = new IOException JavaDoc("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         // But annotations at a particular severity level (usually localized) do
263
// set the severity for the exception.
264
t = new IOException JavaDoc("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         // And that works even if you are just rethrowing someone else's exception.
273
t = new IOException JavaDoc("unloc msg");
274         t2 = new IOException JavaDoc("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         // Almost the same test, but to mimic #31254 message == localizedMessage:
284
t2 = new IOException JavaDoc("loc msg");
285         err.annotate(t2, ErrorManager.USER, null, "loc msg", null, null);
286         t = new IOException JavaDoc("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         // Note that it is stil considered localized even though the messages
293
// are equals: there is a localized annotation.
294
assertTrue(x.isLocalized());
295         
296     }
297     
298     public void testPerPetrKuzelsRequestInIssue62836() throws Exception JavaDoc {
299         class My extends Exception JavaDoc {
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 JavaDoc output = readLog();
311         // wait for a dialog to be shown
312
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 JavaDoc, InvocationTargetException JavaDoc {
321         SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
322             public void run() {
323             }
324         });
325     }
326
327     public void testErrorManagerCompatibilityAsDescribedInIssue79227() throws Exception JavaDoc {
328         MockDD.lastDescriptor = null;
329
330         Exception JavaDoc ex = new ClassNotFoundException JavaDoc();
331         ErrorManager em = ErrorManager.getDefault();
332         String JavaDoc msg = "LocMsg";
333         em.annotate(ex, msg);
334         em.notify(ErrorManager.USER, ex); // Issue 65116 - don't show the exception to the user
335

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 JavaDoc {
342         MockDD.lastDescriptor = null;
343
344         Exception JavaDoc ex = new IOException JavaDoc();
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 JavaDoc {
354         MockDD.lastDescriptor = null;
355
356         Exception JavaDoc ex = new IOException JavaDoc();
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     // Noticed as part of analysis of #59807 stack trace: Throwable.initCause tricky!
367
public void testCatchMarker() throws Exception JavaDoc {
368         try {
369             m1();
370             fail();
371         } catch (IOException JavaDoc e) {
372             err.notify(ErrorManager.INFORMATIONAL, e);
373             String JavaDoc 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 JavaDoc e) {
380             err.notify(ErrorManager.INFORMATIONAL, e);
381             String JavaDoc 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 JavaDoc e) {
388             err.notify(ErrorManager.INFORMATIONAL, e);
389             String JavaDoc 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 JavaDoc("main line\ndata 1\ndata 2\ndata 3\ndata 4");
394         } catch (IOException JavaDoc e) {
395             err.notify(ErrorManager.INFORMATIONAL, e);
396             String JavaDoc 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 JavaDoc {
401         m2();
402     }
403     private static void m2() throws IOException JavaDoc {
404         throw new IOException JavaDoc();
405     }
406     private static void m3() throws IOException JavaDoc {
407         try {
408             m4();
409         } catch (ClassNotFoundException JavaDoc e) {
410             throw (IOException JavaDoc) new IOException JavaDoc().initCause(e);
411         }
412     }
413     private static void m4() throws ClassNotFoundException JavaDoc {
414         throw new ClassNotFoundException JavaDoc();
415     }
416     private static void m5() throws InterruptedException JavaDoc {
417         try {
418             m3();
419         } catch (IOException JavaDoc e) {
420             throw (InterruptedException JavaDoc) new InterruptedException JavaDoc().initCause(e);
421         }
422     }
423
424     private String JavaDoc readLog() throws IOException JavaDoc {
425         LogManager.getLogManager().readConfiguration();
426
427         File JavaDoc log = new File JavaDoc(new File JavaDoc(new File JavaDoc(getWorkDir(), "var"), "log"), "messages.log");
428         assertTrue("Log file exists: " + log, log.canRead());
429
430         FileInputStream JavaDoc is = new FileInputStream JavaDoc(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 JavaDoc(log).close(); // truncate
438

439         return new String JavaDoc(arr);
440     }
441
442     public static final class MockDD extends DialogDisplayer {
443         static NotifyDescriptor lastDescriptor;
444         
445         public Object JavaDoc notify(NotifyDescriptor descriptor) {
446             lastDescriptor = descriptor;
447             return null;
448         }
449
450         public Dialog JavaDoc createDialog(DialogDescriptor descriptor) {
451             lastDescriptor = descriptor;
452             return new JDialog JavaDoc() {
453                 @SuppressWarnings JavaDoc("deprecation")
454                 public void show() {}
455             };
456         }
457         
458     }
459     private static final class OwnLevel extends Level JavaDoc {
460         public static final Level JavaDoc UNKNOWN = new OwnLevel("UNKNOWN", Level.WARNING.intValue() + 1); // NOI18N
461

462         private OwnLevel(String JavaDoc s, int i) {
463             super(s, i);
464         }
465     } // end of UserLevel
466
}
467
Popular Tags