1 package com.thoughtworks.xstream.converters.extended; 2 3 import com.thoughtworks.acceptance.AbstractAcceptanceTest; 4 import junit.framework.AssertionFailedError; 5 6 import java.math.BigDecimal ; 7 8 11 public class ThrowableConverterTest extends AbstractAcceptanceTest { 12 13 public void testDeserializesThrowable() { 14 Throwable expected = new Throwable (); 15 Throwable result = (Throwable ) xstream.fromXML(xstream.toXML(expected)); 16 assertThrowableEquals(expected, result); 17 } 18 19 public void testDeserializesException() { 20 Exception expected = new Exception (); 21 Throwable result = (Throwable ) xstream.fromXML(xstream.toXML(expected)); 22 assertThrowableEquals(expected, result); 23 } 24 25 public void testIncludesMessage() { 26 Throwable expected = new Throwable ("A MESSAGE"); 27 Throwable result = (Throwable ) xstream.fromXML(xstream.toXML(expected)); 28 assertThrowableEquals(expected, result); 29 } 30 31 public void testIncludesCause() { 32 Throwable expected = new Throwable (new Throwable ()); 33 Throwable result = (Throwable ) xstream.fromXML(xstream.toXML(expected)); 34 assertThrowableEquals(expected, result); 35 } 36 37 public void testIncludesCauseAndMessage() { 38 Throwable expected = new Throwable ("MESSAGE", new Throwable ("CAUSE MESSAGE")); 39 Throwable result = (Throwable ) xstream.fromXML(xstream.toXML(expected)); 40 assertThrowableEquals(expected, result); 41 } 42 43 public void testIncludesStackTrace() { 44 try { 45 throw new Exception (); 46 } catch (Exception exception) { 47 Throwable result = (Throwable ) xstream.fromXML(xstream.toXML(exception)); 48 assertThrowableEquals(exception, result); 49 } 50 } 51 52 public static class MyException extends Exception { 53 private BigDecimal number; 54 55 public MyException(String msg, BigDecimal number) { 56 super(msg); 57 this.number = number; 58 } 59 60 public boolean equals(Object o) { 61 return super.equals(o) && o instanceof MyException && number.equals(((MyException)o).number); 62 } 63 64 } 65 66 public void testSerializesExtraFields() { 67 try { 68 throw new MyException("A MESSAGE", new BigDecimal (123.4)); 69 } catch (MyException exception) { 70 Throwable result = (Throwable ) xstream.fromXML(xstream.toXML(exception)); 71 assertThrowableEquals(exception, result); 72 } 73 } 74 75 private static void assertThrowableEquals(final Throwable a, 76 final Throwable b) { 77 assertBoth(a, b, new MoreAssertions() { 78 public void assertMoreSafely(final Object a, 79 final Object b) { 80 final Throwable ta = (Throwable ) a, tb = (Throwable ) b; 81 assertEquals(ta.getClass(), tb.getClass()); 82 assertEquals(ta.getMessage(), tb.getMessage()); 83 assertThrowableEquals(ta.getCause(), tb.getCause()); 84 assertArrayEquals(ta.getStackTrace(), tb.getStackTrace()); 85 } 86 }); 87 } 88 89 private static void assertArrayEquals(final Object [] expected, final Object [] actual) { 90 StringBuffer expectedJoined = new StringBuffer (); 91 StringBuffer actualJoined = new StringBuffer (); 92 for (int i = 0; i < expected.length; i++) { 93 expectedJoined.append(expected[i]).append('\n'); 94 } 95 for (int i = 0; i < actual.length; i++) { 96 actualJoined.append(actual[i]).append('\n'); 97 } 98 assertEquals(expectedJoined.toString(), actualJoined.toString()); 99 } 100 101 private static void assertBoth(Object a, Object b, MoreAssertions moreAssertions) { 102 if (null == a) { 103 if (null == b) { 104 return; 105 } else { 106 fail("Expected null, but was <" + b + ">"); 107 } 108 } else if (null == b) { 109 fail("Expected <" + a + "> but was null"); 110 } else { 111 moreAssertions.assertMoreSafely(a, b); 112 } 113 } 114 115 private interface MoreAssertions { 116 void assertMoreSafely(final Object a, final Object b); 117 } 118 119 } 120 | Popular Tags |