/*
 * Copyright (C) 2004, 2005 Joe Walnes.
 * Copyright (C) 2006, 2007, 2009 XStream Committers.
 * All rights reserved.
 *
 * The software in this package is published under the terms of the BSD
 * style license a copy of which has been included with this distribution in
 * the LICENSE.txt file.
 * 
 * Created on 08. May 2004 by Joe Walnes
 */
package com.thoughtworks.acceptance;

import com.thoughtworks.xstream.converters.ConversionException;
import com.thoughtworks.xstream.core.JVM;
import com.thoughtworks.xstream.io.StreamException;

public class ErrorTest extends AbstractAcceptanceTest {

    public static class Thing {
        String one;
        int two;
    }

    protected void setUp() throws Exception {
        super.setUp();
        xstream.alias("thing", Thing.class);
    }

    public void testUnmarshallerThrowsExceptionWithDebuggingInfo() {
        try {
            xstream.fromXML("<thing>\n" +
                    "  <one>string 1</one>\n" +
                    "  <two>another string</two>\n" +
                    "</thing>");
            fail("Error expected");
        } catch (ConversionException e) {
            assertEquals("java.lang.NumberFormatException",
                    e.get("cause-exception"));
            if (JVM.is14()) {
                assertEquals("For input string: \"another string\"",
                        e.get("cause-message"));
            } else {
                assertEquals("another string",
                        e.get("cause-message"));
            }
            assertEquals(Thing.class.getName(),
                    e.get("class"));
            assertEquals("/thing/two",
                    e.get("path"));
            assertEquals("3",
                    e.get("line number"));
            assertEquals("java.lang.Integer",
                    e.get("required-type"));
        }
    }

    public void testInvalidXml() {
        try {
            xstream.fromXML("<thing>\n" +
                    "  <one>string 1</one>\n" +
                    "  <two><<\n" +
                    "</thing>");
            fail("Error expected");
        } catch (ConversionException e) {
            assertEquals(StreamException.class.getName(),
                    e.get("cause-exception"));
            assertContains("unexpected character in markup",
                    e.get("cause-message"));
            assertEquals("/thing/two",
                    e.get("path"));
            assertEquals("3",
                    e.get("line number"));
        }

    }
    
    public void testNonExistingMember() {
        try {
            xstream.fromXML("<thing>\n" +
                    "  <one>string 1</one>\n" +
                    "  <three>3</three>\n" +
                    "</thing>");
            fail("Error expected");
        } catch (ConversionException e) {
            assertEquals("three",
                    e.get("field"));
            assertEquals("/thing/three",
                    e.get("path"));
            assertEquals("3",
                    e.get("line number"));
        }
        
    }

    private void assertContains(String expected, String actual) {
        assertTrue("Substring not found. Expected <" + expected + "> but got <" + actual + ">",
                actual.indexOf(expected) > -1);
    }
}

@@ -894,32 +894,53 @@
 xstream.fromXML(
+%22%22%0A                + 
 %22%3Cthing%3E%5Cn%22 +%0A  
@@ -927,33 +927,32 @@
   + %22%3Cthing%3E%5Cn%22 
-+
 %0A               
@@ -944,35 +944,33 @@
                 
-   
++
  %22  %3Cone%3Estring 
@@ -972,33 +972,32 @@
 ring 1%3C/one%3E%5Cn%22 
-+
 %0A               
@@ -989,35 +989,33 @@
                 
-   
++
  %22  %3Ctwo%3Eanother
@@ -1027,25 +1027,24 @@
 ng%3C/two%3E%5Cn%22 
-+
 %0A           
@@ -1040,35 +1040,33 @@
                 
-   
++
  %22%3C/thing%3E%22);%0A  
@@ -1937,32 +1937,53 @@
 xstream.fromXML(
+%22%22%0A                + 
 %22%3Cthing%3E%5Cn%22 +%0A  
@@ -1970,33 +1970,32 @@
   + %22%3Cthing%3E%5Cn%22 
-+
 %0A               
@@ -1987,35 +1987,33 @@
                 
-   
++
  %22  %3Cone%3Estring 
@@ -2015,33 +2015,32 @@
 ring 1%3C/one%3E%5Cn%22 
-+
 %0A               
@@ -2036,27 +2036,25 @@
             
-   
++
  %22  %3Ctwo%3E%3C%3C%5C
@@ -2052,25 +2052,24 @@
  %3Ctwo%3E%3C%3C%5Cn%22 
-+
 %0A           
@@ -2065,35 +2065,33 @@
                 
-   
++
  %22%3C/thing%3E%22);%0A  
@@ -2631,16 +2631,38 @@
 fromXML(
+%22%22 %0A                + 
 %22%3Cthing%3E
@@ -2661,25 +2661,24 @@
 %22%3Cthing%3E%5Cn%22 
-+
 %0A           
@@ -2678,27 +2678,25 @@
             
-   
++
  %22  %3Cone%3Estr
@@ -2706,25 +2706,24 @@
  1%3C/one%3E%5Cn%22 
-+
 %0A           
@@ -2727,19 +2727,17 @@
         
-   
++
  %22  %3Cthr
@@ -2752,17 +2752,16 @@
 ree%3E%5Cn%22 
-+
 %0A       
@@ -2769,19 +2769,17 @@
         
-   
++
  %22%3C/thin

Formatted strings.