Removed DOCUMENT ME comments that were generated and applied source code
[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 package org.wamblee.security.authorization;
17
18 import static org.wamblee.security.authorization.AuthorizationResult.DENIED;
19 import static org.wamblee.security.authorization.AuthorizationResult.GRANTED;
20
21 import org.wamblee.usermgt.User;
22
23 /**
24  * Test authorization rule that also counts the number of times the rule
25  * matches.
26  * 
27  * @author Erik Brakkee
28  */
29 public class TestAuthorizationRule extends UrlAuthorizationRule {
30     /**
31      * Counts the number of matches.
32      */
33     private int matches = 0;
34
35     /**
36      * Creates a new TestAuthorizationRule object.
37      * 
38      */
39     public TestAuthorizationRule(AuthorizationResult aResult, String aGroup,
40         String aPath, Class<? extends Operation> aOperation) {
41         super(aResult, new GroupUserCondition(aGroup),
42             new StartsWithPathCondition(aPath), TestResource.class,
43             new IsaOperationCondition(aOperation));
44     }
45
46     /**
47      * Creates a new TestAuthorizationRule object.
48      */
49     protected TestAuthorizationRule() {
50         super();
51     }
52
53     /*
54      * (non-Javadoc)
55      * 
56      * @see
57      * org.wamblee.security.authorization.UrlAuthorizationRule#getPath(java.
58      * lang.Object)
59      */
60     @Override
61     protected String getResourcePath(Object aResource) {
62         return ((TestResource) aResource).getPath();
63     }
64
65     /*
66      * (non-Javadoc)
67      * 
68      * @see
69      * org.wamblee.security.authorization.UrlAuthorizationRule#isAllowed(java
70      * .lang.Object, org.wamblee.security.authorization.Operation,
71      * org.wamblee.usermgt.UserAccessor)
72      */
73     @Override
74     public AuthorizationResult isAllowed(Object aResource,
75         Operation anOperation, User aUser) {
76         AuthorizationResult result = super.isAllowed(aResource, anOperation,
77             aUser);
78
79         if (result.equals(GRANTED) || result.equals(DENIED)) {
80             matches++;
81         }
82
83         return result;
84     }
85
86     public int getMatchCount() {
87         return matches;
88     }
89
90     public void reset() {
91         matches = 0;
92     }
93 }