KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > util > ExceptionsTest


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.util;
21
22 import java.io.PrintWriter JavaDoc;
23 import java.io.StringWriter JavaDoc;
24 import java.util.logging.Level JavaDoc;
25 import junit.framework.TestCase;
26 import org.netbeans.junit.Log;
27
28 /**
29  * @author Jaroslav Tulach
30  */

31 public class ExceptionsTest extends TestCase {
32
33     public ExceptionsTest(String JavaDoc testName) {
34         super(testName);
35     }
36
37     private void assertCleanStackTrace(Throwable JavaDoc t) {
38         StringWriter JavaDoc w = new StringWriter JavaDoc();
39         PrintWriter JavaDoc pw = new PrintWriter JavaDoc(w);
40         t.printStackTrace(pw);
41         pw.flush();
42         String JavaDoc m = w.toString();
43         assertFalse(m.replace("\n", "\\n").replace("\t", "\\t"), m.contains("AnnException"));
44         assertFalse(m.replace("\n", "\\n").replace("\t", "\\t"), m.contains("msg"));
45     }
46
47     public void testAttachMessage() {
48         Exception JavaDoc e = new Exception JavaDoc("Help");
49         String JavaDoc msg = "me please";
50         
51         Exception JavaDoc result = Exceptions.attachMessage(e, msg);
52
53         assertSame(result, e);
54
55         StringWriter JavaDoc w = new StringWriter JavaDoc();
56         result.printStackTrace(new PrintWriter JavaDoc(w));
57
58         String JavaDoc m = w.toString();
59
60         if (m.indexOf(msg) == -1) {
61             fail(msg + " shall be part of output:\n" + m);
62         }
63
64         assertCleanStackTrace(e);
65     }
66     
67     public void testAttachMessageForClassNotFound() {
68         Exception JavaDoc e = new ClassNotFoundException JavaDoc("Help");
69         String JavaDoc msg = "me please";
70         
71         Exception JavaDoc result = Exceptions.attachMessage(e, msg);
72
73         assertSame(result, e);
74
75         CharSequence JavaDoc log = Log.enable("", Level.WARNING);
76         Exceptions.printStackTrace(e);
77
78         String JavaDoc m = log.toString();
79
80         if (m.indexOf(msg) == -1) {
81             fail(msg + " shall be part of output:\n" + m);
82         }
83
84         assertCleanStackTrace(e);
85     }
86
87     public void testAttachLocalizedMessage() {
88         Exception JavaDoc e = new Exception JavaDoc("Help");
89         String JavaDoc msg = "me please";
90         
91         Exception JavaDoc expResult = e;
92         Exception JavaDoc result = Exceptions.attachLocalizedMessage(e, msg);
93         assertEquals(expResult, result);
94
95         String JavaDoc fnd = Exceptions.findLocalizedMessage(e);
96
97         assertEquals("The same msg", msg, fnd);
98
99         assertCleanStackTrace(e);
100     }
101
102     public void testAttachLocalizedMessageForClassNFE() {
103         Exception JavaDoc e = new ClassNotFoundException JavaDoc("Help");
104         String JavaDoc msg = "me please";
105         
106         Exception JavaDoc expResult = e;
107         Exception JavaDoc result = Exceptions.attachLocalizedMessage(e, msg);
108         assertEquals(expResult, result);
109
110         String JavaDoc fnd = Exceptions.findLocalizedMessage(e);
111
112         assertEquals("The same msg", msg, fnd);
113
114         assertCleanStackTrace(e);
115     }
116
117     public void testAttachLocalizedMessageForClassNFEIfNoMsg() {
118         Exception JavaDoc e = new ClassNotFoundException JavaDoc("Help");
119         String JavaDoc msg = "me please";
120         
121         Exception JavaDoc expResult = e;
122         Exception JavaDoc result = Exceptions.attachMessage(e, msg);
123         assertEquals(expResult, result);
124
125         String JavaDoc fnd = Exceptions.findLocalizedMessage(e);
126
127         assertEquals("No localized msg found", null, fnd);
128
129         assertCleanStackTrace(e);
130     }
131     
132 }
133
Popular Tags