View Javadoc

1   /*
2    * $Id: JSONValidationInterceptorTest.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.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.mock.MockActionInvocation;
42  import com.opensymphony.xwork2.mock.MockActionProxy;
43  import com.opensymphony.xwork2.util.ValueStack;
44  import com.opensymphony.xwork2.util.ValueStackFactory;
45  import com.opensymphony.xwork2.validator.annotations.EmailValidator;
46  import com.opensymphony.xwork2.validator.annotations.IntRangeFieldValidator;
47  import com.opensymphony.xwork2.validator.annotations.StringLengthFieldValidator;
48  import com.opensymphony.xwork2.validator.annotations.Validation;
49  
50  public class JSONValidationInterceptorTest extends StrutsTestCase {
51      private MockActionInvocation invocation;
52      private StringWriter stringWriter;
53      private TestAction action;
54      private StrutsMockHttpServletResponse response;
55      private JSONValidationInterceptor interceptor;
56      private StrutsMockHttpServletRequest request;
57      private AnnotationValidationInterceptor validationInterceptor;
58  
59      public void testValidationFails() throws Exception {
60          
61          action.addActionError("General error");
62          
63          Map parameters = new HashMap();
64          parameters.put("struts.enableJSONValidation", "true");
65          request.setParameterMap(parameters);
66          
67          validationInterceptor.intercept(invocation);
68          interceptor.setValidationFailedStatus(HttpServletResponse.SC_BAD_REQUEST);
69          interceptor.intercept(invocation);
70  
71          String json = stringWriter.toString();
72  
73          String normalizedActual = TestUtils.normalize(json, true);
74          String normalizedExpected = TestUtils
75              .normalize(JSONValidationInterceptorTest.class.getResource("json-1.txt"));
76          //json
77          assertEquals(normalizedExpected, normalizedActual);
78          //execution
79          assertFalse(action.isExecuted());
80          //http status
81          assertEquals(HttpServletResponse.SC_BAD_REQUEST, response.getStatus());
82          assertEquals("application/json", response.getContentType());
83  
84      }
85  
86      public void testValidationSucceeds() throws Exception {
87          JSONValidationInterceptor interceptor = new JSONValidationInterceptor();
88  
89          action.setText("abcd@ggg.com");
90          action.setValue(10);
91          
92          Map parameters = new HashMap();
93          parameters.put("struts.enableJSONValidation", "true");
94          request.setParameterMap(parameters);
95  
96          validationInterceptor.intercept(invocation);
97          interceptor.intercept(invocation);
98  
99          String json = stringWriter.toString();
100 
101         String normalizedActual = TestUtils.normalize(json, true);
102         assertEquals("", normalizedActual);
103     }
104     
105     public void testValidationSucceedsValidateOnly() throws Exception {
106         JSONValidationInterceptor interceptor = new JSONValidationInterceptor();
107 
108         action.setText("abcd@ggg.com");
109         action.setValue(10);
110 
111         //just validate
112         Map parameters = new HashMap();
113         parameters.put("struts.validateOnly", "true");
114         parameters.put("struts.enableJSONValidation", "true");
115         request.setParameterMap(parameters);
116         
117         validationInterceptor.intercept(invocation);
118         interceptor.intercept(invocation);
119 
120         String json = stringWriter.toString();
121 
122         String normalizedActual = TestUtils.normalize(json, true);
123         assertEquals("/*{}*/", normalizedActual);
124         assertFalse(action.isExecuted());
125         assertEquals("application/json", response.getContentType());
126     }
127 
128     protected void setUp() throws Exception {
129         super.setUp();
130         this.action = new TestAction();
131         this.interceptor = new JSONValidationInterceptor();
132         this.validationInterceptor = new AnnotationValidationInterceptor();
133         container.inject(validationInterceptor);
134         this.request = new StrutsMockHttpServletRequest();
135         stringWriter = new StringWriter();
136         PrintWriter writer = new PrintWriter(stringWriter);
137         this.response = new StrutsMockHttpServletResponse();
138         response.setWriter(writer);
139 
140         ActionContext context = ActionContext.getContext();
141 
142         context.put(StrutsStatics.HTTP_REQUEST, request);
143         context.put(StrutsStatics.HTTP_RESPONSE, response);
144 
145         StrutsMockServletContext servletContext = new StrutsMockServletContext();
146 
147         context.put(StrutsStatics.SERVLET_CONTEXT, servletContext);
148         invocation = new MockActionInvocation(); 
149         invocation.setAction(action);
150         invocation.setInvocationContext(context);
151         MockActionProxy proxy = new MockActionProxy();
152         proxy.setMethod("execute");
153         proxy.setAction(action);
154         invocation.setProxy(proxy);
155     }
156 
157     @Validation
158     public static class TestAction extends ActionSupport {
159         private String text = "x";
160         private int value = -10;
161         private boolean executed = false;
162         
163         public String execute() {
164             executed = true;
165             return Action.SUCCESS;
166         }
167 
168         @SkipValidation
169         public String skipMe() {
170             return "skipme";
171         }
172 
173         public String getText() {
174             return text;
175         }
176 
177         @StringLengthFieldValidator(minLength = "2", message = "Too short")
178         @EmailValidator(message = "This is no email")
179         public void setText(String text) {
180             this.text = text;
181         }
182 
183         public int getValue() {
184             return value;
185         }
186 
187         @IntRangeFieldValidator(min = "-1", message = "Min value is -1")
188         public void setValue(int value) {
189             this.value = value;
190         }
191 
192         public boolean isExecuted() {
193             return executed;
194         }
195     }
196 }