KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > math > MathExceptionTest


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 JavaDoc;
22 import java.io.PrintStream JavaDoc;
23 import java.io.PrintWriter JavaDoc;
24
25 /**
26  * @version $Revision$ $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 JavaDoc 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 JavaDoc outMsg = "outer message";
53         String JavaDoc inMsg = "inner message";
54         Exception JavaDoc cause = new Exception JavaDoc(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 JavaDoc inMsg = "inner message";
65         Exception JavaDoc cause = new Exception JavaDoc(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 JavaDoc outMsg = "outer message";
75         String JavaDoc inMsg = "inner message";
76         MathException cause = new MathConfigurationException(inMsg);
77         MathException ex = new MathException(outMsg, cause);
78         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
79         PrintStream JavaDoc ps = new PrintStream JavaDoc(baos);
80         ex.printStackTrace(ps);
81         String JavaDoc stack = baos.toString();
82         String JavaDoc outerMsg = "org.apache.commons.math.MathException: outer message";
83         String JavaDoc 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 JavaDoc pw = new PrintWriter JavaDoc(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 JavaDoc outMsg = "outer message";
100         String JavaDoc 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 JavaDoc baos = new ByteArrayOutputStream JavaDoc();
106         PrintStream JavaDoc ps = new PrintStream JavaDoc(baos);
107         PrintWriter JavaDoc pw = new PrintWriter JavaDoc(ps, true);
108         ex.printStackTrace(ps);
109         String JavaDoc stack = baos.toString();
110         
111         ByteArrayOutputStream JavaDoc baos2 = new ByteArrayOutputStream JavaDoc();
112         PrintStream JavaDoc ps2 = new PrintStream JavaDoc(baos2);
113         PrintWriter JavaDoc pw2 = new PrintWriter JavaDoc(ps2, true);
114         image.printStackTrace(ps2);
115         String JavaDoc 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 JavaDoc.class.getDeclaredMethod("getCause", new Class JavaDoc[0]);
122             jdkSupportsNesting = true;
123         } catch (NoSuchMethodException JavaDoc 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 }
135
Popular Tags