001: /*
002: * $Id: VelocityResultTest.java 471756 2006-11-06 15:01:43Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.dispatcher;
022:
023: import junit.framework.TestCase;
024:
025: import org.apache.velocity.Template;
026: import org.apache.velocity.app.VelocityEngine;
027: import org.apache.velocity.exception.ParseErrorException;
028: import org.apache.velocity.exception.ResourceNotFoundException;
029:
030: import com.mockobjects.dynamic.Mock;
031: import com.opensymphony.xwork2.ActionContext;
032: import com.opensymphony.xwork2.ActionInvocation;
033: import com.opensymphony.xwork2.ActionProxy;
034: import com.opensymphony.xwork2.util.ValueStack;
035: import com.opensymphony.xwork2.util.ValueStackFactory;
036:
037: /**
038: *
039: */
040: public class VelocityResultTest extends TestCase {
041:
042: ActionInvocation actionInvocation;
043: Mock mockActionProxy;
044: ValueStack stack;
045: String namespace;
046: TestVelocityEngine velocity;
047: VelocityResult result;
048:
049: public void testCanResolveLocationUsingOgnl() throws Exception {
050: TestResult result = new TestResult();
051:
052: String location = "/myaction.action";
053: Bean bean = new Bean();
054: bean.setLocation(location);
055:
056: ValueStack stack = ActionContext.getContext().getValueStack();
057: stack.push(bean);
058:
059: assertEquals(location, stack.findValue("location"));
060:
061: result.setLocation("${location}");
062: result.execute(actionInvocation);
063: assertEquals(location, result.finalLocation);
064: }
065:
066: public void testCanResolveLocationUsingStaticExpression()
067: throws Exception {
068: TestResult result = new TestResult();
069: String location = "/any.action";
070: result.setLocation("${'" + location + "'}");
071: result.execute(actionInvocation);
072: assertEquals(location, result.finalLocation);
073: }
074:
075: public void testResourcesFoundUsingAbsolutePath() throws Exception {
076: String location = "/WEB-INF/views/registration.vm";
077:
078: Template template = result.getTemplate(stack, velocity,
079: actionInvocation, location, "UTF-8");
080: assertNotNull(template);
081: assertEquals("expect absolute locations to be handled as is",
082: location, velocity.templateName);
083: }
084:
085: public void testResourcesFoundUsingNames() throws Exception {
086: String location = "Registration.vm";
087: String expectedTemplateName = namespace + "/" + location;
088:
089: Template template = result.getTemplate(stack, velocity,
090: actionInvocation, location, "UTF-8");
091: assertNotNull(template);
092: assertEquals(
093: "expect the prefix to be appended to the path when the location is not absolute",
094: expectedTemplateName, velocity.templateName);
095: }
096:
097: protected void setUp() throws Exception {
098: namespace = "/html";
099: result = new VelocityResult();
100: stack = ValueStackFactory.getFactory().createValueStack();
101: ActionContext.getContext().setValueStack(stack);
102: velocity = new TestVelocityEngine();
103: mockActionProxy = new Mock(ActionProxy.class);
104: mockActionProxy.expectAndReturn("getNamespace", "/html");
105:
106: Mock mockActionInvocation = new Mock(ActionInvocation.class);
107: mockActionInvocation.expectAndReturn("getProxy",
108: mockActionProxy.proxy());
109: mockActionInvocation.expectAndReturn("getStack", stack);
110: actionInvocation = (ActionInvocation) mockActionInvocation
111: .proxy();
112: }
113:
114: class Bean {
115: private String location;
116:
117: public void setLocation(String location) {
118: this .location = location;
119: }
120:
121: public String getLocation() {
122: return location;
123: }
124: }
125:
126: class TestResult extends StrutsResultSupport {
127:
128: private static final long serialVersionUID = -1512206785088317315L;
129:
130: public String finalLocation;
131:
132: protected void doExecute(String finalLocation,
133: ActionInvocation invocation) throws Exception {
134: this .finalLocation = finalLocation;
135: }
136: }
137:
138: class TestVelocityEngine extends VelocityEngine {
139: public String templateName;
140:
141: public Template getTemplate(String templateName)
142: throws ResourceNotFoundException, ParseErrorException,
143: Exception {
144: this .templateName = templateName;
145:
146: return new Template();
147: }
148:
149: public Template getTemplate(String templateName, String charSet)
150: throws ResourceNotFoundException, ParseErrorException,
151: Exception {
152: this .templateName = templateName;
153:
154: return new Template();
155: }
156: }
157: }
|