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.openide;
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:
056: /** Verify that things delegation of ErrorManager to logging and back does not cause
057: * stack overflows.
058: *
059: * @author Jaroslav Tulach
060: */
061: public class ErrorManagerCyclicDepTest extends NbTestCase {
062:
063: public ErrorManagerCyclicDepTest(java.lang.String testName) {
064: super (testName);
065: }
066:
067: protected void setUp() throws Exception {
068: Logger l = new LoggerAdapter("double");
069: LogManager.getLogManager().addLogger(l);
070: }
071:
072: public void testSendLogMsg() {
073: ErrorManager e = ErrorManager.getDefault()
074: .getInstance("double");
075: e.log(ErrorManager.WARNING, "Ahoj");
076: }
077:
078: public void testSendNotify() {
079: ErrorManager e = ErrorManager.getDefault()
080: .getInstance("double");
081: e.notify(ErrorManager.WARNING, new Exception("Ahoj"));
082: }
083:
084: /** based on
085: * 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
086: */
087: private static final class LoggerAdapter extends Logger {
088: private static final Map<Level, Integer> levelMap = new HashMap<Level, Integer>();
089: private static final Map<Integer, Level> errorManagerMap = new TreeMap<Integer, Level>();
090: private static final Map<Level, Integer> exceptionLevelMap = new HashMap<Level, Integer>();
091:
092: static {
093: levelMap.put(Level.SEVERE, new Integer(ErrorManager.ERROR));
094: levelMap.put(Level.WARNING, new Integer(
095: ErrorManager.WARNING));
096: levelMap.put(Level.INFO, new Integer(
097: ErrorManager.INFORMATIONAL));
098: levelMap.put(Level.CONFIG, new Integer(
099: ErrorManager.INFORMATIONAL));
100: levelMap.put(Level.FINE, new Integer(3));
101: levelMap.put(Level.FINER, new Integer(2));
102: levelMap.put(Level.FINEST, new Integer(1));
103:
104: for (Iterator<Map.Entry<Level, Integer>> i = levelMap
105: .entrySet().iterator(); i.hasNext();) {
106: Map.Entry<Level, Integer> entry = i.next();
107: errorManagerMap.put(entry.getValue(), entry.getKey());
108: }
109:
110: errorManagerMap.put(
111: new Integer(ErrorManager.INFORMATIONAL),
112: Level.CONFIG);
113:
114: exceptionLevelMap.put(Level.SEVERE, new Integer(
115: ErrorManager.USER));
116: exceptionLevelMap.put(Level.WARNING, new Integer(
117: ErrorManager.USER));
118: exceptionLevelMap.put(Level.INFO, new Integer(
119: ErrorManager.INFORMATIONAL));
120: exceptionLevelMap.put(Level.CONFIG, new Integer(
121: ErrorManager.INFORMATIONAL));
122: exceptionLevelMap.put(Level.FINE, new Integer(3));
123: exceptionLevelMap.put(Level.FINER, new Integer(2));
124: exceptionLevelMap.put(Level.FINEST, new Integer(1));
125: }
126:
127: private ErrorManager errorManager;
128: private final Formatter formatter = new SimpleFormatter();
129:
130: public LoggerAdapter(String name) {
131: super (name, null);
132: }
133:
134: private void init() {
135: if (errorManager != null) {
136: return;
137: }
138:
139: errorManager = ErrorManager.getDefault().getInstance(
140: getName());
141:
142: for (Iterator i = errorManagerMap.entrySet().iterator(); i
143: .hasNext();) {
144: Map.Entry entry = (Map.Entry) i.next();
145:
146: int level = ((Integer) entry.getKey()).intValue();
147:
148: if (errorManager.isLoggable(level)) {
149: setLevel((Level) entry.getValue());
150: break;
151: }
152: }
153: }
154:
155: public void log(LogRecord record) {
156: init();
157:
158: errorManager.log(
159: ((Integer) levelMap.get(record.getLevel()))
160: .intValue(), formatter.format(record));
161:
162: if (record.getThrown() != null) {
163: errorManager.notify(((Integer) exceptionLevelMap
164: .get(record.getLevel())).intValue(), record
165: .getThrown());
166: }
167: }
168: }
169:
170: }
|