1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.math;
19  
20  import junit.framework.TestCase;
21  
22  import java.io.ByteArrayOutputStream;
23  import java.io.PrintStream;
24  import java.io.PrintWriter;
25  import java.util.Locale;
26  
27  /**
28   * @version $Revision: 746578 $ $Date: 2009-02-21 15:01:14 -0500 (Sat, 21 Feb 2009) $
29   */
30  public class MathExceptionTest extends TestCase {
31  
32      public void testConstructor(){
33          MathException ex = new MathException();
34          assertNull(ex.getCause());
35          assertNull(ex.getMessage());
36          assertEquals(0, ex.getMessage(Locale.FRENCH).length());
37      }
38      
39      public void testConstructorPatternArguments(){
40          String pattern = "a {0}x{1} matrix cannot be a rotation matrix";
41          Object[] arguments = { Integer.valueOf(6), Integer.valueOf(4) };
42          MathException ex = new MathException(pattern, arguments);
43          assertNull(ex.getCause());
44          assertEquals(pattern, ex.getPattern());
45          assertEquals(arguments.length, ex.getArguments().length);
46          for (int i = 0; i < arguments.length; ++i) {
47              assertEquals(arguments[i], ex.getArguments()[i]);
48          }
49          assertFalse(pattern.equals(ex.getMessage()));
50          assertFalse(ex.getMessage().equals(ex.getMessage(Locale.FRENCH)));
51      }
52      
53      public void testConstructorCause(){
54          String inMsg = "inner message";
55          Exception cause = new Exception(inMsg);
56          MathException ex = new MathException(cause);
57          assertEquals(cause, ex.getCause());
58      }
59  
60      public void testConstructorPatternArgumentsCause(){
61          String pattern = "a {0}x{1} matrix cannot be a rotation matrix";
62          Object[] arguments = { Integer.valueOf(6), Integer.valueOf(4) };
63          String inMsg = "inner message";
64          Exception cause = new Exception(inMsg);
65          MathException ex = new MathException(cause, pattern, arguments);
66          assertEquals(cause, ex.getCause());
67          assertEquals(pattern, ex.getPattern());
68          assertEquals(arguments.length, ex.getArguments().length);
69          for (int i = 0; i < arguments.length; ++i) {
70              assertEquals(arguments[i], ex.getArguments()[i]);
71          }
72          assertFalse(pattern.equals(ex.getMessage()));
73          assertFalse(ex.getMessage().equals(ex.getMessage(Locale.FRENCH)));
74      }
75      
76      /**
77       * Tests the printStackTrace() operation.
78       */
79      public void testPrintStackTrace() {
80          String outMsg = "outer message";
81          String inMsg = "inner message";
82          MathException cause = new MathConfigurationException(inMsg);
83          MathException ex = new MathException(cause, outMsg);
84          ByteArrayOutputStream baos = new ByteArrayOutputStream();
85          PrintStream ps = new PrintStream(baos);
86          ex.printStackTrace(ps);
87          String stack = baos.toString();
88          String outerMsg = "org.apache.commons.math.MathException: outer message";
89          String innerMsg = "Caused by: " + 
90          "org.apache.commons.math.MathConfigurationException: inner message";
91          assertTrue(stack.startsWith(outerMsg));
92          assertTrue(stack.indexOf(innerMsg) > 0);
93          
94          PrintWriter pw = new PrintWriter(ps, true);
95          ex.printStackTrace(pw);
96          stack = baos.toString();
97          assertTrue(stack.startsWith(outerMsg));
98          assertTrue(stack.indexOf(innerMsg) > 0);
99      }
100     
101     /**
102      * Test serialization
103      */
104     public void testSerialization() {
105         String outMsg = "outer message";
106         String inMsg = "inner message";
107         MathException cause = new MathConfigurationException(inMsg);
108         MathException ex = new MathException(cause, outMsg);
109         MathException image = (MathException) TestUtils.serializeAndRecover(ex);
110         
111         ByteArrayOutputStream baos = new ByteArrayOutputStream();
112         PrintStream ps = new PrintStream(baos);
113         ex.printStackTrace(ps);
114         String stack = baos.toString();
115         
116         ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
117         PrintStream ps2 = new PrintStream(baos2);
118         image.printStackTrace(ps2);
119         String stack2 = baos2.toString();
120         
121         // See if JDK supports nested exceptions.  If not, stack trace of
122         // inner exception will not be serialized
123         boolean jdkSupportsNesting = false;
124         try {
125             Throwable.class.getDeclaredMethod("getCause", new Class[0]);
126             jdkSupportsNesting = true;
127         } catch (NoSuchMethodException e) {
128             jdkSupportsNesting = false;
129         }
130         
131         if (jdkSupportsNesting) {
132             assertEquals(stack, stack2);
133         } else {
134             assertTrue(stack2.indexOf(inMsg) != -1);
135             assertTrue(stack2.indexOf("MathConfigurationException") != -1);
136         }
137     }
138 }