/*
 * eXist-db Open Source Native XML Database
 * Copyright (C) 2001 The eXist-db Authors
 *
 * info@exist-db.org
 * http://www.exist-db.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package org.exist.xquery.functions.util;

import static org.junit.Assert.*;

import org.exist.test.ExistXmldbEmbeddedServer;
import org.exist.xquery.XPathException;
import org.junit.ClassRule;
import org.junit.Test;
import org.xmldb.api.base.ResourceSet;
import org.xmldb.api.base.XMLDBException;

/**
 * @author Casey Jordan
 * @author <a href="mailto:shabanovd@gmail.com">Dmitriy Shabanov</a>
 *
 */
public class ExpandTest {

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

    @Test
    public void testExpandWithDefaultNS() throws XPathException, XMLDBException {
    	final String expected = "<ok xmlns=\"some\">\n    <concept xmlns=\"\"/>\n</ok>";

        String query = "" +
                "let $doc-path := xmldb:store('/db', 'test.xml', <concept/>)\n" +
                "let $doc := doc($doc-path)\n" +
                "return\n" +
                "<ok xmlns='some'>\n" +
                "{util:expand($doc)}\n" +
                "</ok>";
        ResourceSet result = existEmbeddedServer.executeQuery(query);
        String r = (String) result.getResource(0).getContent();
        assertEquals(expected, r);

        query = "" +
                "let $doc-path := xmldb:store('/db', 'test.xml', <concept/>)\n" +
                "let $doc := doc($doc-path)\n" +
                "return\n" +
                "<ok xmlns='some'>\n" +
                "{$doc}\n" +
                "</ok>";
        result = existEmbeddedServer.executeQuery(query);
        r = (String) result.getResource(0).getContent();
        assertEquals(expected, r);
    }
}

@@ -924,16 +924,71 @@
 .util;%0A%0A
+import static java.nio.charset.StandardCharsets.UTF_8;%0A
 import s
@@ -1077,35 +1077,25 @@
 org.
-exist.xquery.XPathException
+junit.BeforeClass
 ;%0Aim
@@ -1143,16 +1143,54 @@
 t.Test;%0A
+import org.xmldb.api.base.Collection;%0A
 import o
@@ -1366,38 +1366,377 @@
 %3E%0A *
-%0A */%0Apublic class ExpandTest %7B
+ @author %3Ca href=%22mailto:adam@evolvedbinary.com%22%3EAdam Retter%3C/a%3E%0A */%0Apublic class ExpandTest %7B%0A%0A    private static final String EOL = System.getProperty(%22line.separator%22);%0A%0A    private static final String DOC1_CONTENT = %22%3Cdoc1%3Edoc1%3C/doc1%3E%22;%0A    private static final String DOC2_CONTENT = %22%3C!-- comment 1 before --%3E%3C!-- comment 2 before --%3E%22 + EOL + %22%3Cdoc2%3Edoc2%3C/doc2%3E%22;
 %0A%0A  
@@ -1874,20 +1874,27 @@
 ;%0A%0A    @
-Test
+BeforeClass
 %0A    pub
@@ -1901,61 +1901,448 @@
 lic 
-void testExpandWithDefaultNS() throws XPathException,
+static void setup() throws XMLDBException %7B%0A        final Collection expandTestCol = existEmbeddedServer.createCollection(existEmbeddedServer.getRoot(), %22expand-test%22);%0A        ExistXmldbEmbeddedServer.storeResource(expandTestCol, %22doc1.xml%22, DOC1_CONTENT.getBytes(UTF_8));%0A        ExistXmldbEmbeddedServer.storeResource(expandTestCol, %22doc2.xml%22, DOC2_CONTENT.getBytes(UTF_8));%0A    %7D%0A%0A    @Test%0A    public void expandWithDefaultNS() throws
  XML
@@ -3337,11 +3337,706 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void expandPersistentDom() throws XMLDBException %7B%0A        final String query = %22util:expand(doc('/db/expand-test/doc1.xml'))%22;%0A        final ResourceSet result = existEmbeddedServer.executeQuery(query);%0A        final String r = (String) result.getResource(0).getContent();%0A        assertEquals(DOC1_CONTENT, r);%0A    %7D%0A%0A    @Test%0A    public void expandPersistentDomCommentsFirst() throws XMLDBException %7B%0A        final String query = %22util:expand(doc('/db/expand-test/doc2.xml'))%22;%0A        final ResourceSet result = existEmbeddedServer.executeQuery(query);%0A        final String r = (String) result.getResource(0).getContent();%0A        assertEquals(DOC2_CONTENT, r);%0A    %7D
 %0A%7D%0A

Add tests for util:expand over persistent DOM