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.PrintStream ; 27 import java.util.logging.Handler ; 28 import java.util.logging.Level ; 29 import java.util.logging.LogManager ; 30 import java.util.logging.Logger ; 31 import java.util.logging.StreamHandler ; 32 import java.util.logging.XMLFormatter ; 33 import java.util.regex.Matcher ; 34 import java.util.regex.Pattern ; 35 import org.netbeans.junit.NbTestCase; 36 37 38 41 public class TopLoggingNbLoggerConsoleTest extends TopLoggingTest { 42 private static ByteArrayOutputStream w; 43 private static PrintStream ps; 44 static { 45 final PrintStream OLD = System.err; 46 System.setProperty("netbeans.logger.console", "true"); 47 w = new ByteArrayOutputStream () { 48 public void write(byte[] b, int off, int len) { 49 super.write(b, off, len); 50 } 51 52 public void write(byte[] b) throws IOException { 53 super.write(b); 54 } 55 56 public void write(int b) { 57 super.write(b); 58 } 59 60 public String toString() { 61 TopLogging.flush(false); 62 OLD.flush(); 63 64 String retValue; 65 retValue = super.toString(); 66 return retValue; 67 } 68 }; 69 70 ps = new PrintStream (w); 71 System.setErr(ps); 72 } 73 74 75 public TopLoggingNbLoggerConsoleTest(String testName) { 76 super(testName); 77 } 78 79 protected void setUp() throws Exception { 80 clearWorkDir(); 81 82 System.setProperty("netbeans.user", getWorkDirPath()); 83 84 TopLogging.initialize(); 86 87 ps.flush(); 88 w.reset(); 89 } 90 91 protected void tearDown() throws Exception { 92 } 93 94 protected ByteArrayOutputStream getStream() { 95 return w; 96 } 97 98 public void testFlushHappensQuickly() throws Exception { 99 Logger.getLogger(TopLoggingTest.class.getName()).log(Level.INFO, "First visible message"); 100 101 Pattern p = Pattern.compile("INFO.*First visible message"); 102 Matcher m = p.matcher(getStream().toString("utf-8")); 103 104 Matcher d = null; 105 String disk = null; 106 for (int i = 0; i < 4; i++) { 108 disk = w.toString("utf-8"); d = p.matcher(disk); 110 if (!d.find()) { 111 Thread.sleep(300); 112 } else { 113 return; 114 } 115 } 116 117 fail("msg shall be logged to file: " + disk); 118 } 119 120 } 121 | Popular Tags |