KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
29 import java.util.Collections JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.logging.Handler JavaDoc;
32 import java.util.logging.Level JavaDoc;
33 import java.util.logging.LogManager JavaDoc;
34 import java.util.logging.LogRecord JavaDoc;
35 import java.util.logging.Logger JavaDoc;
36 import java.util.logging.StreamHandler JavaDoc;
37 import java.util.logging.XMLFormatter JavaDoc;
38 import java.util.regex.Matcher JavaDoc;
39 import java.util.regex.Pattern JavaDoc;
40 import org.netbeans.junit.MockServices;
41 import org.netbeans.junit.NbTestCase;
42 import org.openide.util.Lookup;
43 import org.openide.util.RequestProcessor;
44 import org.openide.util.lookup.AbstractLookup;
45 import org.openide.util.lookup.InstanceContent;
46
47
48 /**
49  * Checks the top logging delegates to handlers in lookup.
50  */

51 public class TopLoggingLookupTest extends NbTestCase {
52     private MyHandler handler;
53     
54     public TopLoggingLookupTest(String JavaDoc testName) {
55         super(testName);
56     }
57
58     protected void setUp() throws Exception JavaDoc {
59         clearWorkDir();
60         System.setProperty("netbeans.user", getWorkDirPath());
61
62         MockServices.setServices();
63
64         // initialize logging
65
TopLogging.initialize();
66     }
67
68
69     protected void tearDown() throws Exception JavaDoc {
70     }
71
72     public void testLogOneLine() throws Exception JavaDoc {
73         MockServices.setServices(MyHandler.class);
74         handler = Lookup.getDefault().lookup(MyHandler.class);
75         assertNotNull("Handler found", handler);
76
77         
78         Logger.getLogger(TopLoggingTest.class.getName()).log(Level.INFO, "First visible message");
79
80         assertEquals("[First visible message]", handler.logs.toString());
81     }
82
83     public void testDeadlock78865() throws Exception JavaDoc {
84         MockServices.setServices(AnotherThreadLoggingHandler.class);
85         handler = Lookup.getDefault().lookup(MyHandler.class);
86         assertNotNull("Handler found", handler);
87
88         Logger.getLogger(TopLoggingTest.class.getName()).log(Level.INFO, "First visible message");
89
90         assertEquals("[First visible message]", handler.logs.toString());
91     }
92
93     public static class MyHandler extends Handler JavaDoc {
94         public List JavaDoc<String JavaDoc> logs = new ArrayList JavaDoc<String JavaDoc>();
95
96         public void publish(LogRecord JavaDoc record) {
97             logs.add(record.getMessage());
98         }
99
100         public void flush() {
101             logs.add("flush");
102         }
103
104         public void close() throws SecurityException JavaDoc {
105             logs.add("close");
106         }
107
108     }
109     public static final class AnotherThreadLoggingHandler extends MyHandler
110     implements Runnable JavaDoc {
111         public AnotherThreadLoggingHandler() {
112             Logger.global.info("in constructor before");
113             RequestProcessor.getDefault().post(this).waitFinished();
114             Logger.global.info("in constructor after");
115         }
116         public void run() {
117             Logger.global.warning("running in parael");
118         }
119
120     }
121 }
122
Popular Tags