KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > ErrorManagerDelegatesToLoggingTest


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.openide;
21
22 import java.io.IOException JavaDoc;
23 import java.io.PrintWriter JavaDoc;
24 import java.io.StringBufferInputStream JavaDoc;
25 import java.io.StringWriter JavaDoc;
26 import java.util.logging.Handler JavaDoc;
27 import java.util.logging.Level JavaDoc;
28 import java.util.logging.LogManager JavaDoc;
29 import java.util.logging.LogRecord JavaDoc;
30 import junit.framework.*;
31 import org.netbeans.junit.*;
32
33 /** Test for general ErrorManager functionality.
34  *
35  * @author Jaroslav Tulach
36  */

37 public class ErrorManagerDelegatesToLoggingTest extends NbTestCase {
38
39     public ErrorManagerDelegatesToLoggingTest(java.lang.String JavaDoc testName) {
40         super(testName);
41     }
42     
43     protected void setUp () throws IOException JavaDoc {
44         assertNull ("No ErrorManager in lookup", org.openide.util.Lookup.getDefault ().lookup (ErrorManager.class));
45         
46         
47         String JavaDoc config =
48             "handlers=" + MyHandler.class.getName() + "\n" +
49             ".level=50\n";
50         LogManager.getLogManager().readConfiguration(new StringBufferInputStream JavaDoc(config));
51
52         MyHandler.messages.setLength(0);
53     }
54     
55     /** Test of getDefault method, of class org.openide.ErrorManager. */
56     public void testGetDefault() {
57         assertNotNull("There has to be a manager", ErrorManager.getDefault ());
58     }
59     
60     /** Test of notify method, of class org.openide.ErrorManager. */
61     public void testNotify() {
62         Throwable JavaDoc t = new Throwable JavaDoc ();
63         ErrorManager.getDefault ().notify (ErrorManager.INFORMATIONAL, t);
64         MyHandler.assertNotify (ErrorManager.INFORMATIONAL, t);
65         t = new Throwable JavaDoc ();
66         ErrorManager.getDefault ().notify (t);
67         MyHandler.assertNotify (ErrorManager.EXCEPTION, t);
68     }
69     
70     /** Test of log method, of class org.openide.ErrorManager. */
71     public void testLog() {
72         ErrorManager.getDefault ().log (ErrorManager.INFORMATIONAL, "A text");
73         MyHandler.assertLog (ErrorManager.INFORMATIONAL, "A text");
74         ErrorManager.getDefault ().log ("Another text");
75         MyHandler.assertLog (ErrorManager.INFORMATIONAL, "Another text");
76     }
77     
78     /** Test of isLoggable method, of class org.openide.ErrorManager. */
79     public void testIsLoggable() {
80         ErrorManager.getDefault ().isLoggable(ErrorManager.INFORMATIONAL);
81         ErrorManager.getDefault ().isLoggable(ErrorManager.INFORMATIONAL + 1);
82     }
83     
84     /** Test of annotate method, of class org.openide.ErrorManager. */
85     public void testReturnValues () {
86         Throwable JavaDoc t = new Throwable JavaDoc ();
87         Throwable JavaDoc value = ErrorManager.getDefault ().annotate(t, ErrorManager.INFORMATIONAL, null, null, null, null);
88         assertEquals ("Annotate must return the same exception", t, value);
89         
90         value = ErrorManager.getDefault ().copyAnnotation (t, new Throwable JavaDoc ());
91         assertEquals ("copyAnnotation must return the same exception", t, value);
92         
93         value = ErrorManager.getDefault ().attachAnnotations(t, new ErrorManager.Annotation[0]);
94         assertEquals ("attachAnnotations must return the same exception", t, value);
95         
96     }
97
98     public void testThatWeNotifyAnException() throws Exception JavaDoc {
99         Throwable JavaDoc t = new NullPointerException JavaDoc("npe");
100         Throwable JavaDoc v = new ClassNotFoundException JavaDoc("cnf", t);
101
102         Throwable JavaDoc a = new IOException JavaDoc();
103         ErrorManager.getDefault().annotate(a, v);
104         ErrorManager.getDefault().notify(a);
105
106         assertEquals("Last throwable is a", a, MyHandler.lastThrowable);
107         assertNotNull("And it has a cause", MyHandler.lastThrowable.getCause());
108
109         StringWriter JavaDoc w = new StringWriter JavaDoc();
110         MyHandler.lastThrowable.getCause().printStackTrace(new PrintWriter JavaDoc(w));
111         String JavaDoc msg = w.toString();
112
113         if (msg.indexOf("npe") == -1) fail("there should be npe: " + msg);
114         if (msg.indexOf("cnf") == -1) fail("there should be cnf: " + msg);
115
116     }
117
118     public void testAnnotatedMessagesShallBePresentInPrintStackTrace() throws Exception JavaDoc {
119         Throwable JavaDoc t = new NullPointerException JavaDoc("npe");
120
121         ErrorManager.getDefault().annotate(t, ErrorManager.UNKNOWN, "Ahoj Null!", null, null, null);
122
123         StringWriter JavaDoc w = new StringWriter JavaDoc();
124         t.printStackTrace(new PrintWriter JavaDoc(w));
125         String JavaDoc msg = w.toString();
126
127         if (msg.indexOf("npe") == -1) fail("there should be npe: " + msg);
128         if (msg.indexOf("Ahoj Null") == -1) fail("there should be Ahoj Null: " + msg);
129
130     }
131
132     public void testAttachLocalizedMessageForClassNFEIfNoMsg() {
133         Exception JavaDoc e = new ClassNotFoundException JavaDoc("Help");
134         String JavaDoc msg = "me please";
135         
136         ErrorManager.getDefault().annotate(e, msg);
137
138         Object JavaDoc[] arr = ErrorManager.getDefault().findAnnotations(e);
139         
140         assertNotNull("Arr exists", arr);
141     }
142     
143     
144     //
145
// Manager to delegate to
146
//
147
public static final class MyHandler extends Handler JavaDoc {
148         public static final StringBuffer JavaDoc messages = new StringBuffer JavaDoc ();
149         
150         private String JavaDoc prefix;
151         
152         private static int lastSeverity;
153         private static Throwable JavaDoc lastThrowable;
154         private static String JavaDoc lastText;
155
156         public static void assertNotify (int sev, Throwable JavaDoc t) {
157             assertEquals ("Severity is same", sev, lastSeverity);
158             assertSame ("Throwable is the same", t, lastThrowable);
159             lastThrowable = null;
160             lastSeverity = -1;
161         }
162         
163         public static void assertLog (int sev, String JavaDoc t) {
164             assertEquals ("Severity is same", sev, lastSeverity);
165             assertEquals ("Text is the same", t, lastText);
166             lastText = null;
167             lastSeverity = -1;
168         }
169
170         public void publish(LogRecord JavaDoc record) {
171             messages.append(record.getMessage());
172             
173             lastText = record.getMessage();
174             lastThrowable = record.getThrown();
175
176             Level JavaDoc l = record.getLevel();
177             if (l.intValue() >= Level.FINEST.intValue()) {
178                 lastSeverity = ErrorManager.INFORMATIONAL;
179             }
180             if (l.intValue() >= Level.WARNING.intValue()) {
181                 lastSeverity = ErrorManager.WARNING;
182             }
183             if (l.intValue() >= Level.SEVERE.intValue()) {
184                 lastSeverity = ErrorManager.EXCEPTION;
185             }
186         }
187
188         public void flush() {
189         }
190
191         public void close() throws SecurityException JavaDoc {
192         }
193         
194     }
195     
196 }
197
Popular Tags