1 19 20 package org.netbeans.core; 21 22 import java.util.HashMap ; 23 import java.util.Iterator ; 24 import java.util.Map ; 25 import java.util.TreeMap ; 26 import java.util.logging.Formatter ; 27 import java.util.logging.Level ; 28 import java.util.logging.LogManager ; 29 import java.util.logging.LogRecord ; 30 import java.util.logging.Logger ; 31 import java.util.logging.SimpleFormatter ; 32 import org.netbeans.junit.*; 33 import org.openide.ErrorManager; 34 35 40 public class NbErrorManagerCyclicDepTest extends NbTestCase { 41 42 43 public NbErrorManagerCyclicDepTest(java.lang.String testName) { 44 super(testName); 45 } 46 47 protected void setUp() throws Exception { 48 Logger 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 ("Ahoj")); 60 } 61 62 65 private static final class LoggerAdapter extends Logger { 66 private static final Map levelMap = new HashMap (); 67 private static final Map errorManagerMap = new TreeMap (); 68 private static final Map exceptionLevelMap = new HashMap (); 69 70 static { 71 levelMap.put(Level.SEVERE, new Integer (ErrorManager.ERROR)); 72 levelMap.put(Level.WARNING, new Integer (ErrorManager.WARNING)); 73 levelMap.put(Level.INFO, new Integer (ErrorManager.INFORMATIONAL)); 74 levelMap.put(Level.CONFIG, new Integer (ErrorManager.INFORMATIONAL)); 75 levelMap.put(Level.FINE, new Integer (3)); 76 levelMap.put(Level.FINER, new Integer (2)); 77 levelMap.put(Level.FINEST, new Integer (1)); 78 79 for (Iterator i = levelMap.entrySet().iterator(); i.hasNext(); ) { 80 Map.Entry entry = (Map.Entry )i.next(); 81 errorManagerMap.put(entry.getValue(), entry.getKey()); 82 } 83 84 errorManagerMap.put(new Integer (ErrorManager.INFORMATIONAL), Level.CONFIG); 85 86 exceptionLevelMap.put(Level.SEVERE, new Integer (ErrorManager.USER)); 87 exceptionLevelMap.put(Level.WARNING, new Integer (ErrorManager.USER)); 88 exceptionLevelMap.put(Level.INFO, new Integer (ErrorManager.INFORMATIONAL)); 89 exceptionLevelMap.put(Level.CONFIG, new Integer (ErrorManager.INFORMATIONAL)); 90 exceptionLevelMap.put(Level.FINE, new Integer (3)); 91 exceptionLevelMap.put(Level.FINER, new Integer (2)); 92 exceptionLevelMap.put(Level.FINEST, new Integer (1)); 93 } 94 95 private ErrorManager errorManager; 96 private final Formatter formatter = new SimpleFormatter (); 97 98 public LoggerAdapter(String 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 i = errorManagerMap.entrySet().iterator(); i.hasNext(); ) { 110 Map.Entry entry = (Map.Entry )i.next(); 111 112 int level = ((Integer )entry.getKey()).intValue(); 113 114 if (errorManager.isLoggable(level)) { 115 setLevel((Level )entry.getValue()); 116 break; 117 } 118 } 119 } 120 121 public void log(LogRecord record) { 122 init(); 123 124 Integer l = (Integer )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 x = (Integer )exceptionLevelMap.get(record.getLevel()); 132 if (x == null) { 133 x = new Integer (1); 134 } 135 errorManager.notify(x.intValue(), record.getThrown()); 136 } 137 } 138 } 139 140 } 141 | Popular Tags |