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 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
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 }