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.acl;
017:
018: import junit.framework.TestCase;
019:
020: import org.acegisecurity.Authentication;
021: import org.acegisecurity.GrantedAuthority;
022: import org.acegisecurity.GrantedAuthorityImpl;
023:
024: import org.acegisecurity.acl.basic.NamedEntityObjectIdentity;
025: import org.acegisecurity.acl.basic.SimpleAclEntry;
026:
027: import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
028:
029: import java.util.List;
030: import java.util.Vector;
031:
032: /**
033: * Tests {@link AclProviderManager}.
034: *
035: * @author Ben Alex
036: * @version $Id: AclProviderManagerTests.java 1784 2007-02-24 21:00:24Z luke_t $
037: */
038: public class AclProviderManagerTests extends TestCase {
039: //~ Constructors ===================================================================================================
040:
041: public AclProviderManagerTests() {
042: super ();
043: }
044:
045: public AclProviderManagerTests(String arg0) {
046: super (arg0);
047: }
048:
049: //~ Methods ========================================================================================================
050:
051: public static void main(String[] args) {
052: junit.textui.TestRunner.run(AclProviderManagerTests.class);
053: }
054:
055: private AclProviderManager makeProviderManager() {
056: MockProvider provider1 = new MockProvider();
057: List providers = new Vector();
058: providers.add(provider1);
059:
060: AclProviderManager mgr = new AclProviderManager();
061: mgr.setProviders(providers);
062:
063: return mgr;
064: }
065:
066: public final void setUp() throws Exception {
067: super .setUp();
068: }
069:
070: public void testAclLookupFails() {
071: AclProviderManager mgr = makeProviderManager();
072: assertNull(mgr.getAcls(new Integer(5)));
073: }
074:
075: public void testAclLookupForGivenAuthenticationSuccess() {
076: AclProviderManager mgr = makeProviderManager();
077: assertNotNull(mgr.getAcls("STRING",
078: new UsernamePasswordAuthenticationToken("marissa",
079: "not used")));
080: }
081:
082: public void testAclLookupSuccess() {
083: AclProviderManager mgr = makeProviderManager();
084: assertNotNull(mgr.getAcls("STRING"));
085: }
086:
087: public void testRejectsNulls() {
088: AclProviderManager mgr = new AclProviderManager();
089:
090: try {
091: mgr.getAcls(null);
092: fail("Should have thrown IllegalArgumentException");
093: } catch (IllegalArgumentException expected) {
094: assertTrue(true);
095: }
096:
097: try {
098: mgr.getAcls(null, new UsernamePasswordAuthenticationToken(
099: "marissa", "not used"));
100: fail("Should have thrown IllegalArgumentException");
101: } catch (IllegalArgumentException expected) {
102: assertTrue(true);
103: }
104:
105: try {
106: mgr.getAcls("SOME_DOMAIN_INSTANCE", null);
107: fail("Should have thrown IllegalArgumentException");
108: } catch (IllegalArgumentException expected) {
109: assertTrue(true);
110: }
111: }
112:
113: public void testReturnsNullIfNoSupportingProvider() {
114: AclProviderManager mgr = makeProviderManager();
115: assertNull(mgr.getAcls(new Integer(4),
116: new UsernamePasswordAuthenticationToken("marissa",
117: "not used")));
118: assertNull(mgr.getAcls(new Integer(4)));
119: }
120:
121: public void testStartupFailsIfProviderListNotContainingProviders()
122: throws Exception {
123: List providers = new Vector();
124: providers.add("THIS_IS_NOT_A_PROVIDER");
125:
126: AclProviderManager mgr = new AclProviderManager();
127:
128: try {
129: mgr.setProviders(providers);
130: fail("Should have thrown IllegalArgumentException");
131: } catch (IllegalArgumentException expected) {
132: assertTrue(true);
133: }
134: }
135:
136: public void testStartupFailsIfProviderListNotSet() throws Exception {
137: AclProviderManager mgr = new AclProviderManager();
138:
139: try {
140: mgr.afterPropertiesSet();
141: fail("Should have thrown IllegalArgumentException");
142: } catch (IllegalArgumentException expected) {
143: assertTrue(true);
144: }
145: }
146:
147: public void testStartupFailsIfProviderListNull() throws Exception {
148: AclProviderManager mgr = new AclProviderManager();
149:
150: try {
151: mgr.setProviders(null);
152: fail("Should have thrown IllegalArgumentException");
153: } catch (IllegalArgumentException expected) {
154: assertTrue(true);
155: }
156: }
157:
158: public void testSuccessfulStartup() throws Exception {
159: AclProviderManager mgr = makeProviderManager();
160: mgr.afterPropertiesSet();
161: assertTrue(true);
162: assertEquals(1, mgr.getProviders().size());
163: }
164:
165: //~ Inner Classes ==================================================================================================
166:
167: private class MockProvider implements AclProvider {
168: private UsernamePasswordAuthenticationToken marissa = new UsernamePasswordAuthenticationToken(
169: "marissa", "not used", new GrantedAuthority[] {
170: new GrantedAuthorityImpl("ROLE_FOO"),
171: new GrantedAuthorityImpl("ROLE_BAR") });
172: private SimpleAclEntry entry100Marissa = new SimpleAclEntry(
173: marissa.getPrincipal(), new NamedEntityObjectIdentity(
174: "OBJECT", "100"), null, 2);
175: private UsernamePasswordAuthenticationToken scott = new UsernamePasswordAuthenticationToken(
176: "scott", "not used", new GrantedAuthority[] {
177: new GrantedAuthorityImpl("ROLE_FOO"),
178: new GrantedAuthorityImpl("ROLE_MANAGER") });
179: private SimpleAclEntry entry100Scott = new SimpleAclEntry(scott
180: .getPrincipal(), new NamedEntityObjectIdentity(
181: "OBJECT", "100"), null, 4);
182:
183: public AclEntry[] getAcls(Object domainInstance,
184: Authentication authentication) {
185: if (authentication.getPrincipal().equals(
186: scott.getPrincipal())) {
187: return new AclEntry[] { entry100Scott };
188: }
189:
190: if (authentication.getPrincipal().equals(
191: marissa.getPrincipal())) {
192: return new AclEntry[] { entry100Marissa };
193: }
194:
195: return null;
196: }
197:
198: public AclEntry[] getAcls(Object domainInstance) {
199: return new AclEntry[] { entry100Marissa, entry100Scott };
200: }
201:
202: /**
203: * Only supports <code>Object</code>s of type <code>String</code>
204: *
205: * @param domainInstance DOCUMENT ME!
206: *
207: * @return DOCUMENT ME!
208: */
209: public boolean supports(Object domainInstance) {
210: return (domainInstance instanceof String);
211: }
212: }
213: }
|