1 19 20 package org.openide; 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 34 39 public class ErrorManagerCyclicDepTest extends NbTestCase { 40 41 42 public ErrorManagerCyclicDepTest(java.lang.String testName) { 43 super(testName); 44 } 45 46 protected void setUp() throws Exception { 47 Logger 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 ("Ahoj")); 59 } 60 61 64 private static final class LoggerAdapter extends Logger { 65 private static final Map <Level ,Integer > levelMap = new HashMap <Level ,Integer >(); 66 private static final Map <Integer ,Level > errorManagerMap = new TreeMap <Integer ,Level >(); 67 private static final Map <Level ,Integer > exceptionLevelMap = new HashMap <Level ,Integer >(); 68 69 static { 70 levelMap.put(Level.SEVERE, new Integer (ErrorManager.ERROR)); 71 levelMap.put(Level.WARNING, new Integer (ErrorManager.WARNING)); 72 levelMap.put(Level.INFO, new Integer (ErrorManager.INFORMATIONAL)); 73 levelMap.put(Level.CONFIG, new Integer (ErrorManager.INFORMATIONAL)); 74 levelMap.put(Level.FINE, new Integer (3)); 75 levelMap.put(Level.FINER, new Integer (2)); 76 levelMap.put(Level.FINEST, new Integer (1)); 77 78 for (Iterator <Map.Entry <Level ,Integer >> i = levelMap.entrySet().iterator(); i.hasNext(); ) { 79 Map.Entry <Level ,Integer > entry = i.next(); 80 errorManagerMap.put(entry.getValue(), entry.getKey()); 81 } 82 83 errorManagerMap.put(new Integer (ErrorManager.INFORMATIONAL), Level.CONFIG); 84 85 exceptionLevelMap.put(Level.SEVERE, new Integer (ErrorManager.USER)); 86 exceptionLevelMap.put(Level.WARNING, new Integer (ErrorManager.USER)); 87 exceptionLevelMap.put(Level.INFO, new Integer (ErrorManager.INFORMATIONAL)); 88 exceptionLevelMap.put(Level.CONFIG, new Integer (ErrorManager.INFORMATIONAL)); 89 exceptionLevelMap.put(Level.FINE, new Integer (3)); 90 exceptionLevelMap.put(Level.FINER, new Integer (2)); 91 exceptionLevelMap.put(Level.FINEST, new Integer (1)); 92 } 93 94 private ErrorManager errorManager; 95 private final Formatter formatter = new SimpleFormatter (); 96 97 public LoggerAdapter(String 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 i = errorManagerMap.entrySet().iterator(); i.hasNext(); ) { 109 Map.Entry entry = (Map.Entry )i.next(); 110 111 int level = ((Integer )entry.getKey()).intValue(); 112 113 if (errorManager.isLoggable(level)) { 114 setLevel((Level )entry.getValue()); 115 break; 116 } 117 } 118 } 119 120 public void log(LogRecord record) { 121 init(); 122 123 errorManager.log(((Integer )levelMap.get(record.getLevel())).intValue(), 124 formatter.format(record)); 125 126 if (record.getThrown() != null) { 127 errorManager.notify(((Integer )exceptionLevelMap.get( 128 record.getLevel())).intValue(), record.getThrown()); 129 } 130 } 131 } 132 133 } 134 | Popular Tags |