01: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
02: *
03: * Licensed under the Apache License, Version 2.0 (the "License");
04: * you may not use this file except in compliance with the License.
05: * You may obtain a copy of the License at
06: *
07: * http://www.apache.org/licenses/LICENSE-2.0
08: *
09: * Unless required by applicable law or agreed to in writing, software
10: * distributed under the License is distributed on an "AS IS" BASIS,
11: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: * See the License for the specific language governing permissions and
13: * limitations under the License.
14: */
15:
16: package org.acegisecurity;
17:
18: import junit.framework.TestCase;
19:
20: import org.acegisecurity.AccessDeniedException;
21: import org.acegisecurity.GrantedAuthority;
22: import org.acegisecurity.GrantedAuthorityImpl;
23:
24: import org.acegisecurity.context.SecurityContextHolder;
25: import org.acegisecurity.context.SecurityContextImpl;
26:
27: import org.acegisecurity.providers.TestingAuthenticationToken;
28: import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
29:
30: import org.springframework.context.support.ClassPathXmlApplicationContext;
31:
32: /**
33: * Tests security objects.
34: *
35: * @author Ben Alex
36: * @version $Id: BankTests.java 1496 2006-05-23 13:38:33Z benalex $
37: */
38: public class BankTests extends TestCase {
39: //~ Instance fields ================================================================================================
40:
41: private BankService service;
42: private ClassPathXmlApplicationContext ctx;
43:
44: //~ Constructors ===================================================================================================
45:
46: public BankTests() {
47: super ();
48: }
49:
50: public BankTests(String arg0) {
51: super (arg0);
52: }
53:
54: //~ Methods ========================================================================================================
55:
56: private static void createSecureContext() {
57: UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(
58: "test",
59: "test",
60: new GrantedAuthority[] {
61: new GrantedAuthorityImpl("ROLE_TELLER"),
62: new GrantedAuthorityImpl("ROLE_PERMISSION_LIST") });
63:
64: SecurityContextHolder.getContext().setAuthentication(auth);
65: }
66:
67: private static void destroySecureContext() {
68: SecurityContextHolder.setContext(new SecurityContextImpl());
69: }
70:
71: public static void main(String[] args) {
72: junit.textui.TestRunner.run(BankTests.class);
73: }
74:
75: public final void setUp() throws Exception {
76: super .setUp();
77: ctx = new ClassPathXmlApplicationContext(
78: "org/acegisecurity/config/auto-config.xml");
79: service = (BankService) ctx.getBean("bankService");
80: }
81:
82: public void testDeniedAccess() throws Exception {
83: createSecureContext();
84:
85: try {
86: service.balance("1");
87: fail("Should have thrown AccessDeniedException");
88: } catch (AccessDeniedException expected) {
89: assertTrue(true);
90: }
91: destroySecureContext();
92: }
93:
94: public void testListAccounts() throws Exception {
95: createSecureContext();
96: service.listAccounts();
97: destroySecureContext();
98: }
99: }
|