KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > ErrorManagerCyclicDepTest


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.util.HashMap JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.TreeMap JavaDoc;
26 import java.util.logging.Formatter JavaDoc;
27 import java.util.logging.Level JavaDoc;
28 import java.util.logging.LogManager JavaDoc;
29 import java.util.logging.LogRecord JavaDoc;
30 import java.util.logging.Logger JavaDoc;
31 import java.util.logging.SimpleFormatter JavaDoc;
32 import org.netbeans.junit.*;
33
34 /** Verify that things delegation of ErrorManager to logging and back does not cause
35  * stack overflows.
36  *
37  * @author Jaroslav Tulach
38  */

39 public class ErrorManagerCyclicDepTest extends NbTestCase {
40
41     
42     public ErrorManagerCyclicDepTest(java.lang.String JavaDoc testName) {
43         super(testName);
44     }
45
46     protected void setUp() throws Exception JavaDoc {
47         Logger JavaDoc l = new LoggerAdapter("double");
48         LogManager.getLogManager().addLogger(l);
49     }
50
51     public void testSendLogMsg() {
52         ErrorManager e = ErrorManager.getDefault().getInstance("double");
53         e.log(ErrorManager.WARNING, "Ahoj");
54     }
55
56     public void testSendNotify() {
57         ErrorManager e = ErrorManager.getDefault().getInstance("double");
58         e.notify(ErrorManager.WARNING, new Exception JavaDoc("Ahoj"));
59     }
60
61     /** based on
62      * https://thinnbeditor.dev.java.net/source/browse/thinnbeditor/thinnbeditor/src/net/java/dev/thinnbeditor/logging/LoggerAdapter.java?rev=1.1&view=auto&content-type=text/vnd.viewcvs-markup
63      */

64     private static final class LoggerAdapter extends Logger JavaDoc {
65         private static final Map JavaDoc<Level JavaDoc,Integer JavaDoc> levelMap = new HashMap JavaDoc<Level JavaDoc,Integer JavaDoc>();
66         private static final Map JavaDoc<Integer JavaDoc,Level JavaDoc> errorManagerMap = new TreeMap JavaDoc<Integer JavaDoc,Level JavaDoc>();
67         private static final Map JavaDoc<Level JavaDoc,Integer JavaDoc> exceptionLevelMap = new HashMap JavaDoc<Level JavaDoc,Integer JavaDoc>();
68         
69         static {
70             levelMap.put(Level.SEVERE, new Integer JavaDoc(ErrorManager.ERROR));
71             levelMap.put(Level.WARNING, new Integer JavaDoc(ErrorManager.WARNING));
72             levelMap.put(Level.INFO, new Integer JavaDoc(ErrorManager.INFORMATIONAL));
73             levelMap.put(Level.CONFIG, new Integer JavaDoc(ErrorManager.INFORMATIONAL));
74             levelMap.put(Level.FINE, new Integer JavaDoc(3));
75             levelMap.put(Level.FINER, new Integer JavaDoc(2));
76             levelMap.put(Level.FINEST, new Integer JavaDoc(1));
77             
78             for (Iterator JavaDoc<Map.Entry JavaDoc<Level JavaDoc,Integer JavaDoc>> i = levelMap.entrySet().iterator(); i.hasNext(); ) {
79                 Map.Entry JavaDoc<Level JavaDoc,Integer JavaDoc> entry = i.next();
80                 errorManagerMap.put(entry.getValue(), entry.getKey());
81             }
82             
83             errorManagerMap.put(new Integer JavaDoc(ErrorManager.INFORMATIONAL), Level.CONFIG);
84             
85             exceptionLevelMap.put(Level.SEVERE, new Integer JavaDoc(ErrorManager.USER));
86             exceptionLevelMap.put(Level.WARNING, new Integer JavaDoc(ErrorManager.USER));
87             exceptionLevelMap.put(Level.INFO, new Integer JavaDoc(ErrorManager.INFORMATIONAL));
88             exceptionLevelMap.put(Level.CONFIG, new Integer JavaDoc(ErrorManager.INFORMATIONAL));
89             exceptionLevelMap.put(Level.FINE, new Integer JavaDoc(3));
90             exceptionLevelMap.put(Level.FINER, new Integer JavaDoc(2));
91             exceptionLevelMap.put(Level.FINEST, new Integer JavaDoc(1));
92         }
93         
94         private ErrorManager errorManager;
95         private final Formatter JavaDoc formatter = new SimpleFormatter JavaDoc();
96         
97         public LoggerAdapter(String JavaDoc name) {
98             super(name, null);
99         }
100
101         private void init() {
102             if (errorManager != null) {
103                 return;
104             }
105             
106             errorManager = ErrorManager.getDefault().getInstance(getName());
107             
108             for (Iterator JavaDoc i = errorManagerMap.entrySet().iterator(); i.hasNext(); ) {
109                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc)i.next();
110                 
111                 int level = ((Integer JavaDoc)entry.getKey()).intValue();
112                 
113                 if (errorManager.isLoggable(level)) {
114                     setLevel((Level JavaDoc)entry.getValue());
115                     break;
116                 }
117             }
118         }
119         
120         public void log(LogRecord JavaDoc record) {
121             init();
122
123             errorManager.log(((Integer JavaDoc)levelMap.get(record.getLevel())).intValue(),
124                 formatter.format(record));
125             
126             if (record.getThrown() != null) {
127                 errorManager.notify(((Integer JavaDoc)exceptionLevelMap.get(
128                     record.getLevel())).intValue(), record.getThrown());
129             }
130         }
131     }
132
133 }
134
Popular Tags