1 19 20 package org.openide; 21 22 import java.io.ByteArrayOutputStream ; 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 import java.io.PrintStream ; 26 import java.net.URL ; 27 import java.util.Date ; 28 import java.util.HashMap ; 29 import java.util.LinkedList ; 30 import java.util.regex.Pattern ; 31 import junit.framework.AssertionFailedError; 32 import junit.framework.TestResult; 33 import org.netbeans.junit.MockServices; 34 import org.netbeans.junit.NbTestCase; 35 import org.openide.util.Lookup; 36 37 41 public abstract class LoggingTestCaseHid extends NbTestCase { 42 static { 43 MockServices.setServices(new Class [] {ErrManager.class}); 44 } 45 46 protected LoggingTestCaseHid (String name) { 47 super (name); 48 } 49 50 53 protected void runTest () throws Throwable { 54 55 assertNotNull ("ErrManager has to be in lookup", Lookup.getDefault().lookup(ErrManager.class)); 56 57 ErrManager.clear(getName(), getLog()); 58 59 try { 60 super.runTest (); 61 } catch (AssertionFailedError ex) { 62 AssertionFailedError ne = new AssertionFailedError (ex.getMessage () + " Log:\n" + ErrManager.messages); 63 ne.setStackTrace (ex.getStackTrace ()); 64 throw ne; 65 } catch (IOException iex) { IOException ne = new IOException (iex.getMessage () + " Log:\n" + ErrManager.messages); 67 ne.setStackTrace (iex.getStackTrace ()); 68 throw ne; 69 } finally { 70 ErrManager.clear(getName(), System.err); 72 } 73 } 74 75 80 protected final void registerSwitches(URL url, int timeout) throws IOException { 81 ByteArrayOutputStream os = new ByteArrayOutputStream (); 82 InputStream is = url.openStream(); 83 for (;;) { 84 int ch = is.read (); 85 if (ch == -1) break; 86 os.write (ch); 87 } 88 os.close(); 89 is.close(); 90 91 registerSwitches(new String (os.toByteArray(), "utf-8"), timeout); 92 } 93 94 98 protected final void registerSwitches(String order, int timeout) { 99 ErrManager.timeout = timeout; 100 101 LinkedList switches = new LinkedList (); 102 103 HashMap exprs = new HashMap (); 104 105 int pos = 0; 106 for(;;) { 107 int thr = order.indexOf("THREAD:", pos); 108 if (thr == -1) { 109 break; 110 } 111 int msg = order.indexOf("MSG:", thr); 112 if (msg == -1) { 113 fail("After THREAD: there must be MSG: " + order.substring(thr)); 114 } 115 int end = order.indexOf("THREAD:", msg); 116 if (end == -1) { 117 end = order.length(); 118 } 119 120 String thrName = order.substring(pos + 7, msg).trim(); 121 String msgText = order.substring(msg + 4, end).trim(); 122 123 Pattern p = (Pattern )exprs.get(msgText); 124 if (p == null) { 125 p = Pattern.compile(msgText); 126 exprs.put(msgText, p); 127 } 128 129 Switch s = new Switch(thrName, p); 130 switches.add(s); 131 132 pos = end; 133 } 134 135 ErrManager.switches = switches; 136 } 137 138 public static final class ErrManager extends ErrorManager { 142 public static final StringBuffer messages = new StringBuffer (); 143 static java.io.PrintStream log = System.err; 144 145 private String prefix; 146 147 private static LinkedList switches; 148 private static int timeout; 149 150 private static java.util.Map threads = new java.util.HashMap (); 151 152 public ErrManager () { 153 this (null); 154 } 155 public ErrManager (String prefix) { 156 this.prefix = prefix; 157 } 158 159 public Throwable annotate (Throwable t, int severity, String message, String localizedMessage, Throwable stackTrace, Date date) { 160 return t; 161 } 162 163 public Throwable attachAnnotations (Throwable t, ErrorManager.Annotation[] arr) { 164 return t; 165 } 166 167 public ErrorManager.Annotation[] findAnnotations (Throwable t) { 168 return null; 169 } 170 171 public ErrorManager getInstance (String name) { 172 if ( 173 true 174 ) { 175 return new ErrManager ('[' + name + "] "); 176 } else { 177 return new ErrManager (); 179 } 180 } 181 182 public void log (int severity, String s) { 183 StringBuffer oneMsg = new StringBuffer (); 184 if (prefix != null) { 185 oneMsg.append(prefix); 186 } else { 187 oneMsg.append("[default] "); 188 } 189 oneMsg.append("THREAD:"); 190 oneMsg.append(Thread.currentThread().getName()); 191 oneMsg.append(" MSG:"); 192 oneMsg.append(s); 193 194 195 messages.append(oneMsg.toString()); 196 messages.append ('\n'); 197 198 if (messages.length() > 40000) { 199 messages.delete(0, 20000); 200 } 201 202 log.println(oneMsg.toString()); 203 204 if (switches != null) { 205 boolean log = true; 206 boolean expectingMsg = false; 207 for(;;) { 208 synchronized (switches) { 209 if (switches.isEmpty()) { 210 return; 211 } 212 213 214 Switch w = (Switch)switches.getFirst(); 215 String threadName = Thread.currentThread().getName(); 216 boolean foundMatch = false; 217 218 if (w.matchesThread()) { 219 if (!w.matchesMessage(s)) { 220 return; 222 } 223 switches.removeFirst(); 225 if (switches.isEmpty()) { 226 switches.notifyAll(); 228 return; 229 } 230 w = (Switch)switches.getFirst(); 231 if (w.matchesThread()) { 232 return; 234 } 235 expectingMsg = true; 236 foundMatch = true; 237 } else { 238 java.util.Iterator it = switches.iterator(); 240 while (it.hasNext()) { 241 Switch check = (Switch)it.next(); 242 if (check.matchesMessage(s)) { 243 expectingMsg = true; 244 break; 245 } 246 } 247 } 248 249 Thread t = (Thread )threads.get(w.name); 251 if (t != null) { 252 if (log) { 253 messages.append("t: " + threadName + " interrupts: " + t.getName() + "\n"); 254 } 255 t.interrupt(); 256 } 257 threads.put(threadName, Thread.currentThread()); 258 259 if (!expectingMsg) { 264 return; 265 } 266 267 Thread.interrupted(); 269 try { 270 if (log) { 271 messages.append("t: " + threadName + " log: " + s + " waiting\n"); 272 } 273 switches.wait(timeout); 274 if (log) { 275 messages.append("t: " + threadName + " log: " + s + " timeout\n"); 276 } 277 return; 278 } catch (InterruptedException ex) { 279 if (log) { 281 messages.append("t: " + threadName + " log: " + s + " interrupted\n"); 282 } 283 if (foundMatch) { 284 return; 285 } 286 } 287 } 288 } 289 } 290 } 291 292 public void notify (int severity, Throwable t) { 293 log (severity, t.getMessage ()); 294 } 295 296 public boolean isNotifiable (int severity) { 297 return prefix != null; 298 } 299 300 public boolean isLoggable (int severity) { 301 return prefix != null; 302 } 303 304 private static void clear(String n, PrintStream printStream) { 305 ErrManager.log = printStream; 306 ErrManager.messages.setLength(0); 307 ErrManager.messages.append ("Starting test "); 308 ErrManager.messages.append (n); 309 ErrManager.messages.append ('\n'); 310 threads.clear(); 311 } 312 313 } 315 private static final class Switch { 316 private Pattern msg; 317 private String name; 318 319 public Switch(String n, Pattern m) { 320 this.name = n; 321 this.msg = m; 322 } 323 324 326 public boolean matchesThread() { 327 String thr = Thread.currentThread().getName(); 328 return name.equals(thr); 329 } 330 331 333 public boolean matchesMessage(String logMsg) { 334 return msg.matcher(logMsg).matches(); 335 } 336 337 public String toString() { 338 return "Switch[" + name + "]: " + msg; 339 } 340 } 341 } 342 | Popular Tags |