1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.struts2.interceptor.validation;
23
24 import java.io.PrintWriter;
25 import java.io.StringWriter;
26 import java.util.HashMap;
27 import java.util.Map;
28
29 import javax.servlet.http.HttpServletResponse;
30
31 import org.apache.struts2.StrutsStatics;
32 import org.apache.struts2.StrutsTestCase;
33 import org.apache.struts2.TestUtils;
34 import org.apache.struts2.views.jsp.StrutsMockHttpServletRequest;
35 import org.apache.struts2.views.jsp.StrutsMockHttpServletResponse;
36 import org.apache.struts2.views.jsp.StrutsMockServletContext;
37
38 import com.opensymphony.xwork2.Action;
39 import com.opensymphony.xwork2.ActionContext;
40 import com.opensymphony.xwork2.ActionSupport;
41 import com.opensymphony.xwork2.config.entities.ActionConfig;
42 import com.opensymphony.xwork2.mock.MockActionInvocation;
43 import com.opensymphony.xwork2.mock.MockActionProxy;
44 import com.opensymphony.xwork2.util.ValueStack;
45 import com.opensymphony.xwork2.util.ValueStackFactory;
46 import com.opensymphony.xwork2.validator.annotations.EmailValidator;
47 import com.opensymphony.xwork2.validator.annotations.IntRangeFieldValidator;
48 import com.opensymphony.xwork2.validator.annotations.StringLengthFieldValidator;
49 import com.opensymphony.xwork2.validator.annotations.Validation;
50
51 public class JSONValidationInterceptorTest extends StrutsTestCase {
52 private MockActionInvocation invocation;
53 private StringWriter stringWriter;
54 private TestAction action;
55 private StrutsMockHttpServletResponse response;
56 private JSONValidationInterceptor interceptor;
57 private StrutsMockHttpServletRequest request;
58 private AnnotationValidationInterceptor validationInterceptor;
59
60 public void testValidationFails() throws Exception {
61
62 action.addActionError("General error");
63
64 Map parameters = new HashMap();
65 parameters.put("struts.enableJSONValidation", "true");
66 request.setParameterMap(parameters);
67
68 validationInterceptor.intercept(invocation);
69 interceptor.setValidationFailedStatus(HttpServletResponse.SC_BAD_REQUEST);
70 interceptor.intercept(invocation);
71
72 String json = stringWriter.toString();
73
74 String normalizedActual = TestUtils.normalize(json, true);
75 String normalizedExpected = TestUtils
76 .normalize(JSONValidationInterceptorTest.class.getResource("json-1.txt"));
77
78 assertEquals(normalizedExpected, normalizedActual);
79
80 assertFalse(action.isExecuted());
81
82 assertEquals(HttpServletResponse.SC_BAD_REQUEST, response.getStatus());
83 assertEquals("application/json", response.getContentType());
84 assertEquals("UTF-8", response.getCharacterEncoding());
85 }
86
87 public void testValidationSucceeds() throws Exception {
88 JSONValidationInterceptor interceptor = new JSONValidationInterceptor();
89
90 action.setText("abcd@ggg.com");
91 action.setValue(10);
92
93 Map parameters = new HashMap();
94 parameters.put("struts.enableJSONValidation", "true");
95 request.setParameterMap(parameters);
96
97 validationInterceptor.intercept(invocation);
98 interceptor.intercept(invocation);
99
100 String json = stringWriter.toString();
101
102 String normalizedActual = TestUtils.normalize(json, true);
103 assertEquals("", normalizedActual);
104 }
105
106 public void testValidationSucceedsValidateOnly() throws Exception {
107 JSONValidationInterceptor interceptor = new JSONValidationInterceptor();
108
109 action.setText("abcd@ggg.com");
110 action.setValue(10);
111
112
113 Map parameters = new HashMap();
114 parameters.put("struts.validateOnly", "true");
115 parameters.put("struts.enableJSONValidation", "true");
116 request.setParameterMap(parameters);
117
118 validationInterceptor.intercept(invocation);
119 interceptor.intercept(invocation);
120
121 String json = stringWriter.toString();
122
123 String normalizedActual = TestUtils.normalize(json, true);
124 assertEquals("/*{}*/", normalizedActual);
125 assertFalse(action.isExecuted());
126 assertEquals("application/json", response.getContentType());
127 assertEquals("UTF-8", response.getCharacterEncoding());
128 }
129
130 protected void setUp() throws Exception {
131 super.setUp();
132 ActionConfig config = new ActionConfig.Builder("", "name", "").build();
133 this.action = new TestAction();
134 this.interceptor = new JSONValidationInterceptor();
135 this.validationInterceptor = new AnnotationValidationInterceptor();
136 container.inject(validationInterceptor);
137 this.request = new StrutsMockHttpServletRequest();
138 stringWriter = new StringWriter();
139 PrintWriter writer = new PrintWriter(stringWriter);
140 this.response = new StrutsMockHttpServletResponse();
141 response.setWriter(writer);
142
143 ActionContext context = ActionContext.getContext();
144
145 context.put(StrutsStatics.HTTP_REQUEST, request);
146 context.put(StrutsStatics.HTTP_RESPONSE, response);
147
148 StrutsMockServletContext servletContext = new StrutsMockServletContext();
149
150 context.put(StrutsStatics.SERVLET_CONTEXT, servletContext);
151 invocation = new MockActionInvocation();
152 ActionContext.getContext().setActionInvocation(invocation);
153 invocation.setAction(action);
154 invocation.setInvocationContext(context);
155 MockActionProxy proxy = new MockActionProxy();
156 proxy.setMethod("execute");
157 proxy.setAction(action);
158 proxy.setConfig(config);
159 invocation.setProxy(proxy);
160 }
161
162 @Validation
163 public static class TestAction extends ActionSupport {
164 private String text = "x";
165 private int value = -10;
166 private boolean executed = false;
167
168 public String execute() {
169 executed = true;
170 return Action.SUCCESS;
171 }
172
173 @SkipValidation
174 public String skipMe() {
175 return "skipme";
176 }
177
178 public String getText() {
179 return text;
180 }
181
182 @StringLengthFieldValidator(minLength = "2", message = "Too short")
183 @EmailValidator(message = "This is no email")
184 public void setText(String text) {
185 this.text = text;
186 }
187
188 public int getValue() {
189 return value;
190 }
191
192 @IntRangeFieldValidator(min = "-1", message = "Min value is -1")
193 public void setValue(int value) {
194 this.value = value;
195 }
196
197 public boolean isExecuted() {
198 return executed;
199 }
200 }
201 }