[jboss-cvs] JBossCache/tests/functional/org/jboss/cache/api ...
Manik Surtani
msurtani at jboss.com
Wed Sep 13 11:53:05 EDT 2006
User: msurtani
Date: 06/09/13 11:53:05
Added: tests/functional/org/jboss/cache/api CacheAPITest.java
Log:
- Added unit test for cache API
- Added facility for making certain config elements immutable
Revision Changes Path
1.1 date: 2006/09/13 15:53:05; author: msurtani; state: Exp;JBossCache/tests/functional/org/jboss/cache/api/CacheAPITest.java
Index: CacheAPITest.java
===================================================================
package org.jboss.cache.api;
import junit.framework.TestCase;
import org.jboss.cache.AbstractCacheListener;
import org.jboss.cache.Cache;
import org.jboss.cache.CacheListener;
import org.jboss.cache.DummyTransactionManagerLookup;
import org.jboss.cache.Fqn;
import org.jboss.cache.Node;
import org.jboss.cache.Region;
import org.jboss.cache.config.Configuration;
import org.jboss.cache.config.ConfigurationException;
import org.jboss.cache.factories.DefaultCacheFactory;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Tests the {@link org.jboss.cache.Cache} public API at a high level
*
* @author <a href="mailto:manik at jboss.org">Manik Surtani</a>
*/
public class CacheAPITest extends TestCase
{
private Cache cache;
protected void setUp()
{
// start a single cache instance
cache = new DefaultCacheFactory().createCache("META-INF/local-tx-service.xml");
}
protected void tearDown()
{
if (cache != null) cache.stop();
}
/**
* Tests that the configuration contains the values expected, as well as immutability of certain elements
*/
public void testConfiguration()
{
Configuration c = cache.getConfiguration();
assertEquals(Configuration.CacheMode.LOCAL, c.getCacheMode());
assertEquals(DummyTransactionManagerLookup.class.getName(), c.getTransactionManagerLookupClass());
// note that certain values should be immutable. E.g., CacheMode cannot be changed on the fly.
try
{
c.setCacheMode(Configuration.CacheMode.REPL_SYNC);
fail("Should have thrown an Exception");
}
catch (ConfigurationException e)
{
// expected
}
// others should be changeable though.
c.setLockAcquisitionTimeout(100);
}
/**
* Tests that getRoot() returns the same underlying object as the cache.
*/
public void testGetRoot()
{
assertSame(cache, cache.getRoot());
}
/**
* Basic usage of cache listeners
* <p/>
* A more complete test that tests notifications is in org.jboss.cache.notifications
*/
public void testCacheListeners()
{
assertEquals(0, cache.getCacheListeners().size());
final List events = new ArrayList();
CacheListener dummy = new AbstractCacheListener()
{
public void nodeCreated(Fqn fqn, boolean pre, boolean isLocal)
{
if (pre) events.add("Created");
}
};
cache.addCacheListener(dummy);
assertEquals(1, cache.getCacheListeners().size());
cache.addChild(Fqn.fromString("/blah"));
// test that the event was captured by the listener.
// FOR A FULL TEST ON NOTIFICATIONS SEE TESTS IN org.jboss.cache.notifications
assertEquals(1, events.size());
cache.removeCacheListener(dummy);
assertEquals(0, cache.getCacheListeners().size());
}
/**
* Test that fqn-specific application of cache listeners has not been implemented and will not be implemented
* in 2.0.0. It is a feature for 2.1.0 but the interface needed to be in place now.
*/
public void testFqnBasedCacheListeners()
{
try
{
cache.getCacheListeners(Fqn.ROOT);
fail("Fqn-based cache listener operation should throw an exception");
}
catch (Exception e)
{
// expected
}
try
{
cache.addCacheListener(Fqn.ROOT, new AbstractCacheListener()
{
});
fail("Fqn-based cache listener operation should throw an exception");
}
catch (Exception e)
{
// expected
}
try
{
cache.removeCacheListener(Fqn.ROOT, new AbstractCacheListener()
{
});
fail("Fqn-based cache listener operation should throw an exception");
}
catch (Exception e)
{
// expected
}
}
/**
* All cache operations should happen on a {@link Node} - I.e., you look up a {@link Node} and perform data operations
* on this {@link Node}. For convenience and familiarity with JBoss Cache 1.x, we provide some helpers in {@link Cache}
* which dives you direct data access to nodes.
* <p/>
* This test exercises these.
*/
public void testConvenienceMethods()
{
Fqn fqn = Fqn.fromString("/test/fqn");
Object key = "key", value = "value";
Map data = new HashMap();
data.put(key, value);
assertNull(cache.get(fqn, key));
cache.put(fqn, key, value);
assertEquals(value, cache.get(fqn, key));
cache.remove(fqn, key);
assertNull(cache.get(fqn, key));
cache.put(fqn, data);
assertEquals(value, cache.get(fqn, key));
}
/**
* Another convenience method that tests node removal
*/
public void nodeConvenienceNodeRemoval()
{
// this fqn is relative, but since it is from the root it may as well be absolute
Fqn fqn = Fqn.fromString("/test/fqn");
cache.addChild(fqn);
assertTrue(cache.hasChild(fqn));
cache.removeNode(fqn);
assertFalse(cache.hasChild(fqn));
}
/**
* Tests basic eviction
*/
public void testEvict()
{
Fqn one = Fqn.fromString("/one");
Fqn two = Fqn.fromString("/one/two");
Object key = "key", value = "value";
cache.addChild(one).put(key, value);
cache.addChild(two).put(key, value);
assertTrue(cache.hasChild(one));
assertFalse(cache.getChild(one).getData().isEmpty());
assertTrue(cache.hasChild(two));
assertFalse(cache.getChild(two).getData().isEmpty());
// evict two
cache.evict(two, false);
assertTrue(cache.hasChild(one));
assertTrue(cache.getChild(one).getKeys().contains(key));
assertFalse(cache.hasChild(two));
// now add 2 again...
cache.addChild(two).put(key, value);
// now evict one, NOT recursive
cache.evict(one, false);
// one will NOT be removed, just emptied.
assertTrue(cache.hasChild(one));
assertFalse(cache.getChild(one).getKeys().contains(key));
// two will be unaffected
assertTrue(cache.hasChild(two));
assertTrue(cache.getChild(two).getKeys().contains(key));
}
/**
* Tests recursive eviction
*/
public void testEvictRecursive()
{
Fqn one = Fqn.fromString("/one");
Fqn two = Fqn.fromString("/one/two");
Object key = "key", value = "value";
cache.addChild(one).put(key, value);
cache.addChild(two).put(key, value);
assertTrue(cache.hasChild(one));
assertFalse(cache.getChild(one).getData().isEmpty());
assertTrue(cache.hasChild(two));
assertFalse(cache.getChild(two).getData().isEmpty());
// evict two
cache.evict(two, true);
assertTrue(cache.hasChild(one));
assertFalse(cache.getChild(one).getData().isEmpty());
assertFalse(cache.hasChild(two));
// now add 2 again...
cache.addChild(two).put(key, value);
// now evict one, recursive
cache.evict(one, true);
assertFalse(cache.hasChild(one));
assertFalse(cache.hasChild(two));
}
/**
* Again, see org.jboss.cache for more extensive tests on Regions. This just tests the getRegion API on cache.
*/
public void testRegion()
{
Region rootRegion = cache.getRegion(Fqn.ROOT);
assertNotNull(rootRegion); // guaranteed never to return null. cache.getRegion() will create a new region if necessary.
assertSame(rootRegion, cache.getRegion(Fqn.ROOT));
Region otherRegion = cache.getRegion(Fqn.fromString("/other/region"));
assertNotNull(otherRegion);
assertSame(otherRegion, cache.getRegion(Fqn.fromString("/other/region")));
}
}
More information about the jboss-cvs-commits
mailing list