View Javadoc

1   /*
2    * $Id: StrutsConversionErrorInterceptorTest.java 451544 2006-09-30 05:38:02Z mrdon $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts2.interceptor;
19  
20  import java.util.HashMap;
21  import java.util.Map;
22  
23  import org.apache.struts2.StrutsTestCase;
24  
25  import com.mockobjects.dynamic.C;
26  import com.mockobjects.dynamic.Mock;
27  import com.opensymphony.xwork2.Action;
28  import com.opensymphony.xwork2.ActionContext;
29  import com.opensymphony.xwork2.ActionInvocation;
30  import com.opensymphony.xwork2.ActionSupport;
31  import com.opensymphony.xwork2.util.ValueStack;
32  import com.opensymphony.xwork2.util.ValueStackFactory;
33  
34  
35  /***
36   * StrutsConversionErrorInterceptorTest
37   *
38   */
39  public class StrutsConversionErrorInterceptorTest extends StrutsTestCase {
40  
41      protected ActionContext context;
42      protected ActionInvocation invocation;
43      protected Map conversionErrors;
44      protected Mock mockInvocation;
45      protected ValueStack stack;
46      protected StrutsConversionErrorInterceptor interceptor;
47  
48  
49      public void testEmptyValuesDoNotSetFieldErrors() throws Exception {
50          conversionErrors.put("foo", new Long(123));
51          conversionErrors.put("bar", "");
52          conversionErrors.put("baz", new String[]{""});
53  
54          ActionSupport action = new ActionSupport();
55          mockInvocation.expectAndReturn("getAction", action);
56          stack.push(action);
57          mockInvocation.matchAndReturn("getAction",action);
58          assertNull(action.getFieldErrors().get("foo"));
59          assertNull(action.getFieldErrors().get("bar"));
60          assertNull(action.getFieldErrors().get("baz"));
61          interceptor.intercept(invocation);
62          assertTrue(action.hasFieldErrors());
63          assertNotNull(action.getFieldErrors().get("foo"));
64          assertNull(action.getFieldErrors().get("bar"));
65          assertNull(action.getFieldErrors().get("baz"));
66      }
67  
68      public void testFieldErrorAdded() throws Exception {
69          conversionErrors.put("foo", new Long(123));
70  
71          ActionSupport action = new ActionSupport();
72          mockInvocation.expectAndReturn("getAction", action);
73          stack.push(action);
74          mockInvocation.matchAndReturn("getAction",action);
75          assertNull(action.getFieldErrors().get("foo"));
76          interceptor.intercept(invocation);
77          assertTrue(action.hasFieldErrors());
78          assertNotNull(action.getFieldErrors().get("foo"));
79      }
80  
81      protected void setUp() throws Exception {
82          super.setUp();
83          interceptor = new StrutsConversionErrorInterceptor();
84          mockInvocation = new Mock(ActionInvocation.class);
85          invocation = (ActionInvocation) mockInvocation.proxy();
86          stack = ValueStackFactory.getFactory().createValueStack();
87          context = new ActionContext(stack.getContext());
88          conversionErrors = new HashMap();
89          context.setConversionErrors(conversionErrors);
90          mockInvocation.matchAndReturn("getInvocationContext", context);
91          mockInvocation.expectAndReturn("invoke", Action.SUCCESS);
92          mockInvocation.expectAndReturn("getStack", stack);
93          mockInvocation.expect("addPreResultListener", C.ANY_ARGS);
94      }
95  }