KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > exceptions > CollectorTest


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
21 package org.netbeans.modules.exceptions;
22
23 import junit.framework.*;
24 import java.util.Iterator JavaDoc;
25 import java.util.LinkedList JavaDoc;
26 import java.util.logging.Handler JavaDoc;
27 import java.util.logging.Handler JavaDoc;
28 import java.util.logging.Handler JavaDoc;
29 import java.util.logging.Level JavaDoc;
30 import java.util.logging.LogRecord JavaDoc;
31 import java.util.logging.Logger JavaDoc;
32 import org.netbeans.modules.exceptions.listeners.UIHandler;
33
34 /**
35  *
36  * @author jindra
37  */

38 public class CollectorTest extends TestCase {
39     private final String JavaDoc prop_name = "netbeans.exception.report.min.level";
40     private static LinkedList JavaDoc<LogRecord JavaDoc> recs = null;
41     private static Iterator JavaDoc<LogRecord JavaDoc> it = null;
42
43     public CollectorTest(String JavaDoc testName) {
44         super(testName);
45     }
46
47     /**
48      * Test of getDefault method, of class org.netbeans.modules.exceptions.Collector.
49      */

50     public void testGetDefault() {
51         System.out.println("getDefault");
52         final Collector collector = Collector.getDefault();
53         assertEquals(collector, Collector.getDefault());
54         Thread JavaDoc t = new Thread JavaDoc(new Runnable JavaDoc(){
55             public void run(){
56                 assertEquals(collector, Collector.getDefault());
57             }
58         });
59         t.start();
60     }
61     
62     /**
63      * Test of start method, of class org.netbeans.modules.exceptions.Collector.
64      */

65     public void testStart() {
66         System.out.println("start");
67         Collector coll = Collector.getDefault();
68         coll.start();
69         //test that old NB error manager is disabled
70
String JavaDoc str = System.getProperty(prop_name);
71         assertEquals(str, "1001");
72         //test that handler was added
73
assertTrue(containsHanlder(Logger.getLogger(""), ExceptionsHandler.class));
74         //test that UIHandler was added
75
assertTrue(containsHanlder(Logger.getLogger("org.netbeans.ui"), UIHandler.class));
76         assertTrue(Logger.getLogger("org.netbeans.ui").getLevel().equals(Level.FINEST));
77         //test that listeners were added
78

79     }
80     
81     /**
82      * Test of write method, of class org.netbeans.modules.exceptions.Collector.
83      * Test of getFronta method, of class org.netbeans.modules.exceptions.Collector.
84      */

85     public void testWrite() {
86         System.out.println("write");
87         final Collector instance = Collector.getDefault();
88         Runnable JavaDoc r = new Runnable JavaDoc(){
89             public void run(){
90                 LogRecord JavaDoc l = getNext();
91                 while (l != null){
92                     instance.write(l);
93                     l = getNext();
94                     Thread.yield();
95                 }
96             }
97         };
98         Thread JavaDoc t1 = new Thread JavaDoc(r);
99         Thread JavaDoc t2 = new Thread JavaDoc(r);
100         Thread JavaDoc t3 = new Thread JavaDoc(r);
101         t1.start();
102         t2.start();
103         t3.start();
104         try{
105             t3.join();
106             t2.join();
107             t1.join();
108         }catch(Exception JavaDoc ex){
109             ex.printStackTrace();
110         }
111         LinkedList JavaDoc<LogRecord JavaDoc> fronta = Collector.getDefault().getQueue();
112         LogRecord JavaDoc rec;
113         while ((rec = recs.poll())!= null){
114             assertTrue(fronta.contains(rec));
115         }
116         int i;
117         rec = new LogRecord JavaDoc(Level.SEVERE, "BYE");
118         for (i=0;i<10;i++) instance.write(new LogRecord JavaDoc(Level.SEVERE, "HALLO"));
119         assertTrue(Collector.getDefault().getQueue().size()==Collector.MAX_LOGS+1);
120         for (i=0;i<Collector.MAX_LOGS;i++) instance.write(new LogRecord JavaDoc(Level.SEVERE, "HALLO"));
121         assertTrue(!Collector.getDefault().getQueue().contains(rec));
122         
123     }
124        
125     /**
126      * Test of close method, of class org.netbeans.modules.exceptions.Collector.
127      */

128     public void testClose() {
129         System.out.println("close");
130         
131         Collector.getDefault().close();
132         assertFalse(containsHanlder(Logger.getLogger("org.netbeans.ui"), UIHandler.class));
133         assertFalse(containsHanlder(Logger.getLogger(""), ExceptionsHandler.class));
134         
135         // TODO review the generated test code and remove the default call to fail.
136
// fail("The test case is a prototype.");
137
}
138
139     
140     /*------------------PRIVATE---------------*/
141     private synchronized LogRecord JavaDoc getNext(){
142         if ((recs == null)||(it==null)) {
143             recs = createRecs();
144             it = recs.iterator();
145         }
146         if (it.hasNext()) return it.next();
147         else return null;
148     }
149     
150     private LinkedList JavaDoc<LogRecord JavaDoc> createRecs(){
151         recs = new LinkedList JavaDoc<LogRecord JavaDoc>();
152         int i = Collector.MAX_LOGS;
153         while (i-- > 0) recs.add(new LogRecord JavaDoc(Level.INFO, "log: "+Long.toString(Math.round(Math.random()*Collector.MAX_LOGS))));
154         return recs;
155     }
156
157     private boolean containsHanlder(Logger JavaDoc log, Class JavaDoc cl){
158         Handler JavaDoc[] handlers;
159         int i = 0;
160         handlers = log.getHandlers();
161         while (i < handlers.length){
162             if (cl.isInstance(handlers[i++])) return true;
163         }
164         return false;
165     }
166 }
167
Popular Tags