001 package org.apache.turbine.pipeline;
002
003
004 /*
005 * Licensed to the Apache Software Foundation (ASF) under one
006 * or more contributor license agreements. See the NOTICE file
007 * distributed with this work for additional information
008 * regarding copyright ownership. The ASF licenses this file
009 * to you under the Apache License, Version 2.0 (the
010 * "License"); you may not use this file except in compliance
011 * with the License. You may obtain a copy of the License at
012 *
013 * http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing,
016 * software distributed under the License is distributed on an
017 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
018 * KIND, either express or implied. See the License for the
019 * specific language governing permissions and limitations
020 * under the License.
021 */
022
023
024 import java.util.Vector;
025
026 import javax.servlet.ServletConfig;
027 import javax.servlet.http.HttpServletResponse;
028
029 import org.apache.turbine.modules.actions.VelocityActionDoesNothing;
030 import org.apache.turbine.om.security.TurbineUser;
031 import org.apache.turbine.om.security.User;
032 import org.apache.turbine.services.template.TemplateService;
033 import org.apache.turbine.test.BaseTestCase;
034 import org.apache.turbine.test.EnhancedMockHttpServletRequest;
035 import org.apache.turbine.test.EnhancedMockHttpServletResponse;
036 import org.apache.turbine.test.EnhancedMockHttpSession;
037 import org.apache.turbine.util.RunData;
038 import org.apache.turbine.util.TurbineConfig;
039 import org.apache.turbine.util.uri.URIConstants;
040
041 import com.mockobjects.servlet.MockServletConfig;
042
043 /**
044 * Tests ExecutePageValve.
045 *
046 * @author <a href="mailto:epugh@opensourceConnections.com">Eric Pugh</a>
047 * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
048 * @version $Id: ExecutePageValveTest.java 757213 2009-03-22 16:43:31Z tv $
049 */
050 public class ExecutePageValveTest extends BaseTestCase
051 {
052 private static TurbineConfig tc = null;
053 private static TemplateService ts = null;
054 private MockServletConfig config = null;
055 private EnhancedMockHttpServletRequest request = null;
056 private EnhancedMockHttpSession session = null;
057 private HttpServletResponse response = null;
058 private static ServletConfig sc = null;
059 /**
060 * Constructor
061 */
062 public ExecutePageValveTest(String testName) throws Exception
063 {
064 super(testName);
065 }
066
067 protected void setUp() throws Exception
068 {
069 super.setUp();
070 config = new MockServletConfig();
071 config.setupNoParameters();
072 request = new EnhancedMockHttpServletRequest();
073 request.setupServerName("bob");
074 request.setupGetProtocol("http");
075 request.setupScheme("scheme");
076 request.setupPathInfo("damn");
077 request.setupGetServletPath("damn2");
078 request.setupGetContextPath("wow");
079 request.setupGetContentType("html/text");
080 request.setupAddHeader("Content-type", "html/text");
081 request.setupAddHeader("Accept-Language", "en-US");
082
083 session = new EnhancedMockHttpSession();
084 response = new EnhancedMockHttpServletResponse();
085
086 request.setSession(session);
087
088 sc = config;
089 tc =
090 new TurbineConfig(
091 ".",
092 "/conf/test/CompleteTurbineResources.properties");
093 tc.initialize();
094 }
095
096 public void testValve() throws Exception
097 {
098
099
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 }