001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.core;
043:
044: import java.util.HashMap;
045: import java.util.Iterator;
046: import java.util.Map;
047: import java.util.TreeMap;
048: import java.util.logging.Formatter;
049: import java.util.logging.Level;
050: import java.util.logging.LogManager;
051: import java.util.logging.LogRecord;
052: import java.util.logging.Logger;
053: import java.util.logging.SimpleFormatter;
054: import org.netbeans.junit.*;
055: import org.openide.ErrorManager;
056:
057: /** Verify that things delegation of ErrorManager to logging and back does not cause
058: * stack overflows.
059: *
060: * @author Jaroslav Tulach
061: */
062: public class NbErrorManagerCyclicDepTest extends NbTestCase {
063:
064: public NbErrorManagerCyclicDepTest(java.lang.String testName) {
065: super (testName);
066: }
067:
068: protected void setUp() throws Exception {
069: Logger l = new LoggerAdapter("double");
070: LogManager.getLogManager().addLogger(l);
071: }
072:
073: public void testSendLogMsg() {
074: ErrorManager e = ErrorManager.getDefault()
075: .getInstance("double");
076: e.log(ErrorManager.WARNING, "Ahoj");
077: }
078:
079: public void testSendNotify() {
080: ErrorManager e = ErrorManager.getDefault()
081: .getInstance("double");
082: Logger.global.log(Level.WARNING, null, new Exception("Ahoj"));
083: }
084:
085: /** based on
086: * 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
087: */
088: private static final class LoggerAdapter extends Logger {
089: private static final Map levelMap = new HashMap();
090: private static final Map errorManagerMap = new TreeMap();
091: private static final Map exceptionLevelMap = new HashMap();
092:
093: static {
094: levelMap.put(Level.SEVERE, new Integer(ErrorManager.ERROR));
095: levelMap.put(Level.WARNING, new Integer(
096: ErrorManager.WARNING));
097: levelMap.put(Level.INFO, new Integer(
098: ErrorManager.INFORMATIONAL));
099: levelMap.put(Level.CONFIG, new Integer(
100: ErrorManager.INFORMATIONAL));
101: levelMap.put(Level.FINE, new Integer(3));
102: levelMap.put(Level.FINER, new Integer(2));
103: levelMap.put(Level.FINEST, new Integer(1));
104:
105: for (Iterator i = levelMap.entrySet().iterator(); i
106: .hasNext();) {
107: Map.Entry entry = (Map.Entry) i.next();
108: errorManagerMap.put(entry.getValue(), entry.getKey());
109: }
110:
111: errorManagerMap.put(
112: new Integer(ErrorManager.INFORMATIONAL),
113: Level.CONFIG);
114:
115: exceptionLevelMap.put(Level.SEVERE, new Integer(
116: ErrorManager.USER));
117: exceptionLevelMap.put(Level.WARNING, new Integer(
118: ErrorManager.USER));
119: exceptionLevelMap.put(Level.INFO, new Integer(
120: ErrorManager.INFORMATIONAL));
121: exceptionLevelMap.put(Level.CONFIG, new Integer(
122: ErrorManager.INFORMATIONAL));
123: exceptionLevelMap.put(Level.FINE, new Integer(3));
124: exceptionLevelMap.put(Level.FINER, new Integer(2));
125: exceptionLevelMap.put(Level.FINEST, new Integer(1));
126: }
127:
128: private ErrorManager errorManager;
129: private final Formatter formatter = new SimpleFormatter();
130:
131: public LoggerAdapter(String name) {
132: super (name, null);
133: }
134:
135: private void init() {
136: if (errorManager != null) {
137: return;
138: }
139:
140: errorManager = ErrorManager.getDefault().getInstance(
141: getName());
142:
143: for (Iterator i = errorManagerMap.entrySet().iterator(); i
144: .hasNext();) {
145: Map.Entry entry = (Map.Entry) i.next();
146:
147: int level = ((Integer) entry.getKey()).intValue();
148:
149: if (errorManager.isLoggable(level)) {
150: setLevel((Level) entry.getValue());
151: break;
152: }
153: }
154: }
155:
156: public void log(LogRecord record) {
157: init();
158:
159: Integer l = (Integer) levelMap.get(record.getLevel());
160: if (l == null) {
161: l = Integer.valueOf(1);
162: }
163: errorManager.log(l.intValue(), formatter.format(record));
164:
165: if (record.getThrown() != null) {
166: Integer x = (Integer) exceptionLevelMap.get(record
167: .getLevel());
168: if (x == null) {
169: x = new Integer(1);
170: }
171: errorManager.notify(x.intValue(), record.getThrown());
172: }
173: }
174: }
175:
176: }
|