e6cf01db4f1bf5030614e392a14f60c07ee147ce
[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 /**
25  * Test authorization rule that also counts the number of times the rule
26  * matches.
27  *
28  * @author Erik Brakkee
29  */
30 public class TestAuthorizationRule extends UrlAuthorizationRule {
31     /**
32      * Counts the number of matches.
33      */
34     private int matches = 0;
35
36 /**
37      * Creates a new TestAuthorizationRule object.
38      *
39      * @param aResult DOCUMENT ME!
40      * @param aGroup DOCUMENT ME!
41      * @param aPath DOCUMENT ME!
42      * @param aOperation DOCUMENT ME!
43      */
44     public TestAuthorizationRule(AuthorizationResult aResult, String aGroup,
45         String aPath, Class<?extends Operation> aOperation) {
46         super(aResult, new GroupUserCondition(aGroup),
47             new StartsWithPathCondition(aPath), TestResource.class,
48             new IsaOperationCondition(aOperation));
49     }
50
51 /**
52      * Creates a new TestAuthorizationRule object.
53      */
54     protected TestAuthorizationRule() {
55         super();
56     }
57
58     /* (non-Javadoc)
59      * @see org.wamblee.security.authorization.UrlAuthorizationRule#getPath(java.lang.Object)
60      */
61     /**
62      * DOCUMENT ME!
63      *
64      * @param aResource DOCUMENT ME!
65      *
66      * @return DOCUMENT ME!
67      */
68     @Override
69     protected String getResourcePath(Object aResource) {
70         return ((TestResource) aResource).getPath();
71     }
72
73     /* (non-Javadoc)
74      * @see org.wamblee.security.authorization.UrlAuthorizationRule#isAllowed(java.lang.Object, org.wamblee.security.authorization.Operation, org.wamblee.usermgt.UserAccessor)
75      */
76     /**
77      * DOCUMENT ME!
78      *
79      * @param aResource DOCUMENT ME!
80      * @param anOperation DOCUMENT ME!
81      * @param aUser DOCUMENT ME!
82      *
83      * @return DOCUMENT ME!
84      */
85     @Override
86     public AuthorizationResult isAllowed(Object aResource,
87         Operation anOperation, User aUser) {
88         AuthorizationResult result = super.isAllowed(aResource, anOperation,
89                 aUser);
90
91         if (result.equals(GRANTED) || result.equals(DENIED)) {
92             matches++;
93         }
94
95         return result;
96     }
97
98     /**
99      * DOCUMENT ME!
100      *
101      * @return DOCUMENT ME!
102      */
103     public int getMatchCount() {
104         return matches;
105     }
106
107     /**
108      * DOCUMENT ME!
109      */
110     public void reset() {
111         matches = 0;
112     }
113 }