001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018 package org.apache.commons.math; 019 020 import junit.framework.TestCase; 021 022 import java.io.ByteArrayOutputStream; 023 import java.io.PrintStream; 024 import java.io.PrintWriter; 025 import java.util.Locale; 026 027 /** 028 * @version $Revision: 746578 $ $Date: 2009-02-21 15:01:14 -0500 (Sat, 21 Feb 2009) $ 029 */ 030 public class MathExceptionTest extends TestCase { 031 032 public void testConstructor(){ 033 MathException ex = new MathException(); 034 assertNull(ex.getCause()); 035 assertNull(ex.getMessage()); 036 assertEquals(0, ex.getMessage(Locale.FRENCH).length()); 037 } 038 039 public void testConstructorPatternArguments(){ 040 String pattern = "a {0}x{1} matrix cannot be a rotation matrix"; 041 Object[] arguments = { Integer.valueOf(6), Integer.valueOf(4) }; 042 MathException ex = new MathException(pattern, arguments); 043 assertNull(ex.getCause()); 044 assertEquals(pattern, ex.getPattern()); 045 assertEquals(arguments.length, ex.getArguments().length); 046 for (int i = 0; i < arguments.length; ++i) { 047 assertEquals(arguments[i], ex.getArguments()[i]); 048 } 049 assertFalse(pattern.equals(ex.getMessage())); 050 assertFalse(ex.getMessage().equals(ex.getMessage(Locale.FRENCH))); 051 } 052 053 public void testConstructorCause(){ 054 String inMsg = "inner message"; 055 Exception cause = new Exception(inMsg); 056 MathException ex = new MathException(cause); 057 assertEquals(cause, ex.getCause()); 058 } 059 060 public void testConstructorPatternArgumentsCause(){ 061 String pattern = "a {0}x{1} matrix cannot be a rotation matrix"; 062 Object[] arguments = { Integer.valueOf(6), Integer.valueOf(4) }; 063 String inMsg = "inner message"; 064 Exception cause = new Exception(inMsg); 065 MathException ex = new MathException(cause, pattern, arguments); 066 assertEquals(cause, ex.getCause()); 067 assertEquals(pattern, ex.getPattern()); 068 assertEquals(arguments.length, ex.getArguments().length); 069 for (int i = 0; i < arguments.length; ++i) { 070 assertEquals(arguments[i], ex.getArguments()[i]); 071 } 072 assertFalse(pattern.equals(ex.getMessage())); 073 assertFalse(ex.getMessage().equals(ex.getMessage(Locale.FRENCH))); 074 } 075 076 /** 077 * Tests the printStackTrace() operation. 078 */ 079 public void testPrintStackTrace() { 080 String outMsg = "outer message"; 081 String inMsg = "inner message"; 082 MathException cause = new MathConfigurationException(inMsg); 083 MathException ex = new MathException(cause, outMsg); 084 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 085 PrintStream ps = new PrintStream(baos); 086 ex.printStackTrace(ps); 087 String stack = baos.toString(); 088 String outerMsg = "org.apache.commons.math.MathException: outer message"; 089 String innerMsg = "Caused by: " + 090 "org.apache.commons.math.MathConfigurationException: inner message"; 091 assertTrue(stack.startsWith(outerMsg)); 092 assertTrue(stack.indexOf(innerMsg) > 0); 093 094 PrintWriter pw = new PrintWriter(ps, true); 095 ex.printStackTrace(pw); 096 stack = baos.toString(); 097 assertTrue(stack.startsWith(outerMsg)); 098 assertTrue(stack.indexOf(innerMsg) > 0); 099 } 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 }