KickJava   Java API By Example, From Geeks To Geeks.

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


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.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 import org.openide.ErrorManager;
34
35 /** Verify that things delegation of ErrorManager to logging and back does not cause
36  * stack overflows.
37  *
38  * @author Jaroslav Tulach
39  */

40 public class NbErrorManagerCyclicDepTest extends NbTestCase {
41
42     
43     public NbErrorManagerCyclicDepTest(java.lang.String JavaDoc testName) {
44         super(testName);
45     }
46
47     protected void setUp() throws Exception JavaDoc {
48         Logger JavaDoc l = new LoggerAdapter("double");
49         LogManager.getLogManager().addLogger(l);
50     }
51
52     public void testSendLogMsg() {
53         ErrorManager e = ErrorManager.getDefault().getInstance("double");
54         e.log(ErrorManager.WARNING, "Ahoj");
55     }
56
57     public void testSendNotify() {
58         ErrorManager e = ErrorManager.getDefault().getInstance("double");
59         Logger.global.log(Level.WARNING, null, new Exception JavaDoc("Ahoj"));
60     }
61
62     /** based on
63      * 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
64      */

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