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.acl;
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.acl.basic.NamedEntityObjectIdentity;
24  import org.acegisecurity.acl.basic.SimpleAclEntry;
25  import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
26  
27  import java.util.List;
28  import java.util.Vector;
29  
30  
31  /***
32   * Tests {@link AclProviderManager}.
33   *
34   * @author Ben Alex
35   * @version $Id: AclProviderManagerTests.java,v 1.2 2005/11/17 00:56:07 benalex Exp $
36   */
37  public class AclProviderManagerTests extends TestCase {
38      //~ Constructors ===========================================================
39  
40      public AclProviderManagerTests() {
41          super();
42      }
43  
44      public AclProviderManagerTests(String arg0) {
45          super(arg0);
46      }
47  
48      //~ Methods ================================================================
49  
50      public final void setUp() throws Exception {
51          super.setUp();
52      }
53  
54      public static void main(String[] args) {
55          junit.textui.TestRunner.run(AclProviderManagerTests.class);
56      }
57  
58      public void testAclLookupFails() {
59          AclProviderManager mgr = makeProviderManager();
60          assertNull(mgr.getAcls(new Integer(5)));
61      }
62  
63      public void testAclLookupForGivenAuthenticationSuccess() {
64          AclProviderManager mgr = makeProviderManager();
65          assertNotNull(mgr.getAcls("STRING",
66                  new UsernamePasswordAuthenticationToken("marissa", "not used")));
67      }
68  
69      public void testAclLookupSuccess() {
70          AclProviderManager mgr = makeProviderManager();
71          assertNotNull(mgr.getAcls("STRING"));
72      }
73  
74      public void testRejectsNulls() {
75          AclProviderManager mgr = new AclProviderManager();
76  
77          try {
78              mgr.getAcls(null);
79              fail("Should have thrown IllegalArgumentException");
80          } catch (IllegalArgumentException expected) {
81              assertTrue(true);
82          }
83  
84          try {
85              mgr.getAcls(null,
86                  new UsernamePasswordAuthenticationToken("marissa", "not used"));
87              fail("Should have thrown IllegalArgumentException");
88          } catch (IllegalArgumentException expected) {
89              assertTrue(true);
90          }
91  
92          try {
93              mgr.getAcls("SOME_DOMAIN_INSTANCE", null);
94              fail("Should have thrown IllegalArgumentException");
95          } catch (IllegalArgumentException expected) {
96              assertTrue(true);
97          }
98      }
99  
100     public void testReturnsNullIfNoSupportingProvider() {
101         AclProviderManager mgr = makeProviderManager();
102         assertNull(mgr.getAcls(new Integer(4),
103                 new UsernamePasswordAuthenticationToken("marissa", "not used")));
104         assertNull(mgr.getAcls(new Integer(4)));
105     }
106 
107     public void testStartupFailsIfProviderListNotContainingProviders()
108         throws Exception {
109         List providers = new Vector();
110         providers.add("THIS_IS_NOT_A_PROVIDER");
111 
112         AclProviderManager mgr = new AclProviderManager();
113 
114         try {
115             mgr.setProviders(providers);
116             fail("Should have thrown IllegalArgumentException");
117         } catch (IllegalArgumentException expected) {
118             assertTrue(true);
119         }
120     }
121 
122     public void testStartupFailsIfProviderListNotSet()
123         throws Exception {
124         AclProviderManager mgr = new AclProviderManager();
125 
126         try {
127             mgr.afterPropertiesSet();
128             fail("Should have thrown IllegalArgumentException");
129         } catch (IllegalArgumentException expected) {
130             assertTrue(true);
131         }
132     }
133 
134     public void testStartupFailsIfProviderListNull() throws Exception {
135         AclProviderManager mgr = new AclProviderManager();
136 
137         try {
138             mgr.setProviders(null);
139             fail("Should have thrown IllegalArgumentException");
140         } catch (IllegalArgumentException expected) {
141             assertTrue(true);
142         }
143     }
144 
145     public void testSuccessfulStartup() throws Exception {
146         AclProviderManager mgr = makeProviderManager();
147         mgr.afterPropertiesSet();
148         assertTrue(true);
149         assertEquals(1, mgr.getProviders().size());
150     }
151 
152     private AclProviderManager makeProviderManager() {
153         MockProvider provider1 = new MockProvider();
154         List providers = new Vector();
155         providers.add(provider1);
156 
157         AclProviderManager mgr = new AclProviderManager();
158         mgr.setProviders(providers);
159 
160         return mgr;
161     }
162 
163     //~ Inner Classes ==========================================================
164 
165     private class MockProvider implements AclProvider {
166         private UsernamePasswordAuthenticationToken marissa = new UsernamePasswordAuthenticationToken("marissa",
167                 "not used",
168                 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_FOO"), new GrantedAuthorityImpl("ROLE_BAR")});
169         private SimpleAclEntry entry100Marissa = new SimpleAclEntry(marissa
170                 .getPrincipal(),
171                 new NamedEntityObjectIdentity("OBJECT", "100"), null, 2);
172         private UsernamePasswordAuthenticationToken scott = new UsernamePasswordAuthenticationToken("scott",
173                 "not used",
174                 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_FOO"), new GrantedAuthorityImpl("ROLE_MANAGER")});
175         private SimpleAclEntry entry100Scott = new SimpleAclEntry(scott
176                 .getPrincipal(),
177                 new NamedEntityObjectIdentity("OBJECT", "100"), null, 4);
178 
179         public AclEntry[] getAcls(Object domainInstance,
180             Authentication authentication) {
181             if (authentication.getPrincipal().equals(scott.getPrincipal())) {
182                 return new AclEntry[] {entry100Scott};
183             }
184 
185             if (authentication.getPrincipal().equals(marissa.getPrincipal())) {
186                 return new AclEntry[] {entry100Marissa};
187             }
188 
189             return null;
190         }
191 
192         public AclEntry[] getAcls(Object domainInstance) {
193             return new AclEntry[] {entry100Marissa, entry100Scott};
194         }
195 
196         /***
197          * Only supports <code>Object</code>s of type <code>String</code>
198          *
199          * @param domainInstance DOCUMENT ME!
200          *
201          * @return DOCUMENT ME!
202          */
203         public boolean supports(Object domainInstance) {
204             return (domainInstance instanceof String);
205         }
206     }
207 }