001: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
002: *
003: * Licensed under the Apache License, Version 2.0 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015:
016: package org.acegisecurity.vote;
017:
018: import junit.framework.TestCase;
019:
020: import org.acegisecurity.AccessDeniedException;
021: import org.acegisecurity.Authentication;
022: import org.acegisecurity.ConfigAttribute;
023: import org.acegisecurity.ConfigAttributeDefinition;
024: import org.acegisecurity.SecurityConfig;
025:
026: import java.util.List;
027: import java.util.Vector;
028:
029: /**
030: * Tests {@link AbstractAccessDecisionManager}.
031: *
032: * @author Ben Alex
033: * @version $Id: AbstractAccessDecisionManagerTests.java 1496 2006-05-23 13:38:33Z benalex $
034: */
035: public class AbstractAccessDecisionManagerTests extends TestCase {
036: //~ Constructors ===================================================================================================
037:
038: public AbstractAccessDecisionManagerTests() {
039: super ();
040: }
041:
042: public AbstractAccessDecisionManagerTests(String arg0) {
043: super (arg0);
044: }
045:
046: //~ Methods ========================================================================================================
047:
048: public static void main(String[] args) {
049: junit.textui.TestRunner
050: .run(AbstractAccessDecisionManagerTests.class);
051: }
052:
053: public final void setUp() throws Exception {
054: super .setUp();
055: }
056:
057: public void testAllowIfAccessDecisionManagerDefaults()
058: throws Exception {
059: MockDecisionManagerImpl mock = new MockDecisionManagerImpl();
060: assertTrue(!mock.isAllowIfAllAbstainDecisions()); // default
061: mock.setAllowIfAllAbstainDecisions(true);
062: assertTrue(mock.isAllowIfAllAbstainDecisions()); // changed
063: }
064:
065: public void testDelegatesSupportsClassRequests() throws Exception {
066: MockDecisionManagerImpl mock = new MockDecisionManagerImpl();
067: List list = new Vector();
068: list.add(new DenyVoter());
069: list.add(new MockStringOnlyVoter());
070: mock.setDecisionVoters(list);
071:
072: assertTrue(mock.supports(new String().getClass()));
073: assertTrue(!mock.supports(new Integer(7).getClass()));
074: }
075:
076: public void testDelegatesSupportsRequests() throws Exception {
077: MockDecisionManagerImpl mock = new MockDecisionManagerImpl();
078: List list = new Vector();
079: DenyVoter voter = new DenyVoter();
080: DenyAgainVoter denyVoter = new DenyAgainVoter();
081: list.add(voter);
082: list.add(denyVoter);
083: mock.setDecisionVoters(list);
084:
085: ConfigAttribute attr = new SecurityConfig("DENY_AGAIN_FOR_SURE");
086: assertTrue(mock.supports(attr));
087:
088: ConfigAttribute badAttr = new SecurityConfig(
089: "WE_DONT_SUPPORT_THIS");
090: assertTrue(!mock.supports(badAttr));
091: }
092:
093: public void testProperlyStoresListOfVoters() throws Exception {
094: MockDecisionManagerImpl mock = new MockDecisionManagerImpl();
095: List list = new Vector();
096: DenyVoter voter = new DenyVoter();
097: DenyAgainVoter denyVoter = new DenyAgainVoter();
098: list.add(voter);
099: list.add(denyVoter);
100: mock.setDecisionVoters(list);
101: assertEquals(list.size(), mock.getDecisionVoters().size());
102: }
103:
104: public void testRejectsEmptyList() throws Exception {
105: MockDecisionManagerImpl mock = new MockDecisionManagerImpl();
106: List list = new Vector();
107:
108: try {
109: mock.setDecisionVoters(list);
110: fail("Should have thrown IllegalArgumentException");
111: } catch (IllegalArgumentException expected) {
112: assertTrue(true);
113: }
114: }
115:
116: public void testRejectsListContainingInvalidObjectTypes()
117: throws Exception {
118: MockDecisionManagerImpl mock = new MockDecisionManagerImpl();
119: List list = new Vector();
120: DenyVoter voter = new DenyVoter();
121: DenyAgainVoter denyVoter = new DenyAgainVoter();
122: String notAVoter = "NOT_A_VOTER";
123: list.add(voter);
124: list.add(notAVoter);
125: list.add(denyVoter);
126:
127: try {
128: mock.setDecisionVoters(list);
129: fail("Should have thrown IllegalArgumentException");
130: } catch (IllegalArgumentException expected) {
131: assertTrue(true);
132: }
133: }
134:
135: public void testRejectsNullVotersList() throws Exception {
136: MockDecisionManagerImpl mock = new MockDecisionManagerImpl();
137:
138: try {
139: mock.setDecisionVoters(null);
140: fail("Should have thrown IllegalArgumentException");
141: } catch (IllegalArgumentException expected) {
142: assertTrue(true);
143: }
144: }
145:
146: public void testRoleVoterAlwaysReturnsTrueToSupports() {
147: RoleVoter rv = new RoleVoter();
148: assertTrue(rv.supports(String.class));
149: }
150:
151: public void testWillNotStartIfDecisionVotersNotSet()
152: throws Exception {
153: MockDecisionManagerImpl mock = new MockDecisionManagerImpl();
154:
155: try {
156: mock.afterPropertiesSet();
157: fail("Should have thrown IllegalArgumentException");
158: } catch (IllegalArgumentException expected) {
159: assertTrue(true);
160: }
161: }
162:
163: //~ Inner Classes ==================================================================================================
164:
165: private class MockDecisionManagerImpl extends
166: AbstractAccessDecisionManager {
167: public void decide(Authentication authentication,
168: Object object, ConfigAttributeDefinition config)
169: throws AccessDeniedException {
170: return;
171: }
172: }
173:
174: private class MockStringOnlyVoter implements AccessDecisionVoter {
175: public boolean supports(Class clazz) {
176: if (String.class.isAssignableFrom(clazz)) {
177: return true;
178: } else {
179: return false;
180: }
181: }
182:
183: public boolean supports(ConfigAttribute attribute) {
184: throw new UnsupportedOperationException(
185: "mock method not implemented");
186: }
187:
188: public int vote(Authentication authentication, Object object,
189: ConfigAttributeDefinition config) {
190: throw new UnsupportedOperationException(
191: "mock method not implemented");
192: }
193: }
194: }
|