KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtworks > acceptance > ErrorTest


1 package com.thoughtworks.acceptance;
2
3 import com.thoughtworks.xstream.converters.ConversionException;
4 import com.thoughtworks.xstream.io.StreamException;
5
6 public class ErrorTest extends AbstractAcceptanceTest {
7
8     public static class Thing {
9         String JavaDoc one;
10         int two;
11     }
12
13     protected void setUp() throws Exception JavaDoc {
14         super.setUp();
15         xstream.alias("thing", Thing.class);
16     }
17
18     public void testUnmarshallerThrowsExceptionWithDebuggingInfo() {
19         try {
20             xstream.fromXML("<thing>\n" +
21                     " <one>string 1</one>\n" +
22                     " <two>another string</two>\n" +
23                     "</thing>");
24             fail("Error expected");
25         } catch (ConversionException e) {
26             assertEquals("java.lang.NumberFormatException",
27                     e.get("cause-exception"));
28             assertEquals("For input string: \"another string\"",
29                     e.get("cause-message"));
30             assertEquals(Thing.class.getName(),
31                     e.get("class"));
32             assertEquals("/thing/two",
33                     e.get("path"));
34             assertEquals("3",
35                     e.get("line number"));
36             assertEquals("java.lang.Integer",
37                     e.get("required-type"));
38         }
39     }
40
41     public void testInvalidXml() {
42         try {
43             xstream.fromXML("<thing>\n" +
44                     " <one>string 1</one>\n" +
45                     " <two><<\n" +
46                     "</thing>");
47             fail("Error expected");
48         } catch (ConversionException e) {
49             assertEquals(StreamException.class.getName(),
50                     e.get("cause-exception"));
51             assertContains("unexpected character in markup",
52                     e.get("cause-message"));
53             assertEquals("/thing/two",
54                     e.get("path"));
55             assertEquals("3",
56                     e.get("line number"));
57         }
58
59     }
60
61     private void assertContains(String JavaDoc expected, String JavaDoc actual) {
62         assertTrue("Substring not found. Expected <" + expected + "> but got <" + actual + ">",
63                 actual.indexOf(expected) > -1);
64     }
65 }
66
Popular Tags