KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > ErrorManagerTest


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.*;
23 import java.util.*;
24 import org.netbeans.performance.Benchmark;
25
26 public class ErrorManagerTest extends Benchmark {
27
28     static {
29     Properties prop = System.getProperties();
30         prop.put("perf.test.enabled", "-5");
31 // prop.put("perf.test.disabled", "0x1000000");
32
}
33
34     private static final ErrorManager enabled;
35     private static final ErrorManager disabled;
36
37     static {
38         ErrorManager en = ErrorManager.getDefault().getInstance("perf.test.enabled");
39         enabled = en.isLoggable(ErrorManager.INFORMATIONAL) ? en : null;
40         ErrorManager dis = ErrorManager.getDefault().getInstance("perf.test.disabled");
41         disabled = dis.isLoggable(ErrorManager.INFORMATIONAL) ? dis : null;
42     assertNull("disabled is loggable", disabled);
43     }
44     
45     
46     public ErrorManagerTest(String JavaDoc name) {
47         super( name );
48     }
49  
50     public void testLogEnabled() throws Exception JavaDoc {
51         int count = getIterationCount();
52         ErrorManager en = ErrorManager.getDefault().getInstance("perf.test.enabled");
53
54         while( count-- > 0 ) {
55             // do the stuff here,
56
en.log("Logging event #" + count);
57         }
58     }
59
60     public void testLogDisabled() throws Exception JavaDoc {
61         int count = getIterationCount();
62         ErrorManager dis = ErrorManager.getDefault().getInstance("perf.test.disabled");
63
64         while( count-- > 0 ) {
65             // do the stuff here,
66
dis.log("Logging event #" + count);
67         }
68     }
69     
70     public void testCheckedEnabled() throws Exception JavaDoc {
71         int count = getIterationCount();
72         ErrorManager en = ErrorManager.getDefault().getInstance("perf.test.enabled");
73
74         while( count-- > 0 ) {
75             // do the stuff here,
76
if(en.isLoggable(ErrorManager.INFORMATIONAL)) en.log("Logging event #" + count);
77         }
78     }
79
80     public void testCheckedDisabled() throws Exception JavaDoc {
81         int count = getIterationCount();
82         ErrorManager dis = ErrorManager.getDefault().getInstance("perf.test.disabled");
83
84         while( count-- > 0 ) {
85             // do the stuff here,
86
if(dis.isLoggable(ErrorManager.INFORMATIONAL)) dis.log("Logging event #" + count);
87         }
88     }
89
90     public void testNullEnabled() throws Exception JavaDoc {
91         int count = getIterationCount();
92
93         while( count-- > 0 ) {
94             // do the stuff here,
95
if(enabled != null) enabled.log("Logging event #" + count);
96         }
97     }
98
99     public void testNullDisabled() throws Exception JavaDoc {
100         int count = getIterationCount();
101
102         while( count-- > 0 ) {
103             // do the stuff here,
104
if(disabled != null) disabled.log("Logging event #" + count);
105         }
106     }
107
108     public void testNull16Disabled() throws Exception JavaDoc {
109         int count = getIterationCount();
110
111         while( count-- > 0 ) {
112             // do the stuff here,
113
if(disabled != null) disabled.log("Logging event #" + count);
114         if(disabled != null) disabled.log("Logging event #" + count);
115         if(disabled != null) disabled.log("Logging event #" + count);
116         if(disabled != null) disabled.log("Logging event #" + count);
117         if(disabled != null) disabled.log("Logging event #" + count);
118         if(disabled != null) disabled.log("Logging event #" + count);
119         if(disabled != null) disabled.log("Logging event #" + count);
120         if(disabled != null) disabled.log("Logging event #" + count);
121         if(disabled != null) disabled.log("Logging event #" + count);
122         if(disabled != null) disabled.log("Logging event #" + count);
123         if(disabled != null) disabled.log("Logging event #" + count);
124         if(disabled != null) disabled.log("Logging event #" + count);
125         if(disabled != null) disabled.log("Logging event #" + count);
126         if(disabled != null) disabled.log("Logging event #" + count);
127         if(disabled != null) disabled.log("Logging event #" + count);
128         if(disabled != null) disabled.log("Logging event #" + count);
129         }
130     }
131
132     public static void main(String JavaDoc[] args) {
133     simpleRun( ErrorManager.class );
134     }
135
136     /** Crippled version of NbErrorManager that does the logging the same way */
137     public static final class EM extends ErrorManager {
138     /** The writer to the log file*/
139     private PrintWriter logWriter = new PrintWriter(System.err);
140     
141     /** Minimum value of severity to write message to the log file*/
142     private int minLogSeverity = ErrorManager.INFORMATIONAL + 1; // NOI18N
143

144     /** Prefix preprended to customized loggers, if any. */
145     private String JavaDoc prefix = null;
146     
147     // Make sure two distinct EM impls log differently even with the same name.
148
private int uniquifier = 0; // 0 for root EM (prefix == null), else >= 1
149
static final Map uniquifiedIds = new HashMap(20); // Map<String,Integer>
150

151     
152     /** Initializes the log stream.
153          */

154     private PrintWriter getLogWriter () {
155         return logWriter;
156         }
157
158     public synchronized Throwable JavaDoc annotate (
159             Throwable JavaDoc t,
160             int severity, String JavaDoc message, String JavaDoc localizedMessage,
161             Throwable JavaDoc stackTrace, java.util.Date JavaDoc date
162     ) {
163         return t;
164     }
165
166
167     /** Associates annotations with this thread.
168      *
169      * @param arr array of annotations (or null)
170      */

171     public synchronized Throwable JavaDoc attachAnnotations (Throwable JavaDoc t, Annotation[] arr) {
172         return t;
173     }
174
175     /** Notifies all the exceptions associated with
176      * this thread.
177      */

178     public synchronized void notify (int severity, Throwable JavaDoc t) {
179     }
180
181     public void log(int severity, String JavaDoc s) {
182             if (isLoggable (severity)) {
183             PrintWriter log = getLogWriter ();
184             
185             if (prefix != null) {
186                     boolean showUniquifier;
187                     // Print a unique EM sequence # if there is more than one
188
// with this name. Shortcut: if the # > 1, clearly there are.
189
if (uniquifier > 1) {
190                     showUniquifier = true;
191                     } else if (uniquifier == 1) {
192                     synchronized (uniquifiedIds) {
193                             int count = ((Integer JavaDoc)uniquifiedIds.get(prefix)).intValue();
194                             showUniquifier = count > 1;
195                     }
196                     } else {
197                     throw new IllegalStateException JavaDoc("prefix != null yet uniquifier == 0");
198                     }
199                     if (showUniquifier) {
200                     log.print ("[" + prefix + " #" + uniquifier + "] "); // NOI18N
201
} else {
202                     log.print ("[" + prefix + "] "); // NOI18N
203
}
204             }
205             log.println(s);
206             log.flush();
207             }
208     }
209     
210     /** Allows to test whether messages with given severity will be logged
211          * or not prior to constraction of complicated and time expensive
212          * logging messages.
213          *
214          * @param severity the severity to check
215          * @return false if the next call to log method with the same severity will
216          * discard the message
217          */

218     public boolean isLoggable (int severity) {
219             return severity >= minLogSeverity;
220     }
221     
222     
223     /** Returns an instance with given name. The name
224          * can be dot separated list of names creating
225      * a hierarchy.
226      */

227     public final ErrorManager getInstance(String JavaDoc name) {
228             EM newEM = new EM();
229             newEM.prefix = (prefix == null) ? name : prefix + '.' + name;
230             synchronized (uniquifiedIds) {
231             Integer JavaDoc i = (Integer JavaDoc)uniquifiedIds.get(newEM.prefix);
232             if (i == null) {
233                     newEM.uniquifier = 1;
234             } else {
235                     newEM.uniquifier = i.intValue() + 1;
236             }
237             uniquifiedIds.put(newEM.prefix, new Integer JavaDoc(newEM.uniquifier));
238             }
239             newEM.minLogSeverity = minLogSeverity;
240             String JavaDoc prop = newEM.prefix;
241             while (prop != null) {
242             String JavaDoc value = System.getProperty (prop);
243             //System.err.println ("Trying; prop=" + prop + " value=" + value);
244
if (value != null) {
245                     try {
246                     newEM.minLogSeverity = Integer.parseInt (value);
247                     } catch (NumberFormatException JavaDoc nfe) {
248                     notify (WARNING, nfe);
249                     }
250                 break;
251             } else {
252                 int idx = prop.lastIndexOf ('.');
253                 if (idx == -1)
254                     prop = null;
255                 else
256                     prop = prop.substring (0, idx);
257             }
258             }
259             //System.err.println ("getInstance: prefix=" + prefix + " mls=" + minLogSeverity + " name=" + name + " prefix2=" + newEM.prefix + " mls2=" + newEM.minLogSeverity);
260
return newEM;
261     }
262     
263
264     /** Finds annotations associated with given exception.
265      * @param t the exception
266      * @return array of annotations or null
267      */

268     public synchronized Annotation[] findAnnotations (Throwable JavaDoc t) {
269         return new Annotation[0];
270     }
271
272     public String JavaDoc toString() {
273             return super.toString() + "<" + prefix + "," + minLogSeverity + ">"; // NOI18N
274
}
275     }
276 }
277
Popular Tags