1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
58 for ( Realm realm : realms )
59 {
60 Assert.assertNull( ((AuthorizingRealm)realm).getRolePermissionResolver() );
61 }
62
63
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
74 for ( Realm realm : realms )
75 {
76
77 Assert.assertTrue( ((AuthorizingRealm)realm).getRolePermissionResolver() == rolePermissionResolver );
78 }
79
80
81 MockAuthorizingRealm mockRealm = new MockAuthorizingRealm();
82 realms.add( mockRealm );
83 modRealmAuthz.setRealms( realms );
84 assertTrue( ((AuthorizingRealm) mockRealm).getRolePermissionResolver() == rolePermissionResolver );
85
86
87
88
89
90
91
92
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 }