/**
  * Check event counters in the ControllerBB shared map. For example, if baseKey is
  * ControllerBB.NumForcedDiscEventsKey and vmList is a list containing vmId 2, and expectedCount
  * is 1, then the event count of key with base NumForcedDiscEvents plus vmId 2 is expected to be
  * 1, and any other key found in the map that is prefixed by baseKey should have value be 0.
  * Throws an exception if the conditions are not satisfied.
  *
  * @param baseKey The base string of the key to check (the prefix to the vm id)
  * @param vmList The vms (ClientVmInfo) for the baseKey should have a value of expectedCount.
  * @param expectedCount The expected value of any baseKeys that refer to the given vmIds,
  *     otherwise the expectedCount is 0.
  */
 public static void checkEventCounters(String baseKey, List vmList, int expectedCount) {
   Map aMap = ControllerBB.getBB().getSharedMap().getMap();
   Iterator it = aMap.keySet().iterator();
   while (it.hasNext()) {
     String key = (String) (it.next());
     if (key.startsWith(baseKey)) { // found a key prefixed by baseKey
       int value = ((Integer) (aMap.get(key))).intValue(); // value for the key
       boolean foundInVmIdList = false;
       for (int i = 0; i < vmList.size(); i++) {
         int vmId = ((Integer) ((ClientVmInfo) (vmList.get(i))).getVmid()).intValue();
         if (key.endsWith("_" + vmId)) { // found a baseKey with a vmid in the vmList
           foundInVmIdList = true;
           if (value != expectedCount) {
             throw new TestException(
                 "Expected value for BB key "
                     + key
                     + " to be "
                     + expectedCount
                     + " but it is "
                     + value);
           }
         }
       }
       if (!foundInVmIdList) { // this key's value should be 0
         if (value != 0) {
           throw new TestException(
               "Expected value for BB key " + key + " to be 0 but it is " + value);
         }
       }
     }
   }
 }
  /** Start all cqs running for this VM, and create a CQHistory instance for each CQ. */
  private void startCQsWithHistory() {
    initializeQueryService();
    CqAttributesFactory cqFac = new CqAttributesFactory();
    cqFac.addCqListener(new CQHistoryListener());
    cqFac.addCqListener(new CQGatherListener());
    CqAttributes cqAttr = cqFac.create();
    Iterator it = queryMap.keySet().iterator();
    while (it.hasNext()) {
      String queryName = (String) (it.next());
      String query = (String) (queryMap.get(queryName));
      try {
        CqQuery cq = qService.newCq(queryName, query, cqAttr);
        CQHistory history = new CQHistory(cq.getName());
        CQHistoryListener.recordHistory(history);
        Log.getLogWriter()
            .info(
                "Creating CQ with name " + queryName + ": " + query + ", cq attributes: " + cqAttr);
        Log.getLogWriter().info("Calling executeWithInitialResults on " + cq);
        CqResults rs = cq.executeWithInitialResults();
        SelectResults sr = CQUtil.getSelectResults(rs);
        if (sr == null) {
          throw new TestException(
              "For cq "
                  + cq
                  + " with name "
                  + cq.getName()
                  + " executeWithInitialResults returned "
                  + sr);
        }
        Log.getLogWriter()
            .info(
                "Done calling executeWithInitializResults on "
                    + cq
                    + " with name "
                    + queryName
                    + ", select results size is "
                    + sr.size());
        history.setSelectResults(sr);
        logNumOps();

        // log the select results
        List srList = sr.asList();
        StringBuffer aStr = new StringBuffer();
        aStr.append("SelectResults returned from " + queryName + " is\n");
        for (int i = 0; i < srList.size(); i++) {
          aStr.append(srList.get(i) + "\n");
        }
        Log.getLogWriter().info(aStr.toString());
      } catch (CqExistsException e) {
        throw new TestException(TestHelper.getStackTrace(e));
      } catch (RegionNotFoundException e) {
        throw new TestException(TestHelper.getStackTrace(e));
      } catch (CqException e) {
        throw new TestException(TestHelper.getStackTrace(e));
      }
    }
  }
  /**
   * Verify the result of the CQs registered for this VM. Query results from
   * executeWithInitialResults are combined with subsequent cq events to form the final result.
   */
  protected void verifyQueryResultsCombine() {
    Log.getLogWriter().info("In verifyQueryResultsCombine");
    Iterator it = queryMap.keySet().iterator();
    int count = 0;
    while (it.hasNext()) {
      count++;
      String cqName = (String) (it.next());
      Log.getLogWriter()
          .info("Verifying query " + count + " out of " + queryMap.size() + " with name " + cqName);
      CqQuery cq = qService.getCq(cqName);
      String queryStr = cq.getQueryString();
      String readableQueryStr = CQTest.getReadableQueryString(queryStr);

      // combine the initial selectResults with history of events
      CQHistory history = CQHistoryListener.getCQHistory(cqName);
      Map combinedMap = history.getCombinedResults();
      List combinedList = new ArrayList(combinedMap.values());
      List expectedResults = CQTestInstance.getExpectedResults(queryStr);
      List missingInCombined = new ArrayList(expectedResults);
      List unexpectedInCombined = new ArrayList(combinedList);
      unexpectedInCombined.removeAll(expectedResults);
      missingInCombined.removeAll(combinedList);

      // prepare error Strings
      StringBuffer aStr = new StringBuffer();
      if (unexpectedInCombined.size() > 0) {
        String tmpStr =
            getLocationString(unexpectedInCombined, expectedResults, history)
                + "\n"
                + "Found the following "
                + unexpectedInCombined.size()
                + " unexpected elements in combined results for cq "
                + cqName
                + ", "
                + readableQueryStr
                + ": "
                + QueryObject.toStringFull(unexpectedInCombined);
        Log.getLogWriter().info(tmpStr);
        aStr.append(tmpStr);
      }
      if (missingInCombined.size() > 0) {
        String tmpStr =
            getLocationString(missingInCombined, expectedResults, history)
                + "\n"
                + "The following "
                + missingInCombined.size()
                + " elements were missing from combined results for cq "
                + cqName
                + ", "
                + readableQueryStr
                + ": "
                + QueryObject.toStringFull(missingInCombined);
        Log.getLogWriter().info(tmpStr);
        aStr.append(tmpStr);
      }

      if (aStr.length() > 0) {
        throw new TestException(
            "Probably bug 38065: For cq "
                + cqName
                + ", "
                + readableQueryStr
                + "\n"
                + aStr.toString());
      }
    }
    Log.getLogWriter().info("Done verifying " + count + " queries");
  }