View Javadoc

1   /*
2    * $Id: VelocityResultTest.java 454565 2006-10-10 00:02:56Z jmitchell $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts2.dispatcher;
19  
20  import junit.framework.TestCase;
21  
22  import org.apache.velocity.Template;
23  import org.apache.velocity.app.VelocityEngine;
24  import org.apache.velocity.exception.ParseErrorException;
25  import org.apache.velocity.exception.ResourceNotFoundException;
26  
27  import com.mockobjects.dynamic.Mock;
28  import com.opensymphony.xwork2.ActionContext;
29  import com.opensymphony.xwork2.ActionInvocation;
30  import com.opensymphony.xwork2.ActionProxy;
31  import com.opensymphony.xwork2.util.ValueStack;
32  import com.opensymphony.xwork2.util.ValueStackFactory;
33  
34  
35  /***
36   * 
37   */
38  public class VelocityResultTest extends TestCase {
39  
40      ActionInvocation actionInvocation;
41      Mock mockActionProxy;
42      ValueStack stack;
43      String namespace;
44      TestVelocityEngine velocity;
45      VelocityResult result;
46  
47  
48      public void testCanResolveLocationUsingOgnl() throws Exception {
49          TestResult result = new TestResult();
50  
51          String location = "/myaction.action";
52          Bean bean = new Bean();
53          bean.setLocation(location);
54  
55          ValueStack stack = ActionContext.getContext().getValueStack();
56          stack.push(bean);
57  
58          assertEquals(location, stack.findValue("location"));
59  
60          result.setLocation("${location}");
61          result.execute(actionInvocation);
62          assertEquals(location, result.finalLocation);
63      }
64  
65      public void testCanResolveLocationUsingStaticExpression() throws Exception {
66          TestResult result = new TestResult();
67          String location = "/any.action";
68          result.setLocation("${'" + location + "'}");
69          result.execute(actionInvocation);
70          assertEquals(location, result.finalLocation);
71      }
72  
73      public void testResourcesFoundUsingAbsolutePath() throws Exception {
74          String location = "/WEB-INF/views/registration.vm";
75  
76          Template template = result.getTemplate(stack, velocity, actionInvocation, location, "UTF-8");
77          assertNotNull(template);
78          assertEquals("expect absolute locations to be handled as is", location, velocity.templateName);
79      }
80  
81      public void testResourcesFoundUsingNames() throws Exception {
82          String location = "Registration.vm";
83          String expectedTemplateName = namespace + "/" + location;
84  
85          Template template = result.getTemplate(stack, velocity, actionInvocation, location, "UTF-8");
86          assertNotNull(template);
87          assertEquals("expect the prefix to be appended to the path when the location is not absolute", expectedTemplateName, velocity.templateName);
88      }
89  
90      protected void setUp() throws Exception {
91          namespace = "/html";
92          result = new VelocityResult();
93          stack = ValueStackFactory.getFactory().createValueStack();
94          ActionContext.getContext().setValueStack(stack);
95          velocity = new TestVelocityEngine();
96          mockActionProxy = new Mock(ActionProxy.class);
97          mockActionProxy.expectAndReturn("getNamespace", "/html");
98  
99          Mock mockActionInvocation = new Mock(ActionInvocation.class);
100         mockActionInvocation.expectAndReturn("getProxy", mockActionProxy.proxy());
101         mockActionInvocation.expectAndReturn("getStack", stack);
102         actionInvocation = (ActionInvocation) mockActionInvocation.proxy();
103     }
104 
105 
106     class Bean {
107         private String location;
108 
109         public void setLocation(String location) {
110             this.location = location;
111         }
112 
113         public String getLocation() {
114             return location;
115         }
116     }
117 
118     class TestResult extends StrutsResultSupport {
119     	
120 		private static final long serialVersionUID = -1512206785088317315L;
121 		
122 		public String finalLocation;
123 
124         protected void doExecute(String finalLocation, ActionInvocation invocation) throws Exception {
125             this.finalLocation = finalLocation;
126         }
127     }
128 
129     class TestVelocityEngine extends VelocityEngine {
130         public String templateName;
131 
132         public Template getTemplate(String templateName) throws ResourceNotFoundException, ParseErrorException, Exception {
133             this.templateName = templateName;
134 
135             return new Template();
136         }
137 
138         public Template getTemplate(String templateName, String charSet) throws ResourceNotFoundException, ParseErrorException, Exception {
139             this.templateName = templateName;
140 
141             return new Template();
142         }
143     }
144 }