KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > startup > TopLoggingNbLoggerConsoleTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.core.startup;
20
21 import java.io.ByteArrayOutputStream JavaDoc;
22 import java.io.File JavaDoc;
23 import java.io.FileInputStream JavaDoc;
24 import java.io.FileNotFoundException JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.PrintStream JavaDoc;
27 import java.util.logging.Handler JavaDoc;
28 import java.util.logging.Level JavaDoc;
29 import java.util.logging.LogManager JavaDoc;
30 import java.util.logging.Logger JavaDoc;
31 import java.util.logging.StreamHandler JavaDoc;
32 import java.util.logging.XMLFormatter JavaDoc;
33 import java.util.regex.Matcher JavaDoc;
34 import java.util.regex.Pattern JavaDoc;
35 import org.netbeans.junit.NbTestCase;
36
37
38 /**
39  * Checks that it is possible to log to console.
40  */

41 public class TopLoggingNbLoggerConsoleTest extends TopLoggingTest {
42     private static ByteArrayOutputStream JavaDoc w;
43     private static PrintStream JavaDoc ps;
44     static {
45         final PrintStream JavaDoc OLD = System.err;
46         System.setProperty("netbeans.logger.console", "true");
47         w = new ByteArrayOutputStream JavaDoc() {
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 JavaDoc {
53                 super.write(b);
54             }
55
56             public void write(int b) {
57                 super.write(b);
58             }
59
60             public String JavaDoc toString() {
61                 TopLogging.flush(false);
62                 OLD.flush();
63
64                 String JavaDoc retValue;
65                 retValue = super.toString();
66                 return retValue;
67             }
68         };
69
70         ps = new PrintStream JavaDoc(w);
71         System.setErr(ps);
72     }
73
74
75     public TopLoggingNbLoggerConsoleTest(String JavaDoc testName) {
76         super(testName);
77     }
78
79     protected void setUp() throws Exception JavaDoc {
80         clearWorkDir();
81
82         System.setProperty("netbeans.user", getWorkDirPath());
83
84         // initialize logging
85
TopLogging.initialize();
86
87         ps.flush();
88         w.reset();
89     }
90
91     protected void tearDown() throws Exception JavaDoc {
92     }
93
94     protected ByteArrayOutputStream JavaDoc getStream() {
95         return w;
96     }
97
98     public void testFlushHappensQuickly() throws Exception JavaDoc {
99         Logger.getLogger(TopLoggingTest.class.getName()).log(Level.INFO, "First visible message");
100
101         Pattern JavaDoc p = Pattern.compile("INFO.*First visible message");
102         Matcher JavaDoc m = p.matcher(getStream().toString("utf-8"));
103
104         Matcher JavaDoc d = null;
105         String JavaDoc disk = null;
106         // console gets flushed at 500ms
107
for (int i = 0; i < 4; i++) {
108             disk = w.toString("utf-8"); // this one is not flushing
109
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