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.adapters.cas;
017:
018: import junit.framework.TestCase;
019:
020: import org.springframework.context.ApplicationContext;
021: import org.springframework.context.support.ClassPathXmlApplicationContext;
022:
023: import org.springframework.mock.web.MockHttpServletRequest;
024:
025: import javax.servlet.http.HttpServletRequest;
026:
027: /**
028: * Tests {@link CasPasswordHandlerProxy}.
029: *
030: * @author Ben Alex
031: * @version $Id: CasPasswordHandlerProxyTests.java 1496 2006-05-23 13:38:33Z benalex $
032: */
033: public class CasPasswordHandlerProxyTests extends TestCase {
034: //~ Constructors ===================================================================================================
035:
036: public CasPasswordHandlerProxyTests() {
037: super ();
038: }
039:
040: public CasPasswordHandlerProxyTests(String arg0) {
041: super (arg0);
042: }
043:
044: //~ Methods ========================================================================================================
045:
046: public static void main(String[] args) {
047: junit.textui.TestRunner.run(CasPasswordHandlerProxyTests.class);
048: }
049:
050: public final void setUp() throws Exception {
051: super .setUp();
052: }
053:
054: public void testDetectsIfHttpServletRequestNotPassed() {
055: CasPasswordHandlerProxy proxy = new MockCasPasswordHandlerProxy(
056: "org/acegisecurity/adapters/cas/applicationContext-valid.xml");
057:
058: try {
059: proxy.authenticate(null, "x", "y");
060: fail("Should have thrown IllegalArgumentException");
061: } catch (IllegalArgumentException expected) {
062: assertEquals("Can only process HttpServletRequest",
063: expected.getMessage());
064: }
065: }
066:
067: public void testDetectsMissingDelegate() {
068: CasPasswordHandlerProxy proxy = new MockCasPasswordHandlerProxy(
069: "org/acegisecurity/adapters/cas/applicationContext-invalid.xml");
070:
071: try {
072: proxy.authenticate(new MockHttpServletRequest(), "x", "y");
073: fail("Should have thrown IllegalArgumentException");
074: } catch (IllegalArgumentException expected) {
075: assertEquals(
076: "Bean context must contain at least one bean of type CasPasswordHandler",
077: expected.getMessage());
078: }
079: }
080:
081: public void testNormalOperation() {
082: CasPasswordHandlerProxy proxy = new MockCasPasswordHandlerProxy(
083: "org/acegisecurity/adapters/cas/applicationContext-valid.xml");
084: assertTrue(proxy.authenticate(new MockHttpServletRequest(),
085: "marissa", "koala"));
086: assertFalse(proxy.authenticate(new MockHttpServletRequest(),
087: "marissa", "WRONG_PASSWORD"));
088: assertFalse(proxy.authenticate(new MockHttpServletRequest(),
089: "INVALID_USER_NAME", "WRONG_PASSWORD"));
090: }
091:
092: //~ Inner Classes ==================================================================================================
093:
094: /**
095: * Mock object so that application context source can be specified.
096: */
097: private class MockCasPasswordHandlerProxy extends
098: CasPasswordHandlerProxy {
099: private ApplicationContext ctx;
100:
101: public MockCasPasswordHandlerProxy(String appContextLocation) {
102: ctx = new ClassPathXmlApplicationContext(appContextLocation);
103: }
104:
105: private MockCasPasswordHandlerProxy() {
106: super ();
107: }
108:
109: protected ApplicationContext getContext(
110: HttpServletRequest httpRequest) {
111: return ctx;
112: }
113: }
114: }
|