Clover coverage report -
Coverage timestamp: Thu Jul 5 2007 20:02:32 EDT
file stats: LOC: 226   Methods: 15
NCLOC: 176   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
TcpCacheServerTest.java 50% 94.5% 93.3% 91.5%
coverage coverage
 1    package org.jboss.cache.loader;
 2   
 3    import junit.framework.Test;
 4    import junit.framework.TestCase;
 5    import junit.framework.TestSuite;
 6    import org.apache.commons.logging.Log;
 7    import org.apache.commons.logging.LogFactory;
 8    import org.jboss.cache.Cache;
 9    import org.jboss.cache.CacheException;
 10    import org.jboss.cache.CacheImpl;
 11    import org.jboss.cache.CacheSPI;
 12    import org.jboss.cache.DefaultCacheFactory;
 13    import org.jboss.cache.Fqn;
 14    import org.jboss.cache.config.CacheLoaderConfig;
 15    import org.jboss.cache.config.Configuration;
 16    import org.jboss.cache.config.ConfigurationException;
 17    import org.jboss.cache.factories.XmlConfigurationParser;
 18    import org.jboss.cache.jmx.CacheJmxWrapper;
 19    import org.jboss.cache.loader.tcp.TcpCacheServer;
 20    import org.jboss.cache.misc.TestingUtil;
 21    import org.jboss.cache.xml.XmlHelper;
 22    import org.w3c.dom.Element;
 23   
 24    import java.net.UnknownHostException;
 25   
 26    /**
 27    * Tests various ways of setting up the TcpCacheServer
 28    *
 29    * @author Brian Stansberry
 30    * @version $Id: TcpCacheServerTest.java,v 1.2 2007/05/23 10:31:15 msurtani Exp $
 31    */
 32    public class TcpCacheServerTest extends TestCase
 33    {
 34    private static final String CONFIG_FILE = "META-INF/local-service.xml";
 35    private static final Log log = LogFactory.getLog(TcpCacheServerTest.class);
 36   
 37    static TcpCacheServer cache_server = null;
 38   
 39    private CacheImpl cache;
 40    private CacheLoader loader;
 41   
 42    static
 43    {
 44  1 Runtime.getRuntime().addShutdownHook(new Thread()
 45    {
 46  1 public void run()
 47    {
 48  1 if (cache_server != null)
 49    {
 50  0 System.out.println("Stopping TcpCacheServer");
 51  0 cache_server.stop();
 52    }
 53    }
 54    });
 55    }
 56   
 57  3 protected void setUp() throws Exception
 58    {
 59  3 super.setUp();
 60  3 log.debug("\nTest " + getName() + "\n");
 61    }
 62   
 63  3 private void createCacheAndLoader() throws Exception, ConfigurationException, CacheException
 64    {
 65  3 Configuration c = new Configuration();
 66  3 c.setCacheMode(Configuration.CacheMode.LOCAL);
 67  3 c.setTransactionManagerLookupClass("org.jboss.cache.transaction.DummyTransactionManagerLookup");
 68  3 c.setCacheLoaderConfig(getCacheLoaderConfig());
 69  3 cache = (CacheImpl) DefaultCacheFactory.getInstance().createCache(c);
 70   
 71  3 cache.start();
 72  3 loader = cache.getCacheLoaderManager().getCacheLoader();
 73    }
 74   
 75  3 protected CacheLoaderConfig getCacheLoaderConfig() throws Exception
 76    {
 77  3 String xml = "<config>\n" +
 78    "<passivation>false</passivation>\n" +
 79    "<preload></preload>\n" +
 80    "<cacheloader>\n" +
 81    "<class>org.jboss.cache.loader.TcpDelegatingCacheLoader</class>\n" +
 82    "<properties>host=127.0.0.1\nport=12121</properties>\n" +
 83    "<async>false</async>\n" +
 84    "<shared>true</shared>\n" +
 85    "<fetchPersistentState>true</fetchPersistentState>\n" +
 86    "<purgeOnStartup>false</purgeOnStartup>\n" +
 87    "</cacheloader>\n" +
 88    "</config>";
 89  3 Element element = XmlHelper.stringToElement(xml);
 90  3 return XmlConfigurationParser.parseCacheLoaderConfig(element);
 91    }
 92   
 93  3 public void tearDown() throws Exception
 94    {
 95  3 super.tearDown();
 96   
 97  3 if (cache != null)
 98    {
 99  3 cache.stop();
 100  3 cache.destroy();
 101  3 cache = null;
 102    }
 103   
 104  3 if (cache_server != null)
 105    {
 106  3 System.out.println("Stopping TcpCacheServer");
 107  3 cache_server.stop();
 108  3 cache_server = null;
 109    }
 110    }
 111   
 112   
 113  3 private static void createTcpCacheServer() throws UnknownHostException
 114    {
 115  3 cache_server = new TcpCacheServer();
 116  3 cache_server.setBindAddress("127.0.0.1");
 117  3 cache_server.setPort(12121);
 118    }
 119   
 120  3 private void startTcpCacheServer()
 121    {
 122  3 Thread runner = new Thread()
 123    {
 124  3 public void run()
 125    {
 126  3 try
 127    {
 128  3 System.out.println("Starting TcpCacheServer");
 129  3 cache_server.create();
 130  3 cache_server.start();
 131    }
 132    catch (Exception ex)
 133    {
 134  0 ex.printStackTrace();
 135    }
 136    }
 137    };
 138   
 139  3 runner.setDaemon(true);
 140  3 runner.start();
 141   
 142    // give the tcp cache server time to start up
 143  3 TestingUtil.sleepThread(2000);
 144    }
 145   
 146  1 public void testInjectConfigFilePath() throws Exception
 147    {
 148  1 createTcpCacheServer();
 149  1 cache_server.setConfig(CONFIG_FILE);
 150  1 startTcpCacheServer();
 151  1 createCacheAndLoader();
 152  1 cacheCheck();
 153  1 usabilityCheck();
 154    }
 155   
 156  1 public void testInjectCache() throws Exception
 157    {
 158  1 createTcpCacheServer();
 159  1 CacheSPI cache = (CacheSPI) DefaultCacheFactory.getInstance().createCache(CONFIG_FILE, false);
 160  1 cache.start();
 161  1 cache_server.setCache(cache);
 162  1 startTcpCacheServer();
 163  1 createCacheAndLoader();
 164  1 cacheCheck();
 165  1 usabilityCheck();
 166    }
 167   
 168  1 public void testInjectCacheJmxWrapper() throws Exception
 169    {
 170  1 createTcpCacheServer();
 171  1 CacheSPI cache = (CacheSPI) DefaultCacheFactory.getInstance().createCache(CONFIG_FILE, false);
 172  1 CacheJmxWrapper wrapper = new CacheJmxWrapper(cache);
 173  1 wrapper.start();
 174  1 cache_server.setCacheJmxWrapper(wrapper);
 175  1 startTcpCacheServer();
 176  1 createCacheAndLoader();
 177  1 cacheCheck();
 178  1 usabilityCheck();
 179    }
 180   
 181  3 private void cacheCheck()
 182    {
 183  3 Cache c = cache_server.getCache();
 184  3 assertNotNull("Cache exists", c);
 185  3 Configuration config = c.getConfiguration();
 186    // check a couple properties
 187  3 assertEquals("Correct mode", Configuration.CacheMode.LOCAL, config.getCacheMode());
 188  3 assertEquals("Correct cluster name", "JBossCache-Cluster", config.getClusterName());
 189    }
 190   
 191  3 private void usabilityCheck() throws Exception
 192    {
 193  3 Fqn fqn = new Fqn("key");
 194  3 assertFalse("Fqn does not exist in loader", loader.exists(fqn));
 195   
 196    /* put(Fqn,Object,Object) and get(Fqn,Object) */
 197  3 Object oldVal;
 198  3 oldVal = loader.put(fqn, "one", "two");
 199  3 assertNull("oldVal is null", oldVal);
 200   
 201  3 assertEquals("Got value from cache", "two", cache.get(fqn, "one"));
 202    }
 203   
 204    // protected void configureCache() throws Exception
 205    // {
 206    // cache.getConfiguration().setCacheLoaderConfig(getSingleCacheLoaderConfig("",
 207    // "org.jboss.cache.loader.TcpDelegatingCacheLoader",
 208    // "host=127.0.0.1\nport=12121", false, true, false));
 209    //
 210    // // give the tcp cache server time to start up
 211    // TestingUtil.sleepThread(2000);
 212    // }
 213   
 214   
 215  1 public static Test suite()
 216    {
 217  1 return new TestSuite(TcpCacheServerTest.class);
 218    }
 219   
 220   
 221  0 public static void main(String[] args)
 222    {
 223  0 junit.textui.TestRunner.run(suite());
 224    }
 225   
 226    }