001: /*
002: * Copyright (c) 2002-2003 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.dispatcher;
006:
007: import com.mockobjects.dynamic.Mock;
008: import com.opensymphony.xwork.ActionContext;
009: import com.opensymphony.xwork.ActionInvocation;
010: import com.opensymphony.xwork.ActionProxy;
011: import com.opensymphony.xwork.util.OgnlValueStack;
012: import junit.framework.TestCase;
013: import org.apache.velocity.Template;
014: import org.apache.velocity.app.VelocityEngine;
015: import org.apache.velocity.exception.ParseErrorException;
016: import org.apache.velocity.exception.ResourceNotFoundException;
017:
018: /**
019: * @author Matt Ho <a href="mailto:matt@enginegreen.com"><matt@enginegreen.com></a>
020: * @version $Id: VelocityResultTest.java 1282 2005-10-09 04:26:58Z plightbo $
021: */
022: public class VelocityResultTest extends TestCase {
023:
024: ActionInvocation actionInvocation;
025: Mock mockActionProxy;
026: OgnlValueStack stack;
027: String namespace;
028: TestVelocityEngine velocity;
029: VelocityResult result;
030:
031: public void testCanResolveLocationUsingOgnl() throws Exception {
032: TestResult result = new TestResult();
033:
034: String location = "/myaction.action";
035: Bean bean = new Bean();
036: bean.setLocation(location);
037:
038: OgnlValueStack stack = ActionContext.getContext()
039: .getValueStack();
040: stack.push(bean);
041:
042: assertEquals(location, stack.findValue("location"));
043:
044: result.setLocation("${location}");
045: result.execute(actionInvocation);
046: assertEquals(location, result.finalLocation);
047: }
048:
049: public void testCanResolveLocationUsingStaticExpression()
050: throws Exception {
051: TestResult result = new TestResult();
052: String location = "/any.action";
053: result.setLocation("${'" + location + "'}");
054: result.execute(actionInvocation);
055: assertEquals(location, result.finalLocation);
056: }
057:
058: public void testResourcesFoundUsingAbsolutePath() throws Exception {
059: String location = "/WEB-INF/views/registration.vm";
060:
061: Template template = result.getTemplate(stack, velocity,
062: actionInvocation, location, "UTF-8");
063: assertNotNull(template);
064: assertEquals("expect absolute locations to be handled as is",
065: location, velocity.templateName);
066: }
067:
068: public void testResourcesFoundUsingNames() throws Exception {
069: String location = "Registration.vm";
070: String expectedTemplateName = namespace + "/" + location;
071:
072: Template template = result.getTemplate(stack, velocity,
073: actionInvocation, location, "UTF-8");
074: assertNotNull(template);
075: assertEquals(
076: "expect the prefix to be appended to the path when the location is not absolute",
077: expectedTemplateName, velocity.templateName);
078: }
079:
080: protected void setUp() throws Exception {
081: namespace = "/html";
082: result = new VelocityResult();
083: stack = new OgnlValueStack();
084: ActionContext.getContext().setValueStack(stack);
085: velocity = new TestVelocityEngine();
086: mockActionProxy = new Mock(ActionProxy.class);
087: mockActionProxy.expectAndReturn("getNamespace", "/html");
088:
089: Mock mockActionInvocation = new Mock(ActionInvocation.class);
090: mockActionInvocation.expectAndReturn("getProxy",
091: mockActionProxy.proxy());
092: mockActionInvocation.expectAndReturn("getStack", stack);
093: actionInvocation = (ActionInvocation) mockActionInvocation
094: .proxy();
095: }
096:
097: class Bean {
098: private String location;
099:
100: public void setLocation(String location) {
101: this .location = location;
102: }
103:
104: public String getLocation() {
105: return location;
106: }
107: }
108:
109: class TestResult extends WebWorkResultSupport {
110: public String finalLocation;
111:
112: protected void doExecute(String finalLocation,
113: ActionInvocation invocation) throws Exception {
114: this .finalLocation = finalLocation;
115: }
116: }
117:
118: class TestVelocityEngine extends VelocityEngine {
119: public String templateName;
120:
121: public Template getTemplate(String templateName)
122: throws ResourceNotFoundException, ParseErrorException,
123: Exception {
124: this .templateName = templateName;
125:
126: return new Template();
127: }
128:
129: public Template getTemplate(String templateName, String charSet)
130: throws ResourceNotFoundException, ParseErrorException,
131: Exception {
132: this .templateName = templateName;
133:
134: return new Template();
135: }
136: }
137: }
|