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.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 javax.swing.SwingUtilities ; 37 import org.netbeans.junit.NbTestCase; 38 39 40 43 public class TopLoggingTest extends NbTestCase { 44 private ByteArrayOutputStream w; 45 private Handler handler; 46 private Logger logger; 47 48 public TopLoggingTest(String testName) { 49 super(testName); 50 } 51 52 protected void setUp() throws Exception { 53 clearWorkDir(); 54 55 System.setProperty("netbeans.user", getWorkDirPath()); 56 57 TopLogging.initialize(); 59 60 w = new ByteArrayOutputStream () { 61 public void write(byte[] b, int off, int len) { 62 super.write(b, off, len); 63 } 64 65 public void write(byte[] b) throws IOException { 66 super.write(b); 67 } 68 69 public void write(int b) { 70 super.write(b); 71 } 72 73 public String toString() { 74 handler.flush(); 75 76 String retValue; 77 retValue = super.toString(); 78 return retValue; 79 } 80 81 }; 82 83 handler = TopLogging.createStreamHandler(new PrintStream (getStream())); 84 logger = Logger.getLogger(""); 85 Handler [] old = logger.getHandlers(); 86 logger.addHandler(handler); 91 92 w.reset(); 93 94 } 95 96 97 protected void tearDown() throws Exception { 98 } 99 100 protected ByteArrayOutputStream getStream() { 101 return w; 102 } 103 104 public void testLogOneLine() throws Exception { 105 Logger.getLogger(TopLoggingTest.class.getName()).log(Level.INFO, "First visible message"); 106 107 Pattern p = Pattern.compile("INFO.*First visible message"); 108 Matcher m = p.matcher(getStream().toString()); 109 110 if (!m.find()) { 111 fail("msg shall be logged: " + getStream().toString()); 112 } 113 114 String disk = readLog(true); 115 Matcher d = p.matcher(disk); 116 117 if (!d.find()) { 118 fail("msg shall be logged to file: " + disk); 119 } 120 121 } 122 public void testLogMultiLineIsPrintedWithoutTheWarningPrefix() throws Exception { 123 Logger.getLogger(TopLoggingTest.class.getName()).log(Level.WARNING, "Some info"); 124 Logger.getLogger(TopLoggingTest.class.getName()).log(Level.INFO, "Second msg\nand its second line"); 125 126 String p = "\nSecond msg\nand its second line"; 127 if (getStream().toString().indexOf(p) == -1) { 128 fail("msg shall be logged: " + getStream().toString()); 129 } 130 131 String disk = readLog(true); 132 133 if (disk.indexOf(p) == -1) { 134 fail("msg shall be logged to file: " + disk); 135 } 136 137 } 138 public void testLogLoggingMessagesEndsUpInMultipleFiles() throws Exception { 139 StringBuffer sb = new StringBuffer (); 140 while(sb.length() < 1024) { 141 sb.append("0123456789"); 142 } 143 144 Logger l = Logger.getLogger(TopLoggingTest.class.getName()); 145 for (int i = 0; i < 2048; i++) { 146 l.log(Level.WARNING, sb.toString() + " index: " + i); 147 getStream().reset(); 148 } 149 150 TopLogging.flush(false); 151 152 File log = new File (new File (new File (getWorkDir(), "var"), "log"), "messages.log"); 153 assertTrue("Log file exists: " + log, log.canRead()); 154 155 156 File log2 = new File (new File (new File (getWorkDir(), "var"), "log"), "messages.log.1"); 157 assertFalse("Currently we rotate just one file: " + log2, log2.canRead()); 158 159 TopLogging.close(); 160 TopLogging.flush(true); 162 163 TopLogging.initialize(); 164 165 assertTrue("2 Log file exists: " + log, log.canRead()); 166 assertTrue("Restarrt creates new log file: " + log2, log2.canRead()); 167 168 } 169 170 public void testCanInfluenceBehaviourBySettingALevelProperty() throws Exception { 171 System.setProperty(TopLoggingTest.class.getName() + ".level", "100"); 172 LogManager.getLogManager().readConfiguration(); 173 174 Logger.getLogger(TopLoggingTest.class.getName()).log(Level.FINER, "Finer level msg"); 175 176 Pattern p = Pattern.compile("FINER.*Finer level msg"); 177 String disk = readLog(true); 178 Matcher d = p.matcher(disk); 179 180 if (!d.find()) { 181 fail("msg shall be logged to file: " + disk); 182 } 183 } 184 185 public void testCanInfluenceBehaviourBySettingALevelPropertyOnParent() throws Exception { 186 System.setProperty("ha.nu.level", "100"); 187 LogManager.getLogManager().readConfiguration(); 188 189 Logger.getLogger("ha.nu.wirta").log(Level.FINER, "Finer level msg"); 190 191 Pattern p = Pattern.compile("FINER.*Finer level msg"); 192 String disk = readLog(true); 193 Matcher d = p.matcher(disk); 194 195 if (!d.find()) { 196 fail("msg shall be logged to file: " + disk); 197 } 198 } 199 200 public void testCanInfluenceBehaviourBySettingALevelPropertyOnExistingParent() throws Exception { 201 System.setProperty("ha.nu.level", "100"); 202 203 Logger l = Logger.getLogger("ha.nu.wirta"); 204 205 LogManager.getLogManager().readConfiguration(); 206 207 l.log(Level.FINER, "Finer level msg"); 208 209 Pattern p = Pattern.compile("FINER.*Finer level msg"); 210 String disk = readLog(true); 211 Matcher d = p.matcher(disk); 212 213 if (!d.find()) { 214 fail("msg shall be logged to file: " + disk); 215 } 216 } 217 218 public void testSystemErrIsSentToLog() throws Exception { 219 System.err.println("Ahoj"); 220 System.err.println("Jardo"); 221 new IllegalStateException ("Hi").printStackTrace(); 222 223 if (handler != null) { 224 handler.flush(); 225 } 226 227 String disk = readLog(true); 228 229 Matcher m = Pattern.compile("^Ahoj(.*)Jardo", Pattern.MULTILINE | Pattern.DOTALL).matcher(disk); 230 assertTrue(disk, m.find()); 231 assertEquals("One group found", 1, m.groupCount()); 232 assertTrue("Non empty group: " + m.group(1) + "\n" + disk, m.group(1).length() > 0); 233 char next = m.group(1).charAt(0); 234 if (next != 10 && next != 13) { 235 fail("Expecting 'Ahoj': index: " + 0 + " next char: " + (int)next + "text:\n" + disk); 236 } 237 238 Pattern p = Pattern.compile("IllegalStateException.*Hi"); 239 Matcher d = p.matcher(disk); 240 if (!d.find()) { 241 fail("Expecting exception: " + disk); 242 } 243 } 244 245 public void testSystemErrPrintLnIsSentToLog() throws Exception { 246 System.err.println("BEGIN"); 247 System.err.println(""); 248 System.err.println("END"); 249 250 if (handler != null) { 251 handler.flush(); 252 } 253 254 String disk = readLog(true); 255 Matcher m = Pattern.compile("BEGIN.*END", Pattern.MULTILINE | Pattern.DOTALL).matcher(disk); 256 assertTrue("There is text between BEGINandEND\n" + disk, m.find()); 257 disk = m.group(0); 258 disk = disk.replace('\n', 'n'); 259 disk = disk.replace('\r', 'r'); 260 261 if (org.openide.util.Utilities.isWindows()) { 262 assertEquals("BEGINrnrnEND", disk); 263 } else { 264 assertEquals("BEGINnnEND", disk); 265 } 266 } 267 268 public void testFlushHappensAfterFewSeconds() throws Exception { 269 Logger l = Logger.getLogger(TopLoggingTest.class.getName()); 270 l.log(Level.INFO, "First visible message"); 271 272 Pattern p = Pattern.compile("INFO.*First visible message"); 273 Matcher m = p.matcher(getStream().toString()); 274 275 Matcher d = null; 276 String disk = null; 277 for (int i = 0; i < 30; i++) { 279 disk = readLog(false); 280 d = p.matcher(disk); 281 if (!d.find()) { 282 Thread.sleep(300); 283 } else { 284 return; 285 } 286 } 287 288 fail("msg shall be logged to file: " + disk); 289 } 290 291 public void testLetsTryToReportToABugInAWT() throws Exception { 292 class R implements Runnable { 293 public RuntimeException ex; 294 public void run() { 295 if (ex != null) { 296 throw ex; 297 } 298 } 299 } 300 301 R thrw = new R(); 302 thrw.ex = new IllegalStateException (); 303 304 SwingUtilities.invokeLater(thrw); 305 306 R wai = new R(); 307 SwingUtilities.invokeAndWait(wai); 308 309 310 String log = readLog(true); 311 if (log.indexOf("IllegalStateException") == -1) { 312 fail("There should be IllegalStateException:\n" + log); 313 } 314 } 315 316 private String readLog(boolean doFlush) throws IOException { 317 if (doFlush) { 318 TopLogging.flush(false); 319 } 320 321 File log = new File (new File (new File (getWorkDir(), "var"), "log"), "messages.log"); 322 assertTrue("Log file exists: " + log, log.canRead()); 323 324 FileInputStream is = new FileInputStream (log); 325 326 byte[] arr = new byte[(int)log.length()]; 327 int r = is.read(arr); 328 assertEquals("all read", arr.length, r); 329 is.close(); 330 331 return new String (arr); 332 } 333 334 } 335 | Popular Tags |