001: /*
002: * $Id: XSLTResultTest.java 481180 2006-12-01 08:02:59Z mrdon $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.views.xslt;
022:
023: import java.util.ArrayList;
024: import java.util.List;
025:
026: import javax.xml.transform.Source;
027: import javax.xml.transform.TransformerException;
028: import javax.xml.transform.URIResolver;
029: import javax.xml.transform.stream.StreamSource;
030:
031: import org.apache.struts2.ServletActionContext;
032: import org.apache.struts2.StrutsTestCase;
033: import org.apache.struts2.util.ClassLoaderUtils;
034: import org.springframework.mock.web.MockHttpServletRequest;
035: import org.springframework.mock.web.MockHttpServletResponse;
036: import org.springframework.mock.web.MockServletContext;
037:
038: import com.opensymphony.xwork2.Action;
039: import com.opensymphony.xwork2.ActionContext;
040: import com.opensymphony.xwork2.mock.MockActionInvocation;
041: import com.opensymphony.xwork2.util.ValueStack;
042: import com.opensymphony.xwork2.util.ValueStackFactory;
043:
044: /**
045: * Unit test for {@link XSLTResult}.
046: *
047: */
048: public class XSLTResultTest extends StrutsTestCase {
049:
050: private XSLTResult result;
051: private MockHttpServletResponse response;
052: private MockHttpServletRequest request;
053: private MockServletContext servletContext;
054: private MockActionInvocation mai;
055: private ValueStack stack;
056:
057: public void testNoLocation() throws Exception {
058: try {
059: result.setParse(false);
060: result.setLocation(null);
061: result.execute(mai);
062: fail("Should have thrown an IllegalArgumentException");
063: } catch (IllegalArgumentException e) {
064: // success
065: }
066: }
067:
068: public void testNoFileFound() throws Exception {
069: try {
070: result.setParse(false);
071: result.setLocation("nofile.xsl");
072: result.execute(mai);
073: fail("Should have thrown a TransformerException");
074: } catch (TransformerException e) {
075: // success
076: }
077: }
078:
079: public void testSimpleTransform() throws Exception {
080: result.setParse(false);
081: result.setLocation("XSLTResultTest.xsl");
082: result.execute(mai);
083:
084: String out = response.getContentAsString();
085: assertTrue(out
086: .startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
087: assertTrue(out
088: .indexOf("<result xmlns=\"http://www.w3.org/TR/xhtml1/strict\"") > -1);
089: }
090:
091: public void testSimpleTransformParse() throws Exception {
092: result.setParse(true);
093: result.setLocation("${top.myLocation}");
094: result.execute(mai);
095:
096: String out = response.getContentAsString();
097: assertTrue(out
098: .startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
099: assertTrue(out
100: .indexOf("<result xmlns=\"http://www.w3.org/TR/xhtml1/strict\"") > -1);
101: }
102:
103: public void testTransform2() throws Exception {
104: result.setParse(false);
105: result.setLocation("XSLTResultTest2.xsl");
106: result.execute(mai);
107:
108: String out = response.getContentAsString();
109: assertTrue(out
110: .startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
111: assertTrue(out
112: .indexOf("<html xmlns=\"http://www.w3.org/TR/xhtml1/strict\"") > -1);
113: assertTrue(out.indexOf("Hello Santa Claus how are you?") > -1);
114: }
115:
116: public void testTransform3() throws Exception {
117: result.setParse(false);
118: result.setLocation("XSLTResultTest3.xsl");
119: result.execute(mai);
120:
121: String out = response.getContentAsString();
122: assertTrue(out
123: .startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
124: assertTrue(out
125: .indexOf("<html xmlns=\"http://www.w3.org/TR/xhtml1/strict\"") > -1);
126: assertTrue(out.indexOf("Hello Santa Claus how are you?") > -1);
127: assertTrue(out
128: .indexOf("WebWork in Action by Patrick and Jason") > -1);
129: assertTrue(out.indexOf("XWork not in Action by Superman") > -1);
130: }
131:
132: public void testTransform4WithDocumentInclude() throws Exception {
133: result = new XSLTResult() {
134: protected URIResolver getURIResolver() {
135: return new URIResolver() {
136: public Source resolve(String href, String base)
137: throws TransformerException {
138: return new StreamSource(ClassLoaderUtils
139: .getResourceAsStream(href, this
140: .getClass()));
141: }
142:
143: };
144: }
145:
146: };
147: result.setParse(false);
148: result.setLocation("XSLTResultTest4.xsl");
149: result.execute(mai);
150:
151: String out = response.getContentAsString();
152: assertTrue(out
153: .startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
154: assertTrue(out.indexOf("<validators>") > -1);
155: }
156:
157: protected void setUp() throws Exception {
158: request = new MockHttpServletRequest();
159: response = new MockHttpServletResponse();
160: servletContext = new MockServletContext();
161:
162: result = new XSLTResult();
163: stack = ValueStackFactory.getFactory().createValueStack();
164: ActionContext.getContext().setValueStack(stack);
165:
166: MyAction action = new MyAction();
167:
168: mai = new com.opensymphony.xwork2.mock.MockActionInvocation();
169: mai.setAction(action);
170: mai.setStack(stack);
171: mai.setInvocationContext(ActionContext.getContext());
172: stack.push(action);
173:
174: ActionContext.getContext().put(
175: ServletActionContext.HTTP_REQUEST, request);
176: ActionContext.getContext().put(
177: ServletActionContext.HTTP_RESPONSE, response);
178: ActionContext.getContext().put(
179: ServletActionContext.SERVLET_CONTEXT, servletContext);
180: }
181:
182: protected void tearDown() {
183: request = null;
184: response = null;
185: servletContext = null;
186: result = null;
187: stack = null;
188: mai = null;
189: }
190:
191: private class MyAction implements Action {
192:
193: public String execute() throws Exception {
194: return SUCCESS;
195: }
196:
197: public String getMyLocation() {
198: return ("XSLTResultTest.xsl");
199: }
200:
201: public String getUsername() {
202: return "Santa Claus";
203: }
204:
205: public List getBooks() {
206: List list = new ArrayList();
207: list
208: .add(new Book("WebWork in Action",
209: "Patrick and Jason"));
210: list.add(new Book("XWork not in Action", "Superman"));
211: return list;
212: }
213:
214: }
215:
216: public class Book {
217:
218: private String title;
219: private String author;
220:
221: public Book(String title, String author) {
222: this .title = title;
223: this .author = author;
224: }
225:
226: public String getTitle() {
227: return title;
228: }
229:
230: public String getAuthor() {
231: return author;
232: }
233: }
234: }
|