View Javadoc

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