1
2
3
4
5
6
7
8
9
10
11
12
13
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
32
33
34
35
36 public class AbstractTicketValidatorTests extends TestCase {
37
38
39 public AbstractTicketValidatorTests() {
40 super();
41 }
42
43 public AbstractTicketValidatorTests(String arg0) {
44 super(arg0);
45 }
46
47
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
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 }