Clover coverage report -
Coverage timestamp: Thu Jul 5 2007 20:02:32 EDT
file stats: LOC: 276   Methods: 15
NCLOC: 216   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
LocalConcurrentTest.java 77.3% 93.2% 100% 91.4%
coverage coverage
 1    /*
 2    *
 3    * JBoss, the OpenSource J2EE webOS
 4    *
 5    * Distributable under LGPL license.
 6    * See terms of license at gnu.org.
 7    */
 8   
 9    package org.jboss.cache.pojo;
 10   
 11    import junit.framework.Test;
 12    import junit.framework.TestCase;
 13    import junit.framework.TestSuite;
 14    import org.jboss.cache.config.Configuration;
 15    import org.jboss.cache.lock.UpgradeException;
 16    import org.jboss.cache.pojo.test.Address;
 17    import org.jboss.cache.pojo.test.Person;
 18    import org.jboss.cache.transaction.DummyTransactionManager;
 19   
 20    import javax.naming.Context;
 21    import javax.naming.InitialContext;
 22    import javax.transaction.UserTransaction;
 23    import java.util.ArrayList;
 24    import java.util.Properties;
 25    import java.util.Random;
 26   
 27   
 28    /**
 29    * Local concurrent test for PojoCache. Test attach and detach under load
 30    * and concurrency.
 31    *
 32    * @version $Revision: 1.1 $
 33    * @author<a href="mailto:bwang@jboss.org">Ben Wang</a> December 2004
 34    */
 35    public class LocalConcurrentTest extends TestCase
 36    {
 37    static PojoCache cache_;
 38    Configuration.CacheMode cachingMode_ = Configuration.CacheMode.LOCAL;
 39    Properties p_;
 40    String oldFactory_ = null;
 41    final String FACTORY = "org.jboss.cache.transaction.DummyContextFactory";
 42    static ArrayList nodeList_;
 43    static final int depth_ = 2;
 44    static final int children_ = 2;
 45    static final int MAX_LOOP = 100;
 46    static final int SLEEP_TIME = 50;
 47    static Exception thread_ex = null;
 48    UserTransaction tx_ = null;
 49   
 50  1 public LocalConcurrentTest(String name)
 51    {
 52  1 super(name);
 53    }
 54   
 55  1 public void setUp() throws Exception
 56    {
 57  1 super.setUp();
 58  1 oldFactory_ = System.getProperty(Context.INITIAL_CONTEXT_FACTORY);
 59  1 System.setProperty(Context.INITIAL_CONTEXT_FACTORY, FACTORY);
 60  1 DummyTransactionManager.getInstance();
 61  1 if (p_ == null)
 62    {
 63  1 p_ = new Properties();
 64  1 p_.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.cache.transaction.DummyContextFactory");
 65    }
 66   
 67  1 tx_ = (UserTransaction) new InitialContext(p_).lookup("UserTransaction");
 68   
 69  1 initCaches(Configuration.CacheMode.LOCAL);
 70  1 nodeList_ = nodeGen(depth_, children_);
 71   
 72  1 log("LocalConcurrentTestCase: cacheMode=TRANSIENT, one cache");
 73    }
 74   
 75  1 public void tearDown() throws Exception
 76    {
 77  1 super.tearDown();
 78  1 thread_ex = null;
 79  1 DummyTransactionManager.destroy();
 80  1 destroyCaches();
 81   
 82  1 if (oldFactory_ != null)
 83    {
 84  0 System.setProperty(Context.INITIAL_CONTEXT_FACTORY, oldFactory_);
 85  0 oldFactory_ = null;
 86    }
 87   
 88    }
 89   
 90  1 void initCaches(Configuration.CacheMode caching_mode) throws Exception
 91    {
 92  1 cachingMode_ = caching_mode;
 93  1 boolean toStart = false;
 94  1 cache_ = PojoCacheFactory.createCache("META-INF/local-service.xml", toStart);
 95  1 cache_.start();
 96    }
 97   
 98  1 void destroyCaches() throws Exception
 99    {
 100  1 cache_.stop();
 101  1 cache_ = null;
 102    }
 103   
 104  1 public void testAll_RWLock() throws Exception
 105    {
 106  1 try
 107    {
 108  1 all();
 109    }
 110    catch (UpgradeException ue)
 111    {
 112  0 log("Upgrade exception. Can ingore for repeatable read. " + ue);
 113    }
 114    catch (Exception ex)
 115    {
 116  0 log("Exception: " + ex);
 117  0 throw ex;
 118    }
 119    }
 120   
 121  1 private void all() throws Exception
 122    {
 123  1 RunThread t1 = new RunThread(1);
 124  1 RunThread t2 = new RunThread(2);
 125  1 RunThread t3 = new RunThread(3);
 126  1 RunThread t4 = new RunThread(4);
 127   
 128  1 t1.start();
 129  1 TestingUtil.sleepThread(100);
 130  1 t2.start();
 131  1 TestingUtil.sleepThread(100);
 132  1 t3.start();
 133  1 TestingUtil.sleepThread(100);
 134  1 t4.start();
 135   
 136  1 t1.join(60000);// wait for 20 secs
 137  1 t2.join(60000);// wait for 20 secs
 138  1 t3.join(60000);// wait for 20 secs
 139  1 t4.join(60000);// wait for 20 secs
 140   
 141  1 if (thread_ex != null)
 142    {
 143  0 throw thread_ex;
 144    }
 145    }
 146   
 147    class RunThread extends Thread
 148    {
 149    final int seed_;
 150    Random random_;
 151    Person person_;
 152   
 153  4 public RunThread(int seed)
 154    {
 155  4 seed_ = seed;
 156  4 random_ = new Random(seed);
 157    }
 158   
 159  400 private void createPerson()
 160    {
 161  400 person_ = new Person();
 162  400 person_.setName("Ben");
 163  400 person_.setAge(18);
 164  400 ArrayList<String> lang = new ArrayList<String>();
 165  400 lang.add("English");
 166  400 lang.add("French");
 167  400 lang.add("Mandarin");
 168  400 person_.setLanguages(lang);
 169  400 Address addr = new Address();
 170  400 addr.setZip(95123);
 171  400 addr.setStreet("Almeria");
 172  400 addr.setCity("San Jose");
 173  400 person_.setAddress(addr);
 174    }
 175   
 176  4 public void run()
 177    {
 178  4 try
 179    {
 180  4 _run();
 181    }
 182    catch (Exception e)
 183    {
 184  0 thread_ex = e;
 185    }
 186    }
 187   
 188    /**
 189    */
 190  4 public void _run() throws Exception
 191    {
 192  4 for (int loop = 0; loop < MAX_LOOP; loop++)
 193    {
 194  400 createPerson();// create a new person instance every loop.
 195  400 TestingUtil.sleepThread(random_.nextInt(50));
 196  400 op1();
 197    }
 198    }
 199   
 200    // Operation 1
 201  400 private void op1()
 202    {
 203  400 int i = random_.nextInt(nodeList_.size() - 1);
 204  77 if (i == 0) return;// it is meaningless to test root
 205  323 String node = nodeList_.get(i) + "/aop";
 206  323 cache_.attach(node, person_);
 207  323 TestingUtil.sleepThread(random_.nextInt(SLEEP_TIME));// sleep for max 200 millis
 208  323 TestingUtil.sleepThread(random_.nextInt(SLEEP_TIME));// sleep for max 200 millis
 209  323 cache_.detach(node);
 210    }
 211    }
 212   
 213    /**
 214    * Generate the tree nodes quasi-exponentially. I.e., depth is the level
 215    * of the hierarchy and children is the number of children under each node.
 216    * This strucutre is used to add, get, and remove for each node.
 217    */
 218  1 private ArrayList nodeGen(int depth, int children)
 219    {
 220  1 ArrayList<String> strList = new ArrayList<String>();
 221  1 ArrayList<String> oldList = new ArrayList<String>();
 222  1 ArrayList<String> newList = new ArrayList<String>();
 223   
 224    // Skip root node
 225  1 oldList.add("/");
 226  1 newList.add("/");
 227  1 strList.add("/");
 228   
 229  1 while (depth > 0)
 230    {
 231    // Trying to produce node name at this depth.
 232  2 newList = new ArrayList<String>();
 233  2 for (int i = 0; i < oldList.size(); i++)
 234    {
 235  3 for (int j = 0; j < children; j++)
 236    {
 237  6 String tmp = oldList.get(i);
 238  6 tmp += Integer.toString(j);
 239  6 if (depth != 1)
 240    {
 241  2 tmp += "/";
 242    }
 243   
 244  6 newList.add(tmp);
 245    }
 246    }
 247  2 strList.addAll(newList);
 248  2 oldList = newList;
 249  2 depth--;
 250    }
 251   
 252    // let's prune out root node
 253  1 for (int i = 0; i < strList.size(); i++)
 254    {
 255  1 if (strList.get(i).equals("/"))
 256    {
 257  1 strList.remove(i);
 258  1 break;
 259    }
 260    }
 261  1 log("Nodes generated: " + strList.size());
 262  1 return strList;
 263    }
 264   
 265  1 public static Test suite() throws Exception
 266    {
 267  1 return new TestSuite(LocalConcurrentTest.class);
 268    }
 269   
 270  2 private static void log(String str)
 271    {
 272  2 System.out.println("Thread: " + Thread.currentThread() + ": " + str);
 273    // System.out.println(str);
 274    }
 275   
 276    }