package sop.xml;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

/**
 * XmlSemDiff test class
 *  
 * @author Martin Putniorz
 * @version 24052012
 */
public class XmlSemDiffTest {
    
    private static DocumentBuilderFactory dFact;
    private static DocumentBuilder build;
    private static Document doc;
    private static Element leadingWhitespace;
    private static Element trailingWhitespace;
    private static Element leadingAndTrailingWhitespace;
    private static Element innerWhitespace;
    
    public XmlSemDiffTest() {
    }
    
    @BeforeClass
    public static void setUpClass() throws Exception {
        dFact = DocumentBuilderFactory.newInstance();
        build = dFact.newDocumentBuilder();
        doc = build.newDocument();
        leadingWhitespace = doc.createElement("leadingWhitespace");
        trailingWhitespace = doc.createElement("trailingWhitespace");
        leadingAndTrailingWhitespace = doc.createElement("leadingWhitespace");
        innerWhitespace = doc.createElement("leadingWhitespace");
    }

    @Before
    public void setUp() throws SAXException, ParserConfigurationException {
        leadingWhitespace.setTextContent("      test");
        trailingWhitespace.setTextContent("test         ");
        leadingAndTrailingWhitespace.setTextContent("  test     ");
        innerWhitespace.setTextContent("te st");
    }

    @Test
    public void testLeadAndTrailWhitespaceCompare() {
        XmlSemDiffInterface diff = new XmlSemDiff();
        
        assertTrue(diff.whitespaceCompare(leadingWhitespace, trailingWhitespace, false));
        assertTrue(diff.whitespaceCompare(leadingWhitespace, leadingAndTrailingWhitespace, false));
        assertTrue(diff.whitespaceCompare(trailingWhitespace, leadingAndTrailingWhitespace, false));
        assertFalse(diff.whitespaceCompare(innerWhitespace, leadingAndTrailingWhitespace, false));
    }
    
    @Test
    public void testInnerWhitespaceCompare() {
        XmlSemDiffInterface diff = new XmlSemDiff();
        
        assertTrue(diff.whitespaceCompare(innerWhitespace, leadingWhitespace, true));
        assertTrue(diff.whitespaceCompare(innerWhitespace, trailingWhitespace, true));
        assertTrue(diff.whitespaceCompare(innerWhitespace, leadingAndTrailingWhitespace, true));
    }

}

@@ -853,24 +853,219 @@
 space;%0A    %0A
+    private static Document doc1;%0A    private static Document doc2;%0A    private static Element rootElement;%0A    private static Element rootElement1;%0A    private static Element rootElement2;%0A    %0A
     public X
@@ -1078,24 +1078,24 @@
 iffTest() %7B%0A
-
     %7D%0A    %0A 
@@ -1493,32 +1493,43 @@
 Element(%22leading
+AndTrailing
 Whitespace%22);%0A  
@@ -1571,34 +1571,1259 @@
 lement(%22
-leadingWhitespace%22
+innerWhitespace%22);%0A        %0A        rootElement = doc.createElement(%22rootElement%22);%0A        doc.appendChild(rootElement);%0A        rootElement.appendChild(leadingWhitespace);%0A        rootElement.appendChild(trailingWhitespace);%0A        rootElement.appendChild(leadingAndTrailingWhitespace);%0A        rootElement.appendChild(innerWhitespace);%0A        %0A        doc1 = build.newDocument();%0A        rootElement1 = doc1.createElement(%22rootElement%22);%0A        doc1.appendChild(rootElement1);%0A        rootElement1.appendChild(doc1.createElement(%22leadingWhitespace%22));%0A        rootElement1.appendChild(doc1.createElement(%22trailingWhitespace%22));%0A        rootElement1.appendChild(doc1.createElement(%22leadingAndTrailingWhitespace%22));%0A        rootElement1.appendChild(doc1.createElement(%22innerWhitespace%22));%0A        %0A        doc2 = build.newDocument();%0A        rootElement2 = doc2.createElement(%22rootElement%22);%0A        doc2.appendChild(rootElement2);%0A        rootElement2.appendChild(doc2.createElement(%22leadingAndTrailingWhitespace%22));%0A        rootElement2.appendChild(doc2.createElement(%22trailingWhitespace%22));%0A        rootElement2.appendChild(doc2.createElement(%22leadingWhitespace%22));%0A        rootElement2.appendChild(doc2.createElement(%22innerWhitespace%22)
 );%0A    %7D
@@ -3963,32 +3963,32 @@
 espace, true));%0A
-
         assertTr
@@ -4073,12 +4073,527 @@
 );%0A    %7D
+%0A    %0A    @Test%0A    public void testOrderCompare() %7B%0A        XmlSemDiffInterface diff = new XmlSemDiff();%0A        %0A        assertTrue(diff.orderCompare(doc.getDocumentElement(), doc1.getDocumentElement(), true));%0A        assertTrue(diff.orderCompare(doc.getDocumentElement(), doc1.getDocumentElement(), false));%0A        assertTrue(diff.orderCompare(doc.getDocumentElement(), doc2.getDocumentElement(), true));%0A        assertFalse(diff.orderCompare(doc.getDocumentElement(), doc2.getDocumentElement(), false));%0A    %7D
 %0A%0A%7D%0A

private static Document doc1; private static Document doc2; private static Element rootElement; private static Element rootElement1; private static Element rootElement2; leadingAndTrailingWhitespace = doc.createElement("leadingAndTrailingWhitespace"); innerWhitespace = doc.createElement("innerWhitespace");