now using the simplified user management interface.
[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     @Override
73     public AuthorizationResult isAllowed(Object aResource,
74         Operation aOperation, String aUser) {
75         AuthorizationResult result = super.isAllowed(aResource, aOperation,
76             aUser);
77
78         if (result.equals(GRANTED) || result.equals(DENIED)) {
79             matches++;
80         }
81
82         return result;
83     }
84
85     public int getMatchCount() {
86         return matches;
87     }
88
89     public void reset() {
90         matches = 0;
91     }
92 }