avalon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From co...@apache.org
Subject cvs commit: jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/cache/validator/test TimeoutValidatorTestCase.java
Date Fri, 21 Dec 2001 16:33:06 GMT
colus       01/12/21 08:33:06

  Added:       src/scratchpad/org/apache/avalon/excalibur/cache/validator
                        TimeoutValidator.java
               src/scratchpad/org/apache/avalon/excalibur/cache/validator/test
                        TimeoutValidatorTestCase.java
  Removed:     src/scratchpad/org/apache/avalon/excalibur/cache
                        TimeoutValidator.java
  Log:
  Moved.
  
  Revision  Changes    Path
  1.1                  jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/cache/validator/TimeoutValidator.java
  
  Index: TimeoutValidator.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included  with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.avalon.excalibur.cache.validator;
  
  import java.util.Map;
  import java.util.HashMap;
  import org.apache.avalon.excalibur.cache.CacheEvent;
  import org.apache.avalon.excalibur.cache.CacheListener;
  import org.apache.avalon.excalibur.cache.CacheValidator;
  
  /**
   * General timeout cache validator.
   *
   * @author <a href="mailto:colus@apache.org">Eung-ju Park</a>
   */
  public class TimeoutValidator
      implements CacheValidator, CacheListener
  {
      private long    m_timeout;
      private Map     m_timestamps;
  
      public TimeoutValidator( final long timeout )
      {
          if ( 0 >= timeout )
          {
              throw new IllegalArgumentException( "Timeout must be greatter than 0" );
          }
  
          m_timeout = timeout;
  
          m_timestamps = new HashMap();
      }
  
      public boolean validate( final Object key, final Object value )
      {
          final long timestamp = ((Long)m_timestamps.get( key )).longValue();
          if ( ( System.currentTimeMillis() - timestamp ) > m_timeout )
          {
              return false;
          }
          else
          {
              return true;
          }
      }
  
      public void added( final CacheEvent event )
      {
          m_timestamps.put( event.getKey(),
                            new Long( System.currentTimeMillis() ) );
      }
  
      public void removed( final CacheEvent event )
      {
          m_timestamps.remove( event.getKey() );
      }
  }
  
  
  
  1.1                  jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/cache/validator/test/TimeoutValidatorTestCase.java
  
  Index: TimeoutValidatorTestCase.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included  with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.avalon.excalibur.cache.validator.test;
  
  import org.apache.avalon.excalibur.cache.Cache;
  import org.apache.avalon.excalibur.cache.LRUCache;
  import org.apache.avalon.excalibur.cache.validator.TimeoutValidator;
  import junit.framework.TestCase;
  
  /**
   * JUnit TestCase for TimeoutValidator.
   *
   * @author <a href="mailto:colus@apache.org">Eung-ju Park</a>
   */
  public class TimeoutValidatorTestCase
      extends TestCase
  {
      public TimeoutValidatorTestCase( final String name )
      {
          super( name );
      }
  
      public void testNotExpired()
          throws InterruptedException
      {
          final TimeoutValidator validator = new TimeoutValidator( 1000 );
  
          final Cache cache = new LRUCache( 10 );
          cache.addListener( validator );
          cache.setValidator( validator );
  
          cache.put( "K1", "V1" );
  
          Thread.sleep( 100 );
  
          assertTrue( cache.containsKey( "K1" ) );
      }
  
      public void testExpired()
          throws InterruptedException
      {
          final TimeoutValidator validator = new TimeoutValidator( 1000 );
  
          final Cache cache = new LRUCache( 10 );
          cache.addListener( validator );
          cache.setValidator( validator );
  
          cache.put( "K1", "V1" );
  
          Thread.sleep( 2000 );
  
          assertTrue( ! cache.containsKey( "K1" ) );
      }
  }
  
  
  

--
To unsubscribe, e-mail:   <mailto:avalon-cvs-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:avalon-cvs-help@jakarta.apache.org>


Mime
View raw message