1   /*
2    * Copyright 2003-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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         // See if JDK supports nested exceptions.  If not, stack trace of
118         // inner exception will not be serialized
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 }