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.modules.actions;
18  
19  import junit.framework.*;
20  import org.apache.cactus.*;
21  
22  import org.apache.turbine.modules.ActionLoader;
23  import org.apache.turbine.util.RunData;
24  import org.apache.turbine.util.RunDataFactory;
25  
26  /***
27   * Tests JLoginUser.
28   *
29   * @author <a href="mailto:johnny.cass@epiuse.com">Johnny Cass</a>
30   * @see JLoginUser
31   */
32  public class TestJLoginUser extends ServletTestCase 
33  {
34      
35      /***
36       * Defines the testcase name for JUnit.
37       *
38       * @param name the testcase's name.
39       */
40      public TestJLoginUser ( String name ) 
41      {
42          super( name );
43      }
44      
45      /***
46       * Start the tests.
47       *
48       * @param args the arguments. Not used
49       */
50      public static void main ( String[] args ) 
51      {
52          junit.textui.TestRunner.main ( new String[] { TestJLoginUser.class.getName () } );
53      }
54      
55      /***
56       * Creates the test suite.
57       *
58       * @return a test suite (<code>TestSuite</code>) that includes all methods
59       *         starting with "test"
60       */
61      public static Test suite () 
62      {
63          // All methods starting with "test" will be executed in the test suite.
64          return new TestSuite ( TestJLoginUser.class );
65      }
66      
67      /***
68       * Sets up the test case.
69       *
70       */
71      protected void setUp () throws Exception 
72      {
73          // Create the RunData object to be used during testing.        
74          runData = RunDataFactory.getRunData ( request, response, config );
75      }
76      
77      /***
78       * Tests if an invalid user is logged in correctly.
79       *
80       */
81      public void testLoginInvalidUser () throws Exception 
82      {        
83          // Shouldn't have a user object available at this stage.
84          assertNull ( "User should be 'null' since we have not logged in " +
85          "yet - runData.getUser () ", runData.getUser () );
86          
87          // Add parameters to the RunData that will be used to login a non existent user.
88          runData.getParameters ().setString ( "username", "" );
89          runData.getParameters ().setString ( "password", "" );
90          
91          // Execute the login action.
92          ActionLoader.getInstance ().exec ( runData, "JLoginUser" );
93          
94          // See if we can find a user object.
95          assertNotNull ( "User object not found in RunData - runData.getUser () ",
96          runData.getUser () );
97          
98          // Since the user does not exist, the login should have failed.
99          assertEquals ( "Logged in - runData.getUser ().hasLoggedIn ()",
100         false, runData.getUser ().hasLoggedIn () );                        
101     }
102     
103     /***
104      * Tests if a valid user is logged in correctly.
105      *
106      */
107     public void testLoginValidUser () throws Exception 
108     {        
109         // Shouldn't have a user object available at this stage.
110         assertNull ( "User should be 'null' since we have not logged in " +
111         "yet - runData.getUser () ", runData.getUser () );
112         
113         // Add parameters to the RunData that will be used to login a valid user.
114         runData.getParameters ().setString ( "username", "turbine" );
115         runData.getParameters ().setString ( "password", "turbine" );
116         
117         // Execute the login action.
118         ActionLoader.getInstance ().exec ( runData, "JLoginUser" );
119         
120         // See if we can find a user object.
121         assertNotNull ( "User object not found in RunData - runData.getUser () ",
122         runData.getUser () );
123         
124         // Since we know the user exists, the login should have been successful/
125         assertEquals ( "Not logged in - runData.getUser ().hasLoggedIn ()",
126         true, runData.getUser ().hasLoggedIn () );                
127     }
128     
129     private RunData runData = null;
130 }