1   /* Copyright 2004 Acegi Technology Pty Limited
2    *
3    * Licensed under the Apache License, Version 2.0 (the "License");
4    * you may not use this file except in compliance with the License.
5    * You may obtain a copy of the License at
6    *
7    *     http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  
16  package org.acegisecurity.providers.rcp;
17  
18  import junit.framework.TestCase;
19  
20  import org.acegisecurity.Authentication;
21  import org.acegisecurity.GrantedAuthority;
22  import org.acegisecurity.GrantedAuthorityImpl;
23  import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
24  
25  
26  /***
27   * Tests {@link RemoteAuthenticationProvider}.
28   *
29   * @author Ben Alex
30   * @version $Id: RemoteAuthenticationProviderTests.java,v 1.2 2005/11/17 00:56:09 benalex Exp $
31   */
32  public class RemoteAuthenticationProviderTests extends TestCase {
33      //~ Methods ================================================================
34  
35      public final void setUp() throws Exception {
36          super.setUp();
37      }
38  
39      public static void main(String[] args) {
40          junit.textui.TestRunner.run(RemoteAuthenticationProviderTests.class);
41      }
42  
43      public void testExceptionsGetPassedBackToCaller() {
44          RemoteAuthenticationProvider provider = new RemoteAuthenticationProvider();
45          provider.setRemoteAuthenticationManager(new MockRemoteAuthenticationManager(
46                  false));
47  
48          try {
49              provider.authenticate(new UsernamePasswordAuthenticationToken(
50                      "marissa", "password"));
51              fail("Should have thrown RemoteAuthenticationException");
52          } catch (RemoteAuthenticationException expected) {
53              assertTrue(true);
54          }
55      }
56  
57      public void testGettersSetters() {
58          RemoteAuthenticationProvider provider = new RemoteAuthenticationProvider();
59          provider.setRemoteAuthenticationManager(new MockRemoteAuthenticationManager(
60                  true));
61          assertNotNull(provider.getRemoteAuthenticationManager());
62      }
63  
64      public void testStartupChecksAuthenticationManagerSet()
65          throws Exception {
66          RemoteAuthenticationProvider provider = new RemoteAuthenticationProvider();
67  
68          try {
69              provider.afterPropertiesSet();
70              fail("Should have thrown IllegalArgumentException");
71          } catch (IllegalArgumentException expected) {
72              assertTrue(true);
73          }
74  
75          provider.setRemoteAuthenticationManager(new MockRemoteAuthenticationManager(
76                  true));
77          provider.afterPropertiesSet();
78          assertTrue(true);
79      }
80  
81      public void testSuccessfulAuthenticationCreatesObject() {
82          RemoteAuthenticationProvider provider = new RemoteAuthenticationProvider();
83          provider.setRemoteAuthenticationManager(new MockRemoteAuthenticationManager(
84                  true));
85  
86          Authentication result = provider.authenticate(new UsernamePasswordAuthenticationToken(
87                      "marissa", "password"));
88          assertEquals("marissa", result.getPrincipal());
89          assertEquals("password", result.getCredentials());
90          assertEquals("foo", result.getAuthorities()[0].getAuthority());
91      }
92  
93      public void testSupports() {
94          RemoteAuthenticationProvider provider = new RemoteAuthenticationProvider();
95          assertTrue(provider.supports(UsernamePasswordAuthenticationToken.class));
96      }
97  
98      //~ Inner Classes ==========================================================
99  
100     private class MockRemoteAuthenticationManager
101         implements RemoteAuthenticationManager {
102         private boolean grantAccess;
103 
104         public MockRemoteAuthenticationManager(boolean grantAccess) {
105             this.grantAccess = grantAccess;
106         }
107 
108         public GrantedAuthority[] attemptAuthentication(String username,
109             String password) throws RemoteAuthenticationException {
110             if (grantAccess) {
111                 return new GrantedAuthority[] {new GrantedAuthorityImpl("foo")};
112             } else {
113                 throw new RemoteAuthenticationException("as requested");
114             }
115         }
116     }
117 }