    1   /* Copyright 2004 Acegi Technology Pty Limited
    2    *
    3    * Licensed under the Apache License, Version 2.0 (the "License");
    4    * you may not use this file except in compliance with the License.
    5    * You may obtain a copy of the License at
    6    *
    7    *     http://www.apache.org/licenses/LICENSE-2.0
    8    *
    9    * Unless required by applicable law or agreed to in writing, software
    10   * distributed under the License is distributed on an "AS IS" BASIS,
    11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12   * See the License for the specific language governing permissions and
    13   * limitations under the License.
    14   */
    15
    16  package org.acegisecurity.providers.cas.ticketvalidator;
    17
    18  import junit.framework.TestCase;
    19
    20  import org.acegisecurity.AuthenticationException;
    21  import org.acegisecurity.BadCredentialsException;
    22  import org.acegisecurity.providers.cas.TicketResponse;
    23  import org.acegisecurity.ui.cas.ServiceProperties;
    24
    25  import java.util.Vector;
    26
    27
    28  /***
    29   * Tests {@link AbstractTicketValidator}.
    30   *
    31   * @author Ben Alex
    32   * @version $Id: AbstractTicketValidatorTests.java,v 1.2 2005/11/17 00:55:50 benalex Exp $
    33   */
    34  public class AbstractTicketValidatorTests extends TestCase {
    35      //~ Constructors ===========================================================
    36
    37      public AbstractTicketValidatorTests() {
    38          super();
    39      }
    40
    41      public AbstractTicketValidatorTests(String arg0) {
    42          super(arg0);
    43      }
    44
    45      //~ Methods ================================================================
    46
    47      public final void setUp() throws Exception {
    48          super.setUp();
    49      }
    50
    51      public static void main(String[] args) {
    52          junit.textui.TestRunner.run(AbstractTicketValidatorTests.class);
    53      }
    54
    55      public void testDetectsMissingCasValidate() throws Exception {
    56          AbstractTicketValidator tv = new MockAbstractTicketValidator();
    57          tv.setServiceProperties(new ServiceProperties());
    58
    59          try {
    60              tv.afterPropertiesSet();
    61              fail("Should have thrown IllegalArgumentException");
    62          } catch (IllegalArgumentException expected) {
    63              assertEquals("A casValidate URL must be set", expected.getMessage());
    64          }
    65      }
    66
    67      public void testDetectsMissingServiceProperties() throws Exception {
    68          AbstractTicketValidator tv = new MockAbstractTicketValidator();
    69          tv.setCasValidate("https://company.com/cas/proxyvalidate");
    70
    71          try {
    72              tv.afterPropertiesSet();
    73              fail("Should have thrown IllegalArgumentException");
    74          } catch (IllegalArgumentException expected) {
    75              assertEquals("serviceProperties must be specified",
    76                  expected.getMessage());
    77          }
    78      }
    79
    80      public void testGetters() throws Exception {
    81          AbstractTicketValidator tv = new MockAbstractTicketValidator();
    82          tv.setCasValidate("https://company.com/cas/proxyvalidate");
    83          assertEquals("https://company.com/cas/proxyvalidate",
    84              tv.getCasValidate());
    85
    86          tv.setServiceProperties(new ServiceProperties());
    87          assertTrue(tv.getServiceProperties() != null);
    88
    89          tv.afterPropertiesSet();
    90
    91          tv.setTrustStore("/some/file/cacerts");
    92          assertEquals("/some/file/cacerts", tv.getTrustStore());
    93      }
    94
    95      public void testSystemPropertySetDuringAfterPropertiesSet()
    96          throws Exception {
    97          AbstractTicketValidator tv = new MockAbstractTicketValidator();
    98          tv.setCasValidate("https://company.com/cas/proxyvalidate");
    99          assertEquals("https://company.com/cas/proxyvalidate",
    100             tv.getCasValidate());
    101
    102         tv.setServiceProperties(new ServiceProperties());
    103         assertTrue(tv.getServiceProperties() != null);
    104
    105         tv.setTrustStore("/some/file/cacerts");
    106         assertEquals("/some/file/cacerts", tv.getTrustStore());
    107
    108         String before = System.getProperty("javax.net.ssl.trustStore");
    109         tv.afterPropertiesSet();
    110         assertEquals("/some/file/cacerts",
    111             System.getProperty("javax.net.ssl.trustStore"));
    112
    113         if (before == null) {
    114             System.setProperty("javax.net.ssl.trustStore", "");
    115         } else {
    116             System.setProperty("javax.net.ssl.trustStore", before);
    117         }
    118     }
    119
    120     //~ Inner Classes ==========================================================
    121
    122     private class MockAbstractTicketValidator extends AbstractTicketValidator {
    123         private boolean returnTicket;
    124
    125         public MockAbstractTicketValidator(boolean returnTicket) {
    126             this.returnTicket = returnTicket;
    127         }
    128
    129         private MockAbstractTicketValidator() {
    130             super();
    131         }
    132
    133         public TicketResponse confirmTicketValid(String serviceTicket)
    134             throws AuthenticationException {
    135             if (returnTicket) {
    136                 return new TicketResponse("user", new Vector(),
    137                     "PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt");
    138             }
    139
    140             throw new BadCredentialsException("As requested by mock");
    141         }
    142     }
    143 }