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