View Javadoc

1   /*
2    * $Id: XSLTResultTest.java 803589 2009-08-12 16:37:46Z musachy $
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  
22  package org.apache.struts2.views.xslt;
23  
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  import javax.xml.transform.Source;
28  import javax.xml.transform.TransformerException;
29  import javax.xml.transform.URIResolver;
30  import javax.xml.transform.stream.StreamSource;
31  
32  import org.apache.struts2.ServletActionContext;
33  import org.apache.struts2.StrutsTestCase;
34  import org.apache.struts2.util.ClassLoaderUtils;
35  import org.springframework.mock.web.MockHttpServletRequest;
36  import org.springframework.mock.web.MockHttpServletResponse;
37  import org.springframework.mock.web.MockServletContext;
38  
39  import com.opensymphony.xwork2.Action;
40  import com.opensymphony.xwork2.ActionContext;
41  import com.opensymphony.xwork2.mock.MockActionInvocation;
42  import com.opensymphony.xwork2.util.ValueStack;
43  import com.opensymphony.xwork2.util.ValueStackFactory;
44  
45  /***
46   * Unit test for {@link XSLTResult}.
47   *
48   */
49  public class XSLTResultTest extends StrutsTestCase {
50  
51      private XSLTResult result;
52      private MockHttpServletResponse response;
53      private MockHttpServletRequest request;
54      private MockServletContext servletContext;
55      private MockActionInvocation mai;
56      private ValueStack stack;
57  
58      public void testNoLocation() throws Exception {
59          try {
60              result.setParse(false);
61              result.setLocation(null);
62              result.execute(mai);
63              fail("Should have thrown an IllegalArgumentException");
64          } catch (IllegalArgumentException e) {
65              // success
66          }
67      }
68  
69      public void testNoFileFound() throws Exception {
70          try {
71              result.setParse(false);
72              result.setLocation("nofile.xsl");
73              result.execute(mai);
74              fail("Should have thrown a TransformerException");
75          } catch (TransformerException e) {
76              // success
77          }
78      }
79  
80      public void testSimpleTransform() throws Exception {
81          result.setParse(false);
82          result.setLocation("XSLTResultTest.xsl");
83          result.execute(mai);
84  
85          String out = response.getContentAsString();
86          assertTrue(out.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
87          assertTrue(out.indexOf("<result xmlns=\"http://www.w3.org/TR/xhtml1/strict\"") > -1);
88      }
89  
90      public void testSimpleTransformParse() throws Exception {
91          result.setParse(true);
92          result.setLocation("${top.myLocation}");
93          result.execute(mai);
94  
95          String out = response.getContentAsString();
96          assertTrue(out.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
97          assertTrue(out.indexOf("<result xmlns=\"http://www.w3.org/TR/xhtml1/strict\"") > -1);
98      }
99  
100     public void testTransform2() throws Exception {
101         result.setParse(false);
102         result.setLocation("XSLTResultTest2.xsl");
103         result.execute(mai);
104 
105         String out = response.getContentAsString();
106         assertTrue(out.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
107         assertTrue(out.indexOf("<html xmlns=\"http://www.w3.org/TR/xhtml1/strict\"") > -1);
108         assertTrue(out.indexOf("Hello Santa Claus how are you?") > -1);
109     }
110     
111     public void testTransform3() throws Exception {
112         result.setParse(false);
113         result.setLocation("XSLTResultTest3.xsl");
114         result.execute(mai);
115 
116         String out = response.getContentAsString();
117         assertTrue(out.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
118         assertTrue(out.indexOf("<html xmlns=\"http://www.w3.org/TR/xhtml1/strict\"") > -1);
119         assertTrue(out.indexOf("Hello Santa Claus how are you?") > -1);
120         assertTrue(out.indexOf("WebWork in Action by Patrick and Jason") > -1);
121         assertTrue(out.indexOf("XWork not in Action by Superman") > -1);
122     }
123     
124     public void testTransformWithBoolean() throws Exception {
125         result.setParse(false);
126         result.setLocation("XSLTResultTest5.xsl");
127         result.execute(mai);
128 
129         String out = response.getContentAsString();
130         assertTrue(out.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
131         assertTrue(out.indexOf("<html xmlns=\"http://www.w3.org/TR/xhtml1/strict\"") > -1);
132         assertTrue(out.indexOf("Hello Santa Claus how are you?") > -1);
133         assertTrue(out.indexOf("You are active: true") > -1);
134     }
135     
136     public void testTransform4WithDocumentInclude() throws Exception {
137         result = new XSLTResult(){
138             protected URIResolver getURIResolver() {
139                 return new URIResolver() {
140                     public Source resolve(String href, String base) throws TransformerException {
141                         return new StreamSource(ClassLoaderUtils.getResourceAsStream(href, this.getClass()));
142                     }
143                     
144                 };
145             }
146             
147         };
148         result.setParse(false);
149         result.setLocation("XSLTResultTest4.xsl");
150         result.execute(mai);
151 
152         String out = response.getContentAsString();
153         assertTrue(out.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
154         assertTrue(out.indexOf("<validators>") > -1);
155     }
156 
157     public void testTransform4WithBadDocumentInclude() throws Exception {
158         result = new XSLTResult(){
159             protected URIResolver getURIResolver() {
160                 return new URIResolver() {
161                     public Source resolve(String href, String base) throws TransformerException {
162                         return new StreamSource(ClassLoaderUtils.getResourceAsStream(href, this.getClass()));
163                     }
164 
165                 };
166             }
167 
168         };
169         result.setParse(false);
170         result.setLocation("XSLTResultTest4.badinclude.xsl");
171         try {
172             result.execute(mai);
173             fail("Should have thrown an exception");
174         } catch (Exception ex) {
175             assertEquals("Error transforming result", ex.getMessage());
176         }
177     }
178     
179     public void testTransformWithError() throws Exception {
180         result = new XSLTResult(){
181             protected URIResolver getURIResolver() {
182                 return new URIResolver() {
183                     public Source resolve(String href, String base) throws TransformerException {
184                         throw new TransformerException("Some random error");
185                     }
186                 };
187             }
188         };
189         result.setLocation("XSLTResultTest4.xsl");
190         try {
191             result.execute(mai);
192             fail("Should have thrown an exception");
193         } catch (Exception ex) {
194             assertEquals("Error transforming result", ex.getMessage());
195         }
196     }
197     
198     protected void setUp() throws Exception {
199         super.setUp();
200         request = new MockHttpServletRequest();
201         response = new MockHttpServletResponse();
202         servletContext = new MockServletContext();
203 
204         result = new XSLTResult();
205         stack = ActionContext.getContext().getValueStack();
206 
207         MyAction action = new MyAction();
208 
209         mai = new com.opensymphony.xwork2.mock.MockActionInvocation();
210         mai.setAction(action);
211         mai.setStack(stack);
212         mai.setInvocationContext(ActionContext.getContext());
213         stack.push(action);
214 
215         ActionContext.getContext().put(ServletActionContext.HTTP_REQUEST, request);
216         ActionContext.getContext().put(ServletActionContext.HTTP_RESPONSE, response);
217         ActionContext.getContext().put(ServletActionContext.SERVLET_CONTEXT, servletContext);
218     }
219 
220     protected void tearDown() throws Exception {
221         super.tearDown();
222         request = null;
223         response = null;
224         servletContext = null;
225         result = null;
226         stack = null;
227         mai = null;
228     }
229 
230     private class MyAction implements Action {
231 
232         public String execute() throws Exception {
233             return SUCCESS;
234         }
235 
236         public String getMyLocation() {
237             return ("XSLTResultTest.xsl");
238         }
239 
240         public String getUsername() {
241             return "Santa Claus";
242         }
243         
244         public boolean isActive() {
245             return true;
246         }
247 
248         public List getBooks() {
249             List list = new ArrayList();
250             list.add(new Book("WebWork in Action", "Patrick and Jason"));
251             list.add(null);
252             list.add(new Book("XWork not in Action", "Superman"));
253             return list;
254         }
255 
256     }
257 
258     public class Book {
259 
260         private String title;
261         private String author;
262 
263         public Book(String title, String author) {
264             this.title = title;
265             this.author = author;
266         }
267 
268         public String getTitle() {
269             return title;
270         }
271 
272         public String getAuthor() {
273             return author;
274         }
275     }
276 }