/**
  * Get a random key from the given region that is within this instance's range. If no keys qualify
  * in the region, return null.
  *
  * @param aRegion - The region to get the key from.
  * @param excludeKey - The region to get the key from.
  * @returns A key from aRegion, or null.
  */
 public Object getRandomKey(Region aRegion, Object excludeKey) {
   long start = System.currentTimeMillis();
   int lower = ((Integer) (lowerKeyRange.get())).intValue();
   int upper = ((Integer) (upperKeyRange.get())).intValue();
   long randomKeyIndex = TestConfig.tab().getRandGen().nextLong(lower, upper);
   long startKeyIndex = randomKeyIndex;
   Object key = NameFactory.getObjectNameForCounter(randomKeyIndex);
   do {
     boolean done = false;
     if ((!(key.equals(excludeKey))) && (aRegion.containsKey(key))) done = true;
     if (done) break;
     randomKeyIndex++; // go to the next key
     if (randomKeyIndex > upper) randomKeyIndex = lower;
     if (randomKeyIndex == startKeyIndex) { // considered all keys
       key = null;
       break;
     }
     key = NameFactory.getObjectNameForCounter(randomKeyIndex);
   } while (true);
   long end = System.currentTimeMillis();
   Log.getLogWriter()
       .info(
           "Done in TxUtilKeyRange:getRandomKey, key is "
               + key
               + " "
               + aRegion.getFullPath()
               + " getRandomKey took "
               + (end - start)
               + " millis");
   return key;
 }
 /**
  * Creates a new key/value in the given region by creating a new key within the range and a random
  * value.
  *
  * @param aRegion The region to create the new key in.
  * @param exists Not used in this overridden method; this test wants to use unique keys even on
  *     creates, so we don't do anything different here based on the value of exists.
  * @return An instance of Operation describing the create operation.
  */
 @Override
 public Operation createEntry(Region aRegion, boolean exists) {
   int lower = ((Integer) (lowerKeyRange.get())).intValue();
   int upper = ((Integer) (upperKeyRange.get())).intValue();
   long keyIndex = TestConfig.tab().getRandGen().nextInt(lower, upper);
   long startKeyIndex = keyIndex;
   Object key = NameFactory.getObjectNameForCounter(keyIndex);
   boolean containsKey = aRegion.containsKey(key);
   while (containsKey) { // looking for a key that does not exist
     keyIndex++; // go to the next key
     if (keyIndex > upper) keyIndex = lower;
     if (keyIndex == startKeyIndex) { // considered all keys
       return null;
     }
     key = NameFactory.getObjectNameForCounter(keyIndex);
     containsKey = aRegion.containsKey(key);
   }
   BaseValueHolder vh = new ValueHolder(key, randomValues, new Integer(modValInitializer++));
   try {
     Log.getLogWriter()
         .info(
             "createEntryKeyRange: putting key "
                 + key
                 + ", object "
                 + vh.toString()
                 + " in region "
                 + aRegion.getFullPath());
     aRegion.put(key, vh);
     Log.getLogWriter()
         .info(
             "createEntryKeyRange: done putting key "
                 + key
                 + ", object "
                 + vh.toString()
                 + " in region "
                 + aRegion.getFullPath());
   } catch (Exception e) {
     throw new TestException(TestHelper.getStackTrace(e));
   }
   return new Operation(aRegion.getFullPath(), key, Operation.ENTRY_CREATE, null, vh.modVal);
 }
 /**
  * Register interest with ALL_KEYS, and InterestPolicyResult = KEYS_VALUES which is equivalent to
  * a full GII.
  *
  * @param aRegion The region to register interest on.
  */
 protected static void registerInterest(Region aRegion) {
   Log.getLogWriter()
       .info("Calling registerInterest for all keys, result interest policy KEYS_VALUES");
   aRegion.registerInterest("ALL_KEYS", InterestResultPolicy.KEYS_VALUES);
   Log.getLogWriter()
       .info(
           "Done calling registerInterest for all keys, "
               + "result interest policy KEYS_VALUES, "
               + aRegion.getFullPath()
               + " size is "
               + aRegion.size());
 }
  /**
   * Given a List of QueryObjects known to be inconsistent as determined by validation, log where
   * the suspect objects are found by checking for it in 1) the expected List 2) the CQ history of
   * events 3) the select results 4) the localRegion
   *
   * @param inconsistencies A List of suspect QueryObjects to check in each location.
   * @param expected The expected List of objects for a query.
   * @param history The CQHistory for the query
   * @returns
   */
  private String getLocationString(List inconsistencies, List expected, CQHistory history) {
    StringBuffer aStr = new StringBuffer();
    for (int i = 0; i < inconsistencies.size(); i++) {
      QueryObject suspect = (QueryObject) (inconsistencies.get(i));

      // check the local region
      boolean found = false;
      Iterator it = aRegion.keySet().iterator();
      while (it.hasNext()) {
        Object key = it.next();
        Region.Entry entry = aRegion.getEntry(key);
        QueryObject qo = (QueryObject) (entry.getValue());
        if ((qo != null) && (qo.equals(suspect))) {
          found = true;
          aStr.append(
              qo.toStringAbbreviated()
                  + " was found in "
                  + aRegion.getFullPath()
                  + " at key "
                  + key
                  + "\n");
        }
      }
      if (!found) {
        aStr.append(
            suspect.toStringAbbreviated() + " was NOT found in " + aRegion.getFullPath() + "\n");
      }

      // seach for all occurrences in expected list
      found = false;
      it = expected.iterator();
      while (it.hasNext()) {
        QueryObject qo = (QueryObject) (it.next());
        if (qo.equals(suspect)) {
          found = true;
          aStr.append(qo.toStringAbbreviated() + " was found in expected results\n");
        }
      }
      if (!found) {
        aStr.append(suspect.toStringAbbreviated() + " was NOT found in expected results\n");
      }

      // seach for all occurrences in selectResults
      SelectResults selResults = history.getSelectResults();
      found = false;
      it = selResults.iterator();
      while (it.hasNext()) {
        QueryObject qo = (QueryObject) (it.next());
        if (qo.equals(suspect)) {
          found = true;
          aStr.append(qo.toStringAbbreviated() + " was found in SelectResults\n");
        }
      }
      if (!found) {
        aStr.append(suspect.toStringAbbreviated() + " was NOT found in SelectResults\n");
      }

      // seach for all occurrences in history
      found = false;
      List eventList = history.getEvents();
      for (int j = 0; j < eventList.size(); j++) {
        CqEvent event = (CqEvent) (eventList.get(j));
        QueryObject qo = (QueryObject) (event.getNewValue());
        if ((qo != null) && (qo.equals(suspect))) {
          found = true;
          aStr.append(
              qo.toStringAbbreviated()
                  + " was found in event history as new value "
                  + event
                  + "\n");
        }
      }
      if (!found) {
        aStr.append(suspect.toStringAbbreviated() + " was NOT found in CqEvent history\n");
      }
    }
    return aStr.toString();
  }