package stax2.wstream;

import java.io.*;

import javax.xml.stream.*;

import org.codehaus.stax2.*;

/**
 * This test checks to see that text/attribute value escaping is
 * working properly.
 */
public class TestEscaping
    extends BaseWriterTest
{
    /**
     * This test checks that even though it's 'wrong' to use non-URL/URI
     * namespace URIs, it's not a fatal error; and that the 'uri' value
     * should come back as it was written out.
     */
    public void testBrokenNsURLs()
        throws XMLStreamException
    {
        final String BROKEN_URL1 = "<tag>";
        final String BROKEN_URL2 = "\"";
        final String BROKEN_URL3 = "x&";

        StringWriter strw = new StringWriter();
        XMLStreamWriter2 w = getNonRepairingWriter(strw, true);
            
        w.writeStartDocument();
        w.writeStartElement("", "test", "");
        w.writeNamespace("ns", BROKEN_URL1);
        w.writeStartElement("", "test", "");
        w.writeNamespace("ns", BROKEN_URL2);
        w.writeStartElement("", "test", "");
        w.writeNamespace("ns", BROKEN_URL3);
        
        w.writeEndElement();
        w.writeEndElement();
        w.writeEndElement();
        
        w.writeEndDocument();
        w.close();
        
        // And then let's parse and verify it all:

        String input = strw.toString();

        XMLStreamReader sr = constructNsStreamReader(input, true);
        assertTokenType(START_DOCUMENT, sr.getEventType());
        
        assertTokenType(START_ELEMENT, sr.next());
        assertEquals("test", sr.getLocalName());
        assertEquals(1, sr.getNamespaceCount());
        assertEquals("ns", sr.getNamespacePrefix(0));
        assertEquals(BROKEN_URL1, sr.getNamespaceURI(0));
        
        assertTokenType(START_ELEMENT, sr.next());
        assertEquals("test", sr.getLocalName());
        assertEquals(1, sr.getNamespaceCount());
        assertEquals("ns", sr.getNamespacePrefix(0));
        assertEquals(BROKEN_URL2, sr.getNamespaceURI(0));

        assertTokenType(START_ELEMENT, sr.next());
        assertEquals("test", sr.getLocalName());
        assertEquals(1, sr.getNamespaceCount());
        assertEquals("ns", sr.getNamespacePrefix(0));
        assertEquals(BROKEN_URL3, sr.getNamespaceURI(0));
        
        assertTokenType(END_ELEMENT, sr.next());
        assertTokenType(END_ELEMENT, sr.next());
        assertTokenType(END_ELEMENT, sr.next());
        
        assertTokenType(END_DOCUMENT, sr.next());
        
        sr.close();
    }

    public void testLatin1Quoting()
        throws XMLStreamException
    {
        final String TEXT = "ab\u00A0cd\tef\u00D8gh\u3c00...";

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        XMLStreamWriter2 w = getNonRepairingWriter(bos, "ISO-8859-1", true);

        w.writeStartDocument();
        w.writeStartElement("root");
        w.writeCharacters(TEXT);
        w.writeEndElement();
        w.writeEndDocument();
        w.close();

        InputStream in = new ByteArrayInputStream(bos.toByteArray());
        XMLStreamReader sr = constructNsStreamReader(in, true);
        assertTokenType(START_DOCUMENT, sr.getEventType());
        assertTokenType(START_ELEMENT, sr.next());
        assertEquals("root", sr.getLocalName());
        assertTokenType(CHARACTERS, sr.next());

        assertEquals(TEXT, sr.getText());

        assertTokenType(END_ELEMENT, sr.next());
    }

    public void testAsciiQuoting()
        throws XMLStreamException
    {
        final String TEXT = "ab\u00A0cd\tef\u00D8gh\u3c00...";

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        XMLStreamWriter2 w = getNonRepairingWriter(bos, "US-ASCII", true);

        w.writeStartDocument();
        w.writeStartElement("root");
        w.writeCharacters(TEXT);
        w.writeEndElement();
        w.writeEndDocument();
        w.close();

        InputStream in = new ByteArrayInputStream(bos.toByteArray());
        XMLStreamReader sr = constructNsStreamReader(in, true);
        assertTokenType(START_DOCUMENT, sr.getEventType());
        assertTokenType(START_ELEMENT, sr.next());
        assertEquals("root", sr.getLocalName());
        assertTokenType(CHARACTERS, sr.next());

        assertEquals(TEXT, sr.getText());

        assertTokenType(END_ELEMENT, sr.next());
    }
}

@@ -4323,11 +4323,472 @@
 );%0A    %7D
+%0A%0A    public void testLinefeedQuoting() throws Exception%0A    %7B%0A        ByteArrayOutputStream bos = new ByteArrayOutputStream();%0A        XMLStreamWriter2 w = getNonRepairingWriter(bos, %22US-ASCII%22, true);%0A%0A        w.writeStartElement(%22root%22);%0A        w.writeCharacters(%22a%5Cnb%5Crc%22);%0A        w.writeEndElement();%0A        w.writeEndDocument();%0A        w.close();%0A%0A        String xml = bos.toString(%22UTF-8%22);%0A        assertEquals(%22%3Croot%3Ea%5Cnb&#xd;c%3C/root%3E%22, xml);%0A    %7D
 %0A%7D%0A

add minor test wrt writing/escaping of lf/cr