KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtworks > xstream > converters > extended > ThrowableConverterTest


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 JavaDoc;
7
8 /**
9  * @author <a HREF="mailto:boxley@thoughtworks.com">B. K. Oxley (binkley)</a>
10  */

11 public class ThrowableConverterTest extends AbstractAcceptanceTest {
12
13     public void testDeserializesThrowable() {
14         Throwable JavaDoc expected = new Throwable JavaDoc();
15         Throwable JavaDoc result = (Throwable JavaDoc) xstream.fromXML(xstream.toXML(expected));
16         assertThrowableEquals(expected, result);
17     }
18
19     public void testDeserializesException() {
20         Exception JavaDoc expected = new Exception JavaDoc();
21         Throwable JavaDoc result = (Throwable JavaDoc) xstream.fromXML(xstream.toXML(expected));
22         assertThrowableEquals(expected, result);
23     }
24
25     public void testIncludesMessage() {
26         Throwable JavaDoc expected = new Throwable JavaDoc("A MESSAGE");
27         Throwable JavaDoc result = (Throwable JavaDoc) xstream.fromXML(xstream.toXML(expected));
28         assertThrowableEquals(expected, result);
29     }
30
31     public void testIncludesCause() {
32         Throwable JavaDoc expected = new Throwable JavaDoc(new Throwable JavaDoc());
33         Throwable JavaDoc result = (Throwable JavaDoc) xstream.fromXML(xstream.toXML(expected));
34         assertThrowableEquals(expected, result);
35     }
36
37     public void testIncludesCauseAndMessage() {
38         Throwable JavaDoc expected = new Throwable JavaDoc("MESSAGE", new Throwable JavaDoc("CAUSE MESSAGE"));
39         Throwable JavaDoc result = (Throwable JavaDoc) xstream.fromXML(xstream.toXML(expected));
40         assertThrowableEquals(expected, result);
41     }
42
43     public void testIncludesStackTrace() {
44         try {
45             throw new Exception JavaDoc();
46         } catch (Exception JavaDoc exception) {
47             Throwable JavaDoc result = (Throwable JavaDoc) xstream.fromXML(xstream.toXML(exception));
48             assertThrowableEquals(exception, result);
49         }
50     }
51
52     public static class MyException extends Exception JavaDoc {
53         private BigDecimal JavaDoc number;
54
55         public MyException(String JavaDoc msg, BigDecimal JavaDoc number) {
56             super(msg);
57             this.number = number;
58         }
59
60         public boolean equals(Object JavaDoc 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 JavaDoc(123.4));
69         } catch (MyException exception) {
70             Throwable JavaDoc result = (Throwable JavaDoc) xstream.fromXML(xstream.toXML(exception));
71             assertThrowableEquals(exception, result);
72         }
73     }
74
75     private static void assertThrowableEquals(final Throwable JavaDoc a,
76                                               final Throwable JavaDoc b) {
77         assertBoth(a, b, new MoreAssertions() {
78             public void assertMoreSafely(final Object JavaDoc a,
79                                          final Object JavaDoc b) {
80                 final Throwable JavaDoc ta = (Throwable JavaDoc) a, tb = (Throwable JavaDoc) 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 JavaDoc[] expected, final Object JavaDoc[] actual) {
90         StringBuffer JavaDoc expectedJoined = new StringBuffer JavaDoc();
91         StringBuffer JavaDoc actualJoined = new StringBuffer JavaDoc();
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 JavaDoc a, Object JavaDoc 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 JavaDoc a, final Object JavaDoc b);
117     }
118
119 }
120
Popular Tags