KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > dispatcher > VelocityResultTest


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

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 /**
20  * @author Matt Ho <a HREF="mailto:matt@enginegreen.com">&lt;matt@enginegreen.com&gt;</a>
21  * @version $Id: VelocityResultTest.java,v 1.11 2005/08/08 02:33:03 dres Exp $
22  */

23 public class VelocityResultTest extends TestCase {
24     //~ Instance fields ////////////////////////////////////////////////////////
25

26     ActionInvocation actionInvocation;
27     Mock mockActionProxy;
28     OgnlValueStack stack;
29     String JavaDoc namespace;
30     TestVelocityEngine velocity;
31     VelocityResult result;
32
33     //~ Methods ////////////////////////////////////////////////////////////////
34

35     public void testCanResolveLocationUsingOgnl() throws Exception JavaDoc {
36         TestResult result = new TestResult();
37
38         String JavaDoc 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 JavaDoc {
53         TestResult result = new TestResult();
54         String JavaDoc location = "/any.action";
55         result.setLocation("${'" + location + "'}");
56         result.execute(actionInvocation);
57         assertEquals(location, result.finalLocation);
58     }
59
60     public void testResourcesFoundUsingAbsolutePath() throws Exception JavaDoc {
61         String JavaDoc 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 JavaDoc {
69         String JavaDoc location = "Registration.vm";
70         String JavaDoc 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 JavaDoc {
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     //~ Inner Classes //////////////////////////////////////////////////////////
93

94     class Bean {
95         private String JavaDoc location;
96
97         public void setLocation(String JavaDoc location) {
98             this.location = location;
99         }
100
101         public String JavaDoc getLocation() {
102             return location;
103         }
104     }
105
106     class TestResult extends WebWorkResultSupport {
107         public String JavaDoc finalLocation;
108
109         protected void doExecute(String JavaDoc finalLocation, ActionInvocation invocation) throws Exception JavaDoc {
110             this.finalLocation = finalLocation;
111         }
112     }
113
114     class TestVelocityEngine extends VelocityEngine {
115         public String JavaDoc templateName;
116
117         public Template getTemplate(String JavaDoc templateName) throws ResourceNotFoundException, ParseErrorException, Exception JavaDoc {
118             this.templateName = templateName;
119
120             return new Template();
121         }
122
123         public Template getTemplate(String JavaDoc templateName, String JavaDoc charSet) throws ResourceNotFoundException, ParseErrorException, Exception JavaDoc {
124             this.templateName = templateName;
125
126             return new Template();
127         }
128     }
129 }
130
Popular Tags