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.FileWriter ; 26 import java.io.IOException ; 27 import java.io.PrintStream ; 28 import java.util.logging.Handler ; 29 import java.util.logging.Level ; 30 import java.util.logging.LogManager ; 31 import java.util.logging.Logger ; 32 import java.util.logging.StreamHandler ; 33 import java.util.logging.XMLFormatter ; 34 import java.util.regex.Matcher ; 35 import java.util.regex.Pattern ; 36 import org.netbeans.junit.NbTestCase; 37 38 39 42 public class TopLoggingOwnConfigTest extends NbTestCase { 43 private File log; 44 45 public TopLoggingOwnConfigTest(String testName) { 46 super(testName); 47 } 48 49 protected void setUp() throws Exception { 50 clearWorkDir(); 51 52 System.setProperty("netbeans.user", getWorkDirPath()); 53 54 log = new File (getWorkDir(), "own.log"); 55 File cfg = new File (getWorkDir(), "cfg"); 56 57 FileWriter w = new FileWriter (cfg); 58 w.write("handlers=java.util.logging.FileHandler\n"); 59 w.write(".level=100\n"); 60 w.write("java.util.logging.FileHandler.pattern=" + log.toString().replace('\\', '/') +"\n"); 61 w.close(); 62 63 64 System.setProperty("java.util.logging.config.file", cfg.getPath()); 65 66 TopLogging.initialize(); 68 } 69 70 protected void tearDown() throws Exception { 71 } 72 73 74 public void testLogOneLine() throws Exception { 75 Logger.getLogger(TopLoggingTest.class.getName()).log(Level.FINER, "First visible message"); 76 77 String content = readLog(); 78 if (content.indexOf("<!DOCTYPE") == -1) { 79 fail("Content must be XML based: " + content); 80 } 81 82 if (content.indexOf("First vis") == -1) { 83 fail("It must contain our log message: " + content); 84 } 85 } 86 87 private String readLog() throws IOException { 88 LogManager.getLogManager().reset(); 89 90 assertTrue("Log file exists: " + log, log.canRead()); 91 92 FileInputStream is = new FileInputStream (log); 93 94 byte[] arr = new byte[(int)log.length()]; 95 int r = is.read(arr); 96 assertEquals("all read", arr.length, r); 97 is.close(); 98 99 return new String (arr); 100 } 101 } 102 | Popular Tags |