001: /* Copyright 2006 The JA-SIG Collaborative. All rights reserved.
002: * See license distributed with this file and
003: * available online at http://www.uportal.org/license.html
004: */
005:
006: package org.jasig.portal.security.provider.cas;
007:
008: import java.util.ArrayList;
009: import java.util.List;
010:
011: import junit.framework.TestCase;
012:
013: /**
014: * A JUnit test that exercises the CasSecurityContextMock mock object.
015: * The mock object is useless if it cannot be relied upon to correctly emulate
016: * an ICasSecurityContext instance as configured. This test is intended to
017: * increase confidence that the mock object behaves as advertised.
018: */
019: public class CasSecurityContextMockTest extends TestCase {
020:
021: /**
022: * Test that the mock object properly logs targets.
023: */
024: public void testTargetsLog()
025: throws CasProxyTicketAcquisitionException {
026:
027: CasSecurityContextMock mock = new CasSecurityContextMock();
028:
029: mock.getCasServiceToken("aTarget");
030: mock.getCasServiceToken("anotherTarget");
031:
032: List expectedTargets = new ArrayList();
033: expectedTargets.add("aTarget");
034: expectedTargets.add("anotherTarget");
035:
036: assertEquals(expectedTargets, mock.getServiceTokenTargets());
037:
038: }
039:
040: /**
041: * Test that the mock object returns and throws as designed.
042: */
043: public void testProgrammedReturns() {
044: CasSecurityContextMock mock = new CasSecurityContextMock();
045:
046: List dummyProxyTickets = new ArrayList();
047: dummyProxyTickets.add("token1");
048: dummyProxyTickets.add("token2");
049:
050: mock.setServiceTokensToVend(dummyProxyTickets);
051:
052: try {
053: assertEquals("token1", mock.getCasServiceToken("aTarget"));
054: assertEquals("token2", mock
055: .getCasServiceToken("anotherTarget"));
056: } catch (CasProxyTicketAcquisitionException cptae) {
057: fail("Threw where should have returned the given Strings.");
058: }
059:
060: List vendedTokens = new ArrayList();
061: vendedTokens.add("token1");
062: vendedTokens.add("token2");
063:
064: assertEquals(vendedTokens, mock.getVendedServiceTokens());
065:
066: List dummyExceptions = new ArrayList();
067: RuntimeException dummyRuntimeException = new RuntimeException();
068: dummyExceptions.add(dummyRuntimeException);
069:
070: CasProxyTicketAcquisitionException cptae = new CasProxyTicketAcquisitionException(
071: "dummyservice", "dummypgtiou");
072:
073: dummyExceptions.add(cptae);
074:
075: mock.setServiceTokensToVend(dummyExceptions);
076:
077: boolean exceptionThrown = false;
078: try {
079: mock.getCasServiceToken("yetAnotherTarget");
080: } catch (CasProxyTicketAcquisitionException e) {
081: fail("Expected RuntimeException, not CasProxyTicketAcquisitionException");
082: } catch (RuntimeException re) {
083: assertEquals(dummyRuntimeException, re);
084: exceptionThrown = true;
085: }
086:
087: assertTrue(exceptionThrown);
088:
089: exceptionThrown = false;
090: try {
091: mock.getCasServiceToken("finalTarget");
092: } catch (CasProxyTicketAcquisitionException e) {
093: assertEquals(cptae, e);
094: exceptionThrown = true;
095: }
096:
097: assertTrue(exceptionThrown);
098:
099: vendedTokens.addAll(dummyExceptions);
100:
101: assertEquals(vendedTokens, mock.getVendedServiceTokens());
102:
103: }
104:
105: }
|