View Javadoc

    1   /*
    2    * $Id: XSLTResultTest.java 481180 2006-12-01 08:02:59Z mrdon $
    3    *
    4    * Licensed to the Apache Software Foundation (ASF) under one
    5    * or more contributor license agreements.  See the NOTICE file
    6    * distributed with this work for additional information
    7    * regarding copyright ownership.  The ASF licenses this file
    8    * to you under the Apache License, Version 2.0 (the
    9    * "License"); you may not use this file except in compliance
    10   * with the License.  You may obtain a copy of the License at
    11   *
    12   *  http://www.apache.org/licenses/LICENSE-2.0
    13   *
    14   * Unless required by applicable law or agreed to in writing,
    15   * software distributed under the License is distributed on an
    16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    17   * KIND, either express or implied.  See the License for the
    18   * specific language governing permissions and limitations
    19   * under the License.
    20   */
    21  package org.apache.struts2.views.xslt;
    22
    23  import java.util.ArrayList;
    24  import java.util.List;
    25
    26  import javax.xml.transform.Source;
    27  import javax.xml.transform.TransformerException;
    28  import javax.xml.transform.URIResolver;
    29  import javax.xml.transform.stream.StreamSource;
    30
    31  import org.apache.struts2.ServletActionContext;
    32  import org.apache.struts2.StrutsTestCase;
    33  import org.apache.struts2.util.ClassLoaderUtils;
    34  import org.springframework.mock.web.MockHttpServletRequest;
    35  import org.springframework.mock.web.MockHttpServletResponse;
    36  import org.springframework.mock.web.MockServletContext;
    37
    38  import com.opensymphony.xwork2.Action;
    39  import com.opensymphony.xwork2.ActionContext;
    40  import com.opensymphony.xwork2.mock.MockActionInvocation;
    41  import com.opensymphony.xwork2.util.ValueStack;
    42  import com.opensymphony.xwork2.util.ValueStackFactory;
    43
    44  /***
    45   * Unit test for {@link XSLTResult}.
    46   *
    47   */
    48  public class XSLTResultTest extends StrutsTestCase {
    49
    50      private XSLTResult result;
    51      private MockHttpServletResponse response;
    52      private MockHttpServletRequest request;
    53      private MockServletContext servletContext;
    54      private MockActionInvocation mai;
    55      private ValueStack stack;
    56
    57      public void testNoLocation() throws Exception {
    58          try {
    59              result.setParse(false);
    60              result.setLocation(null);
    61              result.execute(mai);
    62              fail("Should have thrown an IllegalArgumentException");
    63          } catch (IllegalArgumentException e) {
    64              // success
    65          }
    66      }
    67
    68      public void testNoFileFound() throws Exception {
    69          try {
    70              result.setParse(false);
    71              result.setLocation("nofile.xsl");
    72              result.execute(mai);
    73              fail("Should have thrown a TransformerException");
    74          } catch (TransformerException e) {
    75              // success
    76          }
    77      }
    78
    79      public void testSimpleTransform() throws Exception {
    80          result.setParse(false);
    81          result.setLocation("XSLTResultTest.xsl");
    82          result.execute(mai);
    83
    84          String out = response.getContentAsString();
    85          assertTrue(out.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
    86          assertTrue(out.indexOf("<result xmlns=\"http://www.w3.org/TR/xhtml1/strict\"") > -1);
    87      }
    88
    89      public void testSimpleTransformParse() throws Exception {
    90          result.setParse(true);
    91          result.setLocation("${top.myLocation}");
    92          result.execute(mai);
    93
    94          String out = response.getContentAsString();
    95          assertTrue(out.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
    96          assertTrue(out.indexOf("<result xmlns=\"http://www.w3.org/TR/xhtml1/strict\"") > -1);
    97      }
    98
    99      public void testTransform2() throws Exception {
    100         result.setParse(false);
    101         result.setLocation("XSLTResultTest2.xsl");
    102         result.execute(mai);
    103
    104         String out = response.getContentAsString();
    105         assertTrue(out.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
    106         assertTrue(out.indexOf("<html xmlns=\"http://www.w3.org/TR/xhtml1/strict\"") > -1);
    107         assertTrue(out.indexOf("Hello Santa Claus how are you?") > -1);
    108     }
    109
    110     public void testTransform3() throws Exception {
    111         result.setParse(false);
    112         result.setLocation("XSLTResultTest3.xsl");
    113         result.execute(mai);
    114
    115         String out = response.getContentAsString();
    116         assertTrue(out.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
    117         assertTrue(out.indexOf("<html xmlns=\"http://www.w3.org/TR/xhtml1/strict\"") > -1);
    118         assertTrue(out.indexOf("Hello Santa Claus how are you?") > -1);
    119         assertTrue(out.indexOf("WebWork in Action by Patrick and Jason") > -1);
    120         assertTrue(out.indexOf("XWork not in Action by Superman") > -1);
    121     }
    122
    123     public void testTransform4WithDocumentInclude() throws Exception {
    124         result = new XSLTResult(){
    125             protected URIResolver getURIResolver() {
    126                 return new URIResolver() {
    127                     public Source resolve(String href, String base) throws TransformerException {
    128                         return new StreamSource(ClassLoaderUtils.getResourceAsStream(href, this.getClass()));
    129                     }
    130
    131                 };
    132             }
    133
    134         };
    135         result.setParse(false);
    136         result.setLocation("XSLTResultTest4.xsl");
    137         result.execute(mai);
    138
    139         String out = response.getContentAsString();
    140         assertTrue(out.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
    141         assertTrue(out.indexOf("<validators>") > -1);
    142     }
    143
    144     protected void setUp() throws Exception {
    145         request = new MockHttpServletRequest();
    146         response = new MockHttpServletResponse();
    147         servletContext = new MockServletContext();
    148
    149         result = new XSLTResult();
    150         stack = ValueStackFactory.getFactory().createValueStack();
    151         ActionContext.getContext().setValueStack(stack);
    152
    153         MyAction action = new MyAction();
    154
    155         mai = new com.opensymphony.xwork2.mock.MockActionInvocation();
    156         mai.setAction(action);
    157         mai.setStack(stack);
    158         mai.setInvocationContext(ActionContext.getContext());
    159         stack.push(action);
    160
    161         ActionContext.getContext().put(ServletActionContext.HTTP_REQUEST, request);
    162         ActionContext.getContext().put(ServletActionContext.HTTP_RESPONSE, response);
    163         ActionContext.getContext().put(ServletActionContext.SERVLET_CONTEXT, servletContext);
    164     }
    165
    166     protected void tearDown() {
    167         request = null;
    168         response = null;
    169         servletContext = null;
    170         result = null;
    171         stack = null;
    172         mai = null;
    173     }
    174
    175     private class MyAction implements Action {
    176
    177         public String execute() throws Exception {
    178             return SUCCESS;
    179         }
    180
    181         public String getMyLocation() {
    182             return ("XSLTResultTest.xsl");
    183         }
    184
    185         public String getUsername() {
    186             return "Santa Claus";
    187         }
    188
    189         public List getBooks() {
    190             List list = new ArrayList();
    191             list.add(new Book("WebWork in Action", "Patrick and Jason"));
    192             list.add(new Book("XWork not in Action", "Superman"));
    193             return list;
    194         }
    195
    196     }
    197
    198     public class Book {
    199
    200         private String title;
    201         private String author;
    202
    203         public Book(String title, String author) {
    204             this.title = title;
    205             this.author = author;
    206         }
    207
    208         public String getTitle() {
    209             return title;
    210         }
    211
    212         public String getAuthor() {
    213             return author;
    214         }
    215     }
    216 }