1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.math;
18
19 import junit.framework.TestCase;
20
21 import java.io.ByteArrayOutputStream;
22 import java.io.PrintStream;
23 import java.io.PrintWriter;
24
25 /**
26 * @version $Revision: 201820 $ $Date: 2005-06-25 20:26:42 -0700 (Sat, 25 Jun 2005) $
27 */
28 public class MathExceptionTest extends TestCase {
29 /**
30 *
31 */
32 public void testConstructor(){
33 MathException ex = new MathException();
34 assertNull(ex.getCause());
35 assertNull(ex.getMessage());
36 }
37
38 /**
39 *
40 */
41 public void testConstructorMessage(){
42 String msg = "message";
43 MathException ex = new MathException(msg);
44 assertNull(ex.getCause());
45 assertEquals(msg, ex.getMessage());
46 }
47
48 /**
49 *
50 */
51 public void testConstructorMessageCause(){
52 String outMsg = "outer message";
53 String inMsg = "inner message";
54 Exception cause = new Exception(inMsg);
55 MathException ex = new MathException(outMsg, cause);
56 assertEquals(outMsg, ex.getMessage());
57 assertEquals(cause, ex.getCause());
58 }
59
60 /**
61 *
62 */
63 public void testConstructorCause(){
64 String inMsg = "inner message";
65 Exception cause = new Exception(inMsg);
66 MathException ex = new MathException(cause);
67 assertEquals(cause, ex.getCause());
68 }
69
70 /**
71 * Tests the printStackTrace() operation.
72 */
73 public void testPrintStackTrace() {
74 String outMsg = "outer message";
75 String inMsg = "inner message";
76 MathException cause = new MathConfigurationException(inMsg);
77 MathException ex = new MathException(outMsg, cause);
78 ByteArrayOutputStream baos = new ByteArrayOutputStream();
79 PrintStream ps = new PrintStream(baos);
80 ex.printStackTrace(ps);
81 String stack = baos.toString();
82 String outerMsg = "org.apache.commons.math.MathException: outer message";
83 String innerMsg = "Caused by: " +
84 "org.apache.commons.math.MathConfigurationException: inner message";
85 assertTrue(stack.startsWith(outerMsg));
86 assertTrue(stack.indexOf(innerMsg) > 0);
87
88 PrintWriter pw = new PrintWriter(ps, true);
89 ex.printStackTrace(pw);
90 stack = baos.toString();
91 assertTrue(stack.startsWith(outerMsg));
92 assertTrue(stack.indexOf(innerMsg) > 0);
93 }
94
95 /**
96 * Test serialization
97 */
98 public void testSerialization() {
99 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.serializeAndRecover(ex);
104
105 ByteArrayOutputStream baos = new ByteArrayOutputStream();
106 PrintStream ps = new PrintStream(baos);
107 PrintWriter pw = new PrintWriter(ps, true);
108 ex.printStackTrace(ps);
109 String stack = baos.toString();
110
111 ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
112 PrintStream ps2 = new PrintStream(baos2);
113 PrintWriter pw2 = new PrintWriter(ps2, true);
114 image.printStackTrace(ps2);
115 String stack2 = baos2.toString();
116
117
118
119 boolean jdkSupportsNesting = false;
120 try {
121 Throwable.class.getDeclaredMethod("getCause", new Class[0]);
122 jdkSupportsNesting = true;
123 } catch (NoSuchMethodException e) {
124 jdkSupportsNesting = false;
125 }
126
127 if (jdkSupportsNesting) {
128 assertEquals(stack, stack2);
129 } else {
130 assertTrue(stack2.indexOf(inMsg) != -1);
131 assertTrue(stack2.indexOf("MathConfigurationException") != -1);
132 }
133 }
134 }