KickJava   Java API By Example, From Geeks To Geeks.

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


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.netbeans.modules.exceptions;
21
22 import java.io.FileNotFoundException JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.LinkedList JavaDoc;
26 import java.util.Set JavaDoc;
27 import junit.framework.TestCase;
28 import java.util.logging.Level JavaDoc;
29 import java.util.logging.LogRecord JavaDoc;
30
31 /**
32  *
33  * @author jindra
34  */

35 public class ExceptionLoggerTest extends TestCase {
36     
37     public ExceptionLoggerTest(String JavaDoc testName) {
38         super(testName);
39     }
40     
41     protected void setUp() throws Exception JavaDoc {
42     }
43     
44     protected void tearDown() throws Exception JavaDoc {
45     }
46     
47     Set JavaDoc<Throwable JavaDoc> causes = new HashSet JavaDoc<Throwable JavaDoc>();
48     
49     /**
50      * Test of logError method, of class org.netbeans.modules.exceptions.ExceptionLogger.
51      */

52     public void testLogError() {
53         System.out.println("logError");
54         boolean inserted = false;
55         String JavaDoc str = "testing log record";
56         ClassCircularityError JavaDoc error = new ClassCircularityError JavaDoc("testing error");
57         LogRecord JavaDoc rec = new LogRecord JavaDoc(Level.WARNING, str);
58         rec.setThrown(error);
59         ExceptionLogger.logError(rec);
60         LinkedList JavaDoc<LogRecord JavaDoc> logs = Collector.getDefault().getQueue();
61         Iterator JavaDoc<LogRecord JavaDoc> it = logs.iterator();
62         while (it.hasNext()){
63             LogRecord JavaDoc log = it.next();
64             if ((log.getLevel().equals(Level.WARNING))&&(log.getMessage().contains(
65                     "WARNING *********** Exception occurred ************ at "))){
66                 Throwable JavaDoc t = log.getThrown();
67                 if ((t!= null)) inserted=true;
68             }
69         }
70         assertTrue(inserted);
71         
72     }
73     
74     /**
75      * Test of convert method, of class org.netbeans.modules.exceptions.ExceptionLogger.
76      */

77     public void testConvert() {
78         System.out.println("convert");
79         String JavaDoc str = "conversion";
80         String JavaDoc str2 = "reason";
81         String JavaDoc s3 = "reason of reason";
82         Throwable JavaDoc t = new NullPointerException JavaDoc(str);
83         causes.clear();
84         Throwable JavaDoc result = ExceptionLogger.convert(t);
85         assertTrue(testEquals(t, result));
86         Throwable JavaDoc t2 = new ClassCastException JavaDoc(str2);
87         t.initCause(t2);
88         causes.clear();
89         result = ExceptionLogger.convert(t);
90         assertTrue(testEquals(t, result));
91         Throwable JavaDoc t3 = new FileNotFoundException JavaDoc(s3);
92         t3.initCause(t2);
93         causes.clear();
94         result = ExceptionLogger.convert(t3);
95         assertTrue(testEquals(t3, result));
96         Throwable JavaDoc t4 = new NullPointerException JavaDoc(str);
97         Throwable JavaDoc t5 = new NullPointerException JavaDoc(str2);
98         //test cycle annotation
99
t4.initCause(t5);
100         t5.initCause(t4);
101         causes.clear();
102         result = ExceptionLogger.convert(t4);
103         assertTrue(testEquals(t4, result));
104         
105     }
106     
107     private boolean testEquals(Throwable JavaDoc t, Throwable JavaDoc result){ // exceptions are equals in our metrics
108
StackTraceElement JavaDoc[] stack = t.getStackTrace();
109         StackTraceElement JavaDoc[] result_stack = result.getStackTrace();
110         if (t != null) causes.add(t);
111         for (int i = 0; i < stack.length; i++) {
112             assertEquals(stack[i], result_stack[i]);
113         }
114         if ((t.getCause() != null)&&(!causes.contains(t.getCause()))){
115             assertTrue(result.getCause() != null);
116             return testEquals(t.getCause(), result.getCause());
117         }else return true;
118     }
119     
120     /**
121      * Test of log method, of class org.netbeans.modules.exceptions.ExceptionLogger.
122      */

123     public void testLog() {
124         System.out.println("log");
125         Level JavaDoc severity = Level.INFO;
126         String JavaDoc s = "info";
127         boolean contains = false;
128         ExceptionLogger.log(severity, s);
129         LinkedList JavaDoc<LogRecord JavaDoc> list = Collector.getDefault().getQueue();
130         Iterator JavaDoc<LogRecord JavaDoc> it = list.iterator();
131         while (it.hasNext()){
132             LogRecord JavaDoc rec =it.next();
133             if ((rec.getLevel().equals(Level.INFO))&&(rec.getMessage().equals(s))) contains=true;
134         }
135         assertTrue(contains);
136     }
137     
138 }
139
Popular Tags