package com.helger.xml.serialize.write;

import static org.junit.Assert.assertEquals;

import javax.xml.stream.XMLStreamException;

import org.junit.Test;

import com.helger.commons.io.stream.NonBlockingStringWriter;

/**
 * Test class for class {@link SafeXMLStreamWriter}
 *
 * @author Philip Helger
 */
public class SafeXMLStreamWriterTest
{
  @Test
  public void testBasic () throws XMLStreamException
  {
    final NonBlockingStringWriter aSW = new NonBlockingStringWriter ();
    try (final SafeXMLStreamWriter aXSW = SafeXMLStreamWriter.create (aSW, new XMLWriterSettings ()))
    {
      aXSW.writeStartDocument ();
      aXSW.writeStartElement ("Root");
      aXSW.writeAttribute ("Version", "2.0");
      aXSW.writeStartElement ("Child");
      aXSW.writeCharacters ("Hello World");
      aXSW.writeEndElement ();
      aXSW.writeEndElement ();
      aXSW.writeEndDocument ();
      aXSW.flush ();
    }
    assertEquals ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" +
                  "<Root Version=\"2.0\"><Child>Hello World</Child></Root>",
                  aSW.getAsString ());
  }

  @Test
  public void testWithNS () throws XMLStreamException
  {
    final NonBlockingStringWriter aSW = new NonBlockingStringWriter ();
    final String sNSURI = "urn:example.org/test";
    try (final SafeXMLStreamWriter aXSW = SafeXMLStreamWriter.create (aSW, new XMLWriterSettings ()))
    {
      aXSW.writeStartDocument ();
      aXSW.writeStartElement (sNSURI, "Root");
      aXSW.writeDefaultNamespace (sNSURI);
      aXSW.writeAttribute ("Version", "2.0");
      aXSW.writeStartElement (sNSURI, "Child");
      aXSW.writeCharacters ("Hello World");
      aXSW.writeEndElement ();
      aXSW.writeEndElement ();
      aXSW.writeEndDocument ();
      aXSW.flush ();
    }
    assertEquals ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" +
                  "<Root xmlns=\"urn:example.org/test\" Version=\"2.0\"><Child>Hello World</Child></Root>",
                  aSW.getAsString ());
  }
}

@@ -209,16 +209,63 @@
 gWriter;
+%0Aimport com.helger.commons.system.ENewLineMode;
 %0A%0A/**%0A *
@@ -600,32 +600,137 @@
 aSW,
- new XMLWriterSettings (
+%0A                                                                      new XMLWriterSettings ().setNewLineMode (ENewLineMode.UNIX
 )))%0A
@@ -1113,34 +1113,32 @@
 ing=%5C%22UTF-8%5C%22?%3E%5C
-r%5C
 n%22 +%0A           
@@ -1513,32 +1513,137 @@
 aSW,
- new XMLWriterSettings (
+%0A                                                                      new XMLWriterSettings ().setNewLineMode (ENewLineMode.UNIX
 )))%0A
@@ -2023,32 +2023,32 @@
 flush ();%0A    %7D%0A
+
     assertEquals
@@ -2097,10 +2097,8 @@
 %22?%3E%5C
-r%5C
 n%22 +

Test fix for Travis