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.services.security;
18  
19  // Junit imports
20  import junit.framework.Test;
21  import junit.framework.TestSuite;
22  
23  import org.apache.turbine.services.TurbineServices;
24  import org.apache.turbine.util.TurbineConfig;
25  import org.apache.turbine.util.StringUtils;
26  
27  // Jetspeed imports
28  import org.apache.jetspeed.test.JetspeedTestCase;
29  import org.apache.jetspeed.om.security.JetspeedUser;
30  
31  /***
32   * Unit test for PortalAuthentication interface
33   * 
34   * @author <a href="mailto:david@bluesunrise.com">David Sean Taylor</a>
35   * @version $Id: TestPortalAuthentication.java,v 1.1 2004/04/07 22:02:43 jford Exp $
36   */
37  
38  public class TestPortalAuthentication extends JetspeedTestCase {    
39  
40      /***
41       * Defines the testcase name for JUnit.
42       *
43       * @param name the testcase's name.
44       */
45      public TestPortalAuthentication( String name ) {
46          super( name );
47      }
48      
49      /***
50       * Start the tests.
51       *
52       * @param args the arguments. Not used
53       */
54      public static void main(String args[]) 
55      {
56          junit.awtui.TestRunner.main( new String[] { TestPortalAuthentication.class.getName() } );
57      }
58   
59      public void setup() 
60      {
61          System.out.println("Setup: Testing Turbine Authentication");
62      }
63  
64      /***
65       * Creates the test suite.
66       *
67       * @return a test suite (<code>TestSuite</code>) that includes all methods
68       *         starting with "test"
69       */
70      public static Test suite() 
71      {
72          // All methods starting with "test" will be executed in the test suite.
73          return new TestSuite( TestPortalAuthentication.class );
74      }
75  
76  
77      /***
78       * Tests PortalAuthentication.login()
79       *
80       * @throws Exception
81       */
82  
83      public void testLogin() throws Exception 
84      {
85          setup();
86  
87          PortalAuthentication service = getService();
88          JetspeedUser user = null;
89  
90          try
91          {
92              user = service.login("baduser", "badpassword");
93          }
94          catch (Exception e)
95          {
96              System.out.println("exception = " + e.toString());
97              assertTrue(e instanceof FailedLoginException);
98          }
99          try
100         {
101             user = service.login("turbine", "badpassword");
102         }
103         catch (Exception e)
104         {
105             System.out.println("e = " + e);
106             assertTrue(e instanceof FailedLoginException);
107         }
108         try
109         {
110             user = service.login("turbine", "turbine");
111         }
112         catch (Exception e)
113         {
114             fail(StringUtils.stackTrace(e));
115         }
116 
117         assertTrue(user.hasLoggedIn() == true);
118 
119         System.out.println("Completed Login Test OK ");
120 
121     }
122 
123     /***
124      * Tests PortalAuthentication.login()
125      *
126      * @throws Exception
127      */
128 
129     public void testGetAnonymousUser()
130     {
131         setup();
132 
133         PortalAuthentication service = getService();
134         JetspeedUser user = null;
135 
136         try
137         {
138             user = service.getAnonymousUser();
139         }
140         catch (Exception e)
141         {
142             fail(StringUtils.stackTrace(e));
143         }
144 
145         assertTrue(user.hasLoggedIn() == false);
146 
147         System.out.println("Completed Anonymous Test OK");
148 
149     }
150 
151 
152   /*
153     Configuration object to run Turbine outside a servlet container
154     ( uses turbine.properties )
155     */
156     private static TurbineConfig config = null;
157     
158     /***
159     Sets up TurbineConfig using the system property:
160     <pre>turbine.properties</pre>
161     */
162     static
163     {
164         try
165         {
166             config = new TurbineConfig( "webapp", "/WEB-INF/conf/TurbineResources.properties");
167             config.init();
168         }
169         catch (Exception e)
170         {
171             fail(StringUtils.stackTrace(e));
172         }
173     }
174 
175     private static PortalAuthentication getService()
176     {
177         return (PortalAuthentication)TurbineServices
178                 .getInstance()
179                 .getService(PortalAuthentication.SERVICE_NAME);
180     }
181 
182 }
183 
184 
185