1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
108 assertEquals("codebehind-default", actionConfig.getPackageName());
109
110 assertTrue(actionConfig.getInterceptors().size() > 0);
111
112 assertEquals(ActionSupport.class.getName(), actionConfig.getClassName());
113
114 assertEquals(1, actionConfig.getResults().size());
115
116 assertNotNull(actionConfig.getResults().get("success"));
117
118 assertEquals(ServletDispatcherResult.class.getName(), actionConfig.getResults().get("success").getClassName());
119
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 }