01: /*
02: * Copyright (C) 2004, 2005 Joe Walnes.
03: * Copyright (C) 2006, 2007 XStream Committers.
04: * All rights reserved.
05: *
06: * The software in this package is published under the terms of the BSD
07: * style license a copy of which has been included with this distribution in
08: * the LICENSE.txt file.
09: *
10: * Created on 08. May 2004 by Joe Walnes
11: */
12: package com.thoughtworks.acceptance;
13:
14: import com.thoughtworks.xstream.converters.ConversionException;
15: import com.thoughtworks.xstream.core.JVM;
16: import com.thoughtworks.xstream.io.StreamException;
17:
18: public class ErrorTest extends AbstractAcceptanceTest {
19:
20: public static class Thing {
21: String one;
22: int two;
23: }
24:
25: protected void setUp() throws Exception {
26: super .setUp();
27: xstream.alias("thing", Thing.class);
28: }
29:
30: public void testUnmarshallerThrowsExceptionWithDebuggingInfo() {
31: try {
32: xstream.fromXML("<thing>\n" + " <one>string 1</one>\n"
33: + " <two>another string</two>\n" + "</thing>");
34: fail("Error expected");
35: } catch (ConversionException e) {
36: assertEquals("java.lang.NumberFormatException", e
37: .get("cause-exception"));
38: if (JVM.is14()) {
39: assertEquals("For input string: \"another string\"", e
40: .get("cause-message"));
41: } else {
42: assertEquals("another string", e.get("cause-message"));
43: }
44: assertEquals(Thing.class.getName(), e.get("class"));
45: assertEquals("/thing/two", e.get("path"));
46: assertEquals("3", e.get("line number"));
47: assertEquals("java.lang.Integer", e.get("required-type"));
48: }
49: }
50:
51: public void testInvalidXml() {
52: try {
53: xstream.fromXML("<thing>\n" + " <one>string 1</one>\n"
54: + " <two><<\n" + "</thing>");
55: fail("Error expected");
56: } catch (ConversionException e) {
57: assertEquals(StreamException.class.getName(), e
58: .get("cause-exception"));
59: assertContains("unexpected character in markup", e
60: .get("cause-message"));
61: assertEquals("/thing/two", e.get("path"));
62: assertEquals("3", e.get("line number"));
63: }
64:
65: }
66:
67: private void assertContains(String expected, String actual) {
68: assertTrue("Substring not found. Expected <" + expected
69: + "> but got <" + actual + ">", actual
70: .indexOf(expected) > -1);
71: }
72: }
|