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 org.acegisecurity.AccessDeniedException;
019: import org.acegisecurity.AuthenticationManager;
020:
021: import org.acegisecurity.context.SecurityContextHolder;
022:
023: import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
024:
025: import org.springframework.test.AbstractDependencyInjectionSpringContextTests;
026:
027: import java.util.List;
028:
029: /**
030: *
031: DOCUMENT ME!
032: *
033: * @author Greg Turnquist
034: * @version $Id: LabelBasedAclVoterTests.java 1750 2006-11-14 22:07:36Z benalex $
035: */
036: public class LabelBasedAclVoterTests extends
037: AbstractDependencyInjectionSpringContextTests {
038: //~ Instance fields ================================================================================================
039:
040: private SampleService sampleService = null;
041:
042: //~ Methods ========================================================================================================
043:
044: protected String[] getConfigLocations() {
045: return new String[] { "org/acegisecurity/vote/labelBasedSecurityApplicationContext.xml" };
046: }
047:
048: public SampleService getSampleService() {
049: return sampleService;
050: }
051:
052: public static void main(String[] args) {
053: junit.textui.TestRunner.run(LabelBasedAclVoterTests.class);
054: }
055:
056: public void setSampleService(SampleService sampleService) {
057: this .sampleService = sampleService;
058: }
059:
060: private void setupContext(String username, String password) {
061: UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(
062: username, password);
063: AuthenticationManager authenticationManager = (AuthenticationManager) applicationContext
064: .getBean("authenticationManager");
065: SecurityContextHolder.getContext().setAuthentication(
066: authenticationManager.authenticate(token));
067: }
068:
069: public void testDoingSomethingForBlueUser() {
070: setupContext("blueuser", "password");
071:
072: List dataList = sampleService.getTheSampleData();
073: assertNotNull(dataList);
074:
075: SampleBlockOfData block1 = (SampleBlockOfData) dataList.get(0);
076: SampleBlockOfData block2 = (SampleBlockOfData) dataList.get(1);
077: SampleBlockOfData block3 = (SampleBlockOfData) dataList.get(2);
078:
079: sampleService.doSomethingOnThis(block1, block1);
080:
081: try {
082: sampleService.doSomethingOnThis(block2, block2);
083: fail("Expected an AccessDeniedException");
084: } catch (AccessDeniedException e) {
085: } catch (RuntimeException e) {
086: fail("Expected an AccessDeniedException");
087: }
088:
089: try {
090: sampleService.doSomethingOnThis(block1, block2);
091: fail("Expected an AccessDeniedException");
092: } catch (AccessDeniedException e) {
093: } catch (RuntimeException e) {
094: fail("Expected an AccessDeniedException");
095: }
096:
097: try {
098: sampleService.doSomethingOnThis(block2, block1);
099: fail("Expected an AccessDeniedException");
100: } catch (AccessDeniedException e) {
101: } catch (RuntimeException e) {
102: fail("Expected an AccessDeniedException");
103: }
104:
105: sampleService.doSomethingOnThis(block3, block3);
106: }
107:
108: public void testDoingSomethingForMultiUser() {
109: setupContext("multiuser", "password4");
110:
111: List dataList = sampleService.getTheSampleData();
112: assertNotNull(dataList);
113:
114: SampleBlockOfData block1 = (SampleBlockOfData) dataList.get(0);
115: SampleBlockOfData block2 = (SampleBlockOfData) dataList.get(1);
116: SampleBlockOfData block3 = (SampleBlockOfData) dataList.get(2);
117:
118: sampleService.doSomethingOnThis(block1, block1);
119: sampleService.doSomethingOnThis(block2, block2);
120: sampleService.doSomethingOnThis(block1, block2);
121: sampleService.doSomethingOnThis(block2, block1);
122: sampleService.doSomethingOnThis(block3, block3);
123: }
124:
125: public void testDoingSomethingForOrangeUser() {
126: setupContext("orangeuser", "password3");
127:
128: List dataList = sampleService.getTheSampleData();
129: assertNotNull(dataList);
130:
131: SampleBlockOfData block1 = (SampleBlockOfData) dataList.get(0);
132: SampleBlockOfData block2 = (SampleBlockOfData) dataList.get(1);
133: SampleBlockOfData block3 = (SampleBlockOfData) dataList.get(2);
134:
135: sampleService.doSomethingOnThis(block2, block2);
136:
137: try {
138: sampleService.doSomethingOnThis(block1, block1);
139: fail("Expected an AccessDeniedException");
140: } catch (AccessDeniedException e) {
141: } catch (RuntimeException e) {
142: fail("Expected an AccessDeniedException");
143: }
144:
145: try {
146: sampleService.doSomethingOnThis(block1, block2);
147: fail("Expected an AccessDeniedException");
148: } catch (AccessDeniedException e) {
149: } catch (RuntimeException e) {
150: fail("Expected an AccessDeniedException");
151: }
152:
153: try {
154: sampleService.doSomethingOnThis(block2, block1);
155: fail("Expected an AccessDeniedException");
156: } catch (AccessDeniedException e) {
157: } catch (RuntimeException e) {
158: fail("Expected an AccessDeniedException");
159: }
160:
161: sampleService.doSomethingOnThis(block3, block3);
162: }
163:
164: public void testDoingSomethingForSuperUser() {
165: setupContext("superuser", "password2");
166:
167: List dataList = sampleService.getTheSampleData();
168: assertNotNull(dataList);
169:
170: SampleBlockOfData block1 = (SampleBlockOfData) dataList.get(0);
171: SampleBlockOfData block2 = (SampleBlockOfData) dataList.get(1);
172: SampleBlockOfData block3 = (SampleBlockOfData) dataList.get(2);
173:
174: sampleService.doSomethingOnThis(block1, block1);
175: sampleService.doSomethingOnThis(block2, block2);
176: sampleService.doSomethingOnThis(block1, block2);
177: sampleService.doSomethingOnThis(block2, block1);
178: sampleService.doSomethingOnThis(block3, block3);
179: }
180:
181: public void testSampleBlockOfDataPOJO() {
182: SampleBlockOfData block = new SampleBlockOfData();
183: block.setId("ID-ABC");
184: assertEquals(block.getId(), "ID-ABC");
185: }
186: }
|