1   /*
2    * Copyright 2000-2001,2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.jetspeed.test;
18  
19  // Turbine imports
20  import org.apache.turbine.modules.actions.sessionvalidator.SessionValidator;
21  import org.apache.turbine.modules.ActionLoader;
22  import org.apache.turbine.modules.PageLoader;
23  import org.apache.turbine.services.resources.TurbineResources;
24  import org.apache.turbine.services.template.TurbineTemplate;
25  import org.apache.turbine.util.RunData;
26  
27  /***
28   * This class is designed to aid in the testing of Jetspeed
29   * page generation.  It includes methods that mimic what
30   * is done in turbine.
31   *
32   * As a minimum you test case should look like:
33   * <CODE>
34   * {
35   *  setupRunData(rundata);
36   *  generatePage(rundata);
37   *  outputPage(rundata);
38   * }
39   * </CODE>
40   *
41   * @author <a href="paulsp@apache.org">Paul Spencer</a>
42   * @version $Id: TurbineTestUtilities.java,v 1.1 2004/04/07 22:02:41 jford Exp $
43  */
44  public abstract class TurbineTestUtilities
45  {
46     /***
47       * Do all of the initialization and setup of RunData.  This includes
48       * setting up the session, users.
49       *
50       * Note: This code is modeled after Turbine's org.apache.turbine.Turbine
51       *
52       * @param rundata Rundata to setup
53       * @throws Exception General exceptions
54       */    
55      public static void setupRunData(RunData rundata) throws Exception
56      {
57          if (rundata == null)
58              throw new NullPointerException("rundata is null");
59          
60          // Get the instance of the Session Validator.
61          SessionValidator sessionValidator = (SessionValidator)ActionLoader
62               .getInstance().getInstance(TurbineResources.getString(
63                   "action.sessionvalidator"));
64          if (sessionValidator == null)
65              throw new NullPointerException("Failed to get a SessonValidator");
66  
67          // Fill in the screen and action variables.
68          rundata.setScreen( rundata.getParameters().getString("screen") );
69          rundata.setAction( rundata.getParameters().getString("action") );
70          
71          // Login or out if requested
72          if ( rundata.hasAction()
73             && rundata.getAction().equalsIgnoreCase(TurbineResources
74                .getString("action.login"))
75             || rundata.getAction().equalsIgnoreCase(TurbineResources
76                .getString("action.logout")))
77          {
78              if (rundata.getAction().equalsIgnoreCase(TurbineResources
79                .getString("action.login")))
80              {
81                  String[] names = rundata.getSession().getValueNames();
82                  if (names != null)
83                  {
84                      for (int i=0; i< names.length; i++)
85                      {
86                          rundata.getSession().removeValue(names[i]);
87                      }
88                  }
89              }
90              ActionLoader.getInstance().exec( rundata, rundata.getAction() );
91              rundata.setAction(null);
92          }
93  
94          // Do the session validation
95          ActionLoader.getInstance().exec(
96             rundata,TurbineResources.getString("action.sessionvalidator") );
97          
98          // Put the access control list (ACL) into rundata.
99          ActionLoader.getInstance().exec(
100            rundata,TurbineResources.getString("action.accesscontroller"));
101         
102     }
103     
104     /***
105      * Generate the page/content defined by rundata
106      * @param rundata Rundata to setup
107      * @throws Exception General exceptions
108      */    
109     public static void generatePage(RunData rundata) throws Exception
110     {
111         if (rundata == null)
112             throw new NullPointerException("rundata is null");
113         
114         String defaultPage = TurbineTemplate.getDefaultPageName(rundata);
115         PageLoader.getInstance().exec(rundata, defaultPage);
116     }
117 
118     /***
119      * Instuct turbine, via rundata, to output the page.
120      *
121      * Note: This code is modeled after Turbine's org.apache.turbine.Turbine
122      *
123      * @param rundata Rundata to setup
124      * @throws Exception General exceptions
125      */    
126     public static void outputPage(RunData rundata) throws Exception
127     {
128         if (rundata == null)
129             throw new NullPointerException("rundata is null");
130         
131         rundata.getResponse().setLocale( rundata.getLocale() );
132         rundata.getResponse().setContentType( rundata.getContentType() );
133         rundata.getResponse().setStatus( rundata.getStatusCode() );
134         rundata.getPage().output(rundata.getOut());
135         
136         try
137         {
138             rundata.getOut().close();
139         }
140         catch (Exception e)
141         {
142             // Ignore.
143         }
144     }
145 
146 }