FileDocCategorySizeDatePackage
PlainTextResultTest.javaAPI DocExample5614Mon Jul 23 13:26:12 BST 2007org.apache.struts2.dispatcher

PlainTextResultTest

public class PlainTextResultTest extends TestCase
Test case for PlainTextResult.

Fields Summary
com.opensymphony.xwork2.util.ValueStack
stack
com.opensymphony.xwork2.mock.MockActionInvocation
invocation
com.opensymphony.xwork2.ActionContext
context
org.apache.struts2.views.jsp.StrutsMockHttpServletResponse
response
PrintWriter
writer
StringWriter
stringWriter
org.apache.struts2.views.jsp.StrutsMockServletContext
servletContext
Constructors Summary
Methods Summary
protected java.lang.StringreadAsString(java.lang.String resource)

        InputStream is = null;
        try {
            is = ClassLoaderUtil.getResourceAsStream(resource, PlainTextResultTest.class);
            int sizeRead = 0;
            byte[] buffer = new byte[1024];
            StringBuffer stringBuffer = new StringBuffer();
            while((sizeRead = is.read(buffer)) != -1) {
                stringBuffer.append(new String(buffer, 0, sizeRead));
            }
            return stringBuffer.toString();
        }
        finally {
            if (is != null)
                is.close();
        }

    
protected voidsetUp()

        super.setUp();

        stringWriter = new StringWriter();
        writer = new PrintWriter(stringWriter);
        response = new StrutsMockHttpServletResponse();
        response.setWriter(writer);
        servletContext = new StrutsMockServletContext();
        stack = ValueStackFactory.getFactory().createValueStack();
        context = new ActionContext(stack.getContext());
        context.put(StrutsStatics.HTTP_RESPONSE, response);
        context.put(StrutsStatics.SERVLET_CONTEXT, servletContext);
        invocation = new MockActionInvocation();
        invocation.setStack(stack);
        invocation.setInvocationContext(context);
    
protected voidtearDown()

        stack = null;
        invocation = null;
        context = null;
        response = null;
        writer = null;
        stringWriter = null;
        servletContext = null;

        super.tearDown();
    
public voidtestPlainText()

        PlainTextResult result = new PlainTextResult();
        result.setLocation("/someJspFile.jsp");

        response.setExpectedContentType("text/plain");
        response.setExpectedHeader("Content-Disposition", "inline");
        InputStream jspResourceInputStream =
            ClassLoaderUtil.getResourceAsStream(
                "org/apache/struts2/dispatcher/someJspFile.jsp",
                PlainTextResultTest.class);


        try {
            servletContext.setResourceAsStream(jspResourceInputStream);
            result.execute(invocation);

            String r = AbstractUITagTest.normalize(stringWriter.getBuffer().toString(), true);
            String e = AbstractUITagTest.normalize(
                    readAsString("org/apache/struts2/dispatcher/someJspFile.jsp"), true);
            assertEquals(r, e);
        }
        finally {
            jspResourceInputStream.close();
        }
    
public voidtestPlainTextWithEncoding()

        PlainTextResult result = new PlainTextResult();
        result.setLocation("/someJspFile.jsp");
        result.setCharSet("UTF-8");

        response.setExpectedContentType("text/plain; charset=UTF-8");
        response.setExpectedHeader("Content-Disposition", "inline");
        InputStream jspResourceInputStream =
            ClassLoaderUtil.getResourceAsStream(
                "org/apache/struts2/dispatcher/someJspFile.jsp",
                PlainTextResultTest.class);


        try {
            servletContext.setResourceAsStream(jspResourceInputStream);
            result.execute(invocation);

            String r = AbstractUITagTest.normalize(stringWriter.getBuffer().toString(), true);
            String e = AbstractUITagTest.normalize(
                    readAsString("org/apache/struts2/dispatcher/someJspFile.jsp"), true);
            assertEquals(r, e);
        }
        finally {
            jspResourceInputStream.close();
        }