KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > loaders > LoggingControlTest


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
20 package org.openide.loaders;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.logging.Level JavaDoc;
24 import junit.framework.TestFailure;
25 import junit.framework.TestResult;
26 import org.openide.ErrorManager;
27 import org.openide.util.RequestProcessor;
28
29
30 /** Checks that behaviour of LoggingTestCaseHid is correct.
31  *
32  * @author Jaroslav Tulach
33  */

34 public class LoggingControlTest extends LoggingTestCaseHid {
35
36     private ErrorManager err;
37
38     public LoggingControlTest (String JavaDoc name) {
39         super (name);
40     }
41
42     protected void setUp() throws Exception JavaDoc {
43         err = ErrorManager.getDefault().getInstance("TEST-" + getName());
44     }
45     
46     protected Level JavaDoc logLevel() {
47         return Level.ALL;
48     }
49
50     public void testCorrectThreadSwitching() throws Exception JavaDoc {
51         
52         class Run implements Runnable JavaDoc {
53             public ArrayList JavaDoc events = new ArrayList JavaDoc();
54             
55             public void run() {
56                 events.add("A");
57                 err.log("A");
58                 events.add("B");
59                 err.log("B");
60                 events.add("C");
61                 err.log("C");
62             }
63             
64             public void directly() {
65                 err.log("0");
66                 events.add(new Integer JavaDoc(1));
67                 err.log("1");
68                 events.add(new Integer JavaDoc(2));
69                 err.log("2");
70                 events.add(new Integer JavaDoc(3));
71                 err.log("3");
72             }
73         }
74         
75         Run run = new Run();
76         
77         String JavaDoc order =
78             "THREAD:Para MSG:A" +
79             "THREAD:main MSG:0" +
80             "THREAD:main MSG:1" +
81             "THREAD:Para MSG:B" +
82             "THREAD:main MSG:2" +
83             "THREAD:Para MSG:C" +
84             "THREAD:main MSG:3";
85         registerSwitches(order, 0);
86         
87         
88         RequestProcessor rp = new RequestProcessor("Para");
89         RequestProcessor.Task task = rp.post(run);
90         run.directly();
91         if (!task.waitFinished(10000)) {
92             fail("Runnable deadlocked");
93         }
94         
95         String JavaDoc res = run.events.toString();
96         
97         assertEquals("Really changing the execution according to the provided order: " + res, "[A, 1, B, 2, C, 3]", res);
98     }
99     
100     public void testWorksWithRegularExpressionsAsWell() throws Exception JavaDoc {
101         
102         class Run implements Runnable JavaDoc {
103             public ArrayList JavaDoc events = new ArrayList JavaDoc();
104             
105             public void run() {
106                 events.add("A");
107                 err.log("4329043A");
108                 events.add("B");
109                 err.log("B");
110                 events.add("C");
111                 err.log("CCCC");
112             }
113             
114             public void directly() {
115                 err.log("0");
116                 events.add(new Integer JavaDoc(1));
117                 err.log("1");
118                 events.add(new Integer JavaDoc(2));
119                 err.log("2");
120                 events.add(new Integer JavaDoc(3));
121                 err.log("3");
122             }
123         }
124         
125         Run run = new Run();
126         
127         String JavaDoc order =
128             "THREAD:Para MSG:[0-9]*A" +
129             "THREAD:main MSG:0" +
130             "THREAD:main MSG:^1$" +
131             "THREAD:Para MSG:B" +
132             "THREAD:main MSG:2" +
133             "THREAD:Para MSG:C+" +
134             "THREAD:main MSG:3";
135         registerSwitches(order, 0);
136         
137         
138         RequestProcessor rp = new RequestProcessor("Para");
139         RequestProcessor.Task task = rp.post(run);
140         run.directly();
141         if (!task.waitFinished(10000)) {
142             fail("Runnable deadlocked");
143         }
144         
145         String JavaDoc res = run.events.toString();
146         
147         assertEquals("Really changing the execution according to the provided order: " + res, "[A, 1, B, 2, C, 3]", res);
148     }
149
150     public void testLogMessagesCanRepeat() throws Exception JavaDoc {
151         
152         class Run implements Runnable JavaDoc {
153             public ArrayList JavaDoc events = new ArrayList JavaDoc();
154             
155             public void run() {
156                 events.add("A");
157                 err.log("A");
158                 events.add("A");
159                 err.log("A");
160                 events.add("A");
161                 err.log("A");
162             }
163             
164             public void directly() {
165                 err.log("0");
166                 events.add(new Integer JavaDoc(1));
167                 err.log("1");
168                 events.add(new Integer JavaDoc(2));
169                 err.log("2");
170                 events.add(new Integer JavaDoc(3));
171                 err.log("3");
172             }
173         }
174         
175         Run run = new Run();
176         
177         String JavaDoc order =
178             "THREAD:Para MSG:A" +
179             "THREAD:main MSG:0" +
180             "THREAD:main MSG:^1$" +
181             "THREAD:Para MSG:A" +
182             "THREAD:main MSG:2" +
183             "THREAD:Para MSG:A" +
184             "THREAD:main MSG:3";
185         registerSwitches(order, 0);
186         
187         
188         RequestProcessor rp = new RequestProcessor("Para");
189         RequestProcessor.Task task = rp.post(run);
190         run.directly();
191         if (!task.waitFinished(10000)) {
192             fail("Runnable deadlocked");
193         }
194         
195         String JavaDoc res = run.events.toString();
196         
197         assertEquals("Really changing the execution according to the provided order: " + res, "[A, 1, A, 2, A, 3]", res);
198     }
199
200     private Exception JavaDoc throwIt;
201     public void testRuntimeExceptionsAlsoGenerateLog() throws Exception JavaDoc {
202         if (throwIt != null) {
203             ErrorManager.getDefault().log("Ahoj");
204             throw throwIt;
205         }
206         
207         LoggingControlTest l = new LoggingControlTest("testRuntimeExceptionsAlsoGenerateLog");
208         l.throwIt = new NullPointerException JavaDoc();
209         TestResult res = l.run();
210         assertEquals("No failures", 0, res.failureCount());
211         assertEquals("One error", 1, res.errorCount());
212         
213         Object JavaDoc o = res.errors().nextElement();
214         TestFailure f = (TestFailure)o;
215         
216         if (f.exceptionMessage() == null || f.exceptionMessage().indexOf("Ahoj") == -1) {
217             fail("Logged messages shall be in exception message: " + f.exceptionMessage());
218         }
219     }
220 }
221
Popular Tags