001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.openide.util;
043:
044: import java.io.PrintWriter;
045: import java.io.StringWriter;
046: import java.util.logging.Level;
047: import junit.framework.TestCase;
048: import org.netbeans.junit.Log;
049:
050: /**
051: * @author Jaroslav Tulach
052: */
053: public class ExceptionsTest extends TestCase {
054:
055: public ExceptionsTest(String testName) {
056: super (testName);
057: }
058:
059: private void assertCleanStackTrace(Throwable t) {
060: StringWriter w = new StringWriter();
061: PrintWriter pw = new PrintWriter(w);
062: t.printStackTrace(pw);
063: pw.flush();
064: String m = w.toString();
065: assertFalse(m.replace("\n", "\\n").replace("\t", "\\t"), m
066: .contains("AnnException"));
067: assertFalse(m.replace("\n", "\\n").replace("\t", "\\t"), m
068: .contains("msg"));
069: }
070:
071: public void testAttachMessage() {
072: Exception e = new Exception("Help");
073: String msg = "me please";
074:
075: Exception result = Exceptions.attachMessage(e, msg);
076:
077: assertSame(result, e);
078:
079: StringWriter w = new StringWriter();
080: result.printStackTrace(new PrintWriter(w));
081:
082: String m = w.toString();
083:
084: if (m.indexOf(msg) == -1) {
085: fail(msg + " shall be part of output:\n" + m);
086: }
087:
088: assertCleanStackTrace(e);
089: }
090:
091: public void testAttachMessageForClassNotFound() {
092: Exception e = new ClassNotFoundException("Help");
093: String msg = "me please";
094:
095: Exception result = Exceptions.attachMessage(e, msg);
096:
097: assertSame(result, e);
098:
099: CharSequence log = Log.enable("", Level.WARNING);
100: Exceptions.printStackTrace(e);
101:
102: String m = log.toString();
103:
104: if (m.indexOf(msg) == -1) {
105: fail(msg + " shall be part of output:\n" + m);
106: }
107:
108: assertCleanStackTrace(e);
109: }
110:
111: public void testAttachLocalizedMessage() {
112: Exception e = new Exception("Help");
113: String msg = "me please";
114:
115: Exception expResult = e;
116: Exception result = Exceptions.attachLocalizedMessage(e, msg);
117: assertEquals(expResult, result);
118:
119: String fnd = Exceptions.findLocalizedMessage(e);
120:
121: assertEquals("The same msg", msg, fnd);
122:
123: assertCleanStackTrace(e);
124: }
125:
126: public void testAttachLocalizedMessageForClassNFE() {
127: Exception e = new ClassNotFoundException("Help");
128: String msg = "me please";
129:
130: Exception expResult = e;
131: Exception result = Exceptions.attachLocalizedMessage(e, msg);
132: assertEquals(expResult, result);
133:
134: String fnd = Exceptions.findLocalizedMessage(e);
135:
136: assertEquals("The same msg", msg, fnd);
137:
138: assertCleanStackTrace(e);
139: }
140:
141: public void testAttachLocalizedMessageForClassNFEIfNoMsg() {
142: Exception e = new ClassNotFoundException("Help");
143: String msg = "me please";
144:
145: Exception expResult = e;
146: Exception result = Exceptions.attachMessage(e, msg);
147: assertEquals(expResult, result);
148:
149: String fnd = Exceptions.findLocalizedMessage(e);
150:
151: assertEquals("No localized msg found", null, fnd);
152:
153: assertCleanStackTrace(e);
154: }
155:
156: }
|