1 19 20 package org.openide; 21 22 import java.io.IOException ; 23 import java.io.PrintWriter ; 24 import java.io.StringBufferInputStream ; 25 import java.io.StringWriter ; 26 import java.util.logging.Handler ; 27 import java.util.logging.Level ; 28 import java.util.logging.LogManager ; 29 import java.util.logging.LogRecord ; 30 import junit.framework.*; 31 import org.netbeans.junit.*; 32 33 37 public class ErrorManagerDelegatesToLoggingTest extends NbTestCase { 38 39 public ErrorManagerDelegatesToLoggingTest(java.lang.String testName) { 40 super(testName); 41 } 42 43 protected void setUp () throws IOException { 44 assertNull ("No ErrorManager in lookup", org.openide.util.Lookup.getDefault ().lookup (ErrorManager.class)); 45 46 47 String config = 48 "handlers=" + MyHandler.class.getName() + "\n" + 49 ".level=50\n"; 50 LogManager.getLogManager().readConfiguration(new StringBufferInputStream (config)); 51 52 MyHandler.messages.setLength(0); 53 } 54 55 56 public void testGetDefault() { 57 assertNotNull("There has to be a manager", ErrorManager.getDefault ()); 58 } 59 60 61 public void testNotify() { 62 Throwable t = new Throwable (); 63 ErrorManager.getDefault ().notify (ErrorManager.INFORMATIONAL, t); 64 MyHandler.assertNotify (ErrorManager.INFORMATIONAL, t); 65 t = new Throwable (); 66 ErrorManager.getDefault ().notify (t); 67 MyHandler.assertNotify (ErrorManager.EXCEPTION, t); 68 } 69 70 71 public void testLog() { 72 ErrorManager.getDefault ().log (ErrorManager.INFORMATIONAL, "A text"); 73 MyHandler.assertLog (ErrorManager.INFORMATIONAL, "A text"); 74 ErrorManager.getDefault ().log ("Another text"); 75 MyHandler.assertLog (ErrorManager.INFORMATIONAL, "Another text"); 76 } 77 78 79 public void testIsLoggable() { 80 ErrorManager.getDefault ().isLoggable(ErrorManager.INFORMATIONAL); 81 ErrorManager.getDefault ().isLoggable(ErrorManager.INFORMATIONAL + 1); 82 } 83 84 85 public void testReturnValues () { 86 Throwable t = new Throwable (); 87 Throwable value = ErrorManager.getDefault ().annotate(t, ErrorManager.INFORMATIONAL, null, null, null, null); 88 assertEquals ("Annotate must return the same exception", t, value); 89 90 value = ErrorManager.getDefault ().copyAnnotation (t, new Throwable ()); 91 assertEquals ("copyAnnotation must return the same exception", t, value); 92 93 value = ErrorManager.getDefault ().attachAnnotations(t, new ErrorManager.Annotation[0]); 94 assertEquals ("attachAnnotations must return the same exception", t, value); 95 96 } 97 98 public void testThatWeNotifyAnException() throws Exception { 99 Throwable t = new NullPointerException ("npe"); 100 Throwable v = new ClassNotFoundException ("cnf", t); 101 102 Throwable a = new IOException (); 103 ErrorManager.getDefault().annotate(a, v); 104 ErrorManager.getDefault().notify(a); 105 106 assertEquals("Last throwable is a", a, MyHandler.lastThrowable); 107 assertNotNull("And it has a cause", MyHandler.lastThrowable.getCause()); 108 109 StringWriter w = new StringWriter (); 110 MyHandler.lastThrowable.getCause().printStackTrace(new PrintWriter (w)); 111 String msg = w.toString(); 112 113 if (msg.indexOf("npe") == -1) fail("there should be npe: " + msg); 114 if (msg.indexOf("cnf") == -1) fail("there should be cnf: " + msg); 115 116 } 117 118 public void testAnnotatedMessagesShallBePresentInPrintStackTrace() throws Exception { 119 Throwable t = new NullPointerException ("npe"); 120 121 ErrorManager.getDefault().annotate(t, ErrorManager.UNKNOWN, "Ahoj Null!", null, null, null); 122 123 StringWriter w = new StringWriter (); 124 t.printStackTrace(new PrintWriter (w)); 125 String msg = w.toString(); 126 127 if (msg.indexOf("npe") == -1) fail("there should be npe: " + msg); 128 if (msg.indexOf("Ahoj Null") == -1) fail("there should be Ahoj Null: " + msg); 129 130 } 131 132 public void testAttachLocalizedMessageForClassNFEIfNoMsg() { 133 Exception e = new ClassNotFoundException ("Help"); 134 String msg = "me please"; 135 136 ErrorManager.getDefault().annotate(e, msg); 137 138 Object [] arr = ErrorManager.getDefault().findAnnotations(e); 139 140 assertNotNull("Arr exists", arr); 141 } 142 143 144 public static final class MyHandler extends Handler { 148 public static final StringBuffer messages = new StringBuffer (); 149 150 private String prefix; 151 152 private static int lastSeverity; 153 private static Throwable lastThrowable; 154 private static String lastText; 155 156 public static void assertNotify (int sev, Throwable t) { 157 assertEquals ("Severity is same", sev, lastSeverity); 158 assertSame ("Throwable is the same", t, lastThrowable); 159 lastThrowable = null; 160 lastSeverity = -1; 161 } 162 163 public static void assertLog (int sev, String t) { 164 assertEquals ("Severity is same", sev, lastSeverity); 165 assertEquals ("Text is the same", t, lastText); 166 lastText = null; 167 lastSeverity = -1; 168 } 169 170 public void publish(LogRecord record) { 171 messages.append(record.getMessage()); 172 173 lastText = record.getMessage(); 174 lastThrowable = record.getThrown(); 175 176 Level l = record.getLevel(); 177 if (l.intValue() >= Level.FINEST.intValue()) { 178 lastSeverity = ErrorManager.INFORMATIONAL; 179 } 180 if (l.intValue() >= Level.WARNING.intValue()) { 181 lastSeverity = ErrorManager.WARNING; 182 } 183 if (l.intValue() >= Level.SEVERE.intValue()) { 184 lastSeverity = ErrorManager.EXCEPTION; 185 } 186 } 187 188 public void flush() { 189 } 190 191 public void close() throws SecurityException { 192 } 193 194 } 195 196 } 197 | Popular Tags |