Commits

dr...@81dbac14-341a-0410-aa85-cbcd92e6f43e  committed b249aed

Unit test fixes

  • Participants
  • Parent commits f92fdae
  • Branches amarch_sandbox

Comments (0)

Files changed (14)

File src/core/java/com/opensymphony/oscache/core/BaseCache.java

 import java.util.Properties;
 import java.util.Set;
 
+import sun.tools.tree.ThisExpression;
+
 import com.opensymphony.oscache.algorithm.LRUEvictionAlgorithm;
 import com.opensymphony.oscache.core.EntryRefreshPolicy;
 import com.opensymphony.oscache.core.CacheEntry;
 import com.opensymphony.oscache.events.CacheEvent;
 import com.opensymphony.oscache.events.CacheGroupEvent;
 import com.opensymphony.oscache.events.CacheListener;
+import com.opensymphony.oscache.events.CacheMapAccessEvent;
 import com.opensymphony.oscache.events.CachewideEvent;
 import com.opensymphony.oscache.util.FastCronParser;
+import com.opensymphony.oscache.web.filter.ExpiresRefreshPolicy;
 
 /**
  * A base class that provides most of the core caching functionality for a
 	 */
 	private Date flushDateTime = null;
 
-	private Map groupMap;
+	private Map groupMap = new HashMap();
 
 	private int capacity;
 
 		CacheEntry cacheEntry = getEntry(key);
 		Object content = null;
 		if (cacheEntry != null) {
-			content = cacheEntry.getValue();
-			if (this.isStale(cacheEntry, refreshPeriod, cronExpiry)) {
-				content = null;
+			if (!this.isStale(cacheEntry, refreshPeriod, cronExpiry)) {
+				content = cacheEntry.getValue();
 			}
 		}
 		return content;
 		CacheEntry result = removeInternal(key);
 		if (result != null) {
 			algorithm.evaluateRemove(key);
-			fireEvent(result, CacheEvent.REMOVE);
+			fireEntryEvent(result, CacheEntryEvent.ENTRY_REMOVED);
 		}
 		return result;
 	}
 
 		// fire off a notification message
 		if (oldEntry == null) {
-			fireEvent(newEntry, CacheEvent.ADD);
+			fireEntryEvent(newEntry, CacheEntryEvent.ENTRY_ADDED);
 			return null;
 		}
-		fireEvent(newEntry, CacheEvent.UPDATE);
+		fireEntryEvent(newEntry, CacheEntryEvent.ENTRY_UPDATED);
 
 		return oldEntry.getValue();
 	}
 		CacheEntry cacheEntry = getInternal(key);
 		if (cacheEntry != null) {
 			algorithm.evaluateGet(key);
+			fireEvent(new CacheMapAccessEvent(this, cacheEntry,
+					CacheMapAccessEvent.HIT));
+		} else {
+			fireEvent(new CacheMapAccessEvent(this, cacheEntry,
+					CacheMapAccessEvent.MISS));
 		}
 		return cacheEntry;
 	}
 	 * @param group
 	 *            The group to flush
 	 */
-	public void flushGroup(String group) {
+	public synchronized void flushGroup(String group) {
 		// Flush all objects in the group
 		Set groupEntries = (Set) groupMap.get(group);
 
 			while (itr.hasNext()) {
 				key = itr.next();
 				entry = getEntry(key);
-
-				if ((entry != null)
+				if (entry != null
 						&& !entry.needsRefresh(CacheEntry.INDEFINITE_EXPIRY)) {
-					remove(key);
+					entry.flush();
+					fireEntryEvent(entry, CacheEntryEvent.ENTRY_FLUSHED);
 				}
 			}
 		}
+
+		groupMap.remove(group);
+
 		fireEvent(new CacheGroupEvent(this, group));
 
 	}
 	 *            the type of event that occurred. See {@link CacheEvent} for
 	 *            the possible event types.
 	 */
-	protected void fireEvent(CacheEntry entry, int eventType) {
+	protected void fireEntryEvent(CacheEntry entry, int eventType) {
 		CacheEntryEvent event = new CacheEntryEvent(this, entry, eventType);
 		fireEvent(event);
 	}
 		this.algorithm = algorithm;
 	}
 
+	public EvictionAlgorithm getEvictionAlgorithm() {
+		return this.algorithm;
+	}
+
 	/**
 	 * Retrieves the cache entry from the underlying datastore. <p/> The
 	 * implementation of this method does not need to be synchronized; the
 	public void flushAll(Date date) {
 		synchronized (flushDateTime) {
 			flushDateTime = date;
+			clearInternal();
 			fireEvent(new CachewideEvent(this, date));
 		}
 	}

File src/core/java/com/opensymphony/oscache/core/MemoryCache.java

 	 */
 	private Map store = new HashMap();
 
-	private int capacity;
+	private int capacity = 1000;
 
 	/**
 	 * 

File src/core/java/com/opensymphony/oscache/events/CacheEntryEvent.java

 	/**
      * Get an event type for an entry added.
      */
-    public static int ENTRY_ADDED = 0;
+    public static final int ENTRY_ADDED = 0;
 
     /**
      * Get an event type for an entry updated.
      */
-    public static int ENTRY_UPDATED = 1;
+    public static final int ENTRY_UPDATED = 1;
 
     /**
      * Get an event type for an entry flushed.
      */
-    public static int ENTRY_FLUSHED = 2;
+    public static final int ENTRY_FLUSHED = 2;
 
     /**
      * Get an event type for an entry removed.
      */
-    public static int ENTRY_REMOVED = 4;
+    public static final int ENTRY_REMOVED = 4;
 
     /**
      * Get an event type for a group flush event.
      */
-    public static int GROUP_FLUSHED = 8;
-
-    /**
-     * Get an event type for a pattern flush event.
-     */
-    public static int PATTERN_FLUSHED = 16;
+    public static final int GROUP_FLUSHED = 8;
     
 
     /**

File src/core/java/com/opensymphony/oscache/events/CacheEntryEventListener.java

     void cacheGroupFlushed(CacheGroupEvent event);
 
     /**
-     * Event fired when a key pattern is flushed from the cache.
-     * Note that this event will <em>not</em> be fired if the pattern
-     * is <code>null</code> or an empty string, instead the flush
-     * request will silently be ignored.
-     */
-    void cachePatternFlushed(CachePatternEvent event);
-
-    /**
      * An event that is fired when an entire cache gets flushed.
      */
     void cacheFlushed(CachewideEvent event);

File src/core/java/com/opensymphony/oscache/events/CacheEvent.java

  * </ul>
  */
 public class CacheEvent extends EventObject {
-  public static final int ADD = 0;
-  public static final int UPDATE = 1;
-  public static final int REMOVE = 2;
-  public static final int CLEAR = 4;
+  
   public static final int LOAD_FAILED = 8;
   private Cache cache;
   private int eventType;
     return (Cache) getSource();
   }
 
-  public boolean isAddEvent() {
-    return eventType == ADD;
-  }
-
-  public boolean isUpdateEvent() {
-    return eventType == UPDATE;
-  }
-
-  public boolean isRemoveEvent() {
-    return eventType == REMOVE;
-  }
-
-  public boolean isClearEvent() {
-    return eventType == CLEAR;
-  }
-
   public boolean isLoadFailedEvent() {
     return eventType == LOAD_FAILED;
   }
 
   public String getEventTypeString() {
     switch (eventType) {
-      case ADD:
-        return "ADD";
-
-      case UPDATE:
-        return "UPDATE";
-
-      case REMOVE:
-        return "REMOVE";
-
-      case CLEAR:
-        return "CLEAR";
 
       case LOAD_FAILED:
         return "LOAD FAILED";

File src/core/java/com/opensymphony/oscache/events/CacheGroupEvent.java

  */
 public final class CacheGroupEvent extends CacheEvent {
     
-    private static final int GROUP_FLUSHED = 0;
+    public static final int GROUP_FLUSHED = 0;
 	/**
      * The group that the event applies to.
      */

File src/core/java/com/opensymphony/oscache/events/CacheMapAccessEvent.java

  */
 package com.opensymphony.oscache.events;
 
+import com.opensymphony.oscache.core.Cache;
 import com.opensymphony.oscache.core.CacheEntry;
 
 /**
      * @param eventType   Type of the event.
      * @param entry       The cache entry that the event applies to.
      */
-    public CacheMapAccessEvent(int eventType, CacheEntry entry) {
-        super(null, entry, eventType);
+    public CacheMapAccessEvent(Cache source, CacheEntry entry, int eventType ) {
+        super(source, entry, eventType);
     }
 
   

File src/core/java/com/opensymphony/oscache/events/CacheMapAccessEventListener.java

  * @author <a href="mailto:fbeauregard@pyxis-tech.com">Francois Beauregard</a>
  */
 public interface CacheMapAccessEventListener extends CacheListener {
-    /**
-     * Event fired when an entry is accessed.
-     * Use getEventType to differentiate between access events.
-     */
-    public void accessed(CacheMapAccessEvent event);
+    
 }

File src/core/java/com/opensymphony/oscache/events/CachewideEvent.java

     /**
      * Get an event type for a cache flush event.
      */
-    public static int CACHE_FLUSHED = 0;
+    public static final int CACHE_FLUSHED = 0;
 
     /**
      * Constructs a cachewide event with the specified origin.

File src/core/java/com/opensymphony/oscache/extra/CacheEntryEventListenerImpl.java

 package com.opensymphony.oscache.extra;
 
 import com.opensymphony.oscache.events.*;
+import com.opensymphony.oscache.web.CacheContextListener;
 
 /**
- * Implementation of a CacheEntryEventListener. It use the events to count
- * the operations performed on the cache.
+ * Implementation of a CacheEntryEventListener. It use the events to count the
+ * operations performed on the cache.
  * <p>
  * We are not using any synchronized so that this does not become a bottleneck.
  * The consequence is that on retrieving values, the operations that are

File src/core/java/com/opensymphony/oscache/extra/CacheMapAccessEventListenerImpl.java

     }
 
     /**
-     * This method handles an event each time the cache is accessed
-     *
-     * @param event The event triggered when the cache was accessed
+     * Resets all of the totals to zero
      */
-    public void accessed(CacheMapAccessEvent event) {
-        // Retrieve the event type and update the counters
+    public void reset() {
+        hitCount = 0;
+        staleHitCount = 0;
+        missCount = 0;
+    }
+
+    /**
+     * Return the counters in a string form
+     */
+    public String toString() {
+        return ("Hit count = " + hitCount + ", stale hit count = " + staleHitCount + " and miss count = " + missCount);
+    }
+
+	public void onChange(CacheEvent event) {
+//		 Retrieve the event type and update the counters
         int type = event.getEventType();
 
         // Handles a hit event
             // Unknown event!
             throw new IllegalArgumentException("Unknown Cache Map Access event received");
         }
-    }
-
-    /**
-     * Resets all of the totals to zero
-     */
-    public void reset() {
-        hitCount = 0;
-        staleHitCount = 0;
-        missCount = 0;
-    }
-
-    /**
-     * Return the counters in a string form
-     */
-    public String toString() {
-        return ("Hit count = " + hitCount + ", stale hit count = " + staleHitCount + " and miss count = " + missCount);
-    }
-
-	public void onChange(CacheEvent event) {
-		// TODO Auto-generated method stub
 		
 	}
 }

File src/core/test/com/opensymphony/oscache/core/TestAbstractCacheAdministrator.java

    
 
     public void testGetAndPut() {
-    		Cache cache = new MemoryCache();
+    		Cache cache = new MemoryCache(5);
     		cache.setEvictionAlgorithm(new LRUEvictionAlgorithm()	);
     		Object value = cache.get(ENTRY_KEY);
     		assertNull(value);
     public void testGetCachePath() {
     }
 
-    /**
-     * Validate that the properties retrieved by the admin are the same as the one
-     * specified in the property file. Do not test cache path or memory cache
-     * since it changes with the tests
-     */
-    public void testGetProperty() {
-        // Check if all the default properties are OK
-//        assertNull(getAdmin().getProperty(INVALID_PROP_NAME));
-//        assertNull(getAdmin().getProperty(""));
-//
-//        try {
-//            assertNull(getAdmin().getProperty(null));
-//            fail("NullPointerException expected (property Key null).");
-//        } catch (Exception e) {
-//        }
-    }
+
 
     /**
      * We cannot test this method because the value depends on the property
     public void testIsMemoryCaching() {
     }
 
-    /**
-     * Perform a call to the log method. Unfornately, there is no way to check
-     * if the logging is done correctly, we only invoke it
-     */
-    public void testLog() {
-        // Invoke the log
-        // The other log method is not tested since it calls the same as we do
-        //TODO
-
-        /*getAdmin().log(TEST_LOG, System.out);
-        getAdmin().log("", System.out);
-        getAdmin().log(null, System.out);
-        getAdmin().log(TEST_LOG, null);
-          */
-    }
+  
 
     // Abstract method that returns an instance of an admin
-    protected AbstractCacheAdministrator getAdmin() {
-    		return new AbstractCacheAdministrator() {
+    protected Cache getAdmin() {
+    		return new MemoryCache() {
     		};
     }
 }

File src/core/test/com/opensymphony/oscache/core/algorithm/TestUnlimitedCache.java

 import junit.framework.Test;
 import junit.framework.TestSuite;
 
+import com.opensymphony.oscache.algorithm.UnlimitedEvictionAlgorithm;
+import com.opensymphony.oscache.core.BaseCache;
+import com.opensymphony.oscache.core.EvictionAlgorithm;
+import com.opensymphony.oscache.core.MemoryCache;
+
 /**
  * Test class for the Unlimited cache algorithm. Most of the tests are done
  * in the TestNonQueueCache class, so only algorithm specific tests are done
     /**
      * Unlimited Cache object
      */
-    private static UnlimitedCache cache = null;
+    private static MemoryCache cache = null;
 
     /**
      * Constructor
      * <p>
      * @return  A cache instance
      */
-    public AbstractConcurrentReadCache getCache() {
+    public BaseCache getCache() {
         return cache;
     }
 
     public void setUp() {
         // Create a cache instance on first invocation
         if (cache == null) {
-            cache = new UnlimitedCache();
+            cache = new MemoryCache();
+            cache.setEvictionAlgorithm(new UnlimitedEvictionAlgorithm());
             assertNotNull(cache);
         }
     }
      */
     public void testGetSetMaxEntries() {
         // Check that the max entries cannot be changed
-        int entryCount = getCache().getMaxEntries();
-        getCache().setMaxEntries(entryCount - 1);
-        assertEquals(entryCount, getCache().getMaxEntries());
+        int entryCount = getCache().getCapacity();
+        getCache().setCapacity(entryCount - 1);
+        assertEquals(entryCount, getCache().getCapacity());
     }
 
     /**
      */
     public void testRemoveItem() {
         // Add an item, and ensure that it is not removable
-        cache.itemPut(KEY);
-        assertNull(cache.removeItem());
+    	getAlgorithm().evaluatePut(KEY);
+        assertNull(getAlgorithm().evict());
     }
+
+	@Override
+	protected EvictionAlgorithm getAlgorithm() {
+		// TODO Auto-generated method stub
+		return cache.getEvictionAlgorithm();
+	}
 }

File src/core/test/com/opensymphony/oscache/general/TestGeneralCacheAdministrator.java

  */
 package com.opensymphony.oscache.general;
 
-import com.opensymphony.oscache.core.*;
-import com.opensymphony.oscache.events.CacheEntryEventListener;
-import com.opensymphony.oscache.events.CacheMapAccessEventListener;
-import com.opensymphony.oscache.extra.CacheEntryEventListenerImpl;
-import com.opensymphony.oscache.extra.CacheMapAccessEventListenerImpl;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.util.Properties;
 
 import junit.framework.Test;
 import junit.framework.TestSuite;
 
+import com.opensymphony.oscache.core.Cache;
+import com.opensymphony.oscache.core.CacheEntry;
+import com.opensymphony.oscache.core.DummyAlwayRefreshEntryPolicy;
+import com.opensymphony.oscache.core.TestAbstractCacheAdministrator;
+import com.opensymphony.oscache.extra.CacheEntryEventListenerImpl;
+import com.opensymphony.oscache.extra.CacheMapAccessEventListenerImpl;
+
 /**
  * Test all the public methods of the GeneralCacheAdministrator class. Since
  * this class extends the TestAbstractCacheAdministrator class, the