View Javadoc

    1   /*
    2    * $Id: VelocityResultTest.java 471756 2006-11-06 15:01:43Z husted $
    3    *
    4    * Licensed to the Apache Software Foundation (ASF) under one
    5    * or more contributor license agreements.  See the NOTICE file
    6    * distributed with this work for additional information
    7    * regarding copyright ownership.  The ASF licenses this file
    8    * to you under the Apache License, Version 2.0 (the
    9    * "License"); you may not use this file except in compliance
    10   * with the License.  You may obtain a copy of the License at
    11   *
    12   *  http://www.apache.org/licenses/LICENSE-2.0
    13   *
    14   * Unless required by applicable law or agreed to in writing,
    15   * software distributed under the License is distributed on an
    16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    17   * KIND, either express or implied.  See the License for the
    18   * specific language governing permissions and limitations
    19   * under the License.
    20   */
    21  package org.apache.struts2.dispatcher;
    22
    23  import junit.framework.TestCase;
    24
    25  import org.apache.velocity.Template;
    26  import org.apache.velocity.app.VelocityEngine;
    27  import org.apache.velocity.exception.ParseErrorException;
    28  import org.apache.velocity.exception.ResourceNotFoundException;
    29
    30  import com.mockobjects.dynamic.Mock;
    31  import com.opensymphony.xwork2.ActionContext;
    32  import com.opensymphony.xwork2.ActionInvocation;
    33  import com.opensymphony.xwork2.ActionProxy;
    34  import com.opensymphony.xwork2.util.ValueStack;
    35  import com.opensymphony.xwork2.util.ValueStackFactory;
    36
    37
    38  /***
    39   *
    40   */
    41  public class VelocityResultTest extends TestCase {
    42
    43      ActionInvocation actionInvocation;
    44      Mock mockActionProxy;
    45      ValueStack stack;
    46      String namespace;
    47      TestVelocityEngine velocity;
    48      VelocityResult result;
    49
    50
    51      public void testCanResolveLocationUsingOgnl() throws Exception {
    52          TestResult result = new TestResult();
    53
    54          String location = "/myaction.action";
    55          Bean bean = new Bean();
    56          bean.setLocation(location);
    57
    58          ValueStack stack = ActionContext.getContext().getValueStack();
    59          stack.push(bean);
    60
    61          assertEquals(location, stack.findValue("location"));
    62
    63          result.setLocation("${location}");
    64          result.execute(actionInvocation);
    65          assertEquals(location, result.finalLocation);
    66      }
    67
    68      public void testCanResolveLocationUsingStaticExpression() throws Exception {
    69          TestResult result = new TestResult();
    70          String location = "/any.action";
    71          result.setLocation("${'" + location + "'}");
    72          result.execute(actionInvocation);
    73          assertEquals(location, result.finalLocation);
    74      }
    75
    76      public void testResourcesFoundUsingAbsolutePath() throws Exception {
    77          String location = "/WEB-INF/views/registration.vm";
    78
    79          Template template = result.getTemplate(stack, velocity, actionInvocation, location, "UTF-8");
    80          assertNotNull(template);
    81          assertEquals("expect absolute locations to be handled as is", location, velocity.templateName);
    82      }
    83
    84      public void testResourcesFoundUsingNames() throws Exception {
    85          String location = "Registration.vm";
    86          String expectedTemplateName = namespace + "/" + location;
    87
    88          Template template = result.getTemplate(stack, velocity, actionInvocation, location, "UTF-8");
    89          assertNotNull(template);
    90          assertEquals("expect the prefix to be appended to the path when the location is not absolute", expectedTemplateName, velocity.templateName);
    91      }
    92
    93      protected void setUp() throws Exception {
    94          namespace = "/html";
    95          result = new VelocityResult();
    96          stack = ValueStackFactory.getFactory().createValueStack();
    97          ActionContext.getContext().setValueStack(stack);
    98          velocity = new TestVelocityEngine();
    99          mockActionProxy = new Mock(ActionProxy.class);
    100         mockActionProxy.expectAndReturn("getNamespace", "/html");
    101
    102         Mock mockActionInvocation = new Mock(ActionInvocation.class);
    103         mockActionInvocation.expectAndReturn("getProxy", mockActionProxy.proxy());
    104         mockActionInvocation.expectAndReturn("getStack", stack);
    105         actionInvocation = (ActionInvocation) mockActionInvocation.proxy();
    106     }
    107
    108
    109     class Bean {
    110         private String location;
    111
    112         public void setLocation(String location) {
    113             this.location = location;
    114         }
    115
    116         public String getLocation() {
    117             return location;
    118         }
    119     }
    120
    121     class TestResult extends StrutsResultSupport {
    122
    123         private static final long serialVersionUID = -1512206785088317315L;
    124
    125         public String finalLocation;
    126
    127         protected void doExecute(String finalLocation, ActionInvocation invocation) throws Exception {
    128             this.finalLocation = finalLocation;
    129         }
    130     }
    131
    132     class TestVelocityEngine extends VelocityEngine {
    133         public String templateName;
    134
    135         public Template getTemplate(String templateName) throws ResourceNotFoundException, ParseErrorException, Exception {
    136             this.templateName = templateName;
    137
    138             return new Template();
    139         }
    140
    141         public Template getTemplate(String templateName, String charSet) throws ResourceNotFoundException, ParseErrorException, Exception {
    142             this.templateName = templateName;
    143
    144             return new Template();
    145         }
    146     }
    147 }