Clover coverage report -
Coverage timestamp: Thu Jul 5 2007 20:02:32 EDT
file stats: LOC: 350   Methods: 17
NCLOC: 212   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
TestingUtil.java 54% 61.5% 70.6% 60.1%
coverage coverage
 1    /*
 2    * JBoss, the OpenSource J2EE webOS
 3    *
 4    * Distributable under LGPL license.
 5    * See terms of license at gnu.org.
 6    */
 7   
 8    package org.jboss.cache.misc;
 9   
 10    import org.jboss.cache.Cache;
 11    import org.jboss.cache.CacheImpl;
 12    import org.jboss.cache.CacheSPI;
 13   
 14    import java.io.File;
 15    import java.util.List;
 16    import java.util.Random;
 17   
 18    /**
 19    * Utilities for unit testing JBossCache.
 20    *
 21    * @author <a href="mailto://brian.stansberry@jboss.com">Brian Stansberry</a>
 22    * @version $Revision$
 23    */
 24    public class TestingUtil
 25    {
 26   
 27    private static Random random = new Random();
 28    public static final Object ALWAYS_EQUALS_OBJECT = new Object()
 29    {
 30  0 public boolean equals()
 31    {
 32  0 return true;
 33    }
 34    };
 35   
 36    /**
 37    * Loops, continually calling {@link #areCacheViewsComplete(org.jboss.cache.Cache[])}
 38    * until it either returns true or <code>timeout</code> ms have elapsed.
 39    *
 40    * @param caches caches which must all have consistent views
 41    * @param timeout max number of ms to loop
 42    * @throws RuntimeException if <code>timeout</code> ms have elapse without
 43    * all caches having the same number of members.
 44    */
 45  98 public static void blockUntilViewsReceived(Cache[] caches, long timeout)
 46    {
 47  98 long failTime = System.currentTimeMillis() + timeout;
 48   
 49  98 while (System.currentTimeMillis() < failTime)
 50    {
 51  98 sleepThread(100);
 52  97 if (areCacheViewsComplete(caches))
 53    {
 54  97 return;
 55    }
 56    }
 57   
 58  0 throw new RuntimeException("timed out before caches had complete views");
 59    }
 60   
 61    /**
 62    * Version of blockUntilViewsReceived that uses varargs
 63    */
 64  31 public static void blockUntilViewsReceived(long timeout, Cache... caches)
 65    {
 66  31 blockUntilViewsReceived(caches, timeout);
 67    }
 68   
 69    /**
 70    * Loops, continually calling {@link #areCacheViewsComplete(CacheImpl[])}
 71    * until it either returns true or <code>timeout</code> ms have elapsed.
 72    *
 73    * @param caches caches which must all have consistent views
 74    * @param timeout max number of ms to loop
 75    * @throws RuntimeException if <code>timeout</code> ms have elapse without
 76    * all caches having the same number of members.
 77    */
 78  66 public static void blockUntilViewsReceived(CacheImpl[] caches, long timeout)
 79    {
 80  66 long failTime = System.currentTimeMillis() + timeout;
 81   
 82  148 while (System.currentTimeMillis() < failTime)
 83    {
 84  148 sleepThread(100);
 85  148 if (areCacheViewsComplete(caches))
 86    {
 87  66 return;
 88    }
 89    }
 90   
 91  0 throw new RuntimeException("timed out before caches had complete views");
 92    }
 93   
 94    /**
 95    * An overloaded version of {@link #blockUntilViewsReceived(long,org.jboss.cache.Cache[])} that allows for 'shrinking' clusters.
 96    * I.e., the usual method barfs if there are more members than expected. This one takes a param (barfIfTooManyMembers) which,
 97    * if false, will NOT barf but will wait until the cluster 'shrinks' to the desired size. Useful if in tests, you kill
 98    * a member and want to wait until this fact is known across the cluster.
 99    *
 100    * @param timeout
 101    * @param barfIfTooManyMembers
 102    * @param caches
 103    */
 104  3 public static void blockUntilViewsReceived(long timeout, boolean barfIfTooManyMembers, Cache... caches)
 105    {
 106  3 long failTime = System.currentTimeMillis() + timeout;
 107   
 108  3 while (System.currentTimeMillis() < failTime)
 109    {
 110  3 sleepThread(100);
 111  3 if (areCacheViewsComplete(caches, barfIfTooManyMembers))
 112    {
 113  3 return;
 114    }
 115    }
 116   
 117  0 throw new RuntimeException("timed out before caches had complete views");
 118    }
 119   
 120    /**
 121    * Loops, continually calling {@link #areCacheViewsComplete(CacheImpl[])}
 122    * until it either returns true or <code>timeout</code> ms have elapsed.
 123    *
 124    * @param groupSize number of caches expected in the group
 125    * @param timeout max number of ms to loop
 126    * @throws RuntimeException if <code>timeout</code> ms have elapse without
 127    * all caches having the same number of members.
 128    */
 129  0 public static void blockUntilViewReceived(CacheSPI cache, int groupSize, long timeout)
 130    {
 131  0 long failTime = System.currentTimeMillis() + timeout;
 132   
 133  0 while (System.currentTimeMillis() < failTime)
 134    {
 135  0 sleepThread(100);
 136  0 if (isCacheViewComplete(cache, groupSize))
 137    {
 138  0 return;
 139    }
 140    }
 141   
 142  0 throw new RuntimeException("timed out before caches had complete views");
 143    }
 144   
 145    /**
 146    * Loops, continually calling {@link #areCacheViewsComplete(CacheImpl[])}
 147    * until it either returns true or <code>timeout</code> ms have elapsed.
 148    *
 149    * @param groupSize number of caches expected in the group
 150    * @param timeout max number of ms to loop
 151    * @throws RuntimeException if <code>timeout</code> ms have elapse without
 152    * all caches having the same number of members.
 153    */
 154  0 public static void blockUntilViewReceived(CacheImpl cache, int groupSize, long timeout)
 155    {
 156  0 long failTime = System.currentTimeMillis() + timeout;
 157   
 158  0 while (System.currentTimeMillis() < failTime)
 159    {
 160  0 sleepThread(100);
 161  0 if (isCacheViewComplete(cache, groupSize))
 162    {
 163  0 return;
 164    }
 165    }
 166   
 167  0 throw new RuntimeException("timed out before caches had complete views");
 168    }
 169   
 170    /**
 171    * Checks each cache to see if the number of elements in the array
 172    * returned by {@link CacheSPI#getMembers()} matches the size of
 173    * the <code>caches</code> parameter.
 174    *
 175    * @param caches caches that should form a View
 176    * @return <code>true</code> if all caches have
 177    * <code>caches.length</code> members; false otherwise
 178    * @throws IllegalStateException if any of the caches have MORE view
 179    * members than caches.length
 180    */
 181  246 public static boolean areCacheViewsComplete(Cache[] caches)
 182    {
 183  246 return areCacheViewsComplete(caches, true);
 184    }
 185   
 186  249 public static boolean areCacheViewsComplete(Cache[] caches, boolean barfIfTooManyMembers)
 187    {
 188  249 int memberCount = caches.length;
 189   
 190  249 for (int i = 0; i < memberCount; i++)
 191    {
 192  487 if (!isCacheViewComplete(caches[i], memberCount, barfIfTooManyMembers))
 193    {
 194  82 return false;
 195    }
 196    }
 197   
 198  166 return true;
 199    }
 200   
 201    /**
 202    * Checks each cache to see if the number of elements in the array
 203    * returned by {@link CacheImpl#getMembers()} matches the size of
 204    * the <code>caches</code> parameter.
 205    *
 206    * @param caches caches that should form a View
 207    * @return <code>true</code> if all caches have
 208    * <code>caches.length</code> members; false otherwise
 209    * @throws IllegalStateException if any of the caches have MORE view
 210    * members than caches.length
 211    */
 212  148 public static boolean areCacheViewsComplete(CacheImpl[] caches)
 213    {
 214  0 if (caches == null) throw new NullPointerException("Cache impl array is null");
 215  148 Cache[] c = new Cache[caches.length];
 216  837 for (int i = 0; i < caches.length; i++) c[i] = caches[i];
 217  148 return areCacheViewsComplete(c);
 218    }
 219   
 220    /**
 221    * @param cache
 222    * @param memberCount
 223    */
 224  0 public static boolean isCacheViewComplete(CacheImpl cache, int memberCount)
 225    {
 226  0 List members = cache.getMembers();
 227  0 if (members == null || memberCount > members.size())
 228    {
 229  0 return false;
 230    }
 231  0 else if (memberCount < members.size())
 232    {
 233    // This is an exceptional condition
 234  0 StringBuffer sb = new StringBuffer("Cache at address ");
 235  0 sb.append(cache.getLocalAddress());
 236  0 sb.append(" had ");
 237  0 sb.append(members.size());
 238  0 sb.append(" members; expecting ");
 239  0 sb.append(memberCount);
 240  0 sb.append(". Members were (");
 241  0 for (int j = 0; j < members.size(); j++)
 242    {
 243  0 if (j > 0)
 244    {
 245  0 sb.append(", ");
 246    }
 247  0 sb.append(members.get(j));
 248    }
 249  0 sb.append(')');
 250   
 251  0 throw new IllegalStateException(sb.toString());
 252    }
 253   
 254  0 return true;
 255    }
 256   
 257    /**
 258    * @param c
 259    * @param memberCount
 260    */
 261  0 public static boolean isCacheViewComplete(Cache c, int memberCount)
 262    {
 263  0 return isCacheViewComplete(c, memberCount, true);
 264    }
 265   
 266  487 public static boolean isCacheViewComplete(Cache c, int memberCount, boolean barfIfTooManyMembers)
 267    {
 268  487 CacheSPI cache = (CacheSPI) c;
 269  487 List members = cache.getMembers();
 270  487 if (members == null || memberCount > members.size())
 271    {
 272  82 return false;
 273    }
 274  405 else if (memberCount < members.size())
 275    {
 276  1 if (barfIfTooManyMembers)
 277    {
 278    // This is an exceptional condition
 279  1 StringBuffer sb = new StringBuffer("Cache at address ");
 280  1 sb.append(cache.getLocalAddress());
 281  1 sb.append(" had ");
 282  1 sb.append(members.size());
 283  1 sb.append(" members; expecting ");
 284  1 sb.append(memberCount);
 285  1 sb.append(". Members were (");
 286  1 for (int j = 0; j < members.size(); j++)
 287    {
 288  10 if (j > 0)
 289    {
 290  9 sb.append(", ");
 291    }
 292  10 sb.append(members.get(j));
 293    }
 294  1 sb.append(')');
 295   
 296  1 throw new IllegalStateException(sb.toString());
 297    }
 298  0 else return false;
 299    }
 300   
 301  404 return true;
 302    }
 303   
 304   
 305    /**
 306    * Puts the current thread to sleep for the desired number of ms, suppressing
 307    * any exceptions.
 308    *
 309    * @param sleeptime number of ms to sleep
 310    */
 311  14522 public static void sleepThread(long sleeptime)
 312    {
 313  14522 try
 314    {
 315  14522 Thread.sleep(sleeptime);
 316    }
 317    catch (InterruptedException ie)
 318    {
 319    }
 320    }
 321   
 322  64 public static void sleepRandom(int maxTime)
 323    {
 324  64 sleepThread(random.nextInt(maxTime));
 325    }
 326   
 327  323 public static void recursiveFileRemove(String directoryName)
 328    {
 329  323 File file = new File(directoryName);
 330  323 if (file.exists())
 331    {
 332  321 System.out.println("Deleting file " + file);
 333  321 recursivedelete(file);
 334    }
 335    }
 336   
 337  612 private static void recursivedelete(File f)
 338    {
 339  612 if (f.isDirectory())
 340    {
 341  349 File[] files = f.listFiles();
 342  349 for (File file : files)
 343    {
 344  291 recursivedelete(file);
 345    }
 346    }
 347    //System.out.println("File " + f.toURI() + " deleted = " + f.delete());
 348  612 f.delete();
 349    }
 350    }