removed the CleanUpRegistry, Cleanable interface, etc. and just use a simple static getter on the Filter that returns the Storage. The TimerTask just calls this static getter directly. This was deemed simpler than the CleanUpRegistry solution.

for Spring config: rather than use Quartz, we just use the java.util.Timer support in Spring
This commit is contained in:
Brad Cupit 2009-03-21 21:24:31 +00:00
parent 866e2b99ba
commit b38a27626a
16 changed files with 241 additions and 424 deletions

View File

@ -101,22 +101,6 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>opensymphony</groupId>
<artifactId>quartz-all</artifactId>
<version>1.6.0</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>

View File

@ -1,59 +0,0 @@
package org.jasig.cas.client.cleanup;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
/**
* One of two timer implementations that regularly calls
* {@link CleanUpRegistry#cleanAll()}. This implementation
* is configured in via Spring and does not require any changes
* to web.xml
* <p>
* {@link CleanUpListener}, the other implementation, does require
* changes to web.xml, but does not require any Spring configuration.
* <p>
* The decision as to which timer implementation you use is entirely
* subjective, as both perform the same operation.
* <p>
* Example Spring config:
* <code>
* <bean id="cleanUpJob" class="org.springframework.scheduling.quartz.JobDetailBean">
* <property name="jobClass" value="org.jasig.cas.client.cleanup.CleanUpJob"/>
* </bean>
*
* <bean id="cleanUpTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
* <property name="jobDetail" ref="cleanUpJob"/>
* <property name="startDelay" value="60000"/>
* <property name="repeatInterval" value="60000"/>
* </bean>
*
* <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean" destroy-method="destroy">
* <property name="triggers">
* <list>
* <ref bean="cleanUpTrigger"/>
* </list>
* </property>
* </bean>
* </code>
*
* Note the destroy-method attribute of SchedulerFactoryBean. Without this attribute,
* a classloader leak may occur.
*
* @author Brad Cupit (brad [at] lsu {dot} edu)
*/
public class CleanUpJob extends QuartzJobBean {
private final CleanUpRegistry cleanUpRegistry;
public CleanUpJob() {
this.cleanUpRegistry = CleanUpRegistryImpl.getInstance();
}
public CleanUpJob(CleanUpRegistry cleanUpRegistry) {
this.cleanUpRegistry = cleanUpRegistry;
}
protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {
cleanUpRegistry.cleanAll();
}
}

View File

@ -1,97 +0,0 @@
package org.jasig.cas.client.cleanup;
import java.util.Timer;
import java.util.TimerTask;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
/**
* One of two timer implementations that regularly calls
* {@link CleanUpRegistry#cleanAll()}. This implementation
* is configured in web.xml and does not require any Spring
* configuration.
* <p>
* {@link CleanUpJob}, the other implementation, does require
* Spring, but does not require changes to web.xml
* <p>
* The decision as to which timer implementation you use is entirely
* subjective, as both perform the same operation.
* <p>
* You can customize the time between cleanup runs by setting a
* context-param in web.xml
* <p>
* Example web.xml config:
* <code>
* <context-param>
* <param-name>millisBetweenCleanUps</param-name>
* <param-value>45000</param-value>
* </context-param>
* </code>
*
* With this listener configured, a timer will be set to clean up any {@link Cleanable}s.
* This timer will automatically shut down on webapp undeploy, preventing any classloader
* leaks from occurring.
*
* @author Brad Cupit (brad [at] lsu {dot} edu)
*/
public class CleanUpListener implements ServletContextListener {
protected static final int DEFAULT_MILLIS_BETWEEN_CLEANUPS = 60 * 1000;
protected static final String MILLIS_BETWEEN_CLEANUPS_INIT_PARAM = "millisBetweenCleanUps";
private final Timer timer;
private final CleanUpRegistry cleanUpRegistry;
public CleanUpListener() {
this.timer = new Timer(true);
this.cleanUpRegistry = CleanUpRegistryImpl.getInstance();
}
protected CleanUpListener(Timer timer, CleanUpRegistry cleanUpRegistry) {
this.timer = timer;
this.cleanUpRegistry = cleanUpRegistry;
}
public void contextInitialized(ServletContextEvent servletContextEvent) {
final Cleaner cleaner = new Cleaner(this.cleanUpRegistry);
final long millisBetweenCleanUps = getMillisBetweenCleanups(servletContextEvent.getServletContext());
final long millisBeforeStart = millisBetweenCleanUps;
this.timer.schedule(cleaner, millisBeforeStart, millisBetweenCleanUps);
}
public void contextDestroyed(ServletContextEvent servletContextEvent) {
this.timer.cancel();
}
protected long getMillisBetweenCleanups(ServletContext servletContext) {
final String millisBetweenCleanUps = servletContext.getInitParameter(MILLIS_BETWEEN_CLEANUPS_INIT_PARAM);
if (millisBetweenCleanUps == null) {
return DEFAULT_MILLIS_BETWEEN_CLEANUPS;
}
try {
return Long.parseLong(millisBetweenCleanUps);
} catch (NumberFormatException exception) {
throw new RuntimeException("The servlet context-param " + MILLIS_BETWEEN_CLEANUPS_INIT_PARAM + " must be a valid number (hint: this is usually set in web.xml)", exception);
}
}
/**
* Does the actual clean up each time the timer goes off.
*
* @author Brad Cupit (brad [at] lsu {dot} edu)
*/
private static final class Cleaner extends TimerTask {
private final CleanUpRegistry cleanUpRegistry;
private Cleaner(CleanUpRegistry cleanUpRegistry) {
this.cleanUpRegistry = cleanUpRegistry;
}
public void run() {
this.cleanUpRegistry.cleanAll();
}
}
}

View File

@ -1,32 +0,0 @@
package org.jasig.cas.client.cleanup;
/**
* A central location for all {@link Cleanable}s to register themselves.
* Then a thread or timer can occasionally call {@link #cleanAll()} to
* run the {@link Cleanable#cleanUp()} methods.
* <p>
* See {@link CleanUpListener} or {@link CleanUpJob} for two implementations
* of a timer which calls {@link #cleanAll()}. Either implementation will
* work, though you only need to choose one.
*
* @author Brad Cupit (brad [at] lsu {dot} edu)
*/
public interface CleanUpRegistry {
/**
* Adds a {@link Cleanable} to the list of objects whose
* {@link Cleanable#cleanUp()} method will be called.
*
* @param cleanable the {@link Cleanable} to add to the list. If a
* {@link Cleanable} is added twice, it's {@link Cleanable#cleanUp()}
* method will be called twice each time the timer goes off
* (this is probably not the desired outcome).
*/
public void addCleanble(Cleanable cleanable);
/**
* Runs {@link Cleanable#cleanUp()} on each {@link Cleanable}
* passed in to {@link #addCleanble(Cleanable)}
*/
public void cleanAll();
}

View File

@ -1,47 +0,0 @@
package org.jasig.cas.client.cleanup;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
/**
* An old-school singleton implementation of {@link CleanUpRegistry}.
* This implementation does not require clients to add any extra Spring
* configuration, hence the old-school singleton.
*
* A thread or timer should occasionally call {@link #cleanAll()} to
* run the {@link Cleanable#cleanUp()} method on each {@link Cleanable}.
*
* @author Brad Cupit (brad [at] lsu {dot} edu)
*/
public final class CleanUpRegistryImpl implements CleanUpRegistry {
private static CleanUpRegistryImpl cleanUpRegistry = new CleanUpRegistryImpl();
private List cleanables = Collections.synchronizedList(new ArrayList());
private CleanUpRegistryImpl() {
}
public static CleanUpRegistryImpl getInstance() {
return cleanUpRegistry;
}
/**
* {@inheritDoc}
*/
public void addCleanble(Cleanable cleanable) {
cleanables.add(cleanable);
}
/**
* {@inheritDoc}
*/
public void cleanAll() {
synchronized (cleanables) {
for (Iterator iterator = cleanables.iterator(); iterator.hasNext();) {
Cleanable cleanable = (Cleanable) iterator.next();
cleanable.cleanUp();
}
}
}
}

View File

@ -1,20 +0,0 @@
package org.jasig.cas.client.cleanup;
/**
* A simple interface representing an object which needs regular cleaning.
*
* @see CleanUpRegistry
*
* @author Brad Cupit (brad [at] lsu {dot} edu)
*/
public interface Cleanable {
/**
* This method will be called on a regular basis
* to perform internal clean up (for example: removing
* old items from a cache).
* <p>
* Objects implementing this interface so they can be
* registered with the {@link CleanUpRegistry}.
*/
void cleanUp();
}

View File

@ -0,0 +1,88 @@
package org.jasig.cas.client.proxy;
import java.util.Timer;
import java.util.TimerTask;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
/**
* One of two choices for cleaning up {@link ProxyGrantingTicketStorage}.
* You must either configure this listener in web.xml or configure
* {@link CleanUpTimerTask} in Spring xml. Both choices perform the same
* operation, the only difference is one is configured via the Spring
* Framework and the other via web.xml.
* <p>
* See below for example web.xml configuration.
* See {@link CleanUpTimerTask} for an example Spring xml configuration.
* <p>
* With this listener configured, a timer will clean up the
* {@link ProxyGrantingTicketStorage storage}. This timer automatically
* shuts down on webapp undeploy, so there will be no classloader leaks
* due to an orphan thread.
* <p>
* Example web.xml configuration:
* <code>
* <listener>
* <listener-class>org.jasig.cas.client.proxy.CleanUpListener</listener-class>
* </listener>
* </code>
* <p>
* The default time between cleanups is 60 seconds, but you can optionally
* customize it by setting a context-param in web.xml. Example config:
* <code>
* <context-param>
* <param-name>millisBetweenCleanUps</param-name>
* <!-- 45 seconds between cleanup runs -->
* <param-value>45000</param-value>
* </context-param>
* </code>
*
* @author Brad Cupit (brad [at] lsu {dot} edu)
*/
public final class CleanUpListener implements ServletContextListener {
protected static final int DEFAULT_MILLIS_BETWEEN_CLEANUPS = 60 * 1000;
protected static final String MILLIS_BETWEEN_CLEANUPS_INIT_PARAM = "millisBetweenCleanUps";
private final Timer timer;
private final TimerTask timerTask;
public CleanUpListener() {
this.timer = new Timer(true);
this.timerTask = new CleanUpTimerTask();
}
/**
* for unit test use only
*/
protected CleanUpListener(final Timer timer, TimerTask timerTask ) {
this.timer = timer;
this.timerTask = timerTask;
}
public void contextInitialized(ServletContextEvent servletContextEvent) {
final long millisBetweenCleanUps = getMillisBetweenCleanups(servletContextEvent.getServletContext());
final long millisBeforeStart = millisBetweenCleanUps;
this.timer.schedule(timerTask, millisBeforeStart, millisBetweenCleanUps);
}
public void contextDestroyed(ServletContextEvent servletContextEvent) {
this.timer.cancel();
}
protected long getMillisBetweenCleanups(ServletContext servletContext) {
final String millisBetweenCleanUps = servletContext.getInitParameter(MILLIS_BETWEEN_CLEANUPS_INIT_PARAM);
if (millisBetweenCleanUps == null) {
return DEFAULT_MILLIS_BETWEEN_CLEANUPS;
}
try {
return Long.parseLong(millisBetweenCleanUps);
} catch (NumberFormatException exception) {
throw new RuntimeException("The servlet context-param " + MILLIS_BETWEEN_CLEANUPS_INIT_PARAM + " must be a valid number (hint: this is usually set in web.xml)", exception);
}
}
}

View File

@ -0,0 +1,44 @@
package org.jasig.cas.client.proxy;
import java.util.TimerTask;
import org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter;
/**
* A {@link TimerTask} implementation which performs the
* actual 'cleaning' by calling {@link ProxyGrantingTicketStorage#cleanUp()}.
* <p>
* You must configure either this TimerTask directly in Spring,
* or the {@link CleanUpListener} (which is configured in
* web.xml). Both choices perform the same operation, the only difference is
* one is configured via the Spring Framework and the other via web.xml.
* <p>
* For an example web.xml configuration, see {@link CleanUpListener}.
* For an example Spring xml configuration, see below:
* <code>
* <bean id="cleanUpTimerTask" class="org.jasig.cas.client.proxy.CleanUpTimerTask"/>
*
* <bean id="scheduledTimerTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
* <!-- first run is 15 seconds after startup -->
* <property name="delay" value="15000"/>
* <!-- subsequent runs every 5 seconds -->
* <property name="period" value="5000"/>
* <property name="timerTask" ref="cleanUpTimerTask"/>
* </bean>
*
* <bean class="org.springframework.scheduling.timer.TimerFactoryBean">
* <property name="scheduledTimerTasks">
* <list>
* <ref bean="scheduledTimerTask"/>
* </list>
* </property>
* </bean>
* </code>
*
* @author Brad Cupit (brad [at] lsu {dot} edu)
*/
public final class CleanUpTimerTask extends TimerTask {
public void run() {
Cas20ProxyReceivingTicketValidationFilter.getProxyGrantingTicketStorage().cleanUp();
}
}

View File

@ -33,4 +33,10 @@ public interface ProxyGrantingTicketStorage {
* @return the ProxyGrantingTicket Id or null if it can't be found
*/
public String retrieve(String proxyGrantingTicketIou);
/**
* Called on a regular basis by an external timer,
* giving implementations a chance to remove stale data.
*/
public void cleanUp();
}

View File

@ -12,8 +12,6 @@ import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jasig.cas.client.cleanup.CleanUpRegistry;
import org.jasig.cas.client.cleanup.Cleanable;
/**
* Implementation of {@link ProxyGrantingTicketStorage} that is backed by a
@ -28,7 +26,7 @@ import org.jasig.cas.client.cleanup.Cleanable;
* @since 3.0
*/
public final class ProxyGrantingTicketStorageImpl implements
ProxyGrantingTicketStorage, Cleanable {
ProxyGrantingTicketStorage {
private final Log log = LogFactory.getLog(getClass());
@ -100,10 +98,8 @@ public final class ProxyGrantingTicketStorageImpl implements
}
/**
* Cleans up old, expired proxy tickets. This method should be
* called regularly via a thread or timer.
*
* @see CleanUpRegistry#cleanAll()
* Cleans up old, expired proxy tickets. This method must be
* called regularly via an external thread or timer.
*/
public void cleanUp() {
synchronized (this.cache) {

View File

@ -21,12 +21,10 @@ import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jasig.cas.client.cleanup.CleanUpRegistry;
import org.jasig.cas.client.cleanup.CleanUpRegistryImpl;
import org.jasig.cas.client.cleanup.Cleanable;
import org.jasig.cas.client.proxy.Cas20ProxyRetriever;
import org.jasig.cas.client.proxy.ProxyGrantingTicketStorage;
import org.jasig.cas.client.proxy.ProxyGrantingTicketStorageImpl;
import org.jasig.cas.client.proxy.CleanUpListener;
import org.jasig.cas.client.util.CommonUtils;
/**
@ -49,16 +47,11 @@ public class Cas20ProxyReceivingTicketValidationFilter extends AbstractTicketVal
* The URL to send to the CAS server as the URL that will process proxying requests on the CAS client.
*/
private String proxyReceptorUrl;
/**
* a place to register implementations of {@link Cleanable}
*/
private CleanUpRegistry cleanUpRegistry;
/**
* Storage location of ProxyGrantingTickets and Proxy Ticket IOUs.
*/
private ProxyGrantingTicketStorage proxyGrantingTicketStorage;
private static ProxyGrantingTicketStorage proxyGrantingTicketStorage = new ProxyGrantingTicketStorageImpl();
protected void initInternal(final FilterConfig filterConfig) throws ServletException {
super.initInternal(filterConfig);
@ -69,13 +62,7 @@ public class Cas20ProxyReceivingTicketValidationFilter extends AbstractTicketVal
public void init() {
super.init();
if (this.cleanUpRegistry == null) {
this.cleanUpRegistry = CleanUpRegistryImpl.getInstance();
}
this.proxyGrantingTicketStorage = newProxyGrantingTicketStorage(this.cleanUpRegistry);
CommonUtils.assertNotNull(this.proxyGrantingTicketStorage, "proxyGrantingTicketStorage cannot be null.");
CommonUtils.assertNotNull(proxyGrantingTicketStorage, "proxyGrantingTicketStorage cannot be null.");
}
/**
@ -99,7 +86,7 @@ public class Cas20ProxyReceivingTicketValidationFilter extends AbstractTicketVal
validator = new Cas20ServiceTicketValidator(casServerUrlPrefix);
}
validator.setProxyCallbackUrl(getPropertyFromInitParams(filterConfig, "proxyCallbackUrl", null));
validator.setProxyGrantingTicketStorage(this.proxyGrantingTicketStorage);
validator.setProxyGrantingTicketStorage(proxyGrantingTicketStorage);
validator.setProxyRetriever(new Cas20ProxyRetriever(casServerUrlPrefix));
validator.setRenew(parseBoolean(getPropertyFromInitParams(filterConfig, "renew", "false")));
@ -143,7 +130,7 @@ public class Cas20ProxyReceivingTicketValidationFilter extends AbstractTicketVal
return true;
}
CommonUtils.readAndRespondToProxyReceptorRequest(request, response, this.proxyGrantingTicketStorage);
CommonUtils.readAndRespondToProxyReceptorRequest(request, response, proxyGrantingTicketStorage);
return false;
}
@ -151,15 +138,17 @@ public class Cas20ProxyReceivingTicketValidationFilter extends AbstractTicketVal
public final void setProxyReceptorUrl(final String proxyReceptorUrl) {
this.proxyReceptorUrl = proxyReceptorUrl;
}
protected final void setCleanUpRegistry(final CleanUpRegistry cleanUpRegistry) {
this.cleanUpRegistry = cleanUpRegistry;
}
private ProxyGrantingTicketStorage newProxyGrantingTicketStorage(final CleanUpRegistry cleanUpRegistry) {
ProxyGrantingTicketStorageImpl proxyGrantingTicketStorageImpl = new ProxyGrantingTicketStorageImpl();
cleanUpRegistry.addCleanble(proxyGrantingTicketStorageImpl);
return proxyGrantingTicketStorageImpl;
/**
* Static getter so {@link CleanUpListener} has some way of retrieving
* the actual storage. This relies on the fact that this class (the Filter)
* will only be instantiated once per webapp.
*/
public static ProxyGrantingTicketStorage getProxyGrantingTicketStorage() {
return proxyGrantingTicketStorage;
}
public void setProxyGrantingTicketStorage(ProxyGrantingTicketStorage storage) {
proxyGrantingTicketStorage = storage;
}
}

View File

@ -1,30 +0,0 @@
package org.jasig.cas.client.cleanup;
import org.jasig.cas.client.util.MethodFlag;
import junit.framework.TestCase;
/**
* Unit test for {@link CleanUpJob}
*
* @author Brad Cupit (brad [at] lsu {dot} edu)
*/
public class CleanUpJobTest extends TestCase {
public void testExecuteInternal() throws Exception {
final MethodFlag cleanAllMethodFlag = new MethodFlag();
CleanUpRegistry localCleanUpRegistry = new CleanUpRegistry() {
public void addCleanble(Cleanable cleanable) {
}
public void cleanAll() {
cleanAllMethodFlag.setCalled();
}
};
final CleanUpJob cleanUpJob = new CleanUpJob(localCleanUpRegistry);
cleanUpJob.executeInternal(null);
assertTrue(cleanAllMethodFlag.wasCalled());
}
}

View File

@ -1,28 +0,0 @@
package org.jasig.cas.client.cleanup;
import org.jasig.cas.client.util.MethodFlag;
import junit.framework.TestCase;
/**
* Unit test for {@link CleanUpRegistryImpl}
*
* @author Brad Cupit (brad [at] lsu {dot} edu)
*/
public class CleanUpRegistryImplTest extends TestCase {
private final CleanUpRegistry cleanUpRegistry = CleanUpRegistryImpl.getInstance();
public void testCleanAll() throws Exception {
final MethodFlag cleanUpMethodFlag = new MethodFlag();
cleanUpRegistry.addCleanble(new Cleanable() {
public void cleanUp() {
cleanUpMethodFlag.setCalled();
}
});
cleanUpRegistry.cleanAll();
assertTrue(cleanUpMethodFlag.wasCalled());
}
}

View File

@ -1,4 +1,4 @@
package org.jasig.cas.client.cleanup;
package org.jasig.cas.client.proxy;
import java.util.Timer;
import java.util.TimerTask;
@ -17,8 +17,8 @@ import org.springframework.mock.web.MockServletContext;
* @author Brad Cupit (brad [at] lsu {dot} edu)
*/
public class CleanUpListenerTest extends TestCase {
private final CleanUpRegistry defaultCleanUpRegistry = CleanUpRegistryImpl.getInstance();
private final Timer defaultTimer = new Timer(true);
private final CleanUpTimerTask defaultTimerTask = new CleanUpTimerTask();
public void testStartsThreadAtStartup() throws Exception {
final MethodFlag scheduleMethodFlag = new MethodFlag();
@ -29,7 +29,7 @@ public class CleanUpListenerTest extends TestCase {
}
};
final CleanUpListener cleanUpListener = new CleanUpListener(timer, defaultCleanUpRegistry);
final CleanUpListener cleanUpListener = new CleanUpListener(timer, defaultTimerTask);
cleanUpListener.contextInitialized(new TestServletContextEvent(1));
assertTrue(scheduleMethodFlag.wasCalled());
@ -45,7 +45,7 @@ public class CleanUpListenerTest extends TestCase {
}
};
final CleanUpListener cleanUpListener = new CleanUpListener(timer, defaultCleanUpRegistry);
final CleanUpListener cleanUpListener = new CleanUpListener(timer, defaultTimerTask);
cleanUpListener.contextInitialized(new TestServletContextEvent(1));
cleanUpListener.contextDestroyed(null);
@ -53,51 +53,45 @@ public class CleanUpListenerTest extends TestCase {
}
public void testCallsCleanAllOnSchedule() throws Exception {
final MethodFlag cleanAllMethodFlag = new MethodFlag();
final MethodFlag timerTaskFlag = new MethodFlag();
final CleanUpRegistry cleanUpRegistry = new CleanUpRegistry() {
public void addCleanble(Cleanable cleanable) {
}
public void cleanAll() {
cleanAllMethodFlag.setCalled();
final TimerTask timerTask = new TimerTask() {
public void run() {
timerTaskFlag.setCalled();
}
};
long millisBetweenCleanUps = 250;
final CleanUpListener cleanUpListener = new CleanUpListener(defaultTimer, cleanUpRegistry);
final CleanUpListener cleanUpListener = new CleanUpListener(defaultTimer, timerTask);
cleanUpListener.contextInitialized(new TestServletContextEvent(millisBetweenCleanUps));
// wait long enough for the clean up to occur
Thread.sleep(millisBetweenCleanUps * 2);
assertTrue(cleanAllMethodFlag.wasCalled());
assertTrue(timerTaskFlag.wasCalled());
}
public void testDelaysFirstCleanAll() throws Exception {
final MethodFlag cleanAllMethodFlag = new MethodFlag();
final MethodFlag timerTaskFlag = new MethodFlag();
final CleanUpRegistry cleanUpRegistry = new CleanUpRegistry() {
public void addCleanble(Cleanable cleanable) {
}
public void cleanAll() {
cleanAllMethodFlag.setCalled();
final TimerTask timerTask = new TimerTask() {
public void run() {
timerTaskFlag.setCalled();
}
};
long millisBetweenCleanUps = 250;
final CleanUpListener cleanUpListener = new CleanUpListener(defaultTimer, cleanUpRegistry);
final CleanUpListener cleanUpListener = new CleanUpListener(defaultTimer, timerTask);
cleanUpListener.contextInitialized(new TestServletContextEvent(millisBetweenCleanUps));
assertFalse(cleanAllMethodFlag.wasCalled());
assertFalse(timerTaskFlag.wasCalled());
// wait long enough for the clean up to occur
Thread.sleep(millisBetweenCleanUps * 2);
assertTrue(cleanAllMethodFlag.wasCalled());
assertTrue(timerTaskFlag.wasCalled());
}
public void testReturnsDefaultWhenNoContextParamConfigured() throws Exception {

View File

@ -0,0 +1,49 @@
package org.jasig.cas.client.proxy;
import java.util.TimerTask;
import org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter;
import junit.framework.TestCase;
/**
* Unit test for the {@link CleanUpTimerTask}
*
* @author Brad Cupit (brad [at] lsu {dot} edu)
*/
public class CleanUpTimerTaskTest extends TestCase {
public void testRun() throws Exception {
TimerTask timerTask = new CleanUpTimerTask();
ProxyGrantingTicketStorageTestImpl storage = new ProxyGrantingTicketStorageTestImpl();
new Cas20ProxyReceivingTicketValidationFilter().setProxyGrantingTicketStorage(storage);
timerTask.run();
assertTrue(storage.cleanUpWasCalled());
}
/**
* implementation of the storage interface used only for testing
*
* @author Brad Cupit (brad [at] lsu {dot} edu)
*/
private static final class ProxyGrantingTicketStorageTestImpl implements ProxyGrantingTicketStorage {
private boolean cleanUpCalled = false;
public boolean cleanUpWasCalled() {
return cleanUpCalled;
}
public void cleanUp() {
cleanUpCalled = true;
}
public String retrieve(String proxyGrantingTicketIou) {
return null;
}
public void save(String proxyGrantingTicketIou, String proxyGrantingTicket) {
}
}
}

View File

@ -1,31 +1,32 @@
package org.jasig.cas.client.validation;
import java.util.ArrayList;
import java.util.List;
import junit.framework.TestCase;
import org.jasig.cas.client.cleanup.CleanUpRegistry;
import org.jasig.cas.client.cleanup.Cleanable;
import org.jasig.cas.client.proxy.ProxyGrantingTicketStorageImpl;
/**
* Unit test for {@link Cas20ProxyReceivingTicketValidationFilter}
*
* @author Brad Cupit (brad [at] lsu {dot} edu)
*/
public class Cas20ProxyReceivingTicketValidationFilterTest extends TestCase {
public void testRegistersPGTicketStorageWithCleanUpRegistry() throws Exception {
final TestCleanUpRegistry cleanUpRegistry = new TestCleanUpRegistry();
final Cas20ProxyReceivingTicketValidationFilter filter = newCas20ProxyReceivingTicketValidationFilter();
filter.setCleanUpRegistry(cleanUpRegistry);
filter.init();
assertEquals(1, cleanUpRegistry.getCleanables().size());
assertSame(ProxyGrantingTicketStorageImpl.class, cleanUpRegistry.getCleanables().get(0).getClass());
public void testHasDefaultStorage() throws Exception {
assertNotNull(Cas20ProxyReceivingTicketValidationFilter.getProxyGrantingTicketStorage());
}
public void testThrowsForNullStorage() throws Exception {
Cas20ProxyReceivingTicketValidationFilter filter = newCas20ProxyReceivingTicketValidationFilter();
filter.setProxyGrantingTicketStorage(null);
try {
filter.init();
fail("expected an exception due to null ProxyGrantingTicketStorage");
} catch (IllegalArgumentException exception) {
// test passes
}
}
/**
* construct a working {@link Cas20ProxyReceivingTicketValidationFilter}
*/
private Cas20ProxyReceivingTicketValidationFilter newCas20ProxyReceivingTicketValidationFilter() {
final Cas20ProxyReceivingTicketValidationFilter filter = new Cas20ProxyReceivingTicketValidationFilter();
filter.setServerName("localhost");
@ -33,25 +34,4 @@ public class Cas20ProxyReceivingTicketValidationFilterTest extends TestCase {
return filter;
}
/**
* A test implementation of {@link CleanUpRegistry} that allows us to see
* which {@link Cleanable}s were registered.
*
* @author Brad Cupit (brad [at] lsu {dot} edu)
*/
private static final class TestCleanUpRegistry implements CleanUpRegistry {
private final List cleanables = new ArrayList();
public void addCleanble(Cleanable cleanable) {
cleanables.add(cleanable);
}
public void cleanAll() {
}
public List getCleanables() {
return cleanables;
}
};
}