1 19 20 21 package org.netbeans.modules.exceptions; 22 23 import junit.framework.*; 24 import java.util.Iterator ; 25 import java.util.LinkedList ; 26 import java.util.logging.Handler ; 27 import java.util.logging.Handler ; 28 import java.util.logging.Handler ; 29 import java.util.logging.Level ; 30 import java.util.logging.LogRecord ; 31 import java.util.logging.Logger ; 32 import org.netbeans.modules.exceptions.listeners.UIHandler; 33 34 38 public class CollectorTest extends TestCase { 39 private final String prop_name = "netbeans.exception.report.min.level"; 40 private static LinkedList <LogRecord > recs = null; 41 private static Iterator <LogRecord > it = null; 42 43 public CollectorTest(String testName) { 44 super(testName); 45 } 46 47 50 public void testGetDefault() { 51 System.out.println("getDefault"); 52 final Collector collector = Collector.getDefault(); 53 assertEquals(collector, Collector.getDefault()); 54 Thread t = new Thread (new Runnable (){ 55 public void run(){ 56 assertEquals(collector, Collector.getDefault()); 57 } 58 }); 59 t.start(); 60 } 61 62 65 public void testStart() { 66 System.out.println("start"); 67 Collector coll = Collector.getDefault(); 68 coll.start(); 69 String str = System.getProperty(prop_name); 71 assertEquals(str, "1001"); 72 assertTrue(containsHanlder(Logger.getLogger(""), ExceptionsHandler.class)); 74 assertTrue(containsHanlder(Logger.getLogger("org.netbeans.ui"), UIHandler.class)); 76 assertTrue(Logger.getLogger("org.netbeans.ui").getLevel().equals(Level.FINEST)); 77 79 } 80 81 85 public void testWrite() { 86 System.out.println("write"); 87 final Collector instance = Collector.getDefault(); 88 Runnable r = new Runnable (){ 89 public void run(){ 90 LogRecord l = getNext(); 91 while (l != null){ 92 instance.write(l); 93 l = getNext(); 94 Thread.yield(); 95 } 96 } 97 }; 98 Thread t1 = new Thread (r); 99 Thread t2 = new Thread (r); 100 Thread t3 = new Thread (r); 101 t1.start(); 102 t2.start(); 103 t3.start(); 104 try{ 105 t3.join(); 106 t2.join(); 107 t1.join(); 108 }catch(Exception ex){ 109 ex.printStackTrace(); 110 } 111 LinkedList <LogRecord > fronta = Collector.getDefault().getQueue(); 112 LogRecord rec; 113 while ((rec = recs.poll())!= null){ 114 assertTrue(fronta.contains(rec)); 115 } 116 int i; 117 rec = new LogRecord (Level.SEVERE, "BYE"); 118 for (i=0;i<10;i++) instance.write(new LogRecord (Level.SEVERE, "HALLO")); 119 assertTrue(Collector.getDefault().getQueue().size()==Collector.MAX_LOGS+1); 120 for (i=0;i<Collector.MAX_LOGS;i++) instance.write(new LogRecord (Level.SEVERE, "HALLO")); 121 assertTrue(!Collector.getDefault().getQueue().contains(rec)); 122 123 } 124 125 128 public void testClose() { 129 System.out.println("close"); 130 131 Collector.getDefault().close(); 132 assertFalse(containsHanlder(Logger.getLogger("org.netbeans.ui"), UIHandler.class)); 133 assertFalse(containsHanlder(Logger.getLogger(""), ExceptionsHandler.class)); 134 135 } 138 139 140 141 private synchronized LogRecord getNext(){ 142 if ((recs == null)||(it==null)) { 143 recs = createRecs(); 144 it = recs.iterator(); 145 } 146 if (it.hasNext()) return it.next(); 147 else return null; 148 } 149 150 private LinkedList <LogRecord > createRecs(){ 151 recs = new LinkedList <LogRecord >(); 152 int i = Collector.MAX_LOGS; 153 while (i-- > 0) recs.add(new LogRecord (Level.INFO, "log: "+Long.toString(Math.round(Math.random()*Collector.MAX_LOGS)))); 154 return recs; 155 } 156 157 private boolean containsHanlder(Logger log, Class cl){ 158 Handler [] handlers; 159 int i = 0; 160 handlers = log.getHandlers(); 161 while (i < handlers.length){ 162 if (cl.isInstance(handlers[i++])) return true; 163 } 164 return false; 165 } 166 } 167 | Popular Tags |