1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.struts2.dispatcher;
22
23 import junit.framework.TestCase;
24
25 import org.apache.velocity.Template;
26 import org.apache.velocity.app.VelocityEngine;
27 import org.apache.velocity.exception.ParseErrorException;
28 import org.apache.velocity.exception.ResourceNotFoundException;
29
30 import com.mockobjects.dynamic.Mock;
31 import com.opensymphony.xwork2.ActionContext;
32 import com.opensymphony.xwork2.ActionInvocation;
33 import com.opensymphony.xwork2.ActionProxy;
34 import com.opensymphony.xwork2.util.ValueStack;
35 import com.opensymphony.xwork2.util.ValueStackFactory;
36
37
38 /***
39 *
40 */
41 public class VelocityResultTest extends TestCase {
42
43 ActionInvocation actionInvocation;
44 Mock mockActionProxy;
45 ValueStack stack;
46 String namespace;
47 TestVelocityEngine velocity;
48 VelocityResult result;
49
50
51 public void testCanResolveLocationUsingOgnl() throws Exception {
52 TestResult result = new TestResult();
53
54 String location = "/myaction.action";
55 Bean bean = new Bean();
56 bean.setLocation(location);
57
58 ValueStack stack = ActionContext.getContext().getValueStack();
59 stack.push(bean);
60
61 assertEquals(location, stack.findValue("location"));
62
63 result.setLocation("${location}");
64 result.execute(actionInvocation);
65 assertEquals(location, result.finalLocation);
66 }
67
68 public void testCanResolveLocationUsingStaticExpression() throws Exception {
69 TestResult result = new TestResult();
70 String location = "/any.action";
71 result.setLocation("${'" + location + "'}");
72 result.execute(actionInvocation);
73 assertEquals(location, result.finalLocation);
74 }
75
76 public void testResourcesFoundUsingAbsolutePath() throws Exception {
77 String location = "/WEB-INF/views/registration.vm";
78
79 Template template = result.getTemplate(stack, velocity, actionInvocation, location, "UTF-8");
80 assertNotNull(template);
81 assertEquals("expect absolute locations to be handled as is", location, velocity.templateName);
82 }
83
84 public void testResourcesFoundUsingNames() throws Exception {
85 String location = "Registration.vm";
86 String expectedTemplateName = namespace + "/" + location;
87
88 Template template = result.getTemplate(stack, velocity, actionInvocation, location, "UTF-8");
89 assertNotNull(template);
90 assertEquals("expect the prefix to be appended to the path when the location is not absolute", expectedTemplateName, velocity.templateName);
91 }
92
93 protected void setUp() throws Exception {
94 namespace = "/html";
95 result = new VelocityResult();
96 stack = ValueStackFactory.getFactory().createValueStack();
97 ActionContext.getContext().setValueStack(stack);
98 velocity = new TestVelocityEngine();
99 mockActionProxy = new Mock(ActionProxy.class);
100 mockActionProxy.expectAndReturn("getNamespace", "/html");
101
102 Mock mockActionInvocation = new Mock(ActionInvocation.class);
103 mockActionInvocation.expectAndReturn("getProxy", mockActionProxy.proxy());
104 mockActionInvocation.expectAndReturn("getStack", stack);
105 actionInvocation = (ActionInvocation) mockActionInvocation.proxy();
106 }
107
108
109 class Bean {
110 private String location;
111
112 public void setLocation(String location) {
113 this.location = location;
114 }
115
116 public String getLocation() {
117 return location;
118 }
119 }
120
121 class TestResult extends StrutsResultSupport {
122
123 private static final long serialVersionUID = -1512206785088317315L;
124
125 public String finalLocation;
126
127 protected void doExecute(String finalLocation, ActionInvocation invocation) throws Exception {
128 this.finalLocation = finalLocation;
129 }
130 }
131
132 class TestVelocityEngine extends VelocityEngine {
133 public String templateName;
134
135 public Template getTemplate(String templateName) throws ResourceNotFoundException, ParseErrorException, Exception {
136 this.templateName = templateName;
137
138 return new Template();
139 }
140
141 public Template getTemplate(String templateName, String charSet) throws ResourceNotFoundException, ParseErrorException, Exception {
142 this.templateName = templateName;
143
144 return new Template();
145 }
146 }
147 }