    1   package org.apache.turbine.pipeline;
    2
    3
    4   /*
    5    * Licensed to the Apache Software Foundation (ASF) under one
    6    * or more contributor license agreements.  See the NOTICE file
    7    * distributed with this work for additional information
    8    * regarding copyright ownership.  The ASF licenses this file
    9    * to you under the Apache License, Version 2.0 (the
    10   * "License"); you may not use this file except in compliance
    11   * with the License.  You may obtain a copy of the License at
    12   *
    13   *   http://www.apache.org/licenses/LICENSE-2.0
    14   *
    15   * Unless required by applicable law or agreed to in writing,
    16   * software distributed under the License is distributed on an
    17   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    18   * KIND, either express or implied.  See the License for the
    19   * specific language governing permissions and limitations
    20   * under the License.
    21   */
    22
    23
    24  import java.util.Vector;
    25
    26  import javax.servlet.ServletConfig;
    27  import javax.servlet.http.HttpServletResponse;
    28
    29  import org.apache.turbine.modules.actions.VelocityActionDoesNothing;
    30  import org.apache.turbine.om.security.TurbineUser;
    31  import org.apache.turbine.om.security.User;
    32  import org.apache.turbine.services.template.TemplateService;
    33  import org.apache.turbine.test.BaseTestCase;
    34  import org.apache.turbine.test.EnhancedMockHttpServletRequest;
    35  import org.apache.turbine.test.EnhancedMockHttpServletResponse;
    36  import org.apache.turbine.test.EnhancedMockHttpSession;
    37  import org.apache.turbine.util.RunData;
    38  import org.apache.turbine.util.TurbineConfig;
    39  import org.apache.turbine.util.uri.URIConstants;
    40
    41  import com.mockobjects.servlet.MockServletConfig;
    42
    43  /**
    44   * Tests ExecutePageValve.
    45   *
    46   * @author <a href="mailto:epugh@opensourceConnections.com">Eric Pugh</a>
    47   * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
    48   * @version $Id: ExecutePageValveTest.java 757213 2009-03-22 16:43:31Z tv $
    49   */
    50  public class ExecutePageValveTest extends BaseTestCase
    51  {
    52      private static TurbineConfig tc = null;
    53      private static TemplateService ts = null;
    54      private MockServletConfig config = null;
    55      private EnhancedMockHttpServletRequest request = null;
    56      private EnhancedMockHttpSession session = null;
    57      private HttpServletResponse response = null;
    58      private static ServletConfig sc = null;
    59      /**
    60       * Constructor
    61       */
    62      public ExecutePageValveTest(String testName) throws Exception
    63      {
    64          super(testName);
    65      }
    66
    67      protected void setUp() throws Exception
    68      {
    69          super.setUp();
    70          config = new MockServletConfig();
    71          config.setupNoParameters();
    72          request = new EnhancedMockHttpServletRequest();
    73          request.setupServerName("bob");
    74          request.setupGetProtocol("http");
    75          request.setupScheme("scheme");
    76          request.setupPathInfo("damn");
    77          request.setupGetServletPath("damn2");
    78          request.setupGetContextPath("wow");
    79          request.setupGetContentType("html/text");
    80          request.setupAddHeader("Content-type", "html/text");
    81          request.setupAddHeader("Accept-Language", "en-US");
    82
    83          session = new EnhancedMockHttpSession();
    84          response = new EnhancedMockHttpServletResponse();
    85
    86          request.setSession(session);
    87
    88          sc = config;
    89          tc =
    90              new TurbineConfig(
    91                  ".",
    92                  "/conf/test/CompleteTurbineResources.properties");
    93          tc.initialize();
    94      }
    95
    96      public void testValve() throws Exception
    97      {
    98
    99
    100
    101         Vector v = new Vector();
    102         v.add(URIConstants.CGI_TEMPLATE_PARAM);
    103         request.setupGetParameterNames(v.elements());
    104         String nulls[] = new String[1];
    105         nulls[0]="Index.vm";
    106         request.setupAddParameter(URIConstants.CGI_TEMPLATE_PARAM, nulls);
    107
    108         RunData runData =
    109             getRunData(request, response, config);
    110
    111
    112
    113         runData.setScreenTemplate("ExistPageWithLayout.vm");
    114
    115
    116         TurbineUser tu = new TurbineUser();
    117         tu.setName("username");
    118         tu.setHasLoggedIn(Boolean.TRUE);
    119         String actionName = VelocityActionDoesNothing.class.getName();
    120         actionName = actionName.substring(actionName.lastIndexOf(".")+1);
    121         runData.setAction(actionName);
    122         runData.setUser(tu);
    123
    124         Pipeline pipeline = new TurbinePipeline();
    125
    126         PipelineData pipelineData = runData;
    127         ExecutePageValve valve = new ExecutePageValve();
    128         pipeline.addValve(valve);
    129         pipeline.initialize();
    130
    131         int numberOfCalls = VelocityActionDoesNothing.numberOfCalls;
    132         pipeline.invoke(pipelineData);
    133         assertEquals("Assert action was called",numberOfCalls +1,VelocityActionDoesNothing.numberOfCalls);
    134         User user = runData.getUser();
    135         assertNotNull(user);
    136         assertEquals("username", user.getName());
    137         assertTrue(user.hasLoggedIn());
    138
    139
    140     }
    141
    142 }