blob: 2266fe8dce3a6f5121d633c300bef734cb700a3f [file] [log] [blame]
/*
* $Id$
*
* Copyright 2006 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.struts2.dispatcher;
import junit.framework.TestCase;
import org.apache.velocity.Template;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;
import com.mockobjects.dynamic.Mock;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionProxy;
import com.opensymphony.xwork2.util.OgnlValueStack;
/**
*
*/
public class VelocityResultTest extends TestCase {
ActionInvocation actionInvocation;
Mock mockActionProxy;
OgnlValueStack stack;
String namespace;
TestVelocityEngine velocity;
VelocityResult result;
public void testCanResolveLocationUsingOgnl() throws Exception {
TestResult result = new TestResult();
String location = "/myaction.action";
Bean bean = new Bean();
bean.setLocation(location);
OgnlValueStack stack = ActionContext.getContext().getValueStack();
stack.push(bean);
assertEquals(location, stack.findValue("location"));
result.setLocation("${location}");
result.execute(actionInvocation);
assertEquals(location, result.finalLocation);
}
public void testCanResolveLocationUsingStaticExpression() throws Exception {
TestResult result = new TestResult();
String location = "/any.action";
result.setLocation("${'" + location + "'}");
result.execute(actionInvocation);
assertEquals(location, result.finalLocation);
}
public void testResourcesFoundUsingAbsolutePath() throws Exception {
String location = "/WEB-INF/views/registration.vm";
Template template = result.getTemplate(stack, velocity, actionInvocation, location, "UTF-8");
assertNotNull(template);
assertEquals("expect absolute locations to be handled as is", location, velocity.templateName);
}
public void testResourcesFoundUsingNames() throws Exception {
String location = "Registration.vm";
String expectedTemplateName = namespace + "/" + location;
Template template = result.getTemplate(stack, velocity, actionInvocation, location, "UTF-8");
assertNotNull(template);
assertEquals("expect the prefix to be appended to the path when the location is not absolute", expectedTemplateName, velocity.templateName);
}
protected void setUp() throws Exception {
namespace = "/html";
result = new VelocityResult();
stack = new OgnlValueStack();
ActionContext.getContext().setValueStack(stack);
velocity = new TestVelocityEngine();
mockActionProxy = new Mock(ActionProxy.class);
mockActionProxy.expectAndReturn("getNamespace", "/html");
Mock mockActionInvocation = new Mock(ActionInvocation.class);
mockActionInvocation.expectAndReturn("getProxy", mockActionProxy.proxy());
mockActionInvocation.expectAndReturn("getStack", stack);
actionInvocation = (ActionInvocation) mockActionInvocation.proxy();
}
class Bean {
private String location;
public void setLocation(String location) {
this.location = location;
}
public String getLocation() {
return location;
}
}
class TestResult extends StrutsResultSupport {
private static final long serialVersionUID = -1512206785088317315L;
public String finalLocation;
protected void doExecute(String finalLocation, ActionInvocation invocation) throws Exception {
this.finalLocation = finalLocation;
}
}
class TestVelocityEngine extends VelocityEngine {
public String templateName;
public Template getTemplate(String templateName) throws ResourceNotFoundException, ParseErrorException, Exception {
this.templateName = templateName;
return new Template();
}
public Template getTemplate(String templateName, String charSet) throws ResourceNotFoundException, ParseErrorException, Exception {
this.templateName = templateName;
return new Template();
}
}
}