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 samples.annotations;
017:
018: import junit.framework.TestCase;
019:
020: import org.acegisecurity.AccessDeniedException;
021: import org.acegisecurity.GrantedAuthority;
022: import org.acegisecurity.GrantedAuthorityImpl;
023:
024: import org.acegisecurity.context.SecurityContextHolder;
025: import org.acegisecurity.context.SecurityContextImpl;
026:
027: import org.acegisecurity.providers.TestingAuthenticationToken;
028:
029: import org.springframework.context.support.ClassPathXmlApplicationContext;
030:
031: import sample.annotations.BankService;
032:
033: /**
034: * Tests security objects.
035: *
036: * @author Ben Alex
037: * @version $Id: BankTests.java 1496 2006-05-23 13:38:33Z benalex $
038: */
039: public class BankTests extends TestCase {
040: //~ Instance fields ================================================================================================
041:
042: private BankService service;
043: private ClassPathXmlApplicationContext ctx;
044:
045: //~ Constructors ===================================================================================================
046:
047: public BankTests() {
048: super ();
049: }
050:
051: public BankTests(String arg0) {
052: super (arg0);
053: }
054:
055: //~ Methods ========================================================================================================
056:
057: private static void createSecureContext() {
058: TestingAuthenticationToken auth = new TestingAuthenticationToken(
059: "test",
060: "test",
061: new GrantedAuthority[] {
062: new GrantedAuthorityImpl("ROLE_TELLER"),
063: new GrantedAuthorityImpl("ROLE_PERMISSION_LIST") });
064:
065: SecurityContextHolder.getContext().setAuthentication(auth);
066: }
067:
068: private static void destroySecureContext() {
069: SecurityContextHolder.setContext(new SecurityContextImpl());
070: }
071:
072: public static void main(String[] args) {
073: junit.textui.TestRunner.run(BankTests.class);
074: }
075:
076: public final void setUp() throws Exception {
077: super .setUp();
078: ctx = new ClassPathXmlApplicationContext(
079: "applicationContext-annotations.xml");
080: service = (BankService) ctx.getBean("bankService");
081: }
082:
083: public void testDeniedAccess() throws Exception {
084: createSecureContext();
085:
086: try {
087: service.balance("1");
088: fail("Should have thrown AccessDeniedException");
089: } catch (AccessDeniedException expected) {
090: assertTrue(true);
091: }
092:
093: destroySecureContext();
094: }
095:
096: public void testListAccounts() throws Exception {
097: createSecureContext();
098: service.listAccounts();
099: destroySecureContext();
100: }
101: }
|