1 19 20 package org.netbeans.core; 21 import java.beans.PropertyChangeEvent ; 22 import java.io.ByteArrayOutputStream ; 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 import java.io.PrintStream ; 26 import java.io.StringWriter ; 27 import java.net.URL ; 28 import java.util.ArrayList ; 29 import java.util.Date ; 30 import java.util.Enumeration ; 31 import java.util.HashMap ; 32 import java.util.HashSet ; 33 import java.util.Iterator ; 34 import java.util.LinkedList ; 35 import java.util.regex.Pattern ; 36 import javax.swing.event.ChangeEvent ; 37 import junit.framework.AssertionFailedError; 38 import junit.framework.TestResult; 39 import org.netbeans.junit.NbTestCase; 40 import org.openide.ErrorManager; 41 import org.openide.util.Enumerations; 42 import org.openide.util.Lookup; 43 import org.openide.util.lookup.AbstractLookup; 44 import org.openide.util.lookup.InstanceContent; 45 import org.openide.util.lookup.Lookups; 46 import org.openide.util.lookup.ProxyLookup; 47 48 49 53 public class LoggingTestCaseHid extends NbTestCase { 54 static { 55 org.netbeans.core.startup.MainLookup.register(new ErrManager()); 56 } 57 58 private HashSet unregister = new HashSet (); 59 60 protected LoggingTestCaseHid (String name) { 61 super (name); 62 } 63 64 public void run(TestResult result) { 65 Lookup l = Lookup.getDefault(); 66 67 assertEquals("We can run only with our Lookup", org.netbeans.core.startup.MainLookup.class, l.getClass()); 68 69 super.run(result); 70 71 for (Iterator it = unregister.iterator(); it.hasNext();) { 72 Object elem = it.next(); 73 74 org.netbeans.core.startup.MainLookup.unregister(elem); 75 } 76 } 77 78 81 protected void runTest () throws Throwable { 82 83 assertNotNull ("ErrManager has to be in lookup", Lookup.getDefault().lookup(ErrManager.class)); 84 85 ErrManager.clear(getName(), getLog()); 86 87 try { 88 super.runTest (); 89 } catch (AssertionFailedError ex) { 90 ErrManager.messages.append("\nGot exception: " + ex.getMessage()); 91 Thread.sleep(1000); 92 AssertionFailedError ne = new AssertionFailedError (ex.getMessage () + " Log:\n" + ErrManager.messages); 93 ne.setStackTrace (ex.getStackTrace ()); 94 throw ne; 95 } catch (IOException iex) { ErrManager.messages.append("\nGot exception: " + iex.getMessage()); 97 Thread.sleep(1000); 98 IOException ne = new IOException (iex.getMessage () + " Log:\n" + ErrManager.messages); 99 ne.setStackTrace (iex.getStackTrace ()); 100 throw ne; 101 } 102 } 103 104 107 protected final void registerIntoLookup(Object instance) { 108 unregister.add(instance); 109 org.netbeans.core.startup.MainLookup.register(instance); 110 } 111 112 public static final class ErrManager extends ErrorManager { 116 public static final StringBuffer messages = new StringBuffer (); 117 static java.io.PrintStream log = System.err; 118 119 private String prefix; 120 121 private static LinkedList switches; 122 123 private static java.util.Map threads = new java.util.HashMap (); 124 125 public ErrManager () { 126 this (null); 127 } 128 public ErrManager (String prefix) { 129 this.prefix = prefix; 130 } 131 132 public Throwable annotate (Throwable t, int severity, String message, String localizedMessage, Throwable stackTrace, Date date) { 133 return t; 134 } 135 136 public Throwable attachAnnotations (Throwable t, ErrorManager.Annotation[] arr) { 137 return t; 138 } 139 140 public ErrorManager.Annotation[] findAnnotations (Throwable t) { 141 return null; 142 } 143 144 public ErrorManager getInstance (String name) { 145 if ( 146 true 147 ) { 150 return new ErrManager ('[' + name + "] "); 151 } else { 152 return new ErrManager (); 154 } 155 } 156 157 public void log (int severity, String s) { 158 if (prefix != null) { 159 StringBuffer oneMsg = new StringBuffer (); 160 oneMsg.append(prefix); 161 oneMsg.append("THREAD:"); 162 oneMsg.append(Thread.currentThread().getName()); 163 oneMsg.append(" MSG:"); 164 oneMsg.append(s); 165 166 167 messages.append(oneMsg.toString()); 168 messages.append ('\n'); 169 170 if (messages.length() > 40000) { 171 messages.delete(0, 20000); 172 } 173 174 log.println(oneMsg.toString()); 175 } 176 177 if (switches != null) { 178 boolean log = true; 179 boolean expectingMsg = false; 180 for(;;) { 181 synchronized (switches) { 182 if (switches.isEmpty()) { 183 return; 184 } 185 186 187 Switch w = (Switch)switches.getFirst(); 188 String threadName = Thread.currentThread().getName(); 189 boolean foundMatch = false; 190 191 if (w.matchesThread()) { 192 if (!w.matchesMessage(s)) { 193 return; 195 } 196 switches.removeFirst(); 198 if (switches.isEmpty()) { 199 return; 201 } 202 w = (Switch)switches.getFirst(); 203 if (w.matchesThread()) { 204 return; 206 } 207 expectingMsg = true; 208 foundMatch = true; 209 } else { 210 java.util.Iterator it = switches.iterator(); 212 while (it.hasNext()) { 213 Switch check = (Switch)it.next(); 214 if (check.matchesMessage(s)) { 215 expectingMsg = true; 216 break; 217 } 218 } 219 } 220 221 Thread t = (Thread )threads.get(w.name); 223 if (t != null) { 224 if (log) { 225 messages.append("t: " + threadName + " interrupts: " + t.getName() + "\n"); 226 } 227 t.interrupt(); 228 } 229 threads.put(threadName, Thread.currentThread()); 230 231 if (!expectingMsg) { 236 return; 237 } 238 239 Thread.interrupted(); 241 try { 242 if (log) { 243 messages.append("t: " + threadName + " log: " + s + " waiting\n"); 244 } 245 switches.wait(300); 246 if (log) { 247 messages.append("t: " + threadName + " log: " + s + " timeout\n"); 248 } 249 return; 250 } catch (InterruptedException ex) { 251 if (log) { 253 messages.append("t: " + threadName + " log: " + s + " interrupted\n"); 254 } 255 if (foundMatch) { 256 return; 257 } 258 } 259 } 260 } 261 } 262 } 263 264 public void notify (int severity, Throwable t) { 265 log (severity, t.getMessage ()); 266 } 267 268 public boolean isNotifiable (int severity) { 269 return prefix != null; 270 } 271 272 public boolean isLoggable (int severity) { 273 return prefix != null; 274 } 275 276 private static void clear(String n, PrintStream printStream) { 277 ErrManager.log = printStream; 278 ErrManager.messages.setLength(0); 279 ErrManager.messages.append ("Starting test "); 280 ErrManager.messages.append (n); 281 ErrManager.messages.append ('\n'); 282 } 283 284 } 286 private static final class Switch { 287 private Pattern msg; 288 private String name; 289 290 public Switch(String n, Pattern m) { 291 this.name = n; 292 this.msg = m; 293 } 294 295 297 public boolean matchesThread() { 298 String thr = Thread.currentThread().getName(); 299 return name.equals(thr); 300 } 301 302 304 public boolean matchesMessage(String logMsg) { 305 return msg.matcher(logMsg).matches(); 306 } 307 308 public String toString() { 309 return "Switch[" + name + "]: " + msg; 310 } 311 } 312 } 313 | Popular Tags |