View Javadoc

1   /*
2    * $Id: AnnotationValidationInterceptorTest.java 805237 2009-08-18 00:36:15Z musachy $
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 org.apache.struts2.StrutsTestCase;
25  import org.easymock.EasyMock;
26  
27  import com.mockobjects.dynamic.Mock;
28  import com.opensymphony.xwork2.*;
29  import com.opensymphony.xwork2.config.entities.ActionConfig;
30  
31  public class AnnotationValidationInterceptorTest extends StrutsTestCase {
32  
33      private AnnotationValidationInterceptor interceptor = new AnnotationValidationInterceptor();
34      private Mock mockActionInvocation;
35      private Mock mockActionProxy;
36      private TestAction test;
37      private ActionConfig config;
38  
39      public void setUp() throws Exception {
40          super.setUp();
41          test = new TestAction();
42          interceptor = new AnnotationValidationInterceptor();
43          container.inject(interceptor);
44          config = new ActionConfig.Builder("", "foo", "").build();
45          mockActionInvocation = new Mock(ActionInvocation.class);
46          mockActionProxy = new Mock(ActionProxy.class);
47          mockActionInvocation.matchAndReturn("getProxy", (ActionProxy) mockActionProxy.proxy());
48          mockActionInvocation.matchAndReturn("getAction", test);
49          mockActionInvocation.expect("invoke");
50  
51          ActionContext.getContext().setActionInvocation((ActionInvocation) mockActionInvocation.proxy());
52      }
53  
54      public void testShouldNotSkip() throws Exception {
55          mockActionProxy.expectAndReturn("getMethod", "execute");
56          mockActionProxy.expectAndReturn("getActionName", "foo");
57          mockActionProxy.expectAndReturn("getMethod", "execute");
58          mockActionProxy.expectAndReturn("getConfig", config);
59          mockActionProxy.expectAndReturn("getMethod", "execute");
60          interceptor.doIntercept((ActionInvocation)mockActionInvocation.proxy());
61          mockActionProxy.verify();
62      }
63  
64      public void testShouldSkip() throws Exception {
65          mockActionProxy.expectAndReturn("getMethod", "skipMe");
66          interceptor.doIntercept((ActionInvocation)mockActionInvocation.proxy());
67          mockActionProxy.verify();
68      }
69  
70      public void testShouldSkipBase() throws Exception {
71          mockActionProxy.expectAndReturn("getMethod", "skipMeBase");
72          interceptor.doIntercept((ActionInvocation)mockActionInvocation.proxy());
73          mockActionProxy.verify();
74      }
75  
76      public void testShouldSkipBase2() throws Exception {
77          mockActionProxy.expectAndReturn("getMethod", "skipMeBase2");
78          interceptor.doIntercept((ActionInvocation)mockActionInvocation.proxy());
79          mockActionProxy.verify();
80      }
81  
82      public void testShouldSkip2() throws Exception {
83          mockActionProxy.expectAndReturn("getMethod", "skipMe2");
84          interceptor.doIntercept((ActionInvocation)mockActionInvocation.proxy());
85          mockActionProxy.verify();
86      }
87  
88      public void testShouldNotSkipBase() throws Exception {
89          mockActionProxy.expectAndReturn("getMethod", "dontSkipMeBase");
90          mockActionProxy.expectAndReturn("getActionName", "foo");
91          mockActionProxy.expectAndReturn("getMethod", "execute");
92          mockActionProxy.expectAndReturn("getConfig", config);
93          mockActionProxy.expectAndReturn("getMethod", "dontSkipMeBase");
94          interceptor.doIntercept((ActionInvocation)mockActionInvocation.proxy());
95          mockActionProxy.verify();
96      }
97  
98      public static class TestAction extends TestActionBase {
99  
100         public String execute() {
101             return "execute";
102         }
103 
104         @SkipValidation
105         public String skipMe() {
106             return "skipme";
107         }
108 
109         @SkipValidation
110         public String skipMe2() {
111             return "skipme2";
112         }
113 
114         public String skipMeBase() {
115             return "skipme";
116         }
117     }
118 
119     public static class TestActionBase  {
120 
121         @SkipValidation
122         public String skipMeBase() {
123             return "skipme";
124         }
125 
126         @SkipValidation
127         public String skipMeBase2() {
128             return "skipme";
129         }
130 
131         public String dontSkipMeBase() {
132             return "dontskipme";
133         }
134 
135         public String skipMe2() {
136             return "skipme2";
137         }
138     }
139 
140 }