/**
* Test serialization
*/
public void testSerialization() {
Localizable outMsg = new DummyLocalizable("outer message");
Localizable inMsg = new DummyLocalizable("inner message");
MathException cause = new MathConfigurationException(inMsg);
MathException ex = new MathException(cause, outMsg);
MathException image = (MathException) TestUtils.serializeAndRecover(ex);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos);
ex.printStackTrace(ps);
String stack = baos.toString();
ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
PrintStream ps2 = new PrintStream(baos2);
image.printStackTrace(ps2);
String stack2 = baos2.toString();
// See if JDK supports nested exceptions. If not, stack trace of
// inner exception will not be serialized
boolean jdkSupportsNesting = false;
try {
Throwable.class.getDeclaredMethod("getCause", new Class[0]);
jdkSupportsNesting = true;
} catch (NoSuchMethodException e) {
jdkSupportsNesting = false;
}
if (jdkSupportsNesting) {
assertEquals(stack, stack2);
} else {
assertTrue(stack2.indexOf(inMsg.getSourceString()) != -1);
assertTrue(stack2.indexOf("MathConfigurationException") != -1);
}
}