View Javadoc

1   /*
2    * $Id: CodebehindUnknownHandlerTest.java 493148 2007-01-05 19:20:09Z mrdon $
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.codebehind;
22  
23  import java.util.HashMap;
24  
25  import javax.servlet.ServletContext;
26  
27  import org.apache.struts2.StrutsTestCase;
28  import org.apache.struts2.config.NullResult;
29  import org.apache.struts2.dispatcher.ServletDispatcherResult;
30  
31  import com.mockobjects.dynamic.Mock;
32  import com.opensymphony.xwork2.ActionContext;
33  import com.opensymphony.xwork2.ActionInvocation;
34  import com.opensymphony.xwork2.ObjectFactory;
35  import com.opensymphony.xwork2.Result;
36  import com.opensymphony.xwork2.config.entities.ResultTypeConfig;
37  
38  public class CodebehindUnknownHandlerTest extends StrutsTestCase {
39  
40      CodebehindUnknownHandler handler;
41      Mock mockServletContext;
42      
43      public void setUp() throws Exception {
44          super.setUp();
45          mockServletContext = new Mock(ServletContext.class);
46          handler = new CodebehindUnknownHandler("codebehind-default", configuration);
47          handler.setPathPrefix("/");
48          handler.setObjectFactory(container.getInstance(ObjectFactory.class));
49          handler.setServletContext((ServletContext)mockServletContext.proxy());
50      }
51  
52      public void testBuildResult() {
53          ActionContext ctx = new ActionContext(new HashMap());
54          ResultTypeConfig config = new ResultTypeConfig("null", SomeResult.class.getName(), "location");
55          
56          Result result = handler.buildResult("/foo.jsp", "success", config, ctx);
57          assertNotNull(result);
58          assertTrue(result instanceof SomeResult);
59          assertEquals("/foo.jsp", ((SomeResult) result).location);
60          
61      }
62  
63      public void testString() {
64          assertEquals("foo.bar.jim", handler.string("foo", ".", "bar", ".", "jim"));
65      }
66  
67      public void testDeterminePath() {
68          assertEquals("/", handler.determinePath("/", ""));
69          assertEquals("/", handler.determinePath("/", null));
70          assertEquals("/", handler.determinePath("/", "/"));
71          assertEquals("/foo/", handler.determinePath("/", "/foo"));
72          assertEquals("/foo/", handler.determinePath("/", "/foo/"));
73          assertEquals("/foo/", handler.determinePath("/", "foo"));
74      }
75      
76      public static class SomeResult implements Result {
77  
78          public String location;
79          public void setLocation(String loc) {
80              this.location = loc;
81          }
82          
83          public void execute(ActionInvocation invocation) throws Exception {
84          }
85          
86      }
87  
88  }