You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by tv...@apache.org on 2014/02/23 11:37:52 UTC
svn commit: r1570993 [22/24] - in /commons/proper/jcs/trunk/src: assembly/
changes/ conf/ java/org/apache/commons/jcs/access/
java/org/apache/commons/jcs/access/exception/
java/org/apache/commons/jcs/admin/ java/org/apache/commons/jcs/auxiliary/
java/o...
Modified: commons/proper/jcs/trunk/src/test/org/apache/commons/jcs/engine/control/CompositeCacheConfiguratorUnitTest.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/src/test/org/apache/commons/jcs/engine/control/CompositeCacheConfiguratorUnitTest.java?rev=1570993&r1=1570992&r2=1570993&view=diff
==============================================================================
--- commons/proper/jcs/trunk/src/test/org/apache/commons/jcs/engine/control/CompositeCacheConfiguratorUnitTest.java (original)
+++ commons/proper/jcs/trunk/src/test/org/apache/commons/jcs/engine/control/CompositeCacheConfiguratorUnitTest.java Sun Feb 23 10:37:48 2014
@@ -1,74 +1,93 @@
-package org.apache.commons.jcs.engine.control;
-
-import java.util.Properties;
-
-import junit.framework.TestCase;
-
-import org.apache.commons.jcs.auxiliary.AuxiliaryCache;
-import org.apache.commons.jcs.auxiliary.AuxiliaryCacheConfigurator;
-import org.apache.commons.jcs.auxiliary.MockAuxiliaryCache;
-import org.apache.commons.jcs.auxiliary.MockAuxiliaryCacheAttributes;
-import org.apache.commons.jcs.auxiliary.MockAuxiliaryCacheFactory;
-import org.apache.commons.jcs.engine.logging.MockCacheEventLogger;
-
-/** Unit tests for the configurator. */
-public class CompositeCacheConfiguratorUnitTest
- extends TestCase
-{
- /**
- * Verify that we can parse the event logger correctly
- */
- public void testParseAuxiliary_CacheEventLogger_Normal()
- {
- // SETUP
- String regionName = "MyRegion";
-
- String auxName = "MockAux";
- String auxPrefix = "jcs.auxiliary." + auxName;
- String auxiliaryClassName = MockAuxiliaryCacheFactory.class.getName();
- String eventLoggerClassName = MockCacheEventLogger.class.getName();
- String auxiliaryAttributeClassName = MockAuxiliaryCacheAttributes.class.getName();
-
- Properties props = new Properties();
- props.put( auxPrefix, auxiliaryClassName );
- props.put( auxPrefix + CompositeCacheConfigurator.ATTRIBUTE_PREFIX, auxiliaryAttributeClassName );
- props.put( auxPrefix + AuxiliaryCacheConfigurator.CACHE_EVENT_LOGGER_PREFIX, eventLoggerClassName );
-
- System.out.print( props );
-
- CompositeCacheManager manager = CompositeCacheManager.getUnconfiguredInstance();
-
- CompositeCacheConfigurator configurator = new CompositeCacheConfigurator( manager );
-
- // DO WORK
- AuxiliaryCache<String, String> aux = configurator.parseAuxiliary( props, auxName, regionName );
- MockAuxiliaryCache<String, String> result = (MockAuxiliaryCache<String, String>)aux;
-
- // VERIFY
- assertNotNull( "Should have an auxcache.", result );
- assertNotNull( "Should have an event logger.", result.getCacheEventLogger() );
- }
-
- /**
- * Verify that we can parse the spool chunk size
- */
- public void testParseSpoolChunkSize_Normal()
- {
- // SETUP
- String regionName = "MyRegion";
- int chunkSize = 5;
-
- Properties props = new Properties();
- props.put( "jcs.default", "" );
- props.put( "jcs.default.cacheattributes.SpoolChunkSize", String.valueOf( chunkSize ) );
-
- CompositeCacheManager manager = CompositeCacheManager.getUnconfiguredInstance();
-
- // DO WORK
- manager.configure( props );
-
- // VERIFY
- CompositeCache<String, String> cache = manager.getCache( regionName );
- assertEquals( "Wrong chunkSize", cache.getCacheAttributes().getSpoolChunkSize(), chunkSize );
- }
-}
+package org.apache.commons.jcs.engine.control;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.util.Properties;
+
+import junit.framework.TestCase;
+
+import org.apache.commons.jcs.auxiliary.AuxiliaryCache;
+import org.apache.commons.jcs.auxiliary.AuxiliaryCacheConfigurator;
+import org.apache.commons.jcs.auxiliary.MockAuxiliaryCache;
+import org.apache.commons.jcs.auxiliary.MockAuxiliaryCacheAttributes;
+import org.apache.commons.jcs.auxiliary.MockAuxiliaryCacheFactory;
+import org.apache.commons.jcs.engine.logging.MockCacheEventLogger;
+
+/** Unit tests for the configurator. */
+public class CompositeCacheConfiguratorUnitTest
+ extends TestCase
+{
+ /**
+ * Verify that we can parse the event logger correctly
+ */
+ public void testParseAuxiliary_CacheEventLogger_Normal()
+ {
+ // SETUP
+ String regionName = "MyRegion";
+
+ String auxName = "MockAux";
+ String auxPrefix = "jcs.auxiliary." + auxName;
+ String auxiliaryClassName = MockAuxiliaryCacheFactory.class.getName();
+ String eventLoggerClassName = MockCacheEventLogger.class.getName();
+ String auxiliaryAttributeClassName = MockAuxiliaryCacheAttributes.class.getName();
+
+ Properties props = new Properties();
+ props.put( auxPrefix, auxiliaryClassName );
+ props.put( auxPrefix + CompositeCacheConfigurator.ATTRIBUTE_PREFIX, auxiliaryAttributeClassName );
+ props.put( auxPrefix + AuxiliaryCacheConfigurator.CACHE_EVENT_LOGGER_PREFIX, eventLoggerClassName );
+
+ System.out.print( props );
+
+ CompositeCacheManager manager = CompositeCacheManager.getUnconfiguredInstance();
+
+ CompositeCacheConfigurator configurator = new CompositeCacheConfigurator( manager );
+
+ // DO WORK
+ AuxiliaryCache<String, String> aux = configurator.parseAuxiliary( props, auxName, regionName );
+ MockAuxiliaryCache<String, String> result = (MockAuxiliaryCache<String, String>)aux;
+
+ // VERIFY
+ assertNotNull( "Should have an auxcache.", result );
+ assertNotNull( "Should have an event logger.", result.getCacheEventLogger() );
+ }
+
+ /**
+ * Verify that we can parse the spool chunk size
+ */
+ public void testParseSpoolChunkSize_Normal()
+ {
+ // SETUP
+ String regionName = "MyRegion";
+ int chunkSize = 5;
+
+ Properties props = new Properties();
+ props.put( "jcs.default", "" );
+ props.put( "jcs.default.cacheattributes.SpoolChunkSize", String.valueOf( chunkSize ) );
+
+ CompositeCacheManager manager = CompositeCacheManager.getUnconfiguredInstance();
+
+ // DO WORK
+ manager.configure( props );
+
+ // VERIFY
+ CompositeCache<String, String> cache = manager.getCache( regionName );
+ assertEquals( "Wrong chunkSize", cache.getCacheAttributes().getSpoolChunkSize(), chunkSize );
+ }
+}
Modified: commons/proper/jcs/trunk/src/test/org/apache/commons/jcs/engine/control/CompositeCacheDiskUsageUnitTest.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/src/test/org/apache/commons/jcs/engine/control/CompositeCacheDiskUsageUnitTest.java?rev=1570993&r1=1570992&r2=1570993&view=diff
==============================================================================
--- commons/proper/jcs/trunk/src/test/org/apache/commons/jcs/engine/control/CompositeCacheDiskUsageUnitTest.java (original)
+++ commons/proper/jcs/trunk/src/test/org/apache/commons/jcs/engine/control/CompositeCacheDiskUsageUnitTest.java Sun Feb 23 10:37:48 2014
@@ -1,491 +1,491 @@
-package org.apache.commons.jcs.engine.control;
-
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-import java.io.IOException;
-import java.io.Serializable;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Set;
-
-import junit.framework.TestCase;
-
-import org.apache.commons.jcs.JCS;
-import org.apache.commons.jcs.access.CacheAccess;
-import org.apache.commons.jcs.access.exception.CacheException;
-import org.apache.commons.jcs.auxiliary.AbstractAuxiliaryCache;
-import org.apache.commons.jcs.auxiliary.AuxiliaryCache;
-import org.apache.commons.jcs.auxiliary.AuxiliaryCacheAttributes;
-import org.apache.commons.jcs.engine.CacheElement;
-import org.apache.commons.jcs.engine.CacheStatus;
-import org.apache.commons.jcs.engine.CompositeCacheAttributes;
-import org.apache.commons.jcs.engine.ElementAttributes;
-import org.apache.commons.jcs.engine.behavior.ICacheElement;
-import org.apache.commons.jcs.engine.behavior.ICacheType.CacheType;
-import org.apache.commons.jcs.engine.behavior.ICompositeCacheAttributes;
-import org.apache.commons.jcs.engine.behavior.IElementAttributes;
-import org.apache.commons.jcs.engine.behavior.IElementSerializer;
-import org.apache.commons.jcs.engine.logging.behavior.ICacheEventLogger;
-import org.apache.commons.jcs.engine.stats.behavior.IStats;
-
-/**
- * Tests of the disk usage settings for the CompositeCache.
- * <p>
- * @author Aaron Smuts
- */
-public class CompositeCacheDiskUsageUnitTest
- extends TestCase
-{
- /**
- * Test setup
- */
- @Override
- public void setUp()
- {
- JCS.setConfigFilename( "/TestDiskCacheUsagePattern.ccf" );
- }
-
- /**
- * Verify that the swap region is set to the correct pattern.
- * <p>
- * @throws CacheException
- */
- public void testSwapConfig()
- throws CacheException
- {
- CacheAccess<String, String> swap = JCS.getInstance( "Swap" );
- assertEquals( ICompositeCacheAttributes.DiskUsagePattern.SWAP, swap.getCacheAttributes()
- .getDiskUsagePattern() );
- }
-
- /**
- * Verify that the swap region is set to the correct pattern.
- * <p>
- * @throws CacheException
- */
- public void testUpdateConfig()
- throws CacheException
- {
- CacheAccess<String, String> swap = JCS.getInstance( "Update" );
- assertEquals( ICompositeCacheAttributes.DiskUsagePattern.UPDATE, swap.getCacheAttributes()
- .getDiskUsagePattern() );
- }
-
- /**
- * 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.setDiskUsagePattern( ICompositeCacheAttributes.DiskUsagePattern.SWAP );
-
- IElementAttributes attr = new ElementAttributes();
-
- CompositeCache<String, String> cache = new CompositeCache<String, String>( "testSpoolAllowed", 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>( "testSpoolAllowed", "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 not swap. Call spool. Verify that the
- * item is not put to disk.
- */
- public void testSpoolNotAllowed()
- {
- // SETUP
- ICompositeCacheAttributes cattr = new CompositeCacheAttributes();
- cattr.setDiskUsagePattern( ICompositeCacheAttributes.DiskUsagePattern.UPDATE );
-
- IElementAttributes attr = new ElementAttributes();
-
- CompositeCache<String, String> cache = new CompositeCache<String, String>( "testSpoolAllowed", 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>( "testSpoolAllowed", "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 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.setDiskUsagePattern( ICompositeCacheAttributes.DiskUsagePattern.UPDATE );
-
- IElementAttributes attr = new ElementAttributes();
-
- CompositeCache<String, String> cache = new CompositeCache<String, String>( "testSpoolAllowed", 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>( "testSpoolAllowed", "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 );
- }
-
- /**
- * Setup a disk cache. Configure the disk usage pattern to UPDATE. Call updateAuxiliaries with
- * local only set to false. 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. The local setting should have no impact on whether the item goes to disk.
- * <p>
- * @throws IOException
- */
- public void testUpdateAllowed_localFalse()
- throws IOException
- {
- // SETUP
- ICompositeCacheAttributes cattr = new CompositeCacheAttributes();
- cattr.setDiskUsagePattern( ICompositeCacheAttributes.DiskUsagePattern.UPDATE );
-
- IElementAttributes attr = new ElementAttributes();
-
- CompositeCache<String, String> cache = new CompositeCache<String, String>( "testSpoolAllowed", 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>( "testSpoolAllowed", "key", "value" );
-
- // DO WORK
- cache.updateAuxiliaries( inputElement, false );
-
- // 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 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.setDiskUsagePattern( ICompositeCacheAttributes.DiskUsagePattern.SWAP );
-
- IElementAttributes attr = new ElementAttributes();
-
- CompositeCache<String, String> cache = new CompositeCache<String, String>( "testSpoolAllowed", 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>( "testSpoolAllowed", "key", "value" );
-
- // DO WORK
- cache.updateAuxiliaries( inputElement, true );
-
- // VERIFY
- assertEquals( "Wrong number of calls to the disk cache update.", 0, mock.updateCount );
- }
-
- /**
- * 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_withOtherCaches()
- throws IOException
- {
- // SETUP
- ICompositeCacheAttributes cattr = new CompositeCacheAttributes();
- cattr.setDiskUsagePattern( ICompositeCacheAttributes.DiskUsagePattern.UPDATE );
-
- IElementAttributes attr = new ElementAttributes();
-
- CompositeCache<String, String> cache = new CompositeCache<String, String>( "testSpoolAllowed", cattr, attr );
-
- MockAuxCache<String, String> mock = new MockAuxCache<String, String>();
- mock.cacheType = CacheType.DISK_CACHE;
-
- MockAuxCache<String, String> mockLateral = new MockAuxCache<String, String>();
- mockLateral.cacheType = CacheType.LATERAL_CACHE;
-
- @SuppressWarnings("unchecked")
- AuxiliaryCache<String, String>[] auxArray = new AuxiliaryCache[] { mock, mockLateral };
- cache.setAuxCaches( auxArray );
-
- ICacheElement<String, String> inputElement = new CacheElement<String, String>( "testSpoolAllowed", "key", "value" );
-
- // DO WORK
- cache.updateAuxiliaries( inputElement, false );
-
- // VERIFY
- assertEquals( "Wrong number of calls to the disk cache update.", 1, mock.updateCount );
- assertEquals( "Wrong element updated.", inputElement, mock.lastUpdatedItem );
-
- assertEquals( "Wrong number of calls to the lateral cache update.", 1, mockLateral.updateCount );
- assertEquals( "Wrong element updated with lateral.", inputElement, mockLateral.lastUpdatedItem );
- }
-
- /**
- * Used to test the disk cache functionality.
- * <p>
- * @author Aaron Smuts
- */
- public static class MockAuxCache<K extends Serializable, V extends Serializable>
- extends AbstractAuxiliaryCache<K, V>
- {
- /** Don't change */
- private static final long serialVersionUID = 1L;
-
- /** The last item passed to update. */
- public ICacheElement<K, V> lastUpdatedItem;
-
- /** The number of times update was called. */
- public int updateCount = 0;
-
- /** The type that should be returned from getCacheType. */
- public CacheType cacheType = CacheType.DISK_CACHE;
-
- /** Resets counters and catchers. */
- public void reset()
- {
- updateCount = 0;
- lastUpdatedItem = null;
- }
-
- /**
- * @param ce
- * @throws IOException
- */
- public void update( ICacheElement<K, V> ce )
- throws IOException
- {
- lastUpdatedItem = ce;
- updateCount++;
- }
-
- /**
- * @param key
- * @return ICacheElement
- * @throws IOException
- */
- public ICacheElement<K, V> get( K key )
- throws IOException
- {
- return null;
- }
-
- /**
- * Gets multiple items from the cache based on the given set of keys.
- * <p>
- * @param keys
- * @return a map of K key to ICacheElement<K, V> element, or an empty map if there is
- * no data in cache for any of these keys
- */
- public Map<K, ICacheElement<K, V>> getMultiple(Set<K> keys)
- {
- return new HashMap<K, ICacheElement<K, V>>();
- }
-
- /**
- * @param key
- * @return false
- * @throws IOException
- */
- public boolean remove( K key )
- throws IOException
- {
- return false;
- }
-
- /** @throws IOException */
- public void removeAll()
- throws IOException
- {
- // noop
- }
-
- /** @throws IOException */
- public void dispose()
- throws IOException
- {
- // noop
- }
-
- /** @return 0 */
- public int getSize()
- {
- return 0;
- }
-
- /** @return 0 */
- public CacheStatus getStatus()
- {
- return CacheStatus.ALIVE;
- }
-
- /** @return null */
- public String getCacheName()
- {
- return null;
- }
-
- /**
- * @return null
- * @throws IOException
- */
- public Set<K> getKeySet( )
- throws IOException
- {
- return null;
- }
-
- /** @return null */
- public IStats getStatistics()
- {
- return null;
- }
-
- /** @return null */
- public String getStats()
- {
- return null;
- }
-
- /**
- * Returns the setup cache type. This allows you to use this mock as multiple cache types.
- * <p>
- * @see org.apache.commons.jcs.engine.behavior.ICacheType#getCacheType()
- * @return cacheType
- */
- public CacheType getCacheType()
- {
- return cacheType;
- }
-
- /**
- * @return Returns the AuxiliaryCacheAttributes.
- */
- public AuxiliaryCacheAttributes getAuxiliaryCacheAttributes()
- {
- return null;
- }
-
- /**
- * @param cacheEventLogger
- */
- @Override
- public void setCacheEventLogger( ICacheEventLogger cacheEventLogger )
- {
- // TODO Auto-generated method stub
-
- }
-
- /**
- * @param elementSerializer
- */
- @Override
- public void setElementSerializer( IElementSerializer elementSerializer )
- {
- // TODO Auto-generated method stub
-
- }
-
- /** @return null */
- @Override
- public String getEventLoggingExtraInfo()
- {
- // TODO Auto-generated method stub
- return null;
- }
-
- /**
- * @param pattern
- * @return Collections.EMPTY_MAP;
- * @throws IOException
- */
- public Map<K, ICacheElement<K, V>> getMatching(String pattern)
- throws IOException
- {
- return Collections.emptyMap();
- }
-
-
- }
-
-}
+package org.apache.commons.jcs.engine.control;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.io.IOException;
+import java.io.Serializable;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+
+import junit.framework.TestCase;
+
+import org.apache.commons.jcs.JCS;
+import org.apache.commons.jcs.access.CacheAccess;
+import org.apache.commons.jcs.access.exception.CacheException;
+import org.apache.commons.jcs.auxiliary.AbstractAuxiliaryCache;
+import org.apache.commons.jcs.auxiliary.AuxiliaryCache;
+import org.apache.commons.jcs.auxiliary.AuxiliaryCacheAttributes;
+import org.apache.commons.jcs.engine.CacheElement;
+import org.apache.commons.jcs.engine.CacheStatus;
+import org.apache.commons.jcs.engine.CompositeCacheAttributes;
+import org.apache.commons.jcs.engine.ElementAttributes;
+import org.apache.commons.jcs.engine.behavior.ICacheElement;
+import org.apache.commons.jcs.engine.behavior.ICacheType.CacheType;
+import org.apache.commons.jcs.engine.behavior.ICompositeCacheAttributes;
+import org.apache.commons.jcs.engine.behavior.IElementAttributes;
+import org.apache.commons.jcs.engine.behavior.IElementSerializer;
+import org.apache.commons.jcs.engine.logging.behavior.ICacheEventLogger;
+import org.apache.commons.jcs.engine.stats.behavior.IStats;
+
+/**
+ * Tests of the disk usage settings for the CompositeCache.
+ * <p>
+ * @author Aaron Smuts
+ */
+public class CompositeCacheDiskUsageUnitTest
+ extends TestCase
+{
+ /**
+ * Test setup
+ */
+ @Override
+ public void setUp()
+ {
+ JCS.setConfigFilename( "/TestDiskCacheUsagePattern.ccf" );
+ }
+
+ /**
+ * Verify that the swap region is set to the correct pattern.
+ * <p>
+ * @throws CacheException
+ */
+ public void testSwapConfig()
+ throws CacheException
+ {
+ CacheAccess<String, String> swap = JCS.getInstance( "Swap" );
+ assertEquals( ICompositeCacheAttributes.DiskUsagePattern.SWAP, swap.getCacheAttributes()
+ .getDiskUsagePattern() );
+ }
+
+ /**
+ * Verify that the swap region is set to the correct pattern.
+ * <p>
+ * @throws CacheException
+ */
+ public void testUpdateConfig()
+ throws CacheException
+ {
+ CacheAccess<String, String> swap = JCS.getInstance( "Update" );
+ assertEquals( ICompositeCacheAttributes.DiskUsagePattern.UPDATE, swap.getCacheAttributes()
+ .getDiskUsagePattern() );
+ }
+
+ /**
+ * 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.setDiskUsagePattern( ICompositeCacheAttributes.DiskUsagePattern.SWAP );
+
+ IElementAttributes attr = new ElementAttributes();
+
+ CompositeCache<String, String> cache = new CompositeCache<String, String>( "testSpoolAllowed", 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>( "testSpoolAllowed", "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 not swap. Call spool. Verify that the
+ * item is not put to disk.
+ */
+ public void testSpoolNotAllowed()
+ {
+ // SETUP
+ ICompositeCacheAttributes cattr = new CompositeCacheAttributes();
+ cattr.setDiskUsagePattern( ICompositeCacheAttributes.DiskUsagePattern.UPDATE );
+
+ IElementAttributes attr = new ElementAttributes();
+
+ CompositeCache<String, String> cache = new CompositeCache<String, String>( "testSpoolAllowed", 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>( "testSpoolAllowed", "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 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.setDiskUsagePattern( ICompositeCacheAttributes.DiskUsagePattern.UPDATE );
+
+ IElementAttributes attr = new ElementAttributes();
+
+ CompositeCache<String, String> cache = new CompositeCache<String, String>( "testSpoolAllowed", 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>( "testSpoolAllowed", "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 );
+ }
+
+ /**
+ * Setup a disk cache. Configure the disk usage pattern to UPDATE. Call updateAuxiliaries with
+ * local only set to false. 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. The local setting should have no impact on whether the item goes to disk.
+ * <p>
+ * @throws IOException
+ */
+ public void testUpdateAllowed_localFalse()
+ throws IOException
+ {
+ // SETUP
+ ICompositeCacheAttributes cattr = new CompositeCacheAttributes();
+ cattr.setDiskUsagePattern( ICompositeCacheAttributes.DiskUsagePattern.UPDATE );
+
+ IElementAttributes attr = new ElementAttributes();
+
+ CompositeCache<String, String> cache = new CompositeCache<String, String>( "testSpoolAllowed", 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>( "testSpoolAllowed", "key", "value" );
+
+ // DO WORK
+ cache.updateAuxiliaries( inputElement, false );
+
+ // 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 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.setDiskUsagePattern( ICompositeCacheAttributes.DiskUsagePattern.SWAP );
+
+ IElementAttributes attr = new ElementAttributes();
+
+ CompositeCache<String, String> cache = new CompositeCache<String, String>( "testSpoolAllowed", 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>( "testSpoolAllowed", "key", "value" );
+
+ // DO WORK
+ cache.updateAuxiliaries( inputElement, true );
+
+ // VERIFY
+ assertEquals( "Wrong number of calls to the disk cache update.", 0, mock.updateCount );
+ }
+
+ /**
+ * 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_withOtherCaches()
+ throws IOException
+ {
+ // SETUP
+ ICompositeCacheAttributes cattr = new CompositeCacheAttributes();
+ cattr.setDiskUsagePattern( ICompositeCacheAttributes.DiskUsagePattern.UPDATE );
+
+ IElementAttributes attr = new ElementAttributes();
+
+ CompositeCache<String, String> cache = new CompositeCache<String, String>( "testSpoolAllowed", cattr, attr );
+
+ MockAuxCache<String, String> mock = new MockAuxCache<String, String>();
+ mock.cacheType = CacheType.DISK_CACHE;
+
+ MockAuxCache<String, String> mockLateral = new MockAuxCache<String, String>();
+ mockLateral.cacheType = CacheType.LATERAL_CACHE;
+
+ @SuppressWarnings("unchecked")
+ AuxiliaryCache<String, String>[] auxArray = new AuxiliaryCache[] { mock, mockLateral };
+ cache.setAuxCaches( auxArray );
+
+ ICacheElement<String, String> inputElement = new CacheElement<String, String>( "testSpoolAllowed", "key", "value" );
+
+ // DO WORK
+ cache.updateAuxiliaries( inputElement, false );
+
+ // VERIFY
+ assertEquals( "Wrong number of calls to the disk cache update.", 1, mock.updateCount );
+ assertEquals( "Wrong element updated.", inputElement, mock.lastUpdatedItem );
+
+ assertEquals( "Wrong number of calls to the lateral cache update.", 1, mockLateral.updateCount );
+ assertEquals( "Wrong element updated with lateral.", inputElement, mockLateral.lastUpdatedItem );
+ }
+
+ /**
+ * Used to test the disk cache functionality.
+ * <p>
+ * @author Aaron Smuts
+ */
+ public static class MockAuxCache<K extends Serializable, V extends Serializable>
+ extends AbstractAuxiliaryCache<K, V>
+ {
+ /** Don't change */
+ private static final long serialVersionUID = 1L;
+
+ /** The last item passed to update. */
+ public ICacheElement<K, V> lastUpdatedItem;
+
+ /** The number of times update was called. */
+ public int updateCount = 0;
+
+ /** The type that should be returned from getCacheType. */
+ public CacheType cacheType = CacheType.DISK_CACHE;
+
+ /** Resets counters and catchers. */
+ public void reset()
+ {
+ updateCount = 0;
+ lastUpdatedItem = null;
+ }
+
+ /**
+ * @param ce
+ * @throws IOException
+ */
+ public void update( ICacheElement<K, V> ce )
+ throws IOException
+ {
+ lastUpdatedItem = ce;
+ updateCount++;
+ }
+
+ /**
+ * @param key
+ * @return ICacheElement
+ * @throws IOException
+ */
+ public ICacheElement<K, V> get( K key )
+ throws IOException
+ {
+ return null;
+ }
+
+ /**
+ * Gets multiple items from the cache based on the given set of keys.
+ * <p>
+ * @param keys
+ * @return a map of K key to ICacheElement<K, V> element, or an empty map if there is
+ * no data in cache for any of these keys
+ */
+ public Map<K, ICacheElement<K, V>> getMultiple(Set<K> keys)
+ {
+ return new HashMap<K, ICacheElement<K, V>>();
+ }
+
+ /**
+ * @param key
+ * @return false
+ * @throws IOException
+ */
+ public boolean remove( K key )
+ throws IOException
+ {
+ return false;
+ }
+
+ /** @throws IOException */
+ public void removeAll()
+ throws IOException
+ {
+ // noop
+ }
+
+ /** @throws IOException */
+ public void dispose()
+ throws IOException
+ {
+ // noop
+ }
+
+ /** @return 0 */
+ public int getSize()
+ {
+ return 0;
+ }
+
+ /** @return 0 */
+ public CacheStatus getStatus()
+ {
+ return CacheStatus.ALIVE;
+ }
+
+ /** @return null */
+ public String getCacheName()
+ {
+ return null;
+ }
+
+ /**
+ * @return null
+ * @throws IOException
+ */
+ public Set<K> getKeySet( )
+ throws IOException
+ {
+ return null;
+ }
+
+ /** @return null */
+ public IStats getStatistics()
+ {
+ return null;
+ }
+
+ /** @return null */
+ public String getStats()
+ {
+ return null;
+ }
+
+ /**
+ * Returns the setup cache type. This allows you to use this mock as multiple cache types.
+ * <p>
+ * @see org.apache.commons.jcs.engine.behavior.ICacheType#getCacheType()
+ * @return cacheType
+ */
+ public CacheType getCacheType()
+ {
+ return cacheType;
+ }
+
+ /**
+ * @return Returns the AuxiliaryCacheAttributes.
+ */
+ public AuxiliaryCacheAttributes getAuxiliaryCacheAttributes()
+ {
+ return null;
+ }
+
+ /**
+ * @param cacheEventLogger
+ */
+ @Override
+ public void setCacheEventLogger( ICacheEventLogger cacheEventLogger )
+ {
+ // TODO Auto-generated method stub
+
+ }
+
+ /**
+ * @param elementSerializer
+ */
+ @Override
+ public void setElementSerializer( IElementSerializer elementSerializer )
+ {
+ // TODO Auto-generated method stub
+
+ }
+
+ /** @return null */
+ @Override
+ public String getEventLoggingExtraInfo()
+ {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ /**
+ * @param pattern
+ * @return Collections.EMPTY_MAP;
+ * @throws IOException
+ */
+ public Map<K, ICacheElement<K, V>> getMatching(String pattern)
+ throws IOException
+ {
+ return Collections.emptyMap();
+ }
+
+
+ }
+
+}
Modified: commons/proper/jcs/trunk/src/test/org/apache/commons/jcs/engine/control/MockElementSerializer.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/src/test/org/apache/commons/jcs/engine/control/MockElementSerializer.java?rev=1570993&r1=1570992&r2=1570993&view=diff
==============================================================================
--- commons/proper/jcs/trunk/src/test/org/apache/commons/jcs/engine/control/MockElementSerializer.java (original)
+++ commons/proper/jcs/trunk/src/test/org/apache/commons/jcs/engine/control/MockElementSerializer.java Sun Feb 23 10:37:48 2014
@@ -1,67 +1,86 @@
-package org.apache.commons.jcs.engine.control;
-
-import java.io.IOException;
-import java.io.Serializable;
-
-import org.apache.commons.jcs.engine.behavior.IElementSerializer;
-import org.apache.commons.jcs.utils.serialization.StandardSerializer;
-
-/** For mocking. */
-public class MockElementSerializer
- implements IElementSerializer
-{
- /** test property */
- private String testProperty;
-
- /** What's used in the background */
- private final StandardSerializer serializer = new StandardSerializer();
-
- /** times out was called */
- public int deSerializeCount = 0;
-
- /** times in was called */
- public int serializeCount = 0;
-
- /**
- * @param bytes
- * @return Object
- * @throws IOException
- * @throws ClassNotFoundException
- *
- */
- public <T extends Serializable> T deSerialize( byte[] bytes )
- throws IOException, ClassNotFoundException
- {
- deSerializeCount++;
- return serializer.deSerialize( bytes );
- }
-
- /**
- * @param obj
- * @return byte[]
- * @throws IOException
- *
- */
- public <T extends Serializable> byte[] serialize( T obj )
- throws IOException
- {
- serializeCount++;
- return serializer.serialize( obj );
- }
-
- /**
- * @param testProperty
- */
- public void setTestProperty( String testProperty )
- {
- this.testProperty = testProperty;
- }
-
- /**
- * @return testProperty
- */
- public String getTestProperty()
- {
- return testProperty;
- }
-}
+package org.apache.commons.jcs.engine.control;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.io.IOException;
+import java.io.Serializable;
+
+import org.apache.commons.jcs.engine.behavior.IElementSerializer;
+import org.apache.commons.jcs.utils.serialization.StandardSerializer;
+
+/** For mocking. */
+public class MockElementSerializer
+ implements IElementSerializer
+{
+ /** test property */
+ private String testProperty;
+
+ /** What's used in the background */
+ private final StandardSerializer serializer = new StandardSerializer();
+
+ /** times out was called */
+ public int deSerializeCount = 0;
+
+ /** times in was called */
+ public int serializeCount = 0;
+
+ /**
+ * @param bytes
+ * @return Object
+ * @throws IOException
+ * @throws ClassNotFoundException
+ *
+ */
+ public <T extends Serializable> T deSerialize( byte[] bytes )
+ throws IOException, ClassNotFoundException
+ {
+ deSerializeCount++;
+ return serializer.deSerialize( bytes );
+ }
+
+ /**
+ * @param obj
+ * @return byte[]
+ * @throws IOException
+ *
+ */
+ public <T extends Serializable> byte[] serialize( T obj )
+ throws IOException
+ {
+ serializeCount++;
+ return serializer.serialize( obj );
+ }
+
+ /**
+ * @param testProperty
+ */
+ public void setTestProperty( String testProperty )
+ {
+ this.testProperty = testProperty;
+ }
+
+ /**
+ * @return testProperty
+ */
+ public String getTestProperty()
+ {
+ return testProperty;
+ }
+}
Modified: commons/proper/jcs/trunk/src/test/org/apache/commons/jcs/engine/logging/CacheEventLoggerDebugLoggerUnitTest.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/src/test/org/apache/commons/jcs/engine/logging/CacheEventLoggerDebugLoggerUnitTest.java?rev=1570993&r1=1570992&r2=1570993&view=diff
==============================================================================
--- commons/proper/jcs/trunk/src/test/org/apache/commons/jcs/engine/logging/CacheEventLoggerDebugLoggerUnitTest.java (original)
+++ commons/proper/jcs/trunk/src/test/org/apache/commons/jcs/engine/logging/CacheEventLoggerDebugLoggerUnitTest.java Sun Feb 23 10:37:48 2014
@@ -1,125 +1,144 @@
-package org.apache.commons.jcs.engine.logging;
-
-import java.io.StringWriter;
-
-import junit.framework.TestCase;
-
-import org.apache.commons.jcs.engine.logging.behavior.ICacheEvent;
-import org.apache.log4j.Level;
-import org.apache.log4j.Logger;
-import org.apache.log4j.PatternLayout;
-import org.apache.log4j.WriterAppender;
-
-/** Unit tests for the debug implementation */
-public class CacheEventLoggerDebugLoggerUnitTest
- extends TestCase
-{
-
- /** verify that we can log */
- public void testLogICacheEvent_normal()
- {
- // SETUP
- String logCategoryName = "testLogEvent_normal";
-
- String source = "mySource";
- String region = "my region";
- String eventName = "MyEventName";
- String optionalDetails = "SomeExtraData";
- String key = "my key";
-
- StringWriter stringWriter = new StringWriter();
- configureLogger( stringWriter, logCategoryName );
-
- CacheEventLoggerDebugLogger logger = new CacheEventLoggerDebugLogger();
- logger.setLogCategoryName( logCategoryName );
-
- ICacheEvent<String> event = logger.createICacheEvent( source, region, eventName, optionalDetails, key );
-
- // DO WORK
- logger.logICacheEvent( event );
-
- // VERIFY
- String result = stringWriter.toString();
- assertTrue( "An event with the source should have been logged:" + result, result.indexOf( source ) != -1 );
- assertTrue( "An event with the region should have been logged:" + result, result.indexOf( region ) != -1 );
- assertTrue( "An event with the event name should have been logged:" + result, result.indexOf( eventName ) != -1 );
- assertTrue( "An event with the optionalDetails should have been logged:" + result, result.indexOf( optionalDetails ) != -1 );
- assertTrue( "An event with the key should have been logged:" + result, result.indexOf( key ) != -1 );
- }
-
- /** verify that we can log */
- public void testLogApplicationEvent_normal()
- {
- // SETUP
- String logCategoryName = "testLogApplicationEvent_normal";
-
- String source = "mySource";
- String eventName = "MyEventName";
- String optionalDetails = "SomeExtraData";
-
- StringWriter stringWriter = new StringWriter();
- configureLogger( stringWriter, logCategoryName );
-
- CacheEventLoggerDebugLogger logger = new CacheEventLoggerDebugLogger();
- logger.setLogCategoryName( logCategoryName );
-
- // DO WORK
- logger.logApplicationEvent( source, eventName, optionalDetails );
-
- // VERIFY
- String result = stringWriter.toString();
- assertTrue( "An event with the source should have been logged:" + result, result.indexOf( source ) != -1 );
- assertTrue( "An event with the event name should have been logged:" + result, result.indexOf( eventName ) != -1 );
- assertTrue( "An event with the optionalDetails should have been logged:" + result, result.indexOf( optionalDetails ) != -1 );
- }
-
- /** verify that we can log */
- public void testLogError_normal()
- {
- // SETUP
- String logCategoryName = "testLogApplicationEvent_normal";
-
- String source = "mySource";
- String eventName = "MyEventName";
- String errorMessage = "SomeExtraData";
-
- StringWriter stringWriter = new StringWriter();
- configureLogger( stringWriter, logCategoryName );
-
- CacheEventLoggerDebugLogger logger = new CacheEventLoggerDebugLogger();
- logger.setLogCategoryName( logCategoryName );
-
- // DO WORK
- logger.logError( source, eventName, errorMessage );
-
- // VERIFY
- String result = stringWriter.toString();
- assertTrue( "An event with the source should have been logged:" + result, result.indexOf( source ) != -1 );
- assertTrue( "An event with the event name should have been logged:" + result, result.indexOf( eventName ) != -1 );
- assertTrue( "An event with the errorMessage should have been logged:" + result, result.indexOf( errorMessage ) != -1 );
- }
-
- /**
- * Configures a logger for the given name. This allows us to check the log output.
- * <p>
- * @param stringWriter
- * @param loggerName
- */
- private void configureLogger( StringWriter stringWriter, String loggerName )
- {
- Logger logger = Logger.getLogger( loggerName );
- WriterAppender appender = null;
-
- try
- {
- appender = new WriterAppender( new PatternLayout(), stringWriter );
- }
- catch ( Exception e )
- {
- // NOOP
- }
-
- logger.addAppender( appender );
- logger.setLevel( Level.DEBUG );
- }
-}
+package org.apache.commons.jcs.engine.logging;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.io.StringWriter;
+
+import junit.framework.TestCase;
+
+import org.apache.commons.jcs.engine.logging.behavior.ICacheEvent;
+import org.apache.log4j.Level;
+import org.apache.log4j.Logger;
+import org.apache.log4j.PatternLayout;
+import org.apache.log4j.WriterAppender;
+
+/** Unit tests for the debug implementation */
+public class CacheEventLoggerDebugLoggerUnitTest
+ extends TestCase
+{
+
+ /** verify that we can log */
+ public void testLogICacheEvent_normal()
+ {
+ // SETUP
+ String logCategoryName = "testLogEvent_normal";
+
+ String source = "mySource";
+ String region = "my region";
+ String eventName = "MyEventName";
+ String optionalDetails = "SomeExtraData";
+ String key = "my key";
+
+ StringWriter stringWriter = new StringWriter();
+ configureLogger( stringWriter, logCategoryName );
+
+ CacheEventLoggerDebugLogger logger = new CacheEventLoggerDebugLogger();
+ logger.setLogCategoryName( logCategoryName );
+
+ ICacheEvent<String> event = logger.createICacheEvent( source, region, eventName, optionalDetails, key );
+
+ // DO WORK
+ logger.logICacheEvent( event );
+
+ // VERIFY
+ String result = stringWriter.toString();
+ assertTrue( "An event with the source should have been logged:" + result, result.indexOf( source ) != -1 );
+ assertTrue( "An event with the region should have been logged:" + result, result.indexOf( region ) != -1 );
+ assertTrue( "An event with the event name should have been logged:" + result, result.indexOf( eventName ) != -1 );
+ assertTrue( "An event with the optionalDetails should have been logged:" + result, result.indexOf( optionalDetails ) != -1 );
+ assertTrue( "An event with the key should have been logged:" + result, result.indexOf( key ) != -1 );
+ }
+
+ /** verify that we can log */
+ public void testLogApplicationEvent_normal()
+ {
+ // SETUP
+ String logCategoryName = "testLogApplicationEvent_normal";
+
+ String source = "mySource";
+ String eventName = "MyEventName";
+ String optionalDetails = "SomeExtraData";
+
+ StringWriter stringWriter = new StringWriter();
+ configureLogger( stringWriter, logCategoryName );
+
+ CacheEventLoggerDebugLogger logger = new CacheEventLoggerDebugLogger();
+ logger.setLogCategoryName( logCategoryName );
+
+ // DO WORK
+ logger.logApplicationEvent( source, eventName, optionalDetails );
+
+ // VERIFY
+ String result = stringWriter.toString();
+ assertTrue( "An event with the source should have been logged:" + result, result.indexOf( source ) != -1 );
+ assertTrue( "An event with the event name should have been logged:" + result, result.indexOf( eventName ) != -1 );
+ assertTrue( "An event with the optionalDetails should have been logged:" + result, result.indexOf( optionalDetails ) != -1 );
+ }
+
+ /** verify that we can log */
+ public void testLogError_normal()
+ {
+ // SETUP
+ String logCategoryName = "testLogApplicationEvent_normal";
+
+ String source = "mySource";
+ String eventName = "MyEventName";
+ String errorMessage = "SomeExtraData";
+
+ StringWriter stringWriter = new StringWriter();
+ configureLogger( stringWriter, logCategoryName );
+
+ CacheEventLoggerDebugLogger logger = new CacheEventLoggerDebugLogger();
+ logger.setLogCategoryName( logCategoryName );
+
+ // DO WORK
+ logger.logError( source, eventName, errorMessage );
+
+ // VERIFY
+ String result = stringWriter.toString();
+ assertTrue( "An event with the source should have been logged:" + result, result.indexOf( source ) != -1 );
+ assertTrue( "An event with the event name should have been logged:" + result, result.indexOf( eventName ) != -1 );
+ assertTrue( "An event with the errorMessage should have been logged:" + result, result.indexOf( errorMessage ) != -1 );
+ }
+
+ /**
+ * Configures a logger for the given name. This allows us to check the log output.
+ * <p>
+ * @param stringWriter
+ * @param loggerName
+ */
+ private void configureLogger( StringWriter stringWriter, String loggerName )
+ {
+ Logger logger = Logger.getLogger( loggerName );
+ WriterAppender appender = null;
+
+ try
+ {
+ appender = new WriterAppender( new PatternLayout(), stringWriter );
+ }
+ catch ( Exception e )
+ {
+ // NOOP
+ }
+
+ logger.addAppender( appender );
+ logger.setLevel( Level.DEBUG );
+ }
+}
Modified: commons/proper/jcs/trunk/src/test/org/apache/commons/jcs/engine/logging/MockCacheEventLogger.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/src/test/org/apache/commons/jcs/engine/logging/MockCacheEventLogger.java?rev=1570993&r1=1570992&r2=1570993&view=diff
==============================================================================
--- commons/proper/jcs/trunk/src/test/org/apache/commons/jcs/engine/logging/MockCacheEventLogger.java (original)
+++ commons/proper/jcs/trunk/src/test/org/apache/commons/jcs/engine/logging/MockCacheEventLogger.java Sun Feb 23 10:37:48 2014
@@ -1,74 +1,93 @@
-package org.apache.commons.jcs.engine.logging;
-
-import java.io.Serializable;
-
-import org.apache.commons.jcs.engine.logging.behavior.ICacheEvent;
-import org.apache.commons.jcs.engine.logging.behavior.ICacheEventLogger;
-
-/**
- * For testing the configurator.
- */
-public class MockCacheEventLogger
- implements ICacheEventLogger
-{
- /** test property */
- private String testProperty;
-
- /**
- * @param source
- * @param eventName
- * @param optionalDetails
- */
- public void logApplicationEvent( String source, String eventName, String optionalDetails )
- {
- // TODO Auto-generated method stub
- }
-
- /**
- * @param source
- * @param eventName
- * @param errorMessage
- */
- public void logError( String source, String eventName, String errorMessage )
- {
- // TODO Auto-generated method stub
- }
-
- /**
- * @param source
- * @param region
- * @param eventName
- * @param optionalDetails
- * @param key
- * @return ICacheEvent
- */
- public <T extends Serializable> ICacheEvent<T> createICacheEvent( String source, String region, String eventName, String optionalDetails,
- T key )
- {
- return new CacheEvent<T>();
- }
-
- /**
- * @param event
- */
- public <T extends Serializable> void logICacheEvent( ICacheEvent<T> event )
- {
- // TODO Auto-generated method stub
- }
-
- /**
- * @param testProperty
- */
- public void setTestProperty( String testProperty )
- {
- this.testProperty = testProperty;
- }
-
- /**
- * @return testProperty
- */
- public String getTestProperty()
- {
- return testProperty;
- }
-}
\ No newline at end of file
+package org.apache.commons.jcs.engine.logging;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.io.Serializable;
+
+import org.apache.commons.jcs.engine.logging.behavior.ICacheEvent;
+import org.apache.commons.jcs.engine.logging.behavior.ICacheEventLogger;
+
+/**
+ * For testing the configurator.
+ */
+public class MockCacheEventLogger
+ implements ICacheEventLogger
+{
+ /** test property */
+ private String testProperty;
+
+ /**
+ * @param source
+ * @param eventName
+ * @param optionalDetails
+ */
+ public void logApplicationEvent( String source, String eventName, String optionalDetails )
+ {
+ // TODO Auto-generated method stub
+ }
+
+ /**
+ * @param source
+ * @param eventName
+ * @param errorMessage
+ */
+ public void logError( String source, String eventName, String errorMessage )
+ {
+ // TODO Auto-generated method stub
+ }
+
+ /**
+ * @param source
+ * @param region
+ * @param eventName
+ * @param optionalDetails
+ * @param key
+ * @return ICacheEvent
+ */
+ public <T extends Serializable> ICacheEvent<T> createICacheEvent( String source, String region, String eventName, String optionalDetails,
+ T key )
+ {
+ return new CacheEvent<T>();
+ }
+
+ /**
+ * @param event
+ */
+ public <T extends Serializable> void logICacheEvent( ICacheEvent<T> event )
+ {
+ // TODO Auto-generated method stub
+ }
+
+ /**
+ * @param testProperty
+ */
+ public void setTestProperty( String testProperty )
+ {
+ this.testProperty = testProperty;
+ }
+
+ /**
+ * @return testProperty
+ */
+ public String getTestProperty()
+ {
+ return testProperty;
+ }
+}
Modified: commons/proper/jcs/trunk/src/test/org/apache/commons/jcs/engine/match/KeyMatcherPatternImpllUnitTest.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/src/test/org/apache/commons/jcs/engine/match/KeyMatcherPatternImpllUnitTest.java?rev=1570993&r1=1570992&r2=1570993&view=diff
==============================================================================
--- commons/proper/jcs/trunk/src/test/org/apache/commons/jcs/engine/match/KeyMatcherPatternImpllUnitTest.java (original)
+++ commons/proper/jcs/trunk/src/test/org/apache/commons/jcs/engine/match/KeyMatcherPatternImpllUnitTest.java Sun Feb 23 10:37:48 2014
@@ -1,99 +1,118 @@
-package org.apache.commons.jcs.engine.match;
-
-import java.util.HashSet;
-import java.util.Set;
-
-import junit.framework.TestCase;
-
-/** Unit tests for the key matcher. */
-public class KeyMatcherPatternImpllUnitTest
- extends TestCase
-{
- /**
- * Verify that the matching method works.
- */
- public void testGetMatchingKeysFromArray_AllMatch()
- {
- // SETUP
- int numToInsertPrefix1 = 10;
- Set<String> keyArray = new HashSet<String>();
-
- String keyprefix1 = "MyPrefixC";
-
- // insert with prefix1
- for ( int i = 0; i < numToInsertPrefix1; i++ )
- {
- keyArray.add(keyprefix1 + String.valueOf( i ));
- }
-
- KeyMatcherPatternImpl<String> keyMatcher = new KeyMatcherPatternImpl<String>();
-
- // DO WORK
- Set<String> result1 = keyMatcher.getMatchingKeysFromArray( keyprefix1 + ".", keyArray );
-
- // VERIFY
- assertEquals( "Wrong number returned 1: " + result1, numToInsertPrefix1, result1.size() );
- }
-
- /**
- * Verify that the matching method works.
- */
- public void testGetMatchingKeysFromArray_AllMatchFirstNull()
- {
- // SETUP
- int numToInsertPrefix1 = 10;
- Set<String> keyArray = new HashSet<String>();
-
- String keyprefix1 = "MyPrefixC";
-
- // insert with prefix1
- for ( int i = 1; i < numToInsertPrefix1 + 1; i++ )
- {
- keyArray.add(keyprefix1 + String.valueOf( i ));
- }
-
- KeyMatcherPatternImpl<String> keyMatcher = new KeyMatcherPatternImpl<String>();
-
- // DO WORK
- Set<String> result1 = keyMatcher.getMatchingKeysFromArray( keyprefix1 + "\\S+", keyArray );
-
- // VERIFY
- assertEquals( "Wrong number returned 1: " + result1, numToInsertPrefix1, result1.size() );
- }
-
- /**
- * Verify that the matching method works.
- */
- public void testGetMatchingKeysFromArray_TwoTypes()
- {
- // SETUP
- int numToInsertPrefix1 = 10;
- int numToInsertPrefix2 = 50;
- Set<String> keyArray = new HashSet<String>();
-
- String keyprefix1 = "MyPrefixA";
- String keyprefix2 = "MyPrefixB";
-
- // insert with prefix1
- for ( int i = 0; i < numToInsertPrefix1; i++ )
- {
- keyArray.add(keyprefix1 + String.valueOf( i ));
- }
-
- // insert with prefix2
- for ( int i = numToInsertPrefix1; i < numToInsertPrefix2 + numToInsertPrefix1; i++ )
- {
- keyArray.add(keyprefix2 + String.valueOf( i ));
- }
-
- KeyMatcherPatternImpl<String> keyMatcher = new KeyMatcherPatternImpl<String>();
-
- // DO WORK
- Set<String> result1 = keyMatcher.getMatchingKeysFromArray( keyprefix1 + ".+", keyArray );
- Set<String> result2 = keyMatcher.getMatchingKeysFromArray( keyprefix2 + ".+", keyArray );
-
- // VERIFY
- assertEquals( "Wrong number returned 1: " + result1, numToInsertPrefix1, result1.size() );
- assertEquals( "Wrong number returned 2: " + result2, numToInsertPrefix2, result2.size() );
- }
-}
+package org.apache.commons.jcs.engine.match;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.util.HashSet;
+import java.util.Set;
+
+import junit.framework.TestCase;
+
+/** Unit tests for the key matcher. */
+public class KeyMatcherPatternImpllUnitTest
+ extends TestCase
+{
+ /**
+ * Verify that the matching method works.
+ */
+ public void testGetMatchingKeysFromArray_AllMatch()
+ {
+ // SETUP
+ int numToInsertPrefix1 = 10;
+ Set<String> keyArray = new HashSet<String>();
+
+ String keyprefix1 = "MyPrefixC";
+
+ // insert with prefix1
+ for ( int i = 0; i < numToInsertPrefix1; i++ )
+ {
+ keyArray.add(keyprefix1 + String.valueOf( i ));
+ }
+
+ KeyMatcherPatternImpl<String> keyMatcher = new KeyMatcherPatternImpl<String>();
+
+ // DO WORK
+ Set<String> result1 = keyMatcher.getMatchingKeysFromArray( keyprefix1 + ".", keyArray );
+
+ // VERIFY
+ assertEquals( "Wrong number returned 1: " + result1, numToInsertPrefix1, result1.size() );
+ }
+
+ /**
+ * Verify that the matching method works.
+ */
+ public void testGetMatchingKeysFromArray_AllMatchFirstNull()
+ {
+ // SETUP
+ int numToInsertPrefix1 = 10;
+ Set<String> keyArray = new HashSet<String>();
+
+ String keyprefix1 = "MyPrefixC";
+
+ // insert with prefix1
+ for ( int i = 1; i < numToInsertPrefix1 + 1; i++ )
+ {
+ keyArray.add(keyprefix1 + String.valueOf( i ));
+ }
+
+ KeyMatcherPatternImpl<String> keyMatcher = new KeyMatcherPatternImpl<String>();
+
+ // DO WORK
+ Set<String> result1 = keyMatcher.getMatchingKeysFromArray( keyprefix1 + "\\S+", keyArray );
+
+ // VERIFY
+ assertEquals( "Wrong number returned 1: " + result1, numToInsertPrefix1, result1.size() );
+ }
+
+ /**
+ * Verify that the matching method works.
+ */
+ public void testGetMatchingKeysFromArray_TwoTypes()
+ {
+ // SETUP
+ int numToInsertPrefix1 = 10;
+ int numToInsertPrefix2 = 50;
+ Set<String> keyArray = new HashSet<String>();
+
+ String keyprefix1 = "MyPrefixA";
+ String keyprefix2 = "MyPrefixB";
+
+ // insert with prefix1
+ for ( int i = 0; i < numToInsertPrefix1; i++ )
+ {
+ keyArray.add(keyprefix1 + String.valueOf( i ));
+ }
+
+ // insert with prefix2
+ for ( int i = numToInsertPrefix1; i < numToInsertPrefix2 + numToInsertPrefix1; i++ )
+ {
+ keyArray.add(keyprefix2 + String.valueOf( i ));
+ }
+
+ KeyMatcherPatternImpl<String> keyMatcher = new KeyMatcherPatternImpl<String>();
+
+ // DO WORK
+ Set<String> result1 = keyMatcher.getMatchingKeysFromArray( keyprefix1 + ".+", keyArray );
+ Set<String> result2 = keyMatcher.getMatchingKeysFromArray( keyprefix2 + ".+", keyArray );
+
+ // VERIFY
+ assertEquals( "Wrong number returned 1: " + result1, numToInsertPrefix1, result1.size() );
+ assertEquals( "Wrong number returned 2: " + result2, numToInsertPrefix2, result2.size() );
+ }
+}
Modified: commons/proper/jcs/trunk/src/test/org/apache/commons/jcs/engine/memory/MockMemoryCache.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/src/test/org/apache/commons/jcs/engine/memory/MockMemoryCache.java?rev=1570993&r1=1570992&r2=1570993&view=diff
==============================================================================
--- commons/proper/jcs/trunk/src/test/org/apache/commons/jcs/engine/memory/MockMemoryCache.java (original)
+++ commons/proper/jcs/trunk/src/test/org/apache/commons/jcs/engine/memory/MockMemoryCache.java Sun Feb 23 10:37:48 2014
@@ -1,247 +1,247 @@
-package org.apache.commons.jcs.engine.memory;
-
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-import java.io.IOException;
-import java.io.Serializable;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.LinkedHashSet;
-import java.util.Map;
-import java.util.Set;
-
-import org.apache.commons.jcs.engine.behavior.ICacheElement;
-import org.apache.commons.jcs.engine.behavior.ICompositeCacheAttributes;
-import org.apache.commons.jcs.engine.control.CompositeCache;
-import org.apache.commons.jcs.engine.memory.behavior.IMemoryCache;
-import org.apache.commons.jcs.engine.memory.util.MemoryElementDescriptor;
-import org.apache.commons.jcs.engine.stats.behavior.IStats;
-
-/**
- * Mock implementation of a memory cache for testing things like the memory shrinker.
- * <p>
- * @author Aaron Smuts
- */
-public class MockMemoryCache<K extends Serializable, V extends Serializable>
- implements IMemoryCache<K, V>
-{
- /** Config */
- private ICompositeCacheAttributes cacheAttr;
-
- /** Internal map */
- private final HashMap<K, ICacheElement<K, V>> map = new HashMap<K, ICacheElement<K, V>>();
-
- /** The number of times waterfall was called. */
- public int waterfallCallCount = 0;
-
- /** The number passed to the last call of free elements. */
- public int lastNumberOfFreedElements = 0;
-
- /**
- * Does nothing
- * @param cache
- */
- public void initialize( CompositeCache<K, V> cache )
- {
- // nothing
- }
-
- /**
- * Destroy the memory cache
- * <p>
- * @throws IOException
- */
- public void dispose()
- throws IOException
- {
- // nothing
- }
-
- /** @return size */
- public int getSize()
- {
- return map.size();
- }
-
- /** @return stats */
- public IStats getStatistics()
- {
- return null;
- }
-
- /** @return null */
- public Iterator<Map.Entry<K, MemoryElementDescriptor<K, V>>> getIterator()
- {
- return null;
- }
-
- /**
- * @return map.keySet().toArray( */
- public Set<K> getKeySet()
- {
- return new LinkedHashSet<K>(map.keySet());
- }
-
- /**
- * @param key
- * @return map.remove( key ) != null
- * @throws IOException
- */
- public boolean remove( K key )
- throws IOException
- {
- return map.remove( key ) != null;
- }
-
- /**
- * @throws IOException
- */
- public void removeAll()
- throws IOException
- {
- map.clear();
- }
-
- /**
- * @param key
- * @return (ICacheElement) map.get( key )
- * @throws IOException
- */
- public ICacheElement<K, V> get( K key )
- throws IOException
- {
- return map.get( key );
- }
-
- /**
- * @param keys
- * @return elements
- * @throws IOException
- */
- public Map<K, ICacheElement<K, V>> getMultiple(Set<K> keys)
- throws IOException
- {
- Map<K, ICacheElement<K, V>> elements = new HashMap<K, ICacheElement<K, V>>();
-
- if ( keys != null && !keys.isEmpty() )
- {
- Iterator<K> iterator = keys.iterator();
-
- while ( iterator.hasNext() )
- {
- K key = iterator.next();
-
- ICacheElement<K, V> element = get( key );
-
- if ( element != null )
- {
- elements.put( key, element );
- }
- }
- }
-
- return elements;
- }
-
- /**
- * @param key
- * @return (ICacheElement) map.get( key )
- * @throws IOException
- */
- public ICacheElement<K, V> getQuiet( K key )
- throws IOException
- {
- return map.get( key );
- }
-
- /**
- * @param ce
- * @throws IOException
- */
- public void waterfal( ICacheElement<K, V> ce )
- throws IOException
- {
- waterfallCallCount++;
- }
-
- /**
- * @param ce
- * @throws IOException
- */
- public void update( ICacheElement<K, V> ce )
- throws IOException
- {
- if ( ce != null )
- {
- map.put( ce.getKey(), ce );
- }
- }
-
- /**
- * @return ICompositeCacheAttributes
- */
- public ICompositeCacheAttributes getCacheAttributes()
- {
- return cacheAttr;
- }
-
- /**
- * @param cattr
- */
- public void setCacheAttributes( ICompositeCacheAttributes cattr )
- {
- this.cacheAttr = cattr;
- }
-
- /** @return null */
- public CompositeCache<K, V> getCompositeCache()
- {
- return null;
- }
-
- /**
- * @param group
- * @return null
- */
- public Set<K> getGroupKeys( String group )
- {
- return null;
- }
-
- /**
- * @return null
- */
- public Set<String> getGroupNames()
- {
- return null;
- }
-
- /**
- * @param numberToFree
- * @return 0
- * @throws IOException
- */
- public int freeElements( int numberToFree )
- throws IOException
- {
- lastNumberOfFreedElements = numberToFree;
- return 0;
- }
-}
+package org.apache.commons.jcs.engine.memory;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.io.IOException;
+import java.io.Serializable;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.LinkedHashSet;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.commons.jcs.engine.behavior.ICacheElement;
+import org.apache.commons.jcs.engine.behavior.ICompositeCacheAttributes;
+import org.apache.commons.jcs.engine.control.CompositeCache;
+import org.apache.commons.jcs.engine.memory.behavior.IMemoryCache;
+import org.apache.commons.jcs.engine.memory.util.MemoryElementDescriptor;
+import org.apache.commons.jcs.engine.stats.behavior.IStats;
+
+/**
+ * Mock implementation of a memory cache for testing things like the memory shrinker.
+ * <p>
+ * @author Aaron Smuts
+ */
+public class MockMemoryCache<K extends Serializable, V extends Serializable>
+ implements IMemoryCache<K, V>
+{
+ /** Config */
+ private ICompositeCacheAttributes cacheAttr;
+
+ /** Internal map */
+ private final HashMap<K, ICacheElement<K, V>> map = new HashMap<K, ICacheElement<K, V>>();
+
+ /** The number of times waterfall was called. */
+ public int waterfallCallCount = 0;
+
+ /** The number passed to the last call of free elements. */
+ public int lastNumberOfFreedElements = 0;
+
+ /**
+ * Does nothing
+ * @param cache
+ */
+ public void initialize( CompositeCache<K, V> cache )
+ {
+ // nothing
+ }
+
+ /**
+ * Destroy the memory cache
+ * <p>
+ * @throws IOException
+ */
+ public void dispose()
+ throws IOException
+ {
+ // nothing
+ }
+
+ /** @return size */
+ public int getSize()
+ {
+ return map.size();
+ }
+
+ /** @return stats */
+ public IStats getStatistics()
+ {
+ return null;
+ }
+
+ /** @return null */
+ public Iterator<Map.Entry<K, MemoryElementDescriptor<K, V>>> getIterator()
+ {
+ return null;
+ }
+
+ /**
+ * @return map.keySet().toArray( */
+ public Set<K> getKeySet()
+ {
+ return new LinkedHashSet<K>(map.keySet());
+ }
+
+ /**
+ * @param key
+ * @return map.remove( key ) != null
+ * @throws IOException
+ */
+ public boolean remove( K key )
+ throws IOException
+ {
+ return map.remove( key ) != null;
+ }
+
+ /**
+ * @throws IOException
+ */
+ public void removeAll()
+ throws IOException
+ {
+ map.clear();
+ }
+
+ /**
+ * @param key
+ * @return (ICacheElement) map.get( key )
+ * @throws IOException
+ */
+ public ICacheElement<K, V> get( K key )
+ throws IOException
+ {
+ return map.get( key );
+ }
+
+ /**
+ * @param keys
+ * @return elements
+ * @throws IOException
+ */
+ public Map<K, ICacheElement<K, V>> getMultiple(Set<K> keys)
+ throws IOException
+ {
+ Map<K, ICacheElement<K, V>> elements = new HashMap<K, ICacheElement<K, V>>();
+
+ if ( keys != null && !keys.isEmpty() )
+ {
+ Iterator<K> iterator = keys.iterator();
+
+ while ( iterator.hasNext() )
+ {
+ K key = iterator.next();
+
+ ICacheElement<K, V> element = get( key );
+
+ if ( element != null )
+ {
+ elements.put( key, element );
+ }
+ }
+ }
+
+ return elements;
+ }
+
+ /**
+ * @param key
+ * @return (ICacheElement) map.get( key )
+ * @throws IOException
+ */
+ public ICacheElement<K, V> getQuiet( K key )
+ throws IOException
+ {
+ return map.get( key );
+ }
+
+ /**
+ * @param ce
+ * @throws IOException
+ */
+ public void waterfal( ICacheElement<K, V> ce )
+ throws IOException
+ {
+ waterfallCallCount++;
+ }
+
+ /**
+ * @param ce
+ * @throws IOException
+ */
+ public void update( ICacheElement<K, V> ce )
+ throws IOException
+ {
+ if ( ce != null )
+ {
+ map.put( ce.getKey(), ce );
+ }
+ }
+
+ /**
+ * @return ICompositeCacheAttributes
+ */
+ public ICompositeCacheAttributes getCacheAttributes()
+ {
+ return cacheAttr;
+ }
+
+ /**
+ * @param cattr
+ */
+ public void setCacheAttributes( ICompositeCacheAttributes cattr )
+ {
+ this.cacheAttr = cattr;
+ }
+
+ /** @return null */
+ public CompositeCache<K, V> getCompositeCache()
+ {
+ return null;
+ }
+
+ /**
+ * @param group
+ * @return null
+ */
+ public Set<K> getGroupKeys( String group )
+ {
+ return null;
+ }
+
+ /**
+ * @return null
+ */
+ public Set<String> getGroupNames()
+ {
+ return null;
+ }
+
+ /**
+ * @param numberToFree
+ * @return 0
+ * @throws IOException
+ */
+ public int freeElements( int numberToFree )
+ throws IOException
+ {
+ lastNumberOfFreedElements = numberToFree;
+ return 0;
+ }
+}