1   /* Copyright 2004, 2005, 2006 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  
23  import org.acegisecurity.providers.cas.TicketResponse;
24  
25  import org.acegisecurity.ui.cas.ServiceProperties;
26  
27  import java.util.Vector;
28  
29  
30  /**
31   * Tests {@link AbstractTicketValidator}.
32   *
33   * @author Ben Alex
34   * @version $Id: AbstractTicketValidatorTests.java 1496 2006-05-23 13:38:33Z benalex $
35   */
36  public class AbstractTicketValidatorTests extends TestCase {
37      //~ Constructors ===================================================================================================
38  
39      public AbstractTicketValidatorTests() {
40          super();
41      }
42  
43      public AbstractTicketValidatorTests(String arg0) {
44          super(arg0);
45      }
46  
47      //~ Methods ========================================================================================================
48  
49      public static void main(String[] args) {
50          junit.textui.TestRunner.run(AbstractTicketValidatorTests.class);
51      }
52  
53      public final void setUp() throws Exception {
54          super.setUp();
55      }
56  
57      public void testDetectsMissingCasValidate() throws Exception {
58          AbstractTicketValidator tv = new MockAbstractTicketValidator();
59          tv.setServiceProperties(new ServiceProperties());
60  
61          try {
62              tv.afterPropertiesSet();
63              fail("Should have thrown IllegalArgumentException");
64          } catch (IllegalArgumentException expected) {
65              assertEquals("A casValidate URL must be set", expected.getMessage());
66          }
67      }
68  
69      public void testDetectsMissingServiceProperties() throws Exception {
70          AbstractTicketValidator tv = new MockAbstractTicketValidator();
71          tv.setCasValidate("https://company.com/cas/proxyvalidate");
72  
73          try {
74              tv.afterPropertiesSet();
75              fail("Should have thrown IllegalArgumentException");
76          } catch (IllegalArgumentException expected) {
77              assertEquals("serviceProperties must be specified", expected.getMessage());
78          }
79      }
80  
81      public void testGetters() throws Exception {
82          AbstractTicketValidator tv = new MockAbstractTicketValidator();
83          tv.setCasValidate("https://company.com/cas/proxyvalidate");
84          assertEquals("https://company.com/cas/proxyvalidate", 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", tv.getCasValidate());
100 
101         tv.setServiceProperties(new ServiceProperties());
102         assertTrue(tv.getServiceProperties() != null);
103 
104         tv.setTrustStore("/some/file/cacerts");
105         assertEquals("/some/file/cacerts", tv.getTrustStore());
106 
107         String before = System.getProperty("javax.net.ssl.trustStore");
108         tv.afterPropertiesSet();
109         assertEquals("/some/file/cacerts", System.getProperty("javax.net.ssl.trustStore"));
110 
111         if (before == null) {
112             System.setProperty("javax.net.ssl.trustStore", "");
113         } else {
114             System.setProperty("javax.net.ssl.trustStore", before);
115         }
116     }
117 
118     //~ Inner Classes ==================================================================================================
119 
120     private class MockAbstractTicketValidator extends AbstractTicketValidator {
121         private boolean returnTicket;
122 
123         public MockAbstractTicketValidator(boolean returnTicket) {
124             this.returnTicket = returnTicket;
125         }
126 
127         private MockAbstractTicketValidator() {
128             super();
129         }
130 
131         public TicketResponse confirmTicketValid(String serviceTicket)
132             throws AuthenticationException {
133             if (returnTicket) {
134                 return new TicketResponse("user", new Vector(),
135                     "PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt");
136             }
137 
138             throw new BadCredentialsException("As requested by mock");
139         }
140     }
141 }