View Javadoc

1   /*
2    * $Id: StrutsConversionErrorInterceptorTest.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;
23  
24  import java.util.HashMap;
25  import java.util.Map;
26  
27  import org.apache.struts2.StrutsTestCase;
28  
29  import com.mockobjects.dynamic.C;
30  import com.mockobjects.dynamic.Mock;
31  import com.opensymphony.xwork2.Action;
32  import com.opensymphony.xwork2.ActionContext;
33  import com.opensymphony.xwork2.ActionInvocation;
34  import com.opensymphony.xwork2.ActionSupport;
35  import com.opensymphony.xwork2.util.ValueStack;
36  import com.opensymphony.xwork2.util.ValueStackFactory;
37  
38  
39  /***
40   * StrutsConversionErrorInterceptorTest
41   *
42   */
43  public class StrutsConversionErrorInterceptorTest extends StrutsTestCase {
44  
45      protected ActionContext context;
46      protected ActionInvocation invocation;
47      protected Map conversionErrors;
48      protected Mock mockInvocation;
49      protected ValueStack stack;
50      protected StrutsConversionErrorInterceptor interceptor;
51  
52  
53      public void testEmptyValuesDoNotSetFieldErrors() throws Exception {
54          conversionErrors.put("foo", new Long(123));
55          conversionErrors.put("bar", "");
56          conversionErrors.put("baz", new String[]{""});
57  
58          ActionSupport action = new ActionSupport();
59          mockInvocation.expectAndReturn("getAction", action);
60          stack.push(action);
61          mockInvocation.matchAndReturn("getAction",action);
62          assertNull(action.getFieldErrors().get("foo"));
63          assertNull(action.getFieldErrors().get("bar"));
64          assertNull(action.getFieldErrors().get("baz"));
65          interceptor.intercept(invocation);
66          assertTrue(action.hasFieldErrors());
67          assertNotNull(action.getFieldErrors().get("foo"));
68          assertNull(action.getFieldErrors().get("bar"));
69          assertNull(action.getFieldErrors().get("baz"));
70      }
71  
72      public void testFieldErrorAdded() throws Exception {
73          conversionErrors.put("foo", new Long(123));
74  
75          ActionSupport action = new ActionSupport();
76          mockInvocation.expectAndReturn("getAction", action);
77          stack.push(action);
78          mockInvocation.matchAndReturn("getAction",action);
79          assertNull(action.getFieldErrors().get("foo"));
80          interceptor.intercept(invocation);
81          assertTrue(action.hasFieldErrors());
82          assertNotNull(action.getFieldErrors().get("foo"));
83      }
84  
85      protected void setUp() throws Exception {
86          super.setUp();
87          interceptor = new StrutsConversionErrorInterceptor();
88          mockInvocation = new Mock(ActionInvocation.class);
89          invocation = (ActionInvocation) mockInvocation.proxy();
90          stack = ActionContext.getContext().getValueStack();
91          context = new ActionContext(stack.getContext());
92          conversionErrors = new HashMap();
93          context.setConversionErrors(conversionErrors);
94          mockInvocation.matchAndReturn("getInvocationContext", context);
95          mockInvocation.expectAndReturn("invoke", Action.SUCCESS);
96          mockInvocation.expectAndReturn("getStack", stack);
97          mockInvocation.expect("addPreResultListener", C.ANY_ARGS);
98      }
99  }