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