FileDocCategorySizeDatePackage
XSLTResultTest.javaAPI DocExample7657Mon Jul 23 13:26:12 BST 2007org.apache.struts2.views.xslt

XSLTResultTest

public class XSLTResultTest extends org.apache.struts2.StrutsTestCase
Unit test for {@link XSLTResult}.

Fields Summary
private XSLTResult
result
private org.springframework.mock.web.MockHttpServletResponse
response
private org.springframework.mock.web.MockHttpServletRequest
request
private org.springframework.mock.web.MockServletContext
servletContext
private com.opensymphony.xwork2.mock.MockActionInvocation
mai
private com.opensymphony.xwork2.util.ValueStack
stack
Constructors Summary
Methods Summary
protected voidsetUp()

        request = new MockHttpServletRequest();
        response = new MockHttpServletResponse();
        servletContext = new MockServletContext();

        result = new XSLTResult();
        stack = ValueStackFactory.getFactory().createValueStack();
        ActionContext.getContext().setValueStack(stack);

        MyAction action = new MyAction();

        mai = new com.opensymphony.xwork2.mock.MockActionInvocation();
        mai.setAction(action);
        mai.setStack(stack);
        mai.setInvocationContext(ActionContext.getContext());
        stack.push(action);

        ActionContext.getContext().put(ServletActionContext.HTTP_REQUEST, request);
        ActionContext.getContext().put(ServletActionContext.HTTP_RESPONSE, response);
        ActionContext.getContext().put(ServletActionContext.SERVLET_CONTEXT, servletContext);
    
protected voidtearDown()

        request = null;
        response = null;
        servletContext = null;
        result = null;
        stack = null;
        mai = null;
    
public voidtestNoFileFound()

        try {
            result.setParse(false);
            result.setLocation("nofile.xsl");
            result.execute(mai);
            fail("Should have thrown a TransformerException");
        } catch (TransformerException e) {
            // success
        }
    
public voidtestNoLocation()

        try {
            result.setParse(false);
            result.setLocation(null);
            result.execute(mai);
            fail("Should have thrown an IllegalArgumentException");
        } catch (IllegalArgumentException e) {
            // success
        }
    
public voidtestSimpleTransform()

        result.setParse(false);
        result.setLocation("XSLTResultTest.xsl");
        result.execute(mai);

        String out = response.getContentAsString();
        assertTrue(out.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
        assertTrue(out.indexOf("<result xmlns=\"http://www.w3.org/TR/xhtml1/strict\"") > -1);
    
public voidtestSimpleTransformParse()

        result.setParse(true);
        result.setLocation("${top.myLocation}");
        result.execute(mai);

        String out = response.getContentAsString();
        assertTrue(out.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
        assertTrue(out.indexOf("<result xmlns=\"http://www.w3.org/TR/xhtml1/strict\"") > -1);
    
public voidtestTransform2()

        result.setParse(false);
        result.setLocation("XSLTResultTest2.xsl");
        result.execute(mai);

        String out = response.getContentAsString();
        assertTrue(out.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
        assertTrue(out.indexOf("<html xmlns=\"http://www.w3.org/TR/xhtml1/strict\"") > -1);
        assertTrue(out.indexOf("Hello Santa Claus how are you?") > -1);
    
public voidtestTransform3()

        result.setParse(false);
        result.setLocation("XSLTResultTest3.xsl");
        result.execute(mai);

        String out = response.getContentAsString();
        assertTrue(out.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
        assertTrue(out.indexOf("<html xmlns=\"http://www.w3.org/TR/xhtml1/strict\"") > -1);
        assertTrue(out.indexOf("Hello Santa Claus how are you?") > -1);
        assertTrue(out.indexOf("WebWork in Action by Patrick and Jason") > -1);
        assertTrue(out.indexOf("XWork not in Action by Superman") > -1);
    
public voidtestTransform4WithDocumentInclude()

        result = new XSLTResult(){
            protected URIResolver getURIResolver() {
                return new URIResolver() {
                    public Source resolve(String href, String base) throws TransformerException {
                        return new StreamSource(ClassLoaderUtils.getResourceAsStream(href, this.getClass()));
                    }
                    
                };
            }
            
        };
        result.setParse(false);
        result.setLocation("XSLTResultTest4.xsl");
        result.execute(mai);

        String out = response.getContentAsString();
        assertTrue(out.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
        assertTrue(out.indexOf("<validators>") > -1);