3644dc8384c00bea8f567d263da06132a7be30f8
[utils] / security / src / test / java / org / wamblee / security / authorization / TestAuthorizationRule.java
1 /*
2  * Copyright 2005 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */ 
16
17 package org.wamblee.security.authorization;
18
19 import static org.wamblee.security.authorization.AuthorizationResult.DENIED;
20 import static org.wamblee.security.authorization.AuthorizationResult.GRANTED;
21
22 import org.wamblee.usermgt.User;
23
24 /**
25  * Test authorization rule that also counts the number of times the rule matches. 
26  *
27  * @author Erik Brakkee
28  */
29 public class TestAuthorizationRule extends UrlAuthorizationRule {
30     
31     /**
32      * Counts the number of matches. 
33      */
34     private int matches = 0; 
35
36     public TestAuthorizationRule( AuthorizationResult aResult, String aGroup,
37             String aPath, Class<? extends Operation> aOperation) {
38        super(aResult, new GroupUserCondition(aGroup), 
39                new StartsWithPathCondition(aPath), TestResource.class, new IsaOperationCondition(aOperation));
40     }
41     
42     protected TestAuthorizationRule() { 
43         super();
44     }
45     
46     /* (non-Javadoc)
47      * @see org.wamblee.security.authorization.UrlAuthorizationRule#getPath(java.lang.Object)
48      */
49     @Override
50     protected String getResourcePath(Object aResource) {
51         return ((TestResource)aResource).getPath();
52     }
53     
54     /* (non-Javadoc)
55      * @see org.wamblee.security.authorization.UrlAuthorizationRule#isAllowed(java.lang.Object, org.wamblee.security.authorization.Operation, org.wamblee.usermgt.UserAccessor)
56      */
57     @Override
58     public AuthorizationResult isAllowed(Object aResource, Operation anOperation, User aUser) {
59         
60         AuthorizationResult result = super.isAllowed(aResource, anOperation, aUser);
61         if ( result.equals(GRANTED) || result.equals(DENIED)) { 
62             matches++; 
63         }
64         return result; 
65     }
66     
67     public int getMatchCount() { 
68         return matches; 
69     }
70     
71     public void reset() { 
72         matches = 0; 
73     }
74    
75 }