View Javadoc

1   /*
2    * $Id: CodebehindUnknownHandlerTest.java 651946 2008-04-27 13:41:38Z apetrelli $
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.codebehind;
23  
24  import java.net.MalformedURLException;
25  import java.net.URL;
26  import java.util.Collections;
27  import java.util.HashMap;
28  
29  import javax.servlet.ServletContext;
30  
31  import org.apache.struts2.StrutsTestCase;
32  import org.apache.struts2.config.NullResult;
33  import org.apache.struts2.dispatcher.ServletDispatcherResult;
34  
35  import com.mockobjects.dynamic.C;
36  import com.mockobjects.dynamic.Mock;
37  import com.opensymphony.xwork2.*;
38  import com.opensymphony.xwork2.config.entities.ResultTypeConfig;
39  import com.opensymphony.xwork2.config.entities.ActionConfig;
40  import com.opensymphony.xwork2.util.XWorkTestCaseHelper;
41  
42  public class CodebehindUnknownHandlerTest extends StrutsTestCase {
43  
44      CodebehindUnknownHandler handler;
45      Mock mockServletContext;
46      
47      public void setUp() throws Exception {
48          configurationManager = XWorkTestCaseHelper.setUp();
49          configuration = configurationManager.getConfiguration();
50          container = configuration.getContainer();
51          actionProxyFactory = container.getInstance(ActionProxyFactory.class);
52          initDispatcher(Collections.singletonMap("actionPackages", "foo.bar"));
53          mockServletContext = new Mock(ServletContext.class);
54          handler = new CodebehindUnknownHandler("codebehind-default", configuration);
55          handler.setPathPrefix("/");
56          handler.setObjectFactory(container.getInstance(ObjectFactory.class));
57          handler.setServletContext((ServletContext)mockServletContext.proxy());
58      }
59  
60      public void testBuildResult() {
61          ActionContext ctx = new ActionContext(new HashMap());
62          ResultTypeConfig config = new ResultTypeConfig.Builder("null", SomeResult.class.getName()).defaultResultParam("location").build();
63          
64          Result result = handler.buildResult("/foo.jsp", "success", config, ctx);
65          assertNotNull(result);
66          assertTrue(result instanceof SomeResult);
67          assertEquals("/foo.jsp", ((SomeResult) result).location);
68          
69      }
70  
71      public void testString() {
72          assertEquals("foo.bar.jim", handler.string("foo", ".", "bar", ".", "jim"));
73      }
74  
75      public void testDeterminePath() {
76          assertEquals("/", handler.determinePath("/", ""));
77          assertEquals("/", handler.determinePath("/", null));
78          assertEquals("/", handler.determinePath("/", "/"));
79          assertEquals("/foo/", handler.determinePath("/", "/foo"));
80          assertEquals("/foo/", handler.determinePath("/", "/foo/"));
81          assertEquals("/foo/", handler.determinePath("/", "foo"));
82      }
83      
84      public void testLocateTemplate() throws MalformedURLException {
85          URL url = new URL("file:/foo.xml");
86          mockServletContext.expectAndReturn("getResource", C.args(C.eq("/foo.xml")), url);
87          assertEquals(url, handler.locateTemplate("/foo.xml"));
88          mockServletContext.verify();
89          
90      }
91      
92      public void testLocateTemplateFromClasspath() throws MalformedURLException {
93          mockServletContext.expectAndReturn("getResource", C.args(C.eq("struts-plugin.xml")), null);
94          URL url = handler.locateTemplate("struts-plugin.xml");
95          assertNotNull(url);
96          assertTrue(url.toString().endsWith("struts-plugin.xml"));
97          mockServletContext.verify();
98      }
99  
100     /***
101      * Assert that an unknown action like /foo maps to ActionSupport with a ServletDispatcherResult to /foo.jsp
102      */
103     public void testBuildActionConfigForUnknownAction() throws MalformedURLException {
104         URL url = new URL("file:/foo.jsp");
105         mockServletContext.expectAndReturn("getResource", C.args(C.eq("/foo.jsp")), url);
106         ActionConfig actionConfig = handler.handleUnknownAction("/", "foo");
107         // we need a package
108         assertEquals("codebehind-default", actionConfig.getPackageName());
109         // a non-empty interceptor stack
110         assertTrue(actionConfig.getInterceptors().size() > 0);
111         // ActionSupport as the implementation
112         assertEquals(ActionSupport.class.getName(), actionConfig.getClassName());
113         // with one result
114         assertEquals(1, actionConfig.getResults().size());
115         // named success
116         assertNotNull(actionConfig.getResults().get("success"));
117         // of ServletDispatcherResult type
118         assertEquals(ServletDispatcherResult.class.getName(), actionConfig.getResults().get("success").getClassName());
119         // and finally pointing to foo.jsp!
120         assertEquals("/foo.jsp", actionConfig.getResults().get("success").getParams().get("location"));
121     }
122 
123     public static class SomeResult implements Result {
124 
125         public String location;
126         public void setLocation(String loc) {
127             this.location = loc;
128         }
129         
130         public void execute(ActionInvocation invocation) throws Exception {
131         }
132         
133     }
134 
135 }