package com.fasterxml.jackson.dataformat.xml.misc;

import java.io.*;
import java.util.*;

import com.fasterxml.jackson.annotation.JsonRootName;
import com.fasterxml.jackson.dataformat.xml.*;
import com.fasterxml.jackson.dataformat.xml.testutil.PrefixInputDecorator;
import com.fasterxml.jackson.dataformat.xml.testutil.PrefixOutputDecorator;

public class StreamingDecoratorsTest extends XmlTestBase
{
    @JsonRootName("wrapper")
    static class Value {
        public String value = "all";
    }
    
    @SuppressWarnings("unchecked")
    public void testInputDecorators() throws IOException
    {
        final byte[] DOC = utf8Bytes("<secret: mum\n");
        final XmlMapper mapper = mapperBuilder(
                streamFactoryBuilder().inputDecorator(new PrefixInputDecorator(DOC))
                .build())
                .build();
        Map<String,Object> value = mapper.readValue(utf8Bytes("value: foo\n"), Map.class);
        assertEquals(2, value.size());
        assertEquals("foo", value.get("value"));
        assertEquals("mum", value.get("secret"));

        // and then via Reader as well
        value = mapper.readValue(new StringReader("value: xyz\n"), Map.class);
        assertEquals(2, value.size());
        assertEquals("xyz", value.get("value"));
        assertEquals("mum", value.get("secret"));
    }

    public void testOutputDecorators() throws IOException
    {
        final String PREFIX = "///////";
        final byte[] DOC = utf8Bytes(PREFIX);
        final XmlMapper mapper = mapperBuilder(
                streamFactoryBuilder().outputDecorator(new PrefixOutputDecorator(DOC))
                .build())
                .build();
        final Value input = new Value();

        // Gets bit tricky because writer will add doc prefix. So let's do simpler check here

        try (ByteArrayOutputStream bytes = new ByteArrayOutputStream()) {
            mapper.writeValue(bytes, input);
    
            String raw = bytes.toString("UTF-8");
            if (!raw.startsWith(PREFIX)) {
                fail("Should start with prefix, did not: ["+raw+"]");
            }
        }

        // and same with char-backed too
        try (StringWriter sw = new StringWriter()) {
            mapper.writeValue(sw, input);
            String raw = sw.toString();
            if (!raw.startsWith(PREFIX)) {
                fail("Should start with prefix, did not: ["+raw+"]");
            }
        }
    }
}

@@ -497,47 +497,8 @@
   %7D%0A
-    %0A    @SuppressWarnings(%22unchecked%22)
 %0A   
@@ -600,19 +600,16 @@
 s(%22%3C
-secret: mum
+wrapper%3E
 %5Cn%22)
@@ -807,26 +807,13 @@
     
-Map%3CString,Object%3E
+Value
  val
@@ -849,121 +849,52 @@
 es(%22
+%3C
 value
-: foo%5Cn%22), Map.class);%0A        assertEquals(2, value.size());%0A        assertEquals(%22foo%22, v
+%3Etest%3C/value%3E%3C/wrapper%3E%22), V
 alue.
-get(%22value%22)
+class
 );%0A 
@@ -906,35 +906,36 @@
   assertEquals(%22
-mum
+test
 %22, value.get(%22se
@@ -927,29 +927,21 @@
 , value.
-get(%22secret%22)
+value
 );%0A%0A    
@@ -1030,121 +1030,53 @@
 er(%22
+%3C
 value
-: xyz%5Cn%22), Map.class);%0A        assertEquals(2, value.size());%0A        assertEquals(%22xyz%22, v
+%3Etest2%3C/value%3E%3C/wrapper%3E%22), V
 alue.
-get(%22value%22)
+class
 );%0A 
@@ -1100,11 +1100,13 @@
 ls(%22
-mum
+test2
 %22, v
@@ -1114,21 +1114,13 @@
 lue.
-get(%22secret%22)
+value
 );%0A 

Fix streaming decorator test