View Javadoc

1   /*
2    * $Id: CodebehindUnknownHandlerTest.java 672982 2008-07-01 03:48:03Z jeromy $
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 com.mockobjects.dynamic.C;
25  import com.mockobjects.dynamic.Mock;
26  import com.opensymphony.xwork2.*;
27  import com.opensymphony.xwork2.config.entities.ActionConfig;
28  import com.opensymphony.xwork2.config.entities.ResultTypeConfig;
29  import com.opensymphony.xwork2.util.XWorkTestCaseHelper;
30  import org.apache.struts2.StrutsTestCase;
31  import org.apache.struts2.dispatcher.ServletDispatcherResult;
32  
33  import javax.servlet.ServletContext;
34  import java.net.MalformedURLException;
35  import java.net.URL;
36  import java.util.Collections;
37  import java.util.HashMap;
38  
39  public class CodebehindUnknownHandlerTest extends StrutsTestCase {
40  
41      CodebehindUnknownHandler handler;
42      Mock mockServletContext;
43      
44      public void setUp() throws Exception {
45          configurationManager = XWorkTestCaseHelper.setUp();
46          configuration = configurationManager.getConfiguration();
47          container = configuration.getContainer();
48          actionProxyFactory = container.getInstance(ActionProxyFactory.class);
49          initDispatcher(Collections.singletonMap("actionPackages", "foo.bar"));
50          mockServletContext = new Mock(ServletContext.class);
51          handler = new CodebehindUnknownHandler("codebehind-default", configuration);
52          handler.setPathPrefix("/");
53          handler.setObjectFactory(container.getInstance(ObjectFactory.class));
54          handler.setServletContext((ServletContext)mockServletContext.proxy());
55      }
56  
57      public void testBuildResult() {
58          ActionContext ctx = new ActionContext(new HashMap());
59          ResultTypeConfig config = new ResultTypeConfig.Builder("null", SomeResult.class.getName()).defaultResultParam("location").build();
60          
61          Result result = handler.buildResult("/foo.jsp", "success", config, ctx);
62          assertNotNull(result);
63          assertTrue(result instanceof SomeResult);
64          assertEquals("/foo.jsp", ((SomeResult) result).location);
65          
66      }
67  
68      public void testString() {
69          assertEquals("foo.bar.jim", handler.string("foo", ".", "bar", ".", "jim"));
70      }
71  
72      public void testDeterminePath() {
73          assertEquals("/", handler.determinePath("/", ""));
74          assertEquals("/", handler.determinePath("/", null));
75          assertEquals("/", handler.determinePath("/", "/"));
76          assertEquals("/foo/", handler.determinePath("/", "/foo"));
77          assertEquals("/foo/", handler.determinePath("/", "/foo/"));
78          assertEquals("/foo/", handler.determinePath("/", "foo"));
79          assertEquals("/", handler.determinePath("", ""));
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 }