001: /*
002: Copyright 2004 Philip Jacob <phil@whirlycott.com>
003: Seth Fitzsimmons <seth@note.amherst.edu>
004:
005: Licensed under the Apache License, Version 2.0 (the "License");
006: you may not use this file except in compliance with the License.
007: You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: */
017: package com.whirlycott.cache.test;
018:
019: import org.apache.commons.logging.Log;
020: import org.apache.commons.logging.LogFactory;
021:
022: import junit.framework.TestCase;
023: import com.whirlycott.cache.CacheDecorator;
024: import com.whirlycott.cache.CacheManager;
025: import com.whirlycott.cache.ManagedCache;
026: import com.whirlycott.cache.CacheMaintenancePolicy;
027: import com.whirlycott.cache.CacheConfiguration;
028: import com.whirlycott.cache.Item;
029: import com.whirlycott.cache.policy.NullPolicy;
030: import com.whirlycott.cache.impl.ConcurrentHashMapImpl;
031:
032: /**
033: * @author <a href="mailto:peter.royal@pobox.com">peter royal </a>
034: */
035: public class BasicFunctionalityTest extends TestCase {
036:
037: /**
038: * Logger.
039: */
040: private static final Log log = LogFactory
041: .getLog(BasicFunctionalityTest.class);
042:
043: private CacheDecorator<Object, Object> _cache;
044:
045: @Override
046: protected void setUp() throws Exception {
047: super .setUp();
048: ManagedCache<Object, Item<Object>> managedCache = new ConcurrentHashMapImpl<Object, Item<Object>>();
049: CacheMaintenancePolicy<Object, Item<Object>> policy = new NullPolicy();
050: CacheConfiguration configuration = new CacheConfiguration();
051:
052: configuration.setName("test");
053: configuration.setMaxSize(100);
054:
055: _cache = new CacheDecorator<Object, Object>(managedCache,
056: configuration, policy);
057: }
058:
059: @Override
060: protected void tearDown() throws Exception {
061: super .tearDown();
062: _cache.clear();
063: _cache.dispose();
064: }
065:
066: public void testPutGetRemove() throws Exception {
067: Object key = new Object();
068: Object value = new Object();
069:
070: assertEquals(0, _cache.size());
071:
072: _cache.store(key, value);
073:
074: assertEquals(1, _cache.size());
075:
076: assertEquals(value, _cache.retrieve(key));
077:
078: assertEquals(value, _cache.remove(key));
079:
080: assertEquals(0, _cache.size());
081: }
082:
083: /**
084: * Verifies that the functionality to get a list of cache names works.
085: * @throws Exception
086: */
087: public void testGetCacheNames() throws Exception {
088: final String[] names = CacheManager.getInstance()
089: .getCacheNames();
090: assertNotNull(names);
091: assertTrue(names.length > 0);
092: boolean defaultCacheFound = false;
093: for (final String cacheName : names) {
094: log.debug("Cache name: " + cacheName);
095: if ("default".equals(cacheName)) {
096: defaultCacheFound = true;
097: }
098: assertTrue(defaultCacheFound);
099: }
100: }
101: }
|