package net.sourceforge.htmlunit.cyberneko;

import java.io.StringReader;
import java.io.StringWriter;

import junit.framework.TestCase;

import org.apache.xerces.xni.QName;
import org.apache.xerces.xni.parser.XMLDocumentFilter;
import org.apache.xerces.xni.parser.XMLInputSource;
import net.sourceforge.htmlunit.cyberneko.parsers.DOMParser;

/**
 * Unit tests for {@link HTMLTagBalancer}'s property {@link HTMLTagBalancer#FRAGMENT_CONTEXT_STACK}.
 * 
 * @author Marc Guillemot
 * @version $Id: HTMLTagBalancingListenerTest.java 145 2008-03-17 18:18:33Z
 *          mguillem $
 */
public class FragmentContextStackTest extends TestCase {

    public void testSimple() throws Exception {
        String expected = "(DIV\r\n"
            + "(SPAN\r\n"
            + "\"hello\r\n"
            + ")SPAN\r\n"
            + ")DIV\r\n";
        doTest("<div><span>hello</span>", new String[] { "html", "body" }, expected);

        doTest("<div><span>hello</span>", new String[] { "html" }, expected);

        doTest("<div><span>hello</span>", new String[]{}, expected);
        doTest("<div><span>hello</span>", null, expected);
    }

    public void testTR() throws Exception {
        String expected = "(TR\r\n"
            + "(TD\r\n"
            + "\"hello\r\n"
            + ")TD\r\n"
            + ")TR\r\n";
        doTest("<tr><td>hello</td></tr>", new String[] { "html", "body", "table", "tbody" }, expected);
        expected = "(TBODY\r\n"
            + expected
            + ")TBODY\n";
        doTest("<tr><td>hello</td></tr>", new String[] { "html", "body", "table" }, expected);
        doTest("<tr><td>hello</td></tr>", new String[] { "html", "body" }, "\"hello");
    }

    public void testFragmentShouldNotCloseContextStack() throws Exception {
        String expected = "\"helloworld\n";
        doTest("hello</div>world", new String[] { "html", "body", "div" }, expected);
        doTest("hello</span>world", new String[] { "html", "body", "div", "span" }, expected);
    }

    private void doTest(final String html, final String[] contextStack,
            final String expected) throws Exception {
        final DOMParser parser = new DOMParser();
        parser.setFeature("http://cyberneko.org/html/features/balance-tags/document-fragment", true);
        if (contextStack != null) {
            parser.setProperty("http://cyberneko.org/html/properties/balance-tags/fragment-context-stack", toQNames(contextStack));
        }

        final StringWriter out = new StringWriter();
        final XMLDocumentFilter[] filters = { new Writer(out) };
        parser.setProperty("http://cyberneko.org/html/properties/filters", filters);

        final StringReader sr = new StringReader(html);
        final XMLInputSource in = new XMLInputSource(null, "foo", null, sr, null);
        parser.parse(in);

        assertEquals(expected.trim(), out.toString().trim());
    }

    private QName[] toQNames(final String[] tags) {
        final QName[] qnames = new QName[tags.length];
        for (int i = 0; i < tags.length; ++i) {
            qnames[i] = new QName(null, tags[i], null, null);
        }

        return qnames;
    }
}
@@ -101,42 +101,8 @@
 r;%0A%0A
-import junit.framework.TestCase;%0A%0A
 impo
@@ -239,16 +239,50 @@
 tSource;
+%0A%0Aimport junit.framework.TestCase;
 %0Aimport 
@@ -632,16 +632,78 @@
 Case %7B%0A%0A
+    private static final String NL = System.lineSeparator();%0A%0A
     publ
@@ -773,21 +773,22 @@
  = %22(DIV
-%5Cr%5Cn%22
+%22 + NL
 %0A       
@@ -796,29 +796,30 @@
     + %22(SPAN
-%5Cr%5Cn%22
+%22 + NL
 %0A           
@@ -821,37 +821,38 @@
       + %22%5C%22hello
-%5Cr%5Cn%22
+%22 + NL
 %0A            + %22
@@ -856,21 +856,22 @@
 + %22)SPAN
-%5Cr%5Cn%22
+%22 + NL
 %0A       
@@ -882,21 +882,22 @@
  + %22)DIV
-%5Cr%5Cn%22
+%22 + NL
 ;%0A      
@@ -1265,21 +1265,22 @@
 d = %22(TR
-%5Cr%5Cn%22
+%22 + NL
 %0A       
@@ -1290,21 +1290,22 @@
   + %22(TD
-%5Cr%5Cn%22
+%22 + NL
 %0A       
@@ -1319,21 +1319,22 @@
 %22%5C%22hello
-%5Cr%5Cn%22
+%22 + NL
 %0A       
@@ -1344,21 +1344,22 @@
   + %22)TD
-%5Cr%5Cn%22
+%22 + NL
 %0A       
@@ -1369,21 +1369,22 @@
   + %22)TR
-%5Cr%5Cn%22
+%22 + NL
 ;%0A      
@@ -1511,13 +1511,14 @@
 BODY
-%5Cr%5Cn%22
+%22 + NL
 %0A   

make test platform independent