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.assertTrue;
22  
23  import java.util.ArrayList;
24  import java.util.Collection;
25  
26  import junit.framework.Assert;
27  
28  import org.apache.shiro.authc.AuthenticationException;
29  import org.apache.shiro.authc.AuthenticationInfo;
30  import org.apache.shiro.authc.AuthenticationToken;
31  import org.apache.shiro.authz.permission.RolePermissionResolver;
32  import org.apache.shiro.realm.AuthorizingRealm;
33  import org.apache.shiro.realm.Realm;
34  import org.apache.shiro.subject.PrincipalCollection;
35  import org.junit.Test;
36  
37  public class ModularRealmAuthorizerTest
38  {
39      
40      @Test
41      public void testSettingOfRolePermissionResolver()
42      {
43          Collection<Realm> realms = new ArrayList<Realm>();
44          
45          realms.add( new MockAuthorizingRealm() );
46          realms.add( new MockAuthorizingRealm() );
47          
48          // its null to start with
49          for ( Realm realm : realms )
50          {
51              Assert.assertNull( ((AuthorizingRealm)realm).getRolePermissionResolver() );
52          }
53          
54          ModularRealmAuthorizer modRealmAuthz = new ModularRealmAuthorizer();
55          modRealmAuthz.setRealms( realms );
56          
57          // make sure they are still null
58          for ( Realm realm : realms )
59          {
60              Assert.assertNull( ((AuthorizingRealm)realm).getRolePermissionResolver() );
61          }
62          
63          // now set the RolePermissionResolver
64          RolePermissionResolver rolePermissionResolver = new RolePermissionResolver()
65          {   
66              public Collection<Permission> resolvePermissionsInRole( String roleString )
67              {
68                  return null;
69              }
70          };
71          modRealmAuthz.setRolePermissionResolver( rolePermissionResolver );
72       
73          // make sure they are set
74          for ( Realm realm : realms )
75          {
76              // check for same instance
77              Assert.assertTrue( ((AuthorizingRealm)realm).getRolePermissionResolver() == rolePermissionResolver );
78          }
79          
80          // add a new realm and make sure the RolePermissionResolver is set
81          MockAuthorizingRealm mockRealm = new MockAuthorizingRealm();
82          realms.add( mockRealm );
83          modRealmAuthz.setRealms( realms );
84          assertTrue( ((AuthorizingRealm) mockRealm).getRolePermissionResolver() == rolePermissionResolver );
85          
86          
87          // TODO: no way to unset them, not sure if that is a valid use case, but this is conistent with the PermissionResolver logic
88  //        // now just to be sure, unset them
89  //        modRealmAuthz.setRolePermissionResolver( null );
90  //        for ( Realm realm : realms )
91  //        {
92  //            Assert.assertNull( ((AuthorizingRealm)realm).getRolePermissionResolver() );
93  //        }
94          
95          
96      }
97      
98      class MockAuthorizingRealm extends AuthorizingRealm
99      {
100 
101         @Override
102         protected AuthorizationInfo doGetAuthorizationInfo( PrincipalCollection principals )
103         {
104             return null;
105         }
106 
107         @Override
108         protected AuthenticationInfo doGetAuthenticationInfo( AuthenticationToken token )
109             throws AuthenticationException
110         {
111             return null;
112         }
113         
114     }
115 }