View Javadoc

1   /*
2    * $Id: RestWorkflowInterceptor.java 666756 2008-06-11 18:11:00Z hermanns $
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.rest;
22  
23  import com.mockobjects.dynamic.AnyConstraintMatcher;
24  import com.mockobjects.dynamic.Mock;
25  import com.opensymphony.xwork2.ActionContext;
26  import com.opensymphony.xwork2.ActionInvocation;
27  import com.opensymphony.xwork2.ActionProxy;
28  import com.opensymphony.xwork2.ActionSupport;
29  import junit.framework.TestCase;
30  import org.apache.struts2.ServletActionContext;
31  import org.apache.struts2.dispatcher.mapper.ActionMapping;
32  
33  import java.util.HashMap;
34  
35  public class RestWorkflowInterceptorTest extends TestCase {
36  
37      public void testCustomValidationFailureStatusCode() throws Exception {
38          RestWorkflowInterceptor wf = new RestWorkflowInterceptor();
39  
40          ActionSupport action = new ActionSupport();
41          action.addActionError("some error");
42  
43          wf.setValidationFailureStatusCode("666");
44          Mock mockActionInvocation = new Mock(ActionInvocation.class);
45          Mock mockActionProxy = new Mock(ActionProxy.class);
46          mockActionProxy.expectAndReturn("getConfig", null);
47          mockActionInvocation.expectAndReturn("getProxy", mockActionProxy.proxy());
48          mockActionInvocation.expectAndReturn("getAction", action);
49          Mock mockContentTypeHandlerManager = new Mock(ContentTypeHandlerManager.class);
50          mockContentTypeHandlerManager.expectAndReturn("handleResult", new AnyConstraintMatcher() {
51              public boolean matches(Object[] args) {
52                  DefaultHttpHeaders headers = (DefaultHttpHeaders) args[1];
53                  return 666 == headers.status;
54              }
55          }, null);
56          wf.setContentTypeHandlerManager((ContentTypeHandlerManager) mockContentTypeHandlerManager.proxy());
57  
58          ActionContext.setContext(new ActionContext(new HashMap() {{
59              put(ServletActionContext.ACTION_MAPPING, new ActionMapping());
60          }}));
61          wf.doIntercept((ActionInvocation) mockActionInvocation.proxy());
62          mockContentTypeHandlerManager.verify();
63          mockActionInvocation.verify();
64      }
65  }