001: /*
002: * Copyright 2003-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.commons.math;
018:
019: import junit.framework.TestCase;
020:
021: import java.io.ByteArrayOutputStream;
022: import java.io.PrintStream;
023: import java.io.PrintWriter;
024:
025: /**
026: * @version $Revision: 201820 $ $Date: 2005-06-25 20:26:42 -0700 (Sat, 25 Jun 2005) $
027: */
028: public class MathExceptionTest extends TestCase {
029: /**
030: *
031: */
032: public void testConstructor() {
033: MathException ex = new MathException();
034: assertNull(ex.getCause());
035: assertNull(ex.getMessage());
036: }
037:
038: /**
039: *
040: */
041: public void testConstructorMessage() {
042: String msg = "message";
043: MathException ex = new MathException(msg);
044: assertNull(ex.getCause());
045: assertEquals(msg, ex.getMessage());
046: }
047:
048: /**
049: *
050: */
051: public void testConstructorMessageCause() {
052: String outMsg = "outer message";
053: String inMsg = "inner message";
054: Exception cause = new Exception(inMsg);
055: MathException ex = new MathException(outMsg, cause);
056: assertEquals(outMsg, ex.getMessage());
057: assertEquals(cause, ex.getCause());
058: }
059:
060: /**
061: *
062: */
063: public void testConstructorCause() {
064: String inMsg = "inner message";
065: Exception cause = new Exception(inMsg);
066: MathException ex = new MathException(cause);
067: assertEquals(cause, ex.getCause());
068: }
069:
070: /**
071: * Tests the printStackTrace() operation.
072: */
073: public void testPrintStackTrace() {
074: String outMsg = "outer message";
075: String inMsg = "inner message";
076: MathException cause = new MathConfigurationException(inMsg);
077: MathException ex = new MathException(outMsg, cause);
078: ByteArrayOutputStream baos = new ByteArrayOutputStream();
079: PrintStream ps = new PrintStream(baos);
080: ex.printStackTrace(ps);
081: String stack = baos.toString();
082: String outerMsg = "org.apache.commons.math.MathException: outer message";
083: String innerMsg = "Caused by: "
084: + "org.apache.commons.math.MathConfigurationException: inner message";
085: assertTrue(stack.startsWith(outerMsg));
086: assertTrue(stack.indexOf(innerMsg) > 0);
087:
088: PrintWriter pw = new PrintWriter(ps, true);
089: ex.printStackTrace(pw);
090: stack = baos.toString();
091: assertTrue(stack.startsWith(outerMsg));
092: assertTrue(stack.indexOf(innerMsg) > 0);
093: }
094:
095: /**
096: * Test serialization
097: */
098: public void testSerialization() {
099: String outMsg = "outer message";
100: String inMsg = "inner message";
101: MathException cause = new MathConfigurationException(inMsg);
102: MathException ex = new MathException(outMsg, cause);
103: MathException image = (MathException) TestUtils
104: .serializeAndRecover(ex);
105:
106: ByteArrayOutputStream baos = new ByteArrayOutputStream();
107: PrintStream ps = new PrintStream(baos);
108: PrintWriter pw = new PrintWriter(ps, true);
109: ex.printStackTrace(ps);
110: String stack = baos.toString();
111:
112: ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
113: PrintStream ps2 = new PrintStream(baos2);
114: PrintWriter pw2 = new PrintWriter(ps2, true);
115: image.printStackTrace(ps2);
116: String stack2 = baos2.toString();
117:
118: // See if JDK supports nested exceptions. If not, stack trace of
119: // inner exception will not be serialized
120: boolean jdkSupportsNesting = false;
121: try {
122: Throwable.class.getDeclaredMethod("getCause", new Class[0]);
123: jdkSupportsNesting = true;
124: } catch (NoSuchMethodException e) {
125: jdkSupportsNesting = false;
126: }
127:
128: if (jdkSupportsNesting) {
129: assertEquals(stack, stack2);
130: } else {
131: assertTrue(stack2.indexOf(inMsg) != -1);
132: assertTrue(stack2.indexOf("MathConfigurationException") != -1);
133: }
134: }
135: }
|