001: /*
002: * Copyright (C) 2004, 2005 Joe Walnes.
003: * Copyright (C) 2006, 2007 XStream Committers.
004: * All rights reserved.
005: *
006: * The software in this package is published under the terms of the BSD
007: * style license a copy of which has been included with this distribution in
008: * the LICENSE.txt file.
009: *
010: * Created on 29. May 2004 by Joe Walnes
011: */
012: package com.thoughtworks.xstream.converters.extended;
013:
014: import com.thoughtworks.acceptance.AbstractAcceptanceTest;
015: import com.thoughtworks.xstream.XStream;
016:
017: import java.math.BigDecimal;
018:
019: /**
020: * @author <a href="mailto:boxley@thoughtworks.com">B. K. Oxley (binkley)</a>
021: */
022: public class ThrowableConverterTest extends AbstractAcceptanceTest {
023:
024: public void testDeserializesThrowable() {
025: Throwable expected = new Throwable();
026: Throwable result = (Throwable) xstream.fromXML(xstream
027: .toXML(expected));
028: assertThrowableEquals(expected, result);
029: }
030:
031: public void testDeserializesException() {
032: Exception expected = new Exception();
033: Throwable result = (Throwable) xstream.fromXML(xstream
034: .toXML(expected));
035: assertThrowableEquals(expected, result);
036: }
037:
038: public void testIncludesMessage() {
039: Throwable expected = new Throwable("A MESSAGE");
040: Throwable result = (Throwable) xstream.fromXML(xstream
041: .toXML(expected));
042: assertThrowableEquals(expected, result);
043: }
044:
045: public void testIncludesCause() {
046: Throwable expected = new Throwable(new Throwable());
047: Throwable result = (Throwable) xstream.fromXML(xstream
048: .toXML(expected));
049: assertThrowableEquals(expected, result);
050: }
051:
052: public void testIncludesCauseAndMessage() {
053: Throwable expected = new Throwable("MESSAGE", new Throwable(
054: "CAUSE MESSAGE"));
055: Throwable result = (Throwable) xstream.fromXML(xstream
056: .toXML(expected));
057: assertThrowableEquals(expected, result);
058: }
059:
060: public void testIncludesStackTrace() {
061: try {
062: throw new Exception();
063: } catch (Exception exception) {
064: Throwable result = (Throwable) xstream.fromXML(xstream
065: .toXML(exception));
066: assertThrowableEquals(exception, result);
067: }
068: }
069:
070: public static class MyException extends Exception {
071: private BigDecimal number;
072:
073: public MyException(String msg, BigDecimal number) {
074: super (msg);
075: this .number = number;
076: }
077:
078: public boolean equals(Object o) {
079: return super .equals(o) && o instanceof MyException
080: && number.equals(((MyException) o).number);
081: }
082:
083: }
084:
085: public void testSerializesExtraFields() {
086: try {
087: throw new MyException("A MESSAGE", new BigDecimal(123.4));
088: } catch (MyException exception) {
089: Throwable result = (Throwable) xstream.fromXML(xstream
090: .toXML(exception));
091: assertThrowableEquals(exception, result);
092: }
093: }
094:
095: public void testSerializesWithNoSelfReferenceForUninitializedCauseInJdk14() {
096: xstream.setMode(XStream.NO_REFERENCES);
097: try {
098: throw new RuntimeException("Without cause");
099: } catch (RuntimeException exception) {
100: Throwable result = (Throwable) xstream.fromXML(xstream
101: .toXML(exception));
102: assertThrowableEquals(exception, result);
103: assertNull(exception.getCause());
104: assertNull(result.getCause());
105: }
106: }
107:
108: public void testSerializesWithInitializedCauseInJdk14() {
109: xstream.setMode(XStream.NO_REFERENCES);
110: try {
111: throw new RuntimeException("Without cause", null);
112: } catch (RuntimeException exception) {
113: Throwable result = (Throwable) xstream.fromXML(xstream
114: .toXML(exception));
115: assertThrowableEquals(exception, result);
116: assertNull(exception.getCause());
117: assertNull(result.getCause());
118: }
119: }
120:
121: private static void assertThrowableEquals(final Throwable a,
122: final Throwable b) {
123: assertBoth(a, b, new MoreAssertions() {
124: public void assertMoreSafely(final Object a, final Object b) {
125: final Throwable ta = (Throwable) a, tb = (Throwable) b;
126: assertEquals(ta.getClass(), tb.getClass());
127: assertEquals(ta.getMessage(), tb.getMessage());
128: assertThrowableEquals(ta.getCause(), tb.getCause());
129: assertArrayEquals(ta.getStackTrace(), tb
130: .getStackTrace());
131: }
132: });
133: }
134:
135: private static void assertArrayEquals(final Object[] expected,
136: final Object[] actual) {
137: StringBuffer expectedJoined = new StringBuffer();
138: StringBuffer actualJoined = new StringBuffer();
139: for (int i = 0; i < expected.length; i++) {
140: expectedJoined.append(expected[i]).append('\n');
141: }
142: for (int i = 0; i < actual.length; i++) {
143: // JRockit adds ":???" for invalid line number
144: actualJoined
145: .append(
146: actual[i].toString().replaceFirst(
147: ":\\?\\?\\?", "")).append('\n');
148: }
149: assertEquals(expectedJoined.toString(), actualJoined.toString());
150: }
151:
152: private static void assertBoth(Object a, Object b,
153: MoreAssertions moreAssertions) {
154: if (null == a) {
155: if (null == b) {
156: return;
157: } else {
158: fail("Expected null, but was <" + b + ">");
159: }
160: } else if (null == b) {
161: fail("Expected <" + a + "> but was null");
162: } else {
163: moreAssertions.assertMoreSafely(a, b);
164: }
165: }
166:
167: private interface MoreAssertions {
168: void assertMoreSafely(final Object a, final Object b);
169: }
170:
171: }
|