01: /*
02: * $Id: StrutsConversionErrorInterceptorTest.java 471756 2006-11-06 15:01:43Z husted $
03: *
04: * Licensed to the Apache Software Foundation (ASF) under one
05: * or more contributor license agreements. See the NOTICE file
06: * distributed with this work for additional information
07: * regarding copyright ownership. The ASF licenses this file
08: * to you under the Apache License, Version 2.0 (the
09: * "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: * StrutsConversionErrorInterceptorTest
39: *
40: */
41: public class StrutsConversionErrorInterceptorTest extends
42: 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: public void testEmptyValuesDoNotSetFieldErrors() throws Exception {
52: conversionErrors.put("foo", new Long(123));
53: conversionErrors.put("bar", "");
54: conversionErrors.put("baz", new String[] { "" });
55:
56: ActionSupport action = new ActionSupport();
57: mockInvocation.expectAndReturn("getAction", action);
58: stack.push(action);
59: mockInvocation.matchAndReturn("getAction", action);
60: assertNull(action.getFieldErrors().get("foo"));
61: assertNull(action.getFieldErrors().get("bar"));
62: assertNull(action.getFieldErrors().get("baz"));
63: interceptor.intercept(invocation);
64: assertTrue(action.hasFieldErrors());
65: assertNotNull(action.getFieldErrors().get("foo"));
66: assertNull(action.getFieldErrors().get("bar"));
67: assertNull(action.getFieldErrors().get("baz"));
68: }
69:
70: public void testFieldErrorAdded() throws Exception {
71: conversionErrors.put("foo", new Long(123));
72:
73: ActionSupport action = new ActionSupport();
74: mockInvocation.expectAndReturn("getAction", action);
75: stack.push(action);
76: mockInvocation.matchAndReturn("getAction", action);
77: assertNull(action.getFieldErrors().get("foo"));
78: interceptor.intercept(invocation);
79: assertTrue(action.hasFieldErrors());
80: assertNotNull(action.getFieldErrors().get("foo"));
81: }
82:
83: protected void setUp() throws Exception {
84: super .setUp();
85: interceptor = new StrutsConversionErrorInterceptor();
86: mockInvocation = new Mock(ActionInvocation.class);
87: invocation = (ActionInvocation) mockInvocation.proxy();
88: stack = ValueStackFactory.getFactory().createValueStack();
89: context = new ActionContext(stack.getContext());
90: conversionErrors = new HashMap();
91: context.setConversionErrors(conversionErrors);
92: mockInvocation.matchAndReturn("getInvocationContext", context);
93: mockInvocation.expectAndReturn("invoke", Action.SUCCESS);
94: mockInvocation.expectAndReturn("getStack", stack);
95: mockInvocation.expect("addPreResultListener", C.ANY_ARGS);
96: }
97: }
|