1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.struts2.views.xslt;
19
20 import java.util.ArrayList;
21 import java.util.List;
22
23 import javax.xml.transform.TransformerException;
24
25 import org.apache.struts2.ServletActionContext;
26 import org.apache.struts2.StrutsTestCase;
27 import org.springframework.core.io.DefaultResourceLoader;
28 import org.springframework.mock.web.MockHttpServletRequest;
29 import org.springframework.mock.web.MockHttpServletResponse;
30 import org.springframework.mock.web.MockServletContext;
31
32 import com.opensymphony.xwork2.Action;
33 import com.opensymphony.xwork2.ActionContext;
34 import com.opensymphony.xwork2.mock.MockActionInvocation;
35 import com.opensymphony.xwork2.util.ValueStack;
36 import com.opensymphony.xwork2.util.ValueStackFactory;
37
38 /***
39 * Unit test for {@link XSLTResult}.
40 *
41 */
42 public class XSLTResultTest extends StrutsTestCase {
43
44 private XSLTResult result;
45 private MockHttpServletResponse response;
46 private MockHttpServletRequest request;
47 private MockServletContext servletContext;
48 private MockActionInvocation mai;
49 private ValueStack stack;
50
51 public void testNoLocation() throws Exception {
52 try {
53 result.setParse(false);
54 result.setLocation(null);
55 result.execute(mai);
56 fail("Should have thrown an IllegalArgumentException");
57 } catch (IllegalArgumentException e) {
58
59 }
60 }
61
62 public void testNoFileFound() throws Exception {
63 try {
64 result.setParse(false);
65 result.setLocation("nofile.xsl");
66 result.execute(mai);
67 fail("Should have thrown a TransformerException");
68 } catch (TransformerException e) {
69
70 }
71 }
72
73 public void testSimpleTransform() throws Exception {
74 result.setParse(false);
75 result.setLocation("XSLTResultTest.xsl");
76 result.execute(mai);
77
78 String out = response.getContentAsString();
79 assertTrue(out.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
80 assertTrue(out.indexOf("<result xmlns=\"http://www.w3.org/TR/xhtml1/strict\"") > -1);
81 }
82
83 public void testSimpleTransformParse() throws Exception {
84 result.setParse(true);
85 result.setLocation("${top.myLocation}");
86 result.execute(mai);
87
88 String out = response.getContentAsString();
89 assertTrue(out.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
90 assertTrue(out.indexOf("<result xmlns=\"http://www.w3.org/TR/xhtml1/strict\"") > -1);
91 }
92
93 public void testTransform2() throws Exception {
94 result.setParse(false);
95 result.setLocation("XSLTResultTest2.xsl");
96 result.execute(mai);
97
98 String out = response.getContentAsString();
99 assertTrue(out.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
100 assertTrue(out.indexOf("<html xmlns=\"http://www.w3.org/TR/xhtml1/strict\"") > -1);
101 assertTrue(out.indexOf("Hello Santa Claus how are you?") > -1);
102 }
103
104 public void testTransform3() throws Exception {
105 result.setParse(false);
106 result.setLocation("XSLTResultTest3.xsl");
107 result.execute(mai);
108
109 String out = response.getContentAsString();
110 assertTrue(out.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
111 assertTrue(out.indexOf("<html xmlns=\"http://www.w3.org/TR/xhtml1/strict\"") > -1);
112 assertTrue(out.indexOf("Hello Santa Claus how are you?") > -1);
113 assertTrue(out.indexOf("WebWork in Action by Patrick and Jason") > -1);
114 assertTrue(out.indexOf("XWork not in Action by Superman") > -1);
115 }
116
117 protected void setUp() throws Exception {
118 request = new MockHttpServletRequest();
119 response = new MockHttpServletResponse();
120 servletContext = new MockServletContext(new DefaultResourceLoader());
121
122 result = new XSLTResult();
123 stack = ValueStackFactory.getFactory().createValueStack();
124 ActionContext.getContext().setValueStack(stack);
125
126 MyAction action = new MyAction();
127
128 mai = new com.opensymphony.xwork2.mock.MockActionInvocation();
129 mai.setAction(action);
130 mai.setStack(stack);
131 mai.setInvocationContext(ActionContext.getContext());
132 stack.push(action);
133
134 ActionContext.getContext().put(ServletActionContext.HTTP_REQUEST, request);
135 ActionContext.getContext().put(ServletActionContext.HTTP_RESPONSE, response);
136 ActionContext.getContext().put(ServletActionContext.SERVLET_CONTEXT, servletContext);
137 }
138
139 protected void tearDown() {
140 request = null;
141 response = null;
142 servletContext = null;
143 result = null;
144 stack = null;
145 mai = null;
146 }
147
148 private class MyAction implements Action {
149
150 public String execute() throws Exception {
151 return SUCCESS;
152 }
153
154 public String getMyLocation() {
155 return ("XSLTResultTest.xsl");
156 }
157
158 public String getUsername() {
159 return "Santa Claus";
160 }
161
162 public List getBooks() {
163 List list = new ArrayList();
164 list.add(new Book("WebWork in Action", "Patrick and Jason"));
165 list.add(new Book("XWork not in Action", "Superman"));
166 return list;
167 }
168
169 }
170
171 public class Book {
172
173 private String title;
174 private String author;
175
176 public Book(String title, String author) {
177 this.title = title;
178 this.author = author;
179 }
180
181 public String getTitle() {
182 return title;
183 }
184
185 public String getAuthor() {
186 return author;
187 }
188 }
189 }