1 5 package com.opensymphony.webwork.dispatcher; 6 7 import com.mockobjects.dynamic.Mock; 8 import com.opensymphony.xwork.ActionContext; 9 import com.opensymphony.xwork.ActionInvocation; 10 import com.opensymphony.xwork.ActionProxy; 11 import com.opensymphony.xwork.util.OgnlValueStack; 12 import junit.framework.TestCase; 13 import org.apache.velocity.Template; 14 import org.apache.velocity.app.VelocityEngine; 15 import org.apache.velocity.exception.ParseErrorException; 16 import org.apache.velocity.exception.ResourceNotFoundException; 17 18 19 23 public class VelocityResultTest extends TestCase { 24 26 ActionInvocation actionInvocation; 27 Mock mockActionProxy; 28 OgnlValueStack stack; 29 String namespace; 30 TestVelocityEngine velocity; 31 VelocityResult result; 32 33 35 public void testCanResolveLocationUsingOgnl() throws Exception { 36 TestResult result = new TestResult(); 37 38 String location = "/myaction.action"; 39 Bean bean = new Bean(); 40 bean.setLocation(location); 41 42 OgnlValueStack stack = ActionContext.getContext().getValueStack(); 43 stack.push(bean); 44 45 assertEquals(location, stack.findValue("location")); 46 47 result.setLocation("${location}"); 48 result.execute(actionInvocation); 49 assertEquals(location, result.finalLocation); 50 } 51 52 public void testCanResolveLocationUsingStaticExpression() throws Exception { 53 TestResult result = new TestResult(); 54 String location = "/any.action"; 55 result.setLocation("${'" + location + "'}"); 56 result.execute(actionInvocation); 57 assertEquals(location, result.finalLocation); 58 } 59 60 public void testResourcesFoundUsingAbsolutePath() throws Exception { 61 String location = "/WEB-INF/views/registration.vm"; 62 63 Template template = result.getTemplate(stack, velocity, actionInvocation, location, "UTF-8"); 64 assertNotNull(template); 65 assertEquals("expect absolute locations to be handled as is", location, velocity.templateName); 66 } 67 68 public void testResourcesFoundUsingNames() throws Exception { 69 String location = "Registration.vm"; 70 String expectedTemplateName = namespace + "/" + location; 71 72 Template template = result.getTemplate(stack, velocity, actionInvocation, location, "UTF-8"); 73 assertNotNull(template); 74 assertEquals("expect the prefix to be appended to the path when the location is not absolute", expectedTemplateName, velocity.templateName); 75 } 76 77 protected void setUp() throws Exception { 78 namespace = "/html"; 79 result = new VelocityResult(); 80 stack = new OgnlValueStack(); 81 ActionContext.getContext().setValueStack(stack); 82 velocity = new TestVelocityEngine(); 83 mockActionProxy = new Mock(ActionProxy.class); 84 mockActionProxy.expectAndReturn("getNamespace", "/html"); 85 86 Mock mockActionInvocation = new Mock(ActionInvocation.class); 87 mockActionInvocation.expectAndReturn("getProxy", mockActionProxy.proxy()); 88 mockActionInvocation.expectAndReturn("getStack", stack); 89 actionInvocation = (ActionInvocation) mockActionInvocation.proxy(); 90 } 91 92 94 class Bean { 95 private String location; 96 97 public void setLocation(String location) { 98 this.location = location; 99 } 100 101 public String getLocation() { 102 return location; 103 } 104 } 105 106 class TestResult extends WebWorkResultSupport { 107 public String finalLocation; 108 109 protected void doExecute(String finalLocation, ActionInvocation invocation) throws Exception { 110 this.finalLocation = finalLocation; 111 } 112 } 113 114 class TestVelocityEngine extends VelocityEngine { 115 public String templateName; 116 117 public Template getTemplate(String templateName) throws ResourceNotFoundException, ParseErrorException, Exception { 118 this.templateName = templateName; 119 120 return new Template(); 121 } 122 123 public Template getTemplate(String templateName, String charSet) throws ResourceNotFoundException, ParseErrorException, Exception { 124 this.templateName = templateName; 125 126 return new Template(); 127 } 128 } 129 } 130 | Popular Tags |