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