/*
 * @author Ranides Atterwim <ranides@gmail.com>
 * @copyright Ranides Atterwim
 * @license WTFPL
 * @url http://ranides.net/projects/assira
 */
package net.ranides.assira.xml;

import net.ranides.assira.TestFiles;
import java.io.IOException;
import net.ranides.assira.io.StringOutput;
import net.ranides.assira.text.Charsets;
import net.ranides.assira.text.IOStrings;
import org.junit.Test;
import static org.junit.Assert.*;
import static net.ranides.assira.xml.XMLElement.$;

/**
 *
 * @author Ranides Atterwim <ranides@gmail.com>
 */
public class XMLWriterTest {
    
    @Test
    public void testWriter() throws IOException {
        XMLElement doc = $(TestFiles.XML_INPUT).normalize().find("? //users/user[4]").first();

        doc.find("? mail[1]").text("tom@c...");
        doc.find("? mail[2]").text("tom@d...");
        doc.find("? mail[3]").text("office@c...");
        doc.find("? mail/@type").text("hidden");
        
        String out1 = doc.writer().indent(0).toString();
        
        String out2 = doc.writer()
            .param(XMLOptions.DOCTYPE_PUBLIC, "yes")
            .param(XMLOptions.INDENT, "yes")
            .param(XMLOptions.INDENT_AMOUNT, "2")
            .toString();
        
        StringOutput out3 = new StringOutput();
        doc.writer()
            .param(XMLOptions.DOCTYPE_PUBLIC, "")
            .param(XMLOptions.INDENT, "yes")
            .param(XMLOptions.INDENT_AMOUNT, "2")
            .save(out3);
        
        String out4 = doc.writer()
            .indent(4)
            .toString();
        
//        System.out.printf("%n%s%n", out1);
//        System.out.printf("%n%s%n", out2);
//        System.out.printf("%n%s%n", out3.toString(Charsets.UTF8));
//        System.out.printf("%n%s%n", out4);

        assertEquals(IOStrings.read(TestFiles.XML_1, Charsets.UTF8), out1);
        assertEquals(IOStrings.read(TestFiles.XML_2, Charsets.UTF8), out2);
        assertEquals(IOStrings.read(TestFiles.XML_3, Charsets.UTF8), out3.toString(Charsets.UTF8));
        assertEquals(IOStrings.read(TestFiles.XML_3, Charsets.PL_WINDOWS), out3.toString(Charsets.PL_WINDOWS));
        assertEquals(IOStrings.read(TestFiles.XML_4, Charsets.UTF8), out4);
        
        assertTrue(true);
    }
    
}
