View Javadoc

1   /*
2    * $Id: XSLTResultTest.java 651946 2008-04-27 13:41:38Z apetrelli $
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 testTransformWithError() 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                         throw new TransformerException("Some random error");
163                     }
164                 };
165             }
166         };
167         result.setLocation("XSLTResultTest4.xsl");
168         try {
169             result.execute(mai);
170             fail("Should have thrown an exception");
171         } catch (Exception ex) {
172             assertEquals("Error transforming result", ex.getMessage());
173         }
174     }
175     
176     protected void setUp() throws Exception {
177         super.setUp();
178         request = new MockHttpServletRequest();
179         response = new MockHttpServletResponse();
180         servletContext = new MockServletContext();
181 
182         result = new XSLTResult();
183         stack = ActionContext.getContext().getValueStack();
184 
185         MyAction action = new MyAction();
186 
187         mai = new com.opensymphony.xwork2.mock.MockActionInvocation();
188         mai.setAction(action);
189         mai.setStack(stack);
190         mai.setInvocationContext(ActionContext.getContext());
191         stack.push(action);
192 
193         ActionContext.getContext().put(ServletActionContext.HTTP_REQUEST, request);
194         ActionContext.getContext().put(ServletActionContext.HTTP_RESPONSE, response);
195         ActionContext.getContext().put(ServletActionContext.SERVLET_CONTEXT, servletContext);
196     }
197 
198     protected void tearDown() throws Exception {
199         super.tearDown();
200         request = null;
201         response = null;
202         servletContext = null;
203         result = null;
204         stack = null;
205         mai = null;
206     }
207 
208     private class MyAction implements Action {
209 
210         public String execute() throws Exception {
211             return SUCCESS;
212         }
213 
214         public String getMyLocation() {
215             return ("XSLTResultTest.xsl");
216         }
217 
218         public String getUsername() {
219             return "Santa Claus";
220         }
221         
222         public boolean isActive() {
223             return true;
224         }
225 
226         public List getBooks() {
227             List list = new ArrayList();
228             list.add(new Book("WebWork in Action", "Patrick and Jason"));
229             list.add(new Book("XWork not in Action", "Superman"));
230             return list;
231         }
232 
233     }
234 
235     public class Book {
236 
237         private String title;
238         private String author;
239 
240         public Book(String title, String author) {
241             this.title = title;
242             this.author = author;
243         }
244 
245         public String getTitle() {
246             return title;
247         }
248 
249         public String getAuthor() {
250             return author;
251         }
252     }
253 }