/** * Description of the Method * <p> * @return ICompositeCacheAttributes a copy */ @Override public ICompositeCacheAttributes copy() { try { ICompositeCacheAttributes cattr = (CompositeCacheAttributes) this.clone(); return cattr; } catch ( Exception e ) { System.err.println( e.toString() ); return new CompositeCacheAttributes(); } }
/** * @param cacheName * @return Returns a CompositeCache */ @Override @SuppressWarnings("unchecked") public <K, V> CompositeCache<K, V> getCache( String cacheName ) { if ( cache == null ) { // System.out.println( "Creating mock cache" ); CompositeCache<K, V> newCache = new CompositeCache<K, V>( new CompositeCacheAttributes(), new ElementAttributes() ); this.setCache( newCache ); } return (CompositeCache<K, V>)cache; }
/** * @param cacheName * @return Returns a CompositeCache */ @Override @SuppressWarnings("unchecked") public <K, V> CompositeCache<K, V> getCache( String cacheName ) { if ( cache == null ) { // System.out.println( "Creating mock cache" ); CompositeCache<K, V> newCache = new CompositeCache<K, V>( new CompositeCacheAttributes(), new ElementAttributes() ); this.setCache( newCache ); } return (CompositeCache<K, V>)cache; }
/** * Verify that we can get a region using the define region method with cache attributes. * @throws Exception */ public void testRegionDefinitonWithAttributes() throws Exception { ICompositeCacheAttributes ca = new CompositeCacheAttributes(); long maxIdleTime = 8765; ca.setMaxMemoryIdleTimeSeconds( maxIdleTime ); CacheAccess<String, String> access = JCS.defineRegion( "testRegionDefinitonWithAttributes", ca ); assertNotNull( "We should have an access class", access ); ICompositeCacheAttributes ca2 = access.getCacheAttributes(); assertEquals( "Wrong idle time setting.", ca.getMaxMemoryIdleTimeSeconds(), ca2.getMaxMemoryIdleTimeSeconds() ); }
/** * Verify that we can get a region using the define region method with cache attributes. * @throws Exception */ public void testRegionDefinitonWithAttributes() throws Exception { ICompositeCacheAttributes ca = new CompositeCacheAttributes(); long maxIdleTime = 8765; ca.setMaxMemoryIdleTimeSeconds( maxIdleTime ); CacheAccess<String, String> access = JCS.getInstance( "testRegionDefinitonWithAttributes", ca ); assertNotNull( "We should have an access class", access ); ICompositeCacheAttributes ca2 = access.getCacheAttributes(); assertEquals( "Wrong idle time setting.", ca.getMaxMemoryIdleTimeSeconds(), ca2.getMaxMemoryIdleTimeSeconds() ); }
/** * Verify that we can get a region using the define region method with cache attributes and * element attributes. * @throws Exception */ public void testRegionDefinitonWithBothAttributes() throws Exception { ICompositeCacheAttributes ca = new CompositeCacheAttributes(); long maxIdleTime = 8765; ca.setMaxMemoryIdleTimeSeconds( maxIdleTime ); long maxLife = 9876; IElementAttributes attr = new ElementAttributes(); attr.setMaxLife(maxLife); CacheAccess<String, String> access = JCS.defineRegion( "testRegionDefinitonWithAttributes", ca, attr ); assertNotNull( "We should have an access class", access ); ICompositeCacheAttributes ca2 = access.getCacheAttributes(); assertEquals( "Wrong idle time setting.", ca.getMaxMemoryIdleTimeSeconds(), ca2.getMaxMemoryIdleTimeSeconds() ); }
/** * Verify that we can get a region using the define region method with cache attributes and * element attributes. * @throws Exception */ public void testRegionDefinitonWithBothAttributes() throws Exception { ICompositeCacheAttributes ca = new CompositeCacheAttributes(); long maxIdleTime = 8765; ca.setMaxMemoryIdleTimeSeconds( maxIdleTime ); long maxLife = 9876; IElementAttributes attr = new ElementAttributes(); attr.setMaxLife(maxLife); CacheAccess<String, String> access = JCS.getInstance( "testRegionDefinitonWithAttributes", ca, attr ); assertNotNull( "We should have an access class", access ); ICompositeCacheAttributes ca2 = access.getCacheAttributes(); assertEquals( "Wrong idle time setting.", ca.getMaxMemoryIdleTimeSeconds(), ca2.getMaxMemoryIdleTimeSeconds() ); }
/** * Setup a disk cache. Configure the disk usage pattern to not swap. Call spool. Verify that the * item is not put to disk. */ public void testSpoolNotAllowed() { // SETUP ICompositeCacheAttributes cattr = new CompositeCacheAttributes(); cattr.setCacheName(CACHE_NAME); cattr.setDiskUsagePattern( ICompositeCacheAttributes.DiskUsagePattern.UPDATE ); IElementAttributes attr = new ElementAttributes(); CompositeCache<String, String> cache = new CompositeCache<String, String>( cattr, attr ); MockAuxCache<String, String> mock = new MockAuxCache<String, String>(); mock.cacheType = CacheType.DISK_CACHE; @SuppressWarnings("unchecked") AuxiliaryCache<String, String>[] auxArray = new AuxiliaryCache[] { mock }; cache.setAuxCaches( auxArray ); ICacheElement<String, String> inputElement = new CacheElement<String, String>( CACHE_NAME, "key", "value" ); // DO WORK cache.spoolToDisk( inputElement ); // VERIFY assertEquals( "Wrong number of calls to the disk cache update.", 0, mock.updateCount ); }
/** * Setup a disk cache. Configure the disk usage pattern to not swap. Call spool. Verify that the * item is not put to disk. */ public void testSpoolNotAllowed() { // SETUP ICompositeCacheAttributes cattr = new CompositeCacheAttributes(); cattr.setCacheName(CACHE_NAME); cattr.setDiskUsagePattern( ICompositeCacheAttributes.DiskUsagePattern.UPDATE ); IElementAttributes attr = new ElementAttributes(); CompositeCache<String, String> cache = new CompositeCache<String, String>( cattr, attr ); MockAuxCache<String, String> mock = new MockAuxCache<String, String>(); mock.cacheType = CacheType.DISK_CACHE; @SuppressWarnings("unchecked") AuxiliaryCache<String, String>[] auxArray = new AuxiliaryCache[] { mock }; cache.setAuxCaches( auxArray ); ICacheElement<String, String> inputElement = new CacheElement<String, String>( CACHE_NAME, "key", "value" ); // DO WORK cache.spoolToDisk( inputElement ); // VERIFY assertEquals( "Wrong number of calls to the disk cache update.", 0, mock.updateCount ); }
/** * Verify that calling release, when there are active clients, the caches are correctly disposed or not. */ public void testRelease() { // See JCS-184 // create the manager CompositeCacheManager manager = CompositeCacheManager.getInstance(); // add a simple cache CompositeCacheAttributes cacheAttributes = new CompositeCacheAttributes(); CompositeCache<String, String> cache = new CompositeCache<String, String>(cacheAttributes, /* attr */ null); manager.addCache("simple_cache", cache); // add a client to the cache CompositeCacheManager.getUnconfiguredInstance(); // won't release as there are still clients. Only disposed when release() is called by // the last client manager.release(); assertEquals("The cache was disposed during release!", CacheStatus.ALIVE, cache.getStatus()); manager.release(); assertEquals("The cache was NOT disposed during release!", CacheStatus.DISPOSED, cache.getStatus()); }
/** * Verify we try a disk aux on a getMatching call. * <p> * @throws IOException */ public void testGetMatching_NotOnDisk() throws IOException { // SETUP int maxMemorySize = 0; String cacheName = "testGetMatching_NotOnDisk"; String memoryCacheClassName = "org.apache.commons.jcs.engine.memory.lru.LRUMemoryCache"; ICompositeCacheAttributes cattr = new CompositeCacheAttributes(); cattr.setCacheName(cacheName); cattr.setMemoryCacheName( memoryCacheClassName ); cattr.setMaxObjects( maxMemorySize ); IElementAttributes attr = new ElementAttributes(); CompositeCache<String, Integer> cache = new CompositeCache<String, Integer>( cattr, attr ); MockAuxiliaryCache<String, Integer> diskMock = new MockAuxiliaryCache<String, Integer>(); diskMock.cacheType = CacheType.DISK_CACHE; @SuppressWarnings("unchecked") AuxiliaryCache<String, Integer>[] aux = new AuxiliaryCache[] { diskMock }; cache.setAuxCaches( aux ); // DO WORK cache.getMatching( "junk" ); // VERIFY assertEquals( "Wrong number of calls", 1, diskMock.getMatchingCallCount ); }
/** * Setup a disk cache. Configure the disk usage pattern to SWAP. Call updateAuxiliaries. Verify * that the item is not put to disk. * <p> * This tests that the items are not put to disk on a normal put when the usage pattern is set * to SWAP. * <p> * @throws IOException */ public void testUpdateNotAllowed() throws IOException { // SETUP ICompositeCacheAttributes cattr = new CompositeCacheAttributes(); cattr.setCacheName(CACHE_NAME); cattr.setDiskUsagePattern( ICompositeCacheAttributes.DiskUsagePattern.SWAP ); IElementAttributes attr = new ElementAttributes(); CompositeCache<String, String> cache = new CompositeCache<String, String>( cattr, attr ); MockAuxCache<String, String> mock = new MockAuxCache<String, String>(); mock.cacheType = CacheType.DISK_CACHE; @SuppressWarnings("unchecked") AuxiliaryCache<String, String>[] auxArray = new AuxiliaryCache[] { mock }; cache.setAuxCaches( auxArray ); ICacheElement<String, String> inputElement = new CacheElement<String, String>( CACHE_NAME, "key", "value" ); // DO WORK cache.updateAuxiliaries( inputElement, true ); // VERIFY assertEquals( "Wrong number of calls to the disk cache update.", 0, mock.updateCount ); }
/** * Verify we try a disk aux on a getMatching call. * <p> * @throws IOException */ public void testGetMatching_NotOnDisk() throws IOException { // SETUP int maxMemorySize = 0; String cacheName = "testGetMatching_NotOnDisk"; String memoryCacheClassName = "org.apache.commons.jcs.engine.memory.lru.LRUMemoryCache"; ICompositeCacheAttributes cattr = new CompositeCacheAttributes(); cattr.setCacheName(cacheName); cattr.setMemoryCacheName( memoryCacheClassName ); cattr.setMaxObjects( maxMemorySize ); IElementAttributes attr = new ElementAttributes(); CompositeCache<String, Integer> cache = new CompositeCache<String, Integer>( cattr, attr ); MockAuxiliaryCache<String, Integer> diskMock = new MockAuxiliaryCache<String, Integer>(); diskMock.cacheType = CacheType.DISK_CACHE; @SuppressWarnings("unchecked") AuxiliaryCache<String, Integer>[] aux = new AuxiliaryCache[] { diskMock }; cache.setAuxCaches( aux ); // DO WORK cache.getMatching( "junk" ); // VERIFY assertEquals( "Wrong number of calls", 1, diskMock.getMatchingCallCount ); }
/** verify the check for removal * <p> * @throws IOException */ public void testCheckForRemoval_NotExpired() throws IOException { // SETUP CompositeCacheAttributes cacheAttr = new CompositeCacheAttributes(); cacheAttr.setCacheName("testRegion"); cacheAttr.setMaxMemoryIdleTimeSeconds( 10 ); cacheAttr.setMaxSpoolPerRun( 10 ); CompositeCache<String, String> cache = new CompositeCache<String, String>(cacheAttr, new ElementAttributes()); String key = "key"; String value = "value"; ICacheElement<String, String> element = new CacheElement<String, String>( "testRegion", key, value ); ElementAttributes elementAttr = new ElementAttributes(); elementAttr.setIsEternal( false ); element.setElementAttributes( elementAttr ); element.getElementAttributes().setMaxLife(1); long now = System.currentTimeMillis(); // subtract two seconds now -= 2000; // DO WORK boolean result = cache.isExpired( element, now, ElementEventType.EXCEEDED_MAXLIFE_BACKGROUND, ElementEventType.EXCEEDED_IDLETIME_BACKGROUND ); // VERIFY assertFalse( "Item should not have expired.", result ); }
/** verify the check for removal * <p> * @throws IOException */ public void testCheckForRemoval_NotExpired() throws IOException { // SETUP CompositeCacheAttributes cacheAttr = new CompositeCacheAttributes(); cacheAttr.setCacheName("testRegion"); cacheAttr.setMaxMemoryIdleTimeSeconds( 10 ); cacheAttr.setMaxSpoolPerRun( 10 ); CompositeCache<String, String> cache = new CompositeCache<String, String>(cacheAttr, new ElementAttributes()); String key = "key"; String value = "value"; ICacheElement<String, String> element = new CacheElement<String, String>( "testRegion", key, value ); ElementAttributes elementAttr = new ElementAttributes(); elementAttr.setIsEternal( false ); element.setElementAttributes( elementAttr ); element.getElementAttributes().setMaxLife(1); long now = System.currentTimeMillis(); // subtract two seconds now -= 2000; // DO WORK boolean result = cache.isExpired( element, now, ElementEventType.EXCEEDED_MAXLIFE_BACKGROUND, ElementEventType.EXCEEDED_IDLETIME_BACKGROUND ); // VERIFY assertFalse( "Item should not have expired.", result ); }
/** * Setup a disk cache. Configure the disk usage pattern to swap. Call spool. Verify that the * item is put to disk. */ public void testSpoolAllowed() { // SETUP ICompositeCacheAttributes cattr = new CompositeCacheAttributes(); cattr.setCacheName(CACHE_NAME); cattr.setDiskUsagePattern( ICompositeCacheAttributes.DiskUsagePattern.SWAP ); IElementAttributes attr = new ElementAttributes(); CompositeCache<String, String> cache = new CompositeCache<String, String>( cattr, attr ); MockAuxCache<String, String> mock = new MockAuxCache<String, String>(); mock.cacheType = CacheType.DISK_CACHE; @SuppressWarnings("unchecked") AuxiliaryCache<String, String>[] auxArray = new AuxiliaryCache[] { mock }; cache.setAuxCaches( auxArray ); ICacheElement<String, String> inputElement = new CacheElement<String, String>( CACHE_NAME, "key", "value" ); // DO WORK cache.spoolToDisk( inputElement ); // VERIFY assertEquals( "Wrong number of calls to the disk cache update.", 1, mock.updateCount ); assertEquals( "Wrong element updated.", inputElement, mock.lastUpdatedItem ); }
/** * Setup a disk cache. Configure the disk usage pattern to swap. Call spool. Verify that the * item is put to disk. */ public void testSpoolAllowed() { // SETUP ICompositeCacheAttributes cattr = new CompositeCacheAttributes(); cattr.setCacheName(CACHE_NAME); cattr.setDiskUsagePattern( ICompositeCacheAttributes.DiskUsagePattern.SWAP ); IElementAttributes attr = new ElementAttributes(); CompositeCache<String, String> cache = new CompositeCache<String, String>( cattr, attr ); MockAuxCache<String, String> mock = new MockAuxCache<String, String>(); mock.cacheType = CacheType.DISK_CACHE; @SuppressWarnings("unchecked") AuxiliaryCache<String, String>[] auxArray = new AuxiliaryCache[] { mock }; cache.setAuxCaches( auxArray ); ICacheElement<String, String> inputElement = new CacheElement<String, String>( CACHE_NAME, "key", "value" ); // DO WORK cache.spoolToDisk( inputElement ); // VERIFY assertEquals( "Wrong number of calls to the disk cache update.", 1, mock.updateCount ); assertEquals( "Wrong element updated.", inputElement, mock.lastUpdatedItem ); }
/** * Setup a disk cache. Configure the disk usage pattern to UPDATE. Call updateAuxiliaries. * Verify that the item is put to disk. * <p> * This tests that the items are put to disk on a normal put when the usage pattern is set * appropriately. * @throws IOException */ public void testUpdateAllowed() throws IOException { // SETUP ICompositeCacheAttributes cattr = new CompositeCacheAttributes(); cattr.setCacheName(CACHE_NAME); cattr.setDiskUsagePattern( ICompositeCacheAttributes.DiskUsagePattern.UPDATE ); IElementAttributes attr = new ElementAttributes(); CompositeCache<String, String> cache = new CompositeCache<String, String>( cattr, attr ); MockAuxCache<String, String> mock = new MockAuxCache<String, String>(); mock.cacheType = CacheType.DISK_CACHE; @SuppressWarnings("unchecked") AuxiliaryCache<String, String>[] auxArray = new AuxiliaryCache[] { mock }; cache.setAuxCaches( auxArray ); ICacheElement<String, String> inputElement = new CacheElement<String, String>( CACHE_NAME, "key", "value" ); // DO WORK cache.updateAuxiliaries( inputElement, true ); // VERIFY assertEquals( "Wrong number of calls to the disk cache update.", 1, mock.updateCount ); assertEquals( "Wrong element updated.", inputElement, mock.lastUpdatedItem ); }
/** verify the check for removal * <p> * @throws IOException */ public void testCheckForRemoval_Expired() throws IOException { // SETUP CompositeCacheAttributes cacheAttr = new CompositeCacheAttributes(); cacheAttr.setCacheName("testRegion"); cacheAttr.setMaxMemoryIdleTimeSeconds( 10 ); cacheAttr.setMaxSpoolPerRun( 10 ); CompositeCache<String, String> cache = new CompositeCache<String, String>(cacheAttr, new ElementAttributes()); String key = "key"; String value = "value"; ICacheElement<String, String> element = new CacheElement<String, String>( "testRegion", key, value ); ElementAttributes elementAttr = new ElementAttributes(); elementAttr.setIsEternal( false ); element.setElementAttributes( elementAttr ); element.getElementAttributes().setMaxLife(1); long now = System.currentTimeMillis(); // add two seconds now += 2000; // DO WORK boolean result = cache.isExpired( element, now, ElementEventType.EXCEEDED_MAXLIFE_BACKGROUND, ElementEventType.EXCEEDED_IDLETIME_BACKGROUND ); // VERIFY assertTrue( "Item should have expired.", result ); }
/** verify the check for removal * <p> * @throws IOException */ public void testCheckForRemoval_IdleTooLong() throws IOException { // SETUP CompositeCacheAttributes cacheAttr = new CompositeCacheAttributes(); cacheAttr.setCacheName("testRegion"); cacheAttr.setMaxMemoryIdleTimeSeconds( 10 ); cacheAttr.setMaxSpoolPerRun( 10 ); CompositeCache<String, String> cache = new CompositeCache<String, String>(cacheAttr, new ElementAttributes()); String key = "key"; String value = "value"; ICacheElement<String, String> element = new CacheElement<String, String>( "testRegion", key, value ); ElementAttributes elementAttr = new ElementAttributes(); elementAttr.setIsEternal( false ); element.setElementAttributes( elementAttr ); element.getElementAttributes().setMaxLife(100); element.getElementAttributes().setIdleTime( 1 ); long now = System.currentTimeMillis(); // add two seconds now += 2000; // DO WORK boolean result = cache.isExpired( element, now, ElementEventType.EXCEEDED_MAXLIFE_BACKGROUND, ElementEventType.EXCEEDED_IDLETIME_BACKGROUND ); // VERIFY assertTrue( "Item should have expired.", result ); }