001: /*
002: * Copyright (c) 2002-2006 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.views.xslt;
006:
007: import com.opensymphony.webwork.ServletActionContext;
008: import com.opensymphony.xwork.Action;
009: import com.opensymphony.xwork.ActionContext;
010: import com.opensymphony.xwork.mock.MockActionInvocation;
011: import com.opensymphony.xwork.util.OgnlValueStack;
012: import junit.framework.TestCase;
013: import org.springframework.core.io.DefaultResourceLoader;
014: import org.springframework.mock.web.MockHttpServletRequest;
015: import org.springframework.mock.web.MockHttpServletResponse;
016: import org.springframework.mock.web.MockServletContext;
017:
018: import javax.xml.transform.TransformerException;
019: import java.util.List;
020: import java.util.ArrayList;
021:
022: /**
023: * Unit test for {@link XSLTResult}.
024: *
025: * @author Claus Ibsen
026: */
027: public class XSLTResultTest extends TestCase {
028:
029: private XSLTResult result;
030: private MockHttpServletResponse response;
031: private MockHttpServletRequest request;
032: private MockServletContext servletContext;
033: private MockActionInvocation mai;
034: private OgnlValueStack stack;
035:
036: public void testNoLocation() throws Exception {
037: try {
038: result.setParse(false);
039: result.setLocation(null);
040: result.execute(mai);
041: fail("Should have thrown an IllegalArgumentException");
042: } catch (IllegalArgumentException e) {
043: // success
044: }
045: }
046:
047: public void testNoFileFound() throws Exception {
048: try {
049: result.setParse(false);
050: result.setLocation("nofile.xsl");
051: result.execute(mai);
052: fail("Should have thrown a TransformerException");
053: } catch (TransformerException e) {
054: // success
055: }
056: }
057:
058: public void testSimpleTransform() throws Exception {
059: // TODO: XSLTResult does not work with JDK1.5
060:
061: result.setParse(false);
062: result.setLocation("XSLTResultTest.xsl");
063: result.execute(mai);
064:
065: String out = response.getContentAsString();
066: assertTrue(out
067: .startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
068: assertTrue(out
069: .indexOf("<result xmlns=\"http://www.w3.org/TR/xhtml1/strict\"") > -1);
070: }
071:
072: public void testSimpleTransformParse() throws Exception {
073: result.setParse(true);
074: result.setLocation("${top.myLocation}");
075: result.execute(mai);
076:
077: String out = response.getContentAsString();
078: assertTrue(out
079: .startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
080: assertTrue(out
081: .indexOf("<result xmlns=\"http://www.w3.org/TR/xhtml1/strict\"") > -1);
082: }
083:
084: public void testTransform2() throws Exception {
085: result.setParse(false);
086: result.setLocation("XSLTResultTest2.xsl");
087: result.execute(mai);
088:
089: String out = response.getContentAsString();
090: assertTrue(out
091: .startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
092: assertTrue(out
093: .indexOf("<html xmlns=\"http://www.w3.org/TR/xhtml1/strict\"") > -1);
094: assertTrue(out.indexOf("Hello Santa Claus how are you?") > -1);
095: }
096:
097: public void testTransform3() throws Exception {
098: result.setParse(false);
099: result.setLocation("XSLTResultTest3.xsl");
100: result.execute(mai);
101:
102: String out = response.getContentAsString();
103: assertTrue(out
104: .startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
105: assertTrue(out
106: .indexOf("<html xmlns=\"http://www.w3.org/TR/xhtml1/strict\"") > -1);
107: assertTrue(out.indexOf("Hello Santa Claus how are you?") > -1);
108: assertTrue(out
109: .indexOf("WebWork in Action by Patrick and Jason") > -1);
110: // TODO: There is a bug in XSLTResult and having collections
111: assertTrue(out.indexOf("XWork not in Action by Superman") > -1);
112: }
113:
114: protected void setUp() throws Exception {
115: request = new MockHttpServletRequest();
116: response = new MockHttpServletResponse();
117: servletContext = new MockServletContext(
118: new DefaultResourceLoader());
119:
120: result = new XSLTResult();
121: stack = new OgnlValueStack();
122: ActionContext.getContext().setValueStack(stack);
123:
124: MyAction action = new MyAction();
125:
126: mai = new com.opensymphony.xwork.mock.MockActionInvocation();
127: mai.setAction(action);
128: mai.setStack(stack);
129: mai.setInvocationContext(ActionContext.getContext());
130: stack.push(action);
131:
132: ActionContext.getContext().put(
133: ServletActionContext.HTTP_REQUEST, request);
134: ActionContext.getContext().put(
135: ServletActionContext.HTTP_RESPONSE, response);
136: ActionContext.getContext().put(
137: ServletActionContext.SERVLET_CONTEXT, servletContext);
138: }
139:
140: protected void tearDown() {
141: request = null;
142: response = null;
143: servletContext = null;
144: result = null;
145: stack = null;
146: mai = null;
147: }
148:
149: private class MyAction implements Action {
150:
151: public String execute() throws Exception {
152: return SUCCESS;
153: }
154:
155: public String getMyLocation() {
156: return ("XSLTResultTest.xsl");
157: }
158:
159: public String getUsername() {
160: return "Santa Claus";
161: }
162:
163: public List getBooks() {
164: List list = new ArrayList();
165: list
166: .add(new Book("WebWork in Action",
167: "Patrick and Jason"));
168: list.add(new Book("XWork not in Action", "Superman"));
169: return list;
170: }
171:
172: }
173:
174: public class Book {
175:
176: private String title;
177: private String author;
178:
179: public Book(String title, String author) {
180: this .title = title;
181: this .author = author;
182: }
183:
184: public String getTitle() {
185: return title;
186: }
187:
188: public String getAuthor() {
189: return author;
190: }
191: }
192:
193: }
|