package org.exist.xquery.functions.util;

import com.googlecode.junittoolbox.ParallelRunner;
import org.custommonkey.xmlunit.exceptions.XpathException;
import org.exist.test.ExistXmldbEmbeddedServer;
import org.junit.ClassRule;
import org.junit.Test;

import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo;
import static org.junit.Assert.*;

import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;

import org.junit.runner.RunWith;
import org.xml.sax.SAXException;
import java.io.IOException;

import org.custommonkey.xmlunit.XMLUnit;
import org.exist.xquery.XPathException;

import org.xmldb.api.base.ResourceSet;
import org.xmldb.api.base.XMLDBException;

/**
 *
 * @author jim.fuller@webcomposite.com
 */
@RunWith(ParallelRunner.class)
public class SerializeTest {

    @ClassRule
    public static final ExistXmldbEmbeddedServer existEmbeddedServer = new ExistXmldbEmbeddedServer(false, true);

    @Test
    public void testSerialize() throws XPathException, XMLDBException, IOException, SAXException {
        final String query = "let $xml := <test/>\n" +
                "return\n" +
                "util:serialize($xml,'method=xml')";
        final ResourceSet result = existEmbeddedServer.executeQuery(query);
        final String r = (String) result.getResource(0).getContent();
        assertXMLEqual(r,"<test/>");
    }

    @Test
    public void testSerialize2() throws XPathException, XMLDBException, IOException, SAXException {
        final String query = "let $xml := <test><a/><b/></test>\n" +
                "return\n" +
                "util:serialize($xml,'method=xml indent=no')";
        final ResourceSet result = existEmbeddedServer.executeQuery(query);
        final String r = (String) result.getResource(0).getContent();
        assertXMLEqual(r,"<test><a/><b/></test>");
    }

    @Test
    public void testSerializeText() throws XPathException, XMLDBException, IOException, SAXException {
        final String query = "let $xml := <test><a>test</a></test>\n" +
                "return\n" +
                "util:serialize($xml,'method=text indent=no')";
        final ResourceSet result = existEmbeddedServer.executeQuery(query);
        final String r = (String) result.getResource(0).getContent();
        assertEquals(r,"test");
    } 

    @Test
    public void testSerializeIndent() throws XPathException, XMLDBException, IOException, SAXException {
        final String query = "let $xml := <test><a/><b/></test>\n" +
                "return\n" +
                "util:serialize($xml,'method=xml indent=yes')";
        final ResourceSet result = existEmbeddedServer.executeQuery(query);
        final String r = (String) result.getResource(0).getContent();
        XMLUnit.setIgnoreWhitespace(true);
        assertXMLEqual(r,"<test><a/><b/></test>");
    }

    @Test
    public void testSerializeXincludes() throws XPathException, XMLDBException, IOException, SAXException, XpathException {
        final String query = "let $xml := " +
                "<test xmlns:xi='http://www.w3.org/2001/XInclude'>" +
                "	<xi:include href='/db/system/security/config.xml'/>" +
                "</test>\n" +
                "return\n" +
                "util:serialize($xml,'enable-xincludes=yes')";

        final ResourceSet result = existEmbeddedServer.executeQuery(query);
        final String r = (String) result.getResource(0).getContent();
        assertXpathEvaluatesTo("2.1","/test//@version",r);
    }    
}
@@ -1330,26 +1330,24 @@
 ertXMLEqual(
-r,
 %22%3Ctest/%3E%22);%0A
@@ -1343,16 +1343,19 @@
 %3Ctest/%3E%22
+, r
 );%0A    %7D
@@ -1788,34 +1788,32 @@
  assertXMLEqual(
-r,
 %22%3Ctest%3E%3Ca/%3E%3Cb/%3E%3C
@@ -1811,32 +1811,35 @@
 %3Ca/%3E%3Cb/%3E%3C/test%3E%22
+, r
 );%0A    %7D%0A%0A    @T
@@ -2281,16 +2281,17 @@
 als(
-r,
 %22test%22
+, r
 );%0A 
@@ -2790,10 +2790,8 @@
 ual(
-r,
 %22%3Cte
@@ -2809,16 +2809,19 @@
 %3C/test%3E%22
+, r
 );%0A    %7D

Correct expected actual test value order