View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.shiro.authz;
20  
21  import static org.junit.Assert.assertNull;
22  import static org.junit.Assert.assertTrue;
23  
24  import java.util.ArrayList;
25  import java.util.Collection;
26  
27  import org.apache.shiro.authc.AuthenticationException;
28  import org.apache.shiro.authc.AuthenticationInfo;
29  import org.apache.shiro.authc.AuthenticationToken;
30  import org.apache.shiro.authz.permission.RolePermissionResolver;
31  import org.apache.shiro.realm.AuthorizingRealm;
32  import org.apache.shiro.realm.Realm;
33  import org.apache.shiro.subject.PrincipalCollection;
34  import org.junit.Test;
35  
36  public class ModularRealmAuthorizerTest
37  {
38      
39      @Test
40      public void testSettingOfRolePermissionResolver()
41      {
42          Collection<Realm> realms = new ArrayList<Realm>();
43          
44          realms.add( new MockAuthorizingRealm() );
45          realms.add( new MockAuthorizingRealm() );
46          
47          // its null to start with
48          for ( Realm realm : realms )
49          {
50              assertNull( ((AuthorizingRealm)realm).getRolePermissionResolver() );
51          }
52          
53          ModularRealmAuthorizer modRealmAuthz = new ModularRealmAuthorizer();
54          modRealmAuthz.setRealms( realms );
55          
56          // make sure they are still null
57          for ( Realm realm : realms )
58          {
59              assertNull( ((AuthorizingRealm)realm).getRolePermissionResolver() );
60          }
61          
62          // now set the RolePermissionResolver
63          RolePermissionResolver rolePermissionResolver = new RolePermissionResolver()
64          {   
65              public Collection<Permission> resolvePermissionsInRole( String roleString )
66              {
67                  return null;
68              }
69          };
70          modRealmAuthz.setRolePermissionResolver( rolePermissionResolver );
71       
72          // make sure they are set
73          for ( Realm realm : realms )
74          {
75              // check for same instance
76              assertTrue( ((AuthorizingRealm)realm).getRolePermissionResolver() == rolePermissionResolver );
77          }
78          
79          // add a new realm and make sure the RolePermissionResolver is set
80          MockAuthorizingRealm mockRealm = new MockAuthorizingRealm();
81          realms.add( mockRealm );
82          modRealmAuthz.setRealms( realms );
83          assertTrue( ((AuthorizingRealm) mockRealm).getRolePermissionResolver() == rolePermissionResolver );
84          
85          
86          // TODO: no way to unset them, not sure if that is a valid use case, but this is conistent with the PermissionResolver logic
87  //        // now just to be sure, unset them
88  //        modRealmAuthz.setRolePermissionResolver( null );
89  //        for ( Realm realm : realms )
90  //        {
91  //            assertNull( ((AuthorizingRealm)realm).getRolePermissionResolver() );
92  //        }
93          
94          
95      }
96      
97      class MockAuthorizingRealm extends AuthorizingRealm
98      {
99  
100         @Override
101         protected AuthorizationInfo doGetAuthorizationInfo( PrincipalCollection principals )
102         {
103             return null;
104         }
105 
106         @Override
107         protected AuthenticationInfo doGetAuthenticationInfo( AuthenticationToken token )
108             throws AuthenticationException
109         {
110             return null;
111         }
112         
113     }
114 }