1 19 package org.netbeans.core.startup; 20 21 import java.io.ByteArrayInputStream ; 22 import java.io.ByteArrayOutputStream ; 23 import java.io.File ; 24 import java.io.FileInputStream ; 25 import java.io.FileNotFoundException ; 26 import java.io.FileWriter ; 27 import java.io.IOException ; 28 import java.io.OutputStreamWriter ; 29 import java.io.PrintStream ; 30 import java.io.StringWriter ; 31 import java.util.logging.Handler ; 32 import java.util.logging.Level ; 33 import java.util.logging.LogManager ; 34 import java.util.logging.Logger ; 35 import java.util.logging.StreamHandler ; 36 import java.util.logging.XMLFormatter ; 37 import java.util.regex.Matcher ; 38 import java.util.regex.Pattern ; 39 import org.netbeans.junit.NbTestCase; 40 41 42 45 public class TopLoggingOwnConfigClassTest extends NbTestCase { 46 static File log; 47 48 public TopLoggingOwnConfigClassTest(String testName) { 49 super(testName); 50 } 51 52 protected void setUp() throws Exception { 53 clearWorkDir(); 54 55 System.setProperty("netbeans.user", getWorkDirPath()); 56 57 log = new File (getWorkDir(), "own.log"); 58 59 System.setProperty("java.util.logging.config.class", Cfg.class.getName()); 60 61 TopLogging.initialize(); 63 } 64 65 protected void tearDown() throws Exception { 66 } 67 68 69 public void testLogOneLine() throws Exception { 70 Logger.getLogger(TopLoggingTest.class.getName()).log(Level.FINER, "First visible message"); 71 72 String content = readLog(); 73 if (content.indexOf("<!DOCTYPE") == -1) { 74 fail("Content must be XML based: " + content); 75 } 76 77 if (content.indexOf("First vis") == -1) { 78 fail("It must contain our log message: " + content); 79 } 80 } 81 82 private String readLog() throws IOException { 83 Handler [] ha = Logger.getLogger("").getHandlers(); 84 assertEquals("There is one handler", 1, ha.length); 85 ha[0].flush(); 86 87 assertTrue("Log file exists: " + log, log.canRead()); 88 89 FileInputStream is = new FileInputStream (log); 90 91 byte[] arr = new byte[(int)log.length()]; 92 int r = is.read(arr); 93 assertEquals("all read", arr.length, r); 94 is.close(); 95 96 return new String (arr); 97 } 98 99 public static final class Cfg extends Object { 100 public Cfg() throws IOException { 101 102 ByteArrayOutputStream os = new ByteArrayOutputStream (); 103 OutputStreamWriter w = new OutputStreamWriter (os); 104 w.write("handlers=java.util.logging.FileHandler\n"); 105 w.write(".level=100\n"); 106 w.write("java.util.logging.FileHandler.pattern=" + log.toString().replace('\\', '/') +"\n"); 107 w.close(); 108 109 LogManager.getLogManager().readConfiguration(new ByteArrayInputStream (os.toByteArray())); 110 111 } 112 } 113 } 114 | Popular Tags |