View Javadoc

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