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.providers.cas.ticketvalidator;
017:
018: import junit.framework.TestCase;
019:
020: import org.acegisecurity.AuthenticationException;
021: import org.acegisecurity.BadCredentialsException;
022:
023: import org.acegisecurity.providers.cas.TicketResponse;
024:
025: import org.acegisecurity.ui.cas.ServiceProperties;
026:
027: import java.util.Vector;
028:
029: /**
030: * Tests {@link AbstractTicketValidator}.
031: *
032: * @author Ben Alex
033: * @version $Id: AbstractTicketValidatorTests.java 1496 2006-05-23 13:38:33Z benalex $
034: */
035: public class AbstractTicketValidatorTests extends TestCase {
036: //~ Constructors ===================================================================================================
037:
038: public AbstractTicketValidatorTests() {
039: super ();
040: }
041:
042: public AbstractTicketValidatorTests(String arg0) {
043: super (arg0);
044: }
045:
046: //~ Methods ========================================================================================================
047:
048: public static void main(String[] args) {
049: junit.textui.TestRunner.run(AbstractTicketValidatorTests.class);
050: }
051:
052: public final void setUp() throws Exception {
053: super .setUp();
054: }
055:
056: public void testDetectsMissingCasValidate() throws Exception {
057: AbstractTicketValidator tv = new MockAbstractTicketValidator();
058: tv.setServiceProperties(new ServiceProperties());
059:
060: try {
061: tv.afterPropertiesSet();
062: fail("Should have thrown IllegalArgumentException");
063: } catch (IllegalArgumentException expected) {
064: assertEquals("A casValidate URL must be set", expected
065: .getMessage());
066: }
067: }
068:
069: public void testDetectsMissingServiceProperties() throws Exception {
070: AbstractTicketValidator tv = new MockAbstractTicketValidator();
071: tv.setCasValidate("https://company.com/cas/proxyvalidate");
072:
073: try {
074: tv.afterPropertiesSet();
075: fail("Should have thrown IllegalArgumentException");
076: } catch (IllegalArgumentException expected) {
077: assertEquals("serviceProperties must be specified",
078: expected.getMessage());
079: }
080: }
081:
082: public void testGetters() throws Exception {
083: AbstractTicketValidator tv = new MockAbstractTicketValidator();
084: tv.setCasValidate("https://company.com/cas/proxyvalidate");
085: assertEquals("https://company.com/cas/proxyvalidate", tv
086: .getCasValidate());
087:
088: tv.setServiceProperties(new ServiceProperties());
089: assertTrue(tv.getServiceProperties() != null);
090:
091: tv.afterPropertiesSet();
092:
093: tv.setTrustStore("/some/file/cacerts");
094: assertEquals("/some/file/cacerts", tv.getTrustStore());
095: }
096:
097: public void testSystemPropertySetDuringAfterPropertiesSet()
098: throws Exception {
099: AbstractTicketValidator tv = new MockAbstractTicketValidator();
100: tv.setCasValidate("https://company.com/cas/proxyvalidate");
101: assertEquals("https://company.com/cas/proxyvalidate", tv
102: .getCasValidate());
103:
104: tv.setServiceProperties(new ServiceProperties());
105: assertTrue(tv.getServiceProperties() != null);
106:
107: tv.setTrustStore("/some/file/cacerts");
108: assertEquals("/some/file/cacerts", tv.getTrustStore());
109:
110: String before = System.getProperty("javax.net.ssl.trustStore");
111: tv.afterPropertiesSet();
112: assertEquals("/some/file/cacerts", System
113: .getProperty("javax.net.ssl.trustStore"));
114:
115: if (before == null) {
116: System.setProperty("javax.net.ssl.trustStore", "");
117: } else {
118: System.setProperty("javax.net.ssl.trustStore", before);
119: }
120: }
121:
122: //~ Inner Classes ==================================================================================================
123:
124: private class MockAbstractTicketValidator extends
125: AbstractTicketValidator {
126: private boolean returnTicket;
127:
128: public MockAbstractTicketValidator(boolean returnTicket) {
129: this .returnTicket = returnTicket;
130: }
131:
132: private MockAbstractTicketValidator() {
133: super ();
134: }
135:
136: public TicketResponse confirmTicketValid(String serviceTicket)
137: throws AuthenticationException {
138: if (returnTicket) {
139: return new TicketResponse("user", new Vector(),
140: "PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt");
141: }
142:
143: throw new BadCredentialsException("As requested by mock");
144: }
145: }
146: }
|