[jboss-cvs] JBossCache/tests/functional/org/jboss/cache/loader ...
Brian Stansberry
brian.stansberry at jboss.com
Tue May 22 16:12:40 EDT 2007
User: bstansberry
Date: 07/05/22 16:12:40
Added: tests/functional/org/jboss/cache/loader
TcpCacheServerTest.java
Log:
[JBCACHE-1053] Remove unused mbean attribs from when we did JMX lookup
[JBCACHE-1069] Support injection of CacheJmxWrapper
[JBCACHE-1068] Get rid of LifeCycle interface.
Revision Changes Path
1.1 date: 2007/05/22 20:12:40; author: bstansberry; state: Exp;JBossCache/tests/functional/org/jboss/cache/loader/TcpCacheServerTest.java
Index: TcpCacheServerTest.java
===================================================================
package org.jboss.cache.loader;
import java.net.UnknownHostException;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.cache.Cache;
import org.jboss.cache.CacheException;
import org.jboss.cache.CacheImpl;
import org.jboss.cache.CacheSPI;
import org.jboss.cache.DefaultCacheFactory;
import org.jboss.cache.Fqn;
import org.jboss.cache.config.CacheLoaderConfig;
import org.jboss.cache.config.Configuration;
import org.jboss.cache.config.ConfigurationException;
import org.jboss.cache.config.Configuration.CacheMode;
import org.jboss.cache.factories.XmlConfigurationParser;
import org.jboss.cache.jmx.CacheJmxWrapper;
import org.jboss.cache.loader.tcp.TcpCacheServer;
import org.jboss.cache.misc.TestingUtil;
import org.jboss.cache.xml.XmlHelper;
import org.w3c.dom.Element;
/**
* Tests various ways of setting up the TcpCacheServer
*
* @author Brian Stansberry
*
* @version $Id: TcpCacheServerTest.java,v 1.1 2007/05/22 20:12:40 bstansberry Exp $
*/
public class TcpCacheServerTest extends TestCase
{
private static final String CONFIG_FILE = "META-INF/local-service.xml";
private static final Log log = LogFactory.getLog(TcpCacheServerTest.class);
static TcpCacheServer cache_server = null;
private CacheImpl cache;
private CacheLoader loader;
static
{
Runtime.getRuntime().addShutdownHook(new Thread()
{
public void run()
{
if (cache_server != null)
{
System.out.println("Stopping TcpCacheServer");
cache_server.stop();
}
}
});
}
protected void setUp() throws Exception
{
super.setUp();
log.debug("\nTest " + getName() + "\n");
}
private void createCacheAndLoader() throws Exception, ConfigurationException, CacheException
{
Configuration c = new Configuration();
c.setCacheMode(Configuration.CacheMode.LOCAL);
c.setTransactionManagerLookupClass("org.jboss.cache.transaction.DummyTransactionManagerLookup");
c.setCacheLoaderConfig(getCacheLoaderConfig());
cache = (CacheImpl) DefaultCacheFactory.getInstance().createCache(c);
cache.start();
loader = cache.getCacheLoaderManager().getCacheLoader();
}
protected CacheLoaderConfig getCacheLoaderConfig() throws Exception
{
String xml = "<config>\n" +
"<passivation>false</passivation>\n" +
"<preload></preload>\n" +
"<cacheloader>\n" +
"<class>org.jboss.cache.loader.TcpDelegatingCacheLoader</class>\n" +
"<properties>host=127.0.0.1\nport=12121</properties>\n" +
"<async>false</async>\n" +
"<shared>true</shared>\n" +
"<fetchPersistentState>true</fetchPersistentState>\n" +
"<purgeOnStartup>false</purgeOnStartup>\n" +
"</cacheloader>\n" +
"</config>";
Element element = XmlHelper.stringToElement(xml);
return XmlConfigurationParser.parseCacheLoaderConfig(element);
}
public void tearDown() throws Exception
{
super.tearDown();
if (cache != null)
{
cache.stop();
cache.destroy();
cache = null;
}
if (cache_server != null)
{
System.out.println("Stopping TcpCacheServer");
cache_server.stop();
cache_server = null;
}
}
private static void createTcpCacheServer() throws UnknownHostException
{
cache_server = new TcpCacheServer();
cache_server.setBindAddress("127.0.0.1");
cache_server.setPort(12121);
}
private void startTcpCacheServer()
{
Thread runner = new Thread()
{
public void run()
{
try
{
System.out.println("Starting TcpCacheServer");
cache_server.create();
cache_server.start();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
};
runner.setDaemon(true);
runner.start();
// give the tcp cache server time to start up
TestingUtil.sleepThread(2000);
}
public void testInjectConfigFilePath() throws Exception
{
createTcpCacheServer();
cache_server.setConfig(CONFIG_FILE);
startTcpCacheServer();
createCacheAndLoader();
cacheCheck();
usabilityCheck();
}
public void testInjectCache() throws Exception
{
createTcpCacheServer();
CacheSPI cache = (CacheSPI) DefaultCacheFactory.getInstance().createCache(CONFIG_FILE, false);
cache.start();
cache_server.setCache(cache);
startTcpCacheServer();
createCacheAndLoader();
cacheCheck();
usabilityCheck();
}
public void testInjectCacheJmxWrapper() throws Exception
{
createTcpCacheServer();
CacheSPI cache = (CacheSPI) DefaultCacheFactory.getInstance().createCache(CONFIG_FILE, false);
CacheJmxWrapper wrapper = new CacheJmxWrapper(cache);
wrapper.start();
cache_server.setCacheJmxWrapper(wrapper);
startTcpCacheServer();
createCacheAndLoader();
cacheCheck();
usabilityCheck();
}
private void cacheCheck()
{
Cache c = cache_server.getCache();
assertNotNull("Cache exists", c);
Configuration config = c.getConfiguration();
// check a couple properties
assertEquals("Correct mode", Configuration.CacheMode.LOCAL, config.getCacheMode());
assertEquals("Correct cluster name", "JBossCache-Cluster", config.getClusterName());
}
private void usabilityCheck() throws Exception
{
Fqn fqn = new Fqn("key");
assertFalse("Fqn does not exist in loader", loader.exists(fqn));
/* put(Fqn,Object,Object) and get(Fqn,Object) */
Object oldVal;
oldVal = loader.put(fqn, "one", "two");
assertNull("oldVal is null", oldVal);
assertEquals("Got value from cache", "two", cache.get(fqn, "one"));
}
// protected void configureCache() throws Exception
// {
// cache.getConfiguration().setCacheLoaderConfig(getSingleCacheLoaderConfig("",
// "org.jboss.cache.loader.TcpDelegatingCacheLoader",
// "host=127.0.0.1\nport=12121", false, true, false));
//
// // give the tcp cache server time to start up
// TestingUtil.sleepThread(2000);
// }
public static Test suite()
{
return new TestSuite(TcpCacheServerTest.class);
}
public static void main(String[] args)
{
junit.textui.TestRunner.run(suite());
}
}
More information about the jboss-cvs-commits
mailing list