package edu.northwestern.bioinformatics.studycalendar.web.taglibs.jsgenerator;

import edu.northwestern.bioinformatics.studycalendar.testing.MockPageContext;
import edu.northwestern.bioinformatics.studycalendar.testing.StudyCalendarTestCase;
import static org.easymock.classextension.EasyMock.expect;

import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTag;
import javax.servlet.jsp.tagext.Tag;


/**
 * @author Rhett Sutphin
 */
public class ReplaceHtmlTest extends StudyCalendarTestCase {
    private static final String TARGET_ELEMENT = "target";

    private ReplaceHtml tag;
    private JspWriter writer;
    private MockPageContext pageContext;

    protected void setUp() throws Exception {
        super.setUp();
        tag = new ReplaceHtml();
        writer = registerMockFor(JspWriter.class);
        pageContext = new MockPageContext(writer);

        tag.setPageContext(pageContext);
        tag.setTargetElement(TARGET_ELEMENT);
    }

    public void testStart() throws Exception {
        replayMocks();
        assertEquals(BodyTag.EVAL_BODY_BUFFERED, tag.doStartTag());
        verifyMocks();
    }

    public void testEnd() throws Exception {
        BodyContent bodyContent = registerMockFor(BodyContent.class);
        tag.setBodyContent(bodyContent);

        expect(bodyContent.getString()).andReturn("content");
        writer.write("Element.update(\"" + TARGET_ELEMENT + "\", \"content\")");

        replayMocks();
        assertEquals(Tag.EVAL_PAGE, tag.doEndTag());
        verifyMocks();
    }
}

@@ -709,16 +709,53 @@
 Context;
+%0A    private BodyContent bodyContent;
 %0A%0A    pr
@@ -951,16 +951,74 @@
 writer);
+%0A        bodyContent = registerMockFor(BodyContent.class);
 %0A%0A      
@@ -1098,16 +1098,57 @@
 EMENT);%0A
+        tag.setBodyContent(bodyContent);%0A
     %7D%0A%0A 
@@ -1363,120 +1363,8 @@
 n %7B%0A
-        BodyContent bodyContent = registerMockFor(BodyContent.class);%0A        tag.setBodyContent(bodyContent);%0A%0A
     
@@ -1499,24 +1499,425 @@
 tent%5C%22)%22);%0A%0A
+        replayMocks();%0A        assertEquals(Tag.EVAL_PAGE, tag.doEndTag());%0A        verifyMocks();%0A    %7D%0A%0A    public void testEndWithComplexContent() throws Exception %7B%0A        expect(bodyContent.getString()).andReturn(%22%3Cdiv id=%5C%22foo%5C%22%3E%5Cn  %3Cstrong%3EBad%3C/strong%3E%5Cn%3C/div%3E%22);%0A        writer.write(%22Element.update(%5C%22%22 + TARGET_ELEMENT + %22%5C%22, %5C%22%3Cdiv id=%5C%5C%5C%22foo%5C%5C%5C%22%3E%5C%5Cn  %3Cstrong%3EBad%3C/strong%3E%5C%5Cn%3C/div%3E%5C%22)%22);%0A%0A
         repl

Add test that bod content is escaped