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  // Junit imports
20  import junit.awtui.TestRunner;
21  import junit.framework.Test;
22  import junit.framework.TestSuite;
23  
24  // Cactus imports
25  import org.apache.cactus.ServletTestCase;
26  import org.apache.cactus.WebRequest;
27  import org.apache.cactus.WebResponse;
28  
29  // Jetspeed imports
30  import org.apache.jetspeed.capability.CapabilityMap;
31  import org.apache.jetspeed.om.profile.Profile;
32  import org.apache.jetspeed.om.profile.ProfileLocator;
33  import org.apache.jetspeed.om.profile.PSMLDocument;
34  import org.apache.jetspeed.services.Profiler;
35  import org.apache.jetspeed.services.rundata.JetspeedRunData;
36  import org.apache.jetspeed.test.TurbineTestUtilities;
37  
38  // Turbine imports
39  import org.apache.turbine.services.pull.TurbinePull;
40  import org.apache.turbine.services.velocity.TurbineVelocity;
41  import org.apache.turbine.util.RunData;
42  import org.apache.turbine.util.RunDataFactory;
43  
44  import org.apache.jetspeed.om.security.JetspeedUser;
45  
46  // Velocity
47  import org.apache.velocity.context.Context;
48  
49  /***
50   *
51   * @author <a href="paulsp@apache.org">Paul Spencer</a>
52   * @version $Id $
53   */
54  
55  public class TestBasicSanity extends ServletTestCase
56  {
57      private static String TEST_ANON_USER_NAME = "";
58      private static String TEST_CONTEXT = null;
59      private static String TEST_DEFAULT_PAGE = "default";
60      private static String TEST_HOST = "localhost";
61      private static String TEST_SERVLET = "/portal";
62      private static String TEST_GROUP = "apache";
63      private static String TEST_PAGE = "news";
64      private static String TEST_USER = "turbine";
65      private static String TEST_USER_PASSWORD ="turbine";
66      
67      private RunData rundata = null;
68  
69      /***
70       * Defines the testcase name for JUnit.
71       *
72       * @param name the testcase's name.
73       */
74      public TestBasicSanity(String name)
75      {
76          super( name );
77      }
78  
79      /***
80       * Start the tests.
81       *
82       * @param args the arguments. Not used
83       */
84      public static void main(String args[])
85      {
86          TestRunner.main( new String[] { TestBasicSanity.class.getName() } );
87      }
88      
89      /***
90       * Creates the test suite.
91       *
92       * @return a test suite (<code>TestSuite</code>) that includes all methods
93       *         starting with "test"
94       */
95      public static Test suite()
96      {
97          // All methods starting with "test" will be executed in the test suite.
98          return new TestSuite( TestBasicSanity.class );
99      }
100     
101     /***
102      * Sets up the test case.
103      *
104      */
105     protected void setUp () throws Exception 
106     {
107     }
108     
109     /***
110      *  Test: DefaultURL
111      *  With the default URL "/"
112      *    1) A page is generated 
113      *    2) The user is anonymous
114      *    3) Group and Role are not set
115      */
116     public void beginDefaultURL(WebRequest theRequest)
117     {
118         System.out.println("URL = " + theRequest.getURL());
119         theRequest.setURL(TEST_HOST, TEST_CONTEXT, TEST_SERVLET, "", null); 
120         System.out.println("post set URL = " + theRequest.getURL());
121     }
122 
123     /***
124      * Execute the test
125      *
126      * @throws Exception
127      */
128     public void testDefaultURL() throws Exception
129     {
130         // Create the RunData object to be used during testing.        
131         rundata = RunDataFactory.getRunData ( request, response, config );
132         assertNotNull( "Got rundata", rundata);
133         TurbineTestUtilities.setupRunData(rundata);
134 
135         // Verify we have a user
136         JetspeedUser user = (JetspeedUser)rundata.getUser();
137         assertNotNull( "Got user", user);
138 
139         // Verify we have a CapabilityMap
140         CapabilityMap cm = ((JetspeedRunData)rundata).getCapability();
141         assertNotNull( "Got Capability", cm);
142 
143         // Verify we have a profile
144         Profile profile = Profiler.getProfile(rundata);
145         assertNotNull( "Got profile from Profiler", profile);
146         
147         // Verify the profile location information in the profile
148         if (profile instanceof ProfileLocator)
149         {
150             ProfileLocator profileLocator = (ProfileLocator) profile;
151             assertTrue("Verify the 'anonymous' is set", profileLocator.getAnonymous());
152             assertNull("Verify the group is null", profileLocator.getGroup());
153             assertNull("Verify the role is null", profileLocator.getRole());
154         } else
155         {
156             assertTrue( "profile does not implement ProfileLocator", false);
157         }
158         
159 
160         // Verify we have a Document
161         PSMLDocument psmlDoc = profile.getDocument();
162         assertNotNull( "Got psmlDocument", psmlDoc);
163 
164         System.out.println("DocumentName = " + profile.getDocument().getName()); 
165 
166         // Get and populate the context
167         Context context = TurbineVelocity.getContext(rundata);
168         assertNotNull( "Got context", context);
169         TurbinePull.populateContext( context, rundata);
170 
171         // Verify tool are in the context
172         assertNotNull( "Got jlink from context", context.get("jlink"));
173 
174         // Generatate and output thre page
175         TurbineTestUtilities.generatePage(rundata);
176         TurbineTestUtilities.outputPage(rundata);
177 
178         // Return the used RunData to the factory for recycling.
179         RunDataFactory.putRunData(rundata);
180     }
181 
182     public void endDefaultURL(org.apache.cactus.WebResponse theResponse)
183     {
184         System.out.println("text length = " + theResponse.getText().length());
185 //        System.out.println("text length = " + theResponse.getText());
186     }
187 
188     /***
189      *  Test: GroupURL
190      *  With the default URL "/group/apache"
191      *    1) A page is generated 
192      *    2) The user is anonymous
193      *    3) Group is set to "apache"
194      *    4) Role is not set
195      */
196     public void beginGroupUrl(WebRequest theRequest)
197     {
198         System.out.println("URL = " + theRequest.getURL());
199         theRequest.setURL(TEST_HOST, TEST_CONTEXT, TEST_SERVLET
200           , "/group/" + TEST_GROUP , null); 
201         System.out.println("post set URL = " + theRequest.getURL());
202     }
203 
204     /***
205      * Test the Group link
206      * @throws Exception
207      */
208     public void testGroupUrl() throws Exception
209     {
210         // Create the RunData object to be used during testing.        
211         rundata = RunDataFactory.getRunData ( request, response, config );
212         assertNotNull( "Got rundata", rundata);
213 
214         TurbineTestUtilities.setupRunData(rundata);
215 
216         // Verify we have a profile
217         Profile profile = Profiler.getProfile(rundata);
218         assertNotNull( "Got profile from Profiler", profile);
219         
220         // Verify the profile location information in the profile
221         if (profile instanceof ProfileLocator)
222         {
223             // FIXME: Need to verify 'anonymous' and group name
224             ProfileLocator profileLocator = (ProfileLocator) profile;
225 //            assertTrue("Verify the 'anonymous' is set", profileLocator.getAnonymous());
226             assertNotNull("Verify the group is not null", profileLocator.getGroup());
227             assertNull("Verify the role is null", profileLocator.getRole());
228         } else
229         {
230             assertTrue( "profile does not implement ProfileLocator", false);
231         }
232         TurbineTestUtilities.generatePage(rundata);
233         TurbineTestUtilities.outputPage(rundata);
234 
235         // Return the used RunData to the factory for recycling.
236         RunDataFactory.putRunData(rundata);
237     }
238     
239     public void endGroupURL(WebResponse theResponse)
240     {
241         System.out.println("text length = " + theResponse.getText().length());
242 //        System.out.println("text length = " + theResponse.getText());
243     }
244 
245     /***
246      *  Test: PageURL
247      *  With the page URL "/page/apache"
248      *    1) A page is generated 
249      *    2) The user is anonymous
250      *    3) Group is set to "apache"
251      *    4) Role is not set
252      */
253     public void beginPageUrl(WebRequest theRequest)
254     {
255         System.out.println("URL = " + theRequest.getURL());
256         theRequest.setURL(TEST_HOST, TEST_CONTEXT, TEST_SERVLET
257           , "/page/" + TEST_PAGE , null); 
258         System.out.println("post set URL = " + theRequest.getURL());
259     }
260 
261     /***
262      * Test the PageURL
263      *
264      * @throws Exception
265      */
266     public void testPageUrl() throws Exception
267     {
268         // Create the RunData object to be used during testing.        
269         rundata = RunDataFactory.getRunData ( request, response, config );
270         assertNotNull( "Got rundata", rundata);
271 
272         TurbineTestUtilities.setupRunData(rundata);
273 
274         // Verify we have a profile
275         Profile profile = Profiler.getProfile(rundata);
276         assertNotNull( "Got profile from Profiler", profile);
277         
278         // Verify the profile location information in the profile
279         if (profile instanceof ProfileLocator)
280         {
281             ProfileLocator profileLocator = (ProfileLocator) profile;
282             // FIXME: Need to verify 'anonymous' and page name
283             assertTrue("Verify the 'anonymous' is set", profileLocator.getAnonymous());
284             assertNull("Verify the group is null", profileLocator.getGroup());
285             assertNull("Verify the role is null", profileLocator.getRole());
286             assertEquals("Verify the page name", profileLocator.getName(), TEST_PAGE + ".psml");
287         } else
288         {
289             assertTrue( "profile does not implement ProfileLocator", false);
290         }
291         TurbineTestUtilities.generatePage(rundata);
292         TurbineTestUtilities.outputPage(rundata);
293 
294         // Return the used RunData to the factory for recycling.
295         RunDataFactory.putRunData(rundata);
296     }
297     
298     public void endPageURL(WebResponse theResponse)
299     {
300         System.out.println("text length = " + theResponse.getText().length());
301 //        System.out.println("text length = " + theResponse.getText());
302     }
303     /***
304      *  Test: PageURL
305      *  With the page URL "/page/apache"
306      *    1) A page is generated 
307      *    2) The user is anonymous
308      *    3) Group is set to "apache"
309      *    4) Role is not set
310      */
311     public void beginUserPageUrl(WebRequest theRequest)
312     {
313         System.out.println("URL = " + theRequest.getURL());
314         theRequest.setURL(TEST_HOST, TEST_CONTEXT, TEST_SERVLET
315           , "/page/" + TEST_DEFAULT_PAGE ,"action=JLoginUser&username=turbine&password=turbine"); 
316         System.out.println("post set URL = " + theRequest.getURL());
317     }
318 
319     /***
320      * Test the PageURL
321      *
322      * @throws Exception
323      */
324     public void testUserPageUrl() throws Exception
325     {
326         // Create the RunData object to be used during testing.        
327         rundata = RunDataFactory.getRunData ( request, response, config );
328         assertNotNull( "Got rundata", rundata);
329 
330         TurbineTestUtilities.setupRunData(rundata);
331 
332         // Verify we have a profile
333         Profile profile = Profiler.getProfile(rundata);
334         assertNotNull( "Got profile from Profiler", profile);
335         
336         // Verify the profile location information in the profile
337         if (profile instanceof ProfileLocator)
338         {
339             ProfileLocator profileLocator = (ProfileLocator) profile;
340             // FIXME: Need to verify 'anonymous' and page name
341             assertTrue("Verify the 'anonymous' is not", !profileLocator.getAnonymous());
342             assertNull("Verify the group is null", profileLocator.getGroup());
343             assertNull("Verify the role is null", profileLocator.getRole());
344             assertNotNull("Verify the user is not null", profileLocator.getUser());
345             assertTrue("Verify the user is logged in", profileLocator.getUser().hasLoggedIn());
346             assertEquals("Verify the user's username", profileLocator.getUser().getUserName(),TEST_USER);
347             assertEquals("Verify the page name", profileLocator.getName(), TEST_DEFAULT_PAGE + ".psml");
348         } else
349         {
350             assertTrue( "profile does not implement ProfileLocator", false);
351         }
352         TurbineTestUtilities.generatePage(rundata);
353         TurbineTestUtilities.outputPage(rundata);
354 
355         // Return the used RunData to the factory for recycling.
356         RunDataFactory.putRunData(rundata);
357     }
358     
359     public void endUserPageURL(WebResponse theResponse)
360     {
361         System.out.println("text length = " + theResponse.getText().length());
362 //        System.out.println("text length = " + theResponse.getText());
363     }
364 }