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

import java.io.IOException;
import java.util.NoSuchElementException;
import net.ranides.assira.TestFiles;
import net.ranides.assira.collection.maps.MultiMap;

import net.ranides.assira.xml.XMLDocument;
import net.ranides.assira.xml.XMLElement;
import static net.ranides.assira.xml.XMLElement.$;
import org.junit.Test;
import static net.ranides.assira.junit.NewAssert.*;
import net.ranides.assira.text.Charsets;
import net.ranides.assira.text.IOStrings;
import net.ranides.assira.xml.XMLException;
import org.w3c.dom.DOMException;

/**
 *
 * @author Ranides Atterwim <ranides@gmail.com>
 */
public class W3DocumentTest {
    
    @Test
    public void testEquals() throws IOException {
        XMLElement doc1 = $("<target></target>");
        XMLElement doc2 = $("<root></root>");
        
        assertEquality(doc1.document(), doc1.document(), doc2);
    }
    
    @Test
    public void testBuilder() throws IOException {
        XMLDocument builder = $("<root></root>").document();
        XMLElement doc = $("<target></target>");

        XMLElement comment1 = builder.comment("hello world");
        XMLElement comment2 = builder.comment("comment");
//        
        doc.append(comment1);
        doc.append(comment2);
        doc.prepend(comment2);
        doc.prepend(builder.processing("home", "data=44"));
        doc.append(builder.processing("EOF", "meta-eof"));
        
        assertThrows(DOMException.class, ()->{
            doc.append(builder.tag("tagname"));
        });
        
        doc.root().append(builder.tag("head"));
        doc.root().append(builder.tag("body"));
        doc.find("head").append(builder.tag("meta"));
        doc.find("head").append(builder.tag("meta"));
        doc.find("head").append(builder.tag("meta"));
        
        doc.find("? //head/meta[1]").first().attrs().append(builder.attr("name").text("charset"));
        doc.find("? //head/meta[1]").first().attrs().append(builder.attr("value").text("UTF-8"));
        
        assertThrows(NoSuchElementException.class, ()->{
            doc.find("? //head/meta[2]").first().attr("name").content("author");
        });
        
        assertThrows(XMLException.class, ()->{
            doc.find("? //head/meta[2]").first().attrs().prepend(builder.tag("value"));
        });
        
        doc.find("? //head/meta[2]").first().attrs().append(builder.attr("name").text("author"));
        doc.find("? //head/meta[2]").first().attrs().prepend(builder.attr("value").text("ranides"));
        
        MultiMap<String, String> meta3 = doc.find("? //head/meta[3]").first().attrs().map();
        meta3.put("name", "keywords");
        meta3.put("value", "assira,ranides,xml");
        
        
        doc.find("body").first().append(builder.cdata("binary"));
        
        
        XMLElement fragment = builder.fragment();
        fragment.append(builder.tag("h1").append(builder.text("header")));
        fragment.append(builder.tag("p").append(builder.text("paragraph")));
        fragment.append(builder.tag("span").append(builder.text("summary")));
        
        doc.find("body").first().append(fragment);
        
        String exp = IOStrings.read(TestFiles.XML_DOC, Charsets.UTF8);
        String out = doc.writer().indent(4).toString();
        
        assertEquals(exp, out);
        
    }
    
}
