KickJava   Java API By Example, From Geeks To Geeks.

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


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.OutputStream JavaDoc;
27 import java.io.PrintStream JavaDoc;
28 import java.util.logging.Handler JavaDoc;
29 import java.util.logging.Level JavaDoc;
30 import java.util.logging.LogManager JavaDoc;
31 import java.util.logging.Logger JavaDoc;
32 import java.util.logging.StreamHandler JavaDoc;
33 import java.util.logging.XMLFormatter JavaDoc;
34 import java.util.regex.Matcher JavaDoc;
35 import java.util.regex.Pattern JavaDoc;
36 import javax.swing.SwingUtilities JavaDoc;
37 import org.netbeans.junit.NbTestCase;
38
39
40 /**
41  * Checks the behaviour of NetBeans logging support.
42  */

43 public class TopLoggingTest extends NbTestCase {
44     private ByteArrayOutputStream JavaDoc w;
45     private Handler JavaDoc handler;
46     private Logger JavaDoc logger;
47     
48     public TopLoggingTest(String JavaDoc testName) {
49         super(testName);
50     }
51
52     protected void setUp() throws Exception JavaDoc {
53         clearWorkDir();
54
55         System.setProperty("netbeans.user", getWorkDirPath());
56
57         // initialize logging
58
TopLogging.initialize();
59
60         w = new ByteArrayOutputStream JavaDoc() {
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 JavaDoc {
66                 super.write(b);
67             }
68
69             public void write(int b) {
70                 super.write(b);
71             }
72
73             public String JavaDoc toString() {
74                 handler.flush();
75
76                 String JavaDoc retValue;
77                 retValue = super.toString();
78                 return retValue;
79             }
80
81         };
82
83         handler = TopLogging.createStreamHandler(new PrintStream JavaDoc(getStream()));
84         logger = Logger.getLogger("");
85         Handler JavaDoc[] old = logger.getHandlers();
86 // do not remove default handlers from CLIOptions.initialize():
87
// for (int i = 0; i < old.length; i++) {
88
// logger.removeHandler(old[i]);
89
// }
90
logger.addHandler(handler);
91
92         w.reset();
93
94     }
95
96
97     protected void tearDown() throws Exception JavaDoc {
98     }
99
100     protected ByteArrayOutputStream JavaDoc getStream() {
101         return w;
102     }
103
104     public void testLogOneLine() throws Exception JavaDoc {
105         Logger.getLogger(TopLoggingTest.class.getName()).log(Level.INFO, "First visible message");
106
107         Pattern JavaDoc p = Pattern.compile("INFO.*First visible message");
108         Matcher JavaDoc m = p.matcher(getStream().toString());
109
110         if (!m.find()) {
111             fail("msg shall be logged: " + getStream().toString());
112         }
113
114         String JavaDoc disk = readLog(true);
115         Matcher JavaDoc 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 JavaDoc {
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 JavaDoc 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 JavaDoc 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 JavaDoc {
139         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
140         while(sb.length() < 1024) {
141             sb.append("0123456789");
142         }
143
144         Logger JavaDoc 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 JavaDoc log = new File JavaDoc(new File JavaDoc(new File JavaDoc(getWorkDir(), "var"), "log"), "messages.log");
153         assertTrue("Log file exists: " + log, log.canRead());
154
155
156         File JavaDoc log2 = new File JavaDoc(new File JavaDoc(new File JavaDoc(getWorkDir(), "var"), "log"), "messages.log.1");
157         assertFalse("Currently we rotate just one file: " + log2, log2.canRead());
158
159         TopLogging.close();
160         // simulate new start
161
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 JavaDoc {
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 JavaDoc p = Pattern.compile("FINER.*Finer level msg");
177         String JavaDoc disk = readLog(true);
178         Matcher JavaDoc 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 JavaDoc {
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 JavaDoc p = Pattern.compile("FINER.*Finer level msg");
192         String JavaDoc disk = readLog(true);
193         Matcher JavaDoc 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 JavaDoc {
201         System.setProperty("ha.nu.level", "100");
202
203         Logger JavaDoc l = Logger.getLogger("ha.nu.wirta");
204
205         LogManager.getLogManager().readConfiguration();
206
207         l.log(Level.FINER, "Finer level msg");
208
209         Pattern JavaDoc p = Pattern.compile("FINER.*Finer level msg");
210         String JavaDoc disk = readLog(true);
211         Matcher JavaDoc 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 JavaDoc {
219         System.err.println("Ahoj");
220         System.err.println("Jardo");
221         new IllegalStateException JavaDoc("Hi").printStackTrace();
222
223         if (handler != null) {
224             handler.flush();
225         }
226
227         String JavaDoc disk = readLog(true);
228
229         Matcher JavaDoc 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 JavaDoc p = Pattern.compile("IllegalStateException.*Hi");
239         Matcher JavaDoc d = p.matcher(disk);
240         if (!d.find()) {
241             fail("Expecting exception: " + disk);
242         }
243     }
244     
245     public void testSystemErrPrintLnIsSentToLog() throws Exception JavaDoc {
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 JavaDoc disk = readLog(true);
255         Matcher JavaDoc 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 JavaDoc {
269         Logger JavaDoc l = Logger.getLogger(TopLoggingTest.class.getName());
270         l.log(Level.INFO, "First visible message");
271
272         Pattern JavaDoc p = Pattern.compile("INFO.*First visible message");
273         Matcher JavaDoc m = p.matcher(getStream().toString());
274
275         Matcher JavaDoc d = null;
276         String JavaDoc disk = null;
277         // at most in 10s the output should be flushed
278
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 JavaDoc {
292         class R implements Runnable JavaDoc {
293             public RuntimeException JavaDoc 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 JavaDoc();
303         
304         SwingUtilities.invokeLater(thrw);
305         
306         R wai = new R();
307         SwingUtilities.invokeAndWait(wai);
308         
309
310         String JavaDoc log = readLog(true);
311         if (log.indexOf("IllegalStateException") == -1) {
312             fail("There should be IllegalStateException:\n" + log);
313         }
314     }
315
316     private String JavaDoc readLog(boolean doFlush) throws IOException JavaDoc {
317         if (doFlush) {
318             TopLogging.flush(false);
319         }
320
321         File JavaDoc log = new File JavaDoc(new File JavaDoc(new File JavaDoc(getWorkDir(), "var"), "log"), "messages.log");
322         assertTrue("Log file exists: " + log, log.canRead());
323
324         FileInputStream JavaDoc is = new FileInputStream JavaDoc(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 JavaDoc(arr);
332     }
333
334 }
335
Popular Tags