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.securechannel;
017:
018: import junit.framework.TestCase;
019:
020: import org.acegisecurity.ConfigAttribute;
021: import org.acegisecurity.ConfigAttributeDefinition;
022: import org.acegisecurity.MockFilterChain;
023: import org.acegisecurity.SecurityConfig;
024:
025: import org.acegisecurity.intercept.web.FilterInvocation;
026:
027: import org.springframework.mock.web.MockHttpServletRequest;
028: import org.springframework.mock.web.MockHttpServletResponse;
029:
030: import java.io.IOException;
031:
032: import java.util.Iterator;
033: import java.util.List;
034: import java.util.Vector;
035:
036: import javax.servlet.ServletException;
037:
038: /**
039: * Tests {@link ChannelDecisionManagerImpl}.
040: *
041: * @author Ben Alex
042: * @version $Id: ChannelDecisionManagerImplTests.java 1678 2006-09-15 06:27:45Z benalex $
043: */
044: public class ChannelDecisionManagerImplTests extends TestCase {
045: //~ Methods ========================================================================================================
046:
047: public static void main(String[] args) {
048: junit.textui.TestRunner
049: .run(ChannelDecisionManagerImplTests.class);
050: }
051:
052: public final void setUp() throws Exception {
053: super .setUp();
054: }
055:
056: public void testCannotSetEmptyChannelProcessorsList()
057: throws Exception {
058: ChannelDecisionManagerImpl cdm = new ChannelDecisionManagerImpl();
059:
060: try {
061: cdm.setChannelProcessors(new Vector());
062: fail("Should have thrown IllegalArgumentException");
063: } catch (IllegalArgumentException expected) {
064: assertEquals("A list of ChannelProcessors is required",
065: expected.getMessage());
066: }
067: }
068:
069: public void testCannotSetIncorrectObjectTypesIntoChannelProcessorsList()
070: throws Exception {
071: ChannelDecisionManagerImpl cdm = new ChannelDecisionManagerImpl();
072: List list = new Vector();
073: list.add("THIS IS NOT A CHANNELPROCESSOR");
074:
075: try {
076: cdm.setChannelProcessors(list);
077: fail("Should have thrown IllegalArgumentException");
078: } catch (IllegalArgumentException expected) {
079: assertTrue(true);
080: }
081: }
082:
083: public void testCannotSetNullChannelProcessorsList()
084: throws Exception {
085: ChannelDecisionManagerImpl cdm = new ChannelDecisionManagerImpl();
086:
087: try {
088: cdm.setChannelProcessors(null);
089: fail("Should have thrown IllegalArgumentException");
090: } catch (IllegalArgumentException expected) {
091: assertEquals("A list of ChannelProcessors is required",
092: expected.getMessage());
093: }
094: }
095:
096: public void testDecideIsOperational() throws Exception {
097: ChannelDecisionManagerImpl cdm = new ChannelDecisionManagerImpl();
098: MockChannelProcessor cpXyz = new MockChannelProcessor("xyz",
099: false);
100: MockChannelProcessor cpAbc = new MockChannelProcessor("abc",
101: true);
102: List list = new Vector();
103: list.add(cpXyz);
104: list.add(cpAbc);
105: cdm.setChannelProcessors(list);
106: cdm.afterPropertiesSet();
107:
108: MockHttpServletRequest request = new MockHttpServletRequest();
109: MockHttpServletResponse response = new MockHttpServletResponse();
110: MockFilterChain chain = new MockFilterChain();
111: FilterInvocation fi = new FilterInvocation(request, response,
112: chain);
113:
114: ConfigAttributeDefinition cad = new ConfigAttributeDefinition();
115: cad.addConfigAttribute(new SecurityConfig("xyz"));
116:
117: cdm.decide(fi, cad);
118: assertTrue(fi.getResponse().isCommitted());
119: }
120:
121: public void testDecideIteratesAllProcessorsIfNoneCommitAResponse()
122: throws Exception {
123: ChannelDecisionManagerImpl cdm = new ChannelDecisionManagerImpl();
124: MockChannelProcessor cpXyz = new MockChannelProcessor("xyz",
125: false);
126: MockChannelProcessor cpAbc = new MockChannelProcessor("abc",
127: false);
128: List list = new Vector();
129: list.add(cpXyz);
130: list.add(cpAbc);
131: cdm.setChannelProcessors(list);
132: cdm.afterPropertiesSet();
133:
134: MockHttpServletRequest request = new MockHttpServletRequest();
135: MockHttpServletResponse response = new MockHttpServletResponse();
136: MockFilterChain chain = new MockFilterChain();
137: FilterInvocation fi = new FilterInvocation(request, response,
138: chain);
139:
140: ConfigAttributeDefinition cad = new ConfigAttributeDefinition();
141: cad.addConfigAttribute(new SecurityConfig(
142: "SOME_ATTRIBUTE_NO_PROCESSORS_SUPPORT"));
143:
144: cdm.decide(fi, cad);
145: assertFalse(fi.getResponse().isCommitted());
146: }
147:
148: public void testDelegatesSupports() throws Exception {
149: ChannelDecisionManagerImpl cdm = new ChannelDecisionManagerImpl();
150: MockChannelProcessor cpXyz = new MockChannelProcessor("xyz",
151: false);
152: MockChannelProcessor cpAbc = new MockChannelProcessor("abc",
153: false);
154: List list = new Vector();
155: list.add(cpXyz);
156: list.add(cpAbc);
157: cdm.setChannelProcessors(list);
158: cdm.afterPropertiesSet();
159:
160: assertTrue(cdm.supports(new SecurityConfig("xyz")));
161: assertTrue(cdm.supports(new SecurityConfig("abc")));
162: assertFalse(cdm.supports(new SecurityConfig("UNSUPPORTED")));
163: }
164:
165: public void testGettersSetters() {
166: ChannelDecisionManagerImpl cdm = new ChannelDecisionManagerImpl();
167: assertNull(cdm.getChannelProcessors());
168:
169: MockChannelProcessor cpXyz = new MockChannelProcessor("xyz",
170: false);
171: MockChannelProcessor cpAbc = new MockChannelProcessor("abc",
172: false);
173: List list = new Vector();
174: list.add(cpXyz);
175: list.add(cpAbc);
176: cdm.setChannelProcessors(list);
177:
178: assertEquals(list, cdm.getChannelProcessors());
179: }
180:
181: public void testStartupFailsWithEmptyChannelProcessorsList()
182: throws Exception {
183: ChannelDecisionManagerImpl cdm = new ChannelDecisionManagerImpl();
184:
185: try {
186: cdm.afterPropertiesSet();
187: fail("Should have thrown IllegalArgumentException");
188: } catch (IllegalArgumentException expected) {
189: assertEquals("A list of ChannelProcessors is required",
190: expected.getMessage());
191: }
192: }
193:
194: //~ Inner Classes ==================================================================================================
195:
196: private class MockChannelProcessor implements ChannelProcessor {
197: private String configAttribute;
198: private boolean failIfCalled;
199:
200: public MockChannelProcessor(String configAttribute,
201: boolean failIfCalled) {
202: this .configAttribute = configAttribute;
203: this .failIfCalled = failIfCalled;
204: }
205:
206: private MockChannelProcessor() {
207: super ();
208: }
209:
210: public void decide(FilterInvocation invocation,
211: ConfigAttributeDefinition config) throws IOException,
212: ServletException {
213: Iterator iter = config.getConfigAttributes();
214:
215: if (failIfCalled) {
216: fail("Should not have called this channel processor: "
217: + configAttribute);
218: }
219:
220: while (iter.hasNext()) {
221: ConfigAttribute attr = (ConfigAttribute) iter.next();
222:
223: if (attr.getAttribute().equals(configAttribute)) {
224: invocation.getHttpResponse().sendRedirect(
225: "/redirected");
226:
227: return;
228: }
229: }
230: }
231:
232: public boolean supports(ConfigAttribute attribute) {
233: if (attribute.getAttribute().equals(configAttribute)) {
234: return true;
235: } else {
236: return false;
237: }
238: }
239: }
240: }
|