1 19 package org.netbeans.core.startup; 20 21 import java.io.ByteArrayOutputStream ; 22 import java.io.File ; 23 import java.io.FileInputStream ; 24 import java.io.FileNotFoundException ; 25 import java.io.IOException ; 26 import java.io.OutputStream ; 27 import java.io.PrintStream ; 28 import java.util.ArrayList ; 29 import java.util.Collections ; 30 import java.util.List ; 31 import java.util.logging.Handler ; 32 import java.util.logging.Level ; 33 import java.util.logging.LogManager ; 34 import java.util.logging.LogRecord ; 35 import java.util.logging.Logger ; 36 import java.util.logging.StreamHandler ; 37 import java.util.logging.XMLFormatter ; 38 import java.util.regex.Matcher ; 39 import java.util.regex.Pattern ; 40 import org.netbeans.junit.MockServices; 41 import org.netbeans.junit.NbTestCase; 42 import org.openide.util.Lookup; 43 import org.openide.util.RequestProcessor; 44 import org.openide.util.lookup.AbstractLookup; 45 import org.openide.util.lookup.InstanceContent; 46 47 48 51 public class TopLoggingLookupTest extends NbTestCase { 52 private MyHandler handler; 53 54 public TopLoggingLookupTest(String testName) { 55 super(testName); 56 } 57 58 protected void setUp() throws Exception { 59 clearWorkDir(); 60 System.setProperty("netbeans.user", getWorkDirPath()); 61 62 MockServices.setServices(); 63 64 TopLogging.initialize(); 66 } 67 68 69 protected void tearDown() throws Exception { 70 } 71 72 public void testLogOneLine() throws Exception { 73 MockServices.setServices(MyHandler.class); 74 handler = Lookup.getDefault().lookup(MyHandler.class); 75 assertNotNull("Handler found", handler); 76 77 78 Logger.getLogger(TopLoggingTest.class.getName()).log(Level.INFO, "First visible message"); 79 80 assertEquals("[First visible message]", handler.logs.toString()); 81 } 82 83 public void testDeadlock78865() throws Exception { 84 MockServices.setServices(AnotherThreadLoggingHandler.class); 85 handler = Lookup.getDefault().lookup(MyHandler.class); 86 assertNotNull("Handler found", handler); 87 88 Logger.getLogger(TopLoggingTest.class.getName()).log(Level.INFO, "First visible message"); 89 90 assertEquals("[First visible message]", handler.logs.toString()); 91 } 92 93 public static class MyHandler extends Handler { 94 public List <String > logs = new ArrayList <String >(); 95 96 public void publish(LogRecord record) { 97 logs.add(record.getMessage()); 98 } 99 100 public void flush() { 101 logs.add("flush"); 102 } 103 104 public void close() throws SecurityException { 105 logs.add("close"); 106 } 107 108 } 109 public static final class AnotherThreadLoggingHandler extends MyHandler 110 implements Runnable { 111 public AnotherThreadLoggingHandler() { 112 Logger.global.info("in constructor before"); 113 RequestProcessor.getDefault().post(this).waitFinished(); 114 Logger.global.info("in constructor after"); 115 } 116 public void run() { 117 Logger.global.warning("running in parael"); 118 } 119 120 } 121 } 122 | Popular Tags |