@Test
  public void testBug40428_2() throws Exception {
    Object shortData1 =
        new Object() {
          public short shortField = 4;
        };
    Object shortData2 =
        new Object() {
          public short shortField = 5;
        };

    Region region = CacheUtils.createRegion("shortFieldTest", Object.class);
    region.put("0", shortData1);
    QueryService qs = CacheUtils.getQueryService();
    String qry = "select * from /shortFieldTest.entries sf where sf.value.shortField < 10 ";
    qs.createIndex(
        "shortIndex", IndexType.FUNCTIONAL, "value.shortField", "/shortFieldTest.entries");
    region.put("1", shortData2);
    Query query = null;
    Object result = null;

    query = qs.newQuery(qry);

    SelectResults rs = (SelectResults) query.execute();
    assertEquals(rs.size(), 2);
  }
 public void xtestNestQueryInWhereClause() throws Exception {
   Region region = CacheUtils.createRegion("Portfolios", Portfolio.class);
   region.put("0", new Portfolio(0));
   region.put("1", new Portfolio(1));
   region.put("2", new Portfolio(2));
   region.put("3", new Portfolio(3));
   Query query =
       CacheUtils.getQueryService()
           .newQuery(
               "SELECT DISTINCT * FROM /Portfolios WHERE NOT (SELECT DISTINCT * FROM positions.values p WHERE p.secId = 'IBM').isEmpty");
   Collection result = (Collection) query.execute();
   Portfolio p = (Portfolio) (result.iterator().next());
   if (!p.positions.containsKey("IBM")) fail(query.getQueryString());
   // query = CacheUtils.getQueryService().newQuery("SELECT DISTINCT * FROM
   // /Portfolios where status = ELEMENT(SELECT DISTINCT * FROM /Portfolios p
   // where p.ID = 0).status");
   // result = (Collection)query.execute();
   // CacheUtils.log(result);
   // query = CacheUtils.getQueryService().newQuery("SELECT DISTINCT * FROM
   // /Portfolios x where status = ELEMENT(SELECT DISTINCT * FROM /Portfolios
   // p where p.ID = x.ID).status");
   // result = (Collection)query.execute();
   // SELECT DISTINCT * FROM /Portfolios where status = ELEMENT(SELECT
   // DISTINCT * FROM /Portfolios where ID = 0).status
   // SELECT DISTINCT * FROM /Portfolios x where status = ELEMENT(SELECT
   // DISTINCT * FROM /Portfolios p where p.ID = x.ID).status
 }
 @Test
 public void testBug37723() {
   Region region = CacheUtils.createRegion("portfolios", Portfolio.class);
   region.put("0", new Portfolio(0));
   region.put("1", new Portfolio(1));
   region.put("2", new Portfolio(2));
   region.put("3", new Portfolio(3));
   QueryService qs = CacheUtils.getQueryService();
   String qry =
       "select distinct getID, status from /portfolios pf where getID < 10 order by getID desc";
   Query q = qs.newQuery(qry);
   try {
     SelectResults result = (SelectResults) q.execute();
     Iterator itr = result.iterator();
     int j = 3;
     while (itr.hasNext()) {
       Struct struct = (Struct) itr.next();
       assertEquals(j--, ((Integer) struct.get("getID")).intValue());
     }
     qry = "select distinct getID, status from /portfolios pf where getID < 10 order by getID asc";
     q = qs.newQuery(qry);
     result = (SelectResults) q.execute();
     itr = result.iterator();
     j = 0;
     while (itr.hasNext()) {
       Struct struct = (Struct) itr.next();
       assertEquals(j++, ((Integer) struct.get("getID")).intValue());
     }
   } catch (Exception e) {
     e.printStackTrace();
     fail("Test failed because of exception=" + e);
   }
 }
 public void xtestMiscQueries() throws Exception {
   String testData[] = {"NULL", "UNDEFINED"};
   for (int i = 0; i < testData.length; i++) {
     Query query = CacheUtils.getQueryService().newQuery("SELECT DISTINCT * FROM " + testData[i]);
     Object result = query.execute();
     if (!result.equals(QueryService.UNDEFINED)) fail(query.getQueryString());
   }
 }
 public void xtestVoidMethods() throws Exception {
   Region region = CacheUtils.createRegion("Data", Data.class);
   region.put("0", new Data());
   Query query =
       CacheUtils.getQueryService().newQuery("SELECT DISTINCT * FROM /Data where voidMethod");
   Collection result = (Collection) query.execute();
   if (result.size() != 0) fail(query.getQueryString());
   query =
       CacheUtils.getQueryService()
           .newQuery("SELECT DISTINCT * FROM /Data where voidMethod = null ");
   result = (Collection) query.execute();
   if (result.size() != 1) fail(query.getQueryString());
 }
 public void xtestNestQueryInFromClause() throws Exception {
   Region region = CacheUtils.createRegion("Portfolios", Portfolio.class);
   region.put("0", new Portfolio(0));
   region.put("1", new Portfolio(1));
   region.put("2", new Portfolio(2));
   region.put("3", new Portfolio(3));
   Query query =
       CacheUtils.getQueryService()
           .newQuery(
               "SELECT DISTINCT * FROM (SELECT DISTINCT * FROM /Portfolios where status = 'active') p  where p.ID = 0");
   //    DebuggerSupport.waitForJavaDebugger(CacheUtils.getLogger());
   Collection result = (Collection) query.execute();
   Portfolio p = (Portfolio) (result.iterator().next());
   if (!p.status.equals("active") || p.getID() != 0) fail(query.getQueryString());
 }
  @Test
  public void testMultipleOrderByClauses() {
    Region region = CacheUtils.createRegion("portfolios", Portfolio.class);
    region.put("2", new Portfolio(2));
    region.put("3", new Portfolio(3));
    region.put("4", new Portfolio(4));
    region.put("5", new Portfolio(5));
    region.put("6", new Portfolio(6));
    region.put("7", new Portfolio(7));
    QueryService qs = CacheUtils.getQueryService();
    String qry =
        "select distinct status, getID from /portfolios pf where getID < 10 order by status asc, getID desc";
    Query q = qs.newQuery(qry);
    try {
      SelectResults result = (SelectResults) q.execute();
      Iterator itr = result.iterator();
      int j = 6;
      while (itr.hasNext() && j > 0) {
        Struct struct = (Struct) itr.next();
        assertEquals("active", struct.get("status"));
        assertEquals(j, ((Integer) struct.get("getID")).intValue());

        j -= 2;
      }
      j = 7;
      while (itr.hasNext()) {
        Struct struct = (Struct) itr.next();
        assertEquals(j, ((Integer) struct.get("getID")).intValue());
        assertEquals("inactive", struct.get("status"));
        j -= 2;
      }
      /*
      qry = "select distinct getID, status from /portfolios pf where getID < 10 order by getID asc";
      q = qs.newQuery(qry);
      result = (SelectResults) q.execute();
      itr = result.iterator();
      j = 0;
      while ( itr.hasNext()) {
       Struct struct = (Struct)itr.next();
       assertEquals(j++, ((Integer)struct.get("getID")).intValue());
      }*/
    } catch (Exception e) {
      e.printStackTrace();
      fail("Test failed because of exception=" + e);
    }
  }
  @Test
  public void testBug()
      throws TimeoutException, CacheWriterException, FunctionDomainException, TypeMismatchException,
          NameResolutionException, QueryInvocationTargetException, Exception {
    Region region = CacheUtils.createRegion("portfolios", Portfolio.class);
    region.put("0", new Portfolio(0));
    region.put("1", new Portfolio(1));
    region.put("2", new Portfolio(2));
    region.put("3", new Portfolio(3));
    QueryService qs = CacheUtils.getQueryService();
    /*String qStr = "Select distinct structset.sos, structset.key " +
    "from /portfolios pfos, pfos.positions.values outerPos, " +
    "(SELECT DISTINCT key: key, sos: pos.sharesOutstanding "+
    "from /portfolios.entries pf, pf.value.positions.values pos " +
    "where outerPos.secId != 'IBM' AND " +
    "pf.key IN (select distinct * from pf.value.collectionHolderMap['0'].arr)) structset " +
     "where structset.sos > 2000";*/

    String qStr =
        "Select distinct * from /portfolios pf, pf.positions.values where status = 'active' and secId = 'IBM'";
    qs.createIndex("index1", IndexType.FUNCTIONAL, "status", "/portfolios pf");

    qs.createIndex(
        "index4",
        IndexType.FUNCTIONAL,
        "itr",
        "/portfolios pf, pf.collectionHolderMap chm, chm.value.arr itr");
    qs.createIndex(
        "index2", IndexType.FUNCTIONAL, "status", "/portfolios pf, positions.values pos");
    qs.createIndex("index3", IndexType.FUNCTIONAL, "secId", "/portfolios pf, positions.values pos");
    qs.createIndex(
        "index5",
        IndexType.FUNCTIONAL,
        "pos.secId",
        "/portfolios pf, pf.collectionHolderMap chm, chm.value.arr, pf.positions.values pos");
    qs.createIndex(
        "index6", IndexType.FUNCTIONAL, "status", "/portfolios pf, pf.collectionHolderMap chm");
    qs.createIndex(
        "index7",
        IndexType.FUNCTIONAL,
        "itr",
        "/portfolios pf, positions.values, pf.collectionHolderMap chm, chm.value.arr itr");
    Query q = qs.newQuery(qStr);
    SelectResults result = (SelectResults) q.execute();
    if (result.size() == 0) fail("Test failed as size is zero");
  }
  @Test
  public void testBug40441() throws Exception {
    CacheUtils.startCache();
    final Cache cache = CacheUtils.getCache();
    AttributesFactory attributesFactory = new AttributesFactory();
    RegionAttributes ra = attributesFactory.create();
    final Region region = cache.createRegion("new_pos", ra);
    String queryStr1 =
        " select distinct r.name, pVal, r.\"type\"  "
            + " from /new_pos r , r.positions.values pVal where "
            + " ( r.undefinedTestField.toString = UNDEFINED  OR false ) "; // AND pVal.mktValue =
                                                                           // 1.00";
    String queryStr2 =
        " select distinct r.name, pVal, r.\"type\"  "
            + " from /new_pos r , r.positions.values pVal where "
            + " ( r.undefinedTestField.toString = UNDEFINED  AND true ) AND pVal.mktValue = 1.00";
    final QueryService qs = CacheUtils.getQueryService();
    for (int i = 1; i < 100; ++i) {
      NewPortfolio pf = new NewPortfolio("name" + i, i);
      region.put("name" + i, pf);
    }

    Index indx1 =
        qs.createIndex(
            "MarketValues",
            IndexType.FUNCTIONAL,
            "itr2.mktValue",
            "/new_pos itr1, itr1.positions.values itr2");
    Index indx2 = qs.createIndex("Name", IndexType.FUNCTIONAL, "itr1.name", "/new_pos itr1");
    Index indx3 = qs.createIndex("nameIndex", IndexType.PRIMARY_KEY, "name", "/new_pos");
    Index indx4 = qs.createIndex("idIndex", IndexType.FUNCTIONAL, "id", "/new_pos");
    Index indx5 = qs.createIndex("statusIndex", IndexType.FUNCTIONAL, "status", "/new_pos");
    Index indx6 =
        qs.createIndex(
            "undefinedFieldIndex", IndexType.FUNCTIONAL, "undefinedTestField.toString", "/new_pos");
    final Query q1 = qs.newQuery(queryStr1);
    final Query q2 = qs.newQuery(queryStr2);
    try {
      SelectResults sr1 = (SelectResults) q1.execute();
      SelectResults sr2 = (SelectResults) q2.execute();
    } catch (Throwable e) {
      e.printStackTrace();
      fail("Test failed due to = " + e.toString());
    }
  }
 @Test
 public void testIndexOnCommitForDestroy() throws Exception {
   AttributesFactory af = new AttributesFactory();
   af.setDataPolicy(DataPolicy.REPLICATE);
   Region region = cache.createRegion("sample", af.create());
   qs.createIndex("foo", IndexType.FUNCTIONAL, "age", "/sample");
   Context ctx = cache.getJNDIContext();
   UserTransaction utx = (UserTransaction) ctx.lookup("java:/UserTransaction");
   Integer x = new Integer(0);
   utx.begin();
   region.create(x, new Person("xyz", 45));
   utx.commit();
   Query q = qs.newQuery("select * from /sample where age < 50");
   assertEquals(1, ((SelectResults) q.execute()).size());
   Person dsample = (Person) CopyHelper.copy(region.get(x));
   dsample.setAge(55);
   utx.begin();
   region.destroy(x);
   utx.commit();
   CacheUtils.log(((Person) region.get(x)));
   assertEquals(0, ((SelectResults) q.execute()).size());
 }
 public void xtestBug32763()
     throws FunctionDomainException, TypeMismatchException, NameResolutionException,
         QueryInvocationTargetException, TimeoutException, CacheWriterException {
   Region region = CacheUtils.createRegion("pos", Portfolio.class);
   region.put("0", new Portfolio(0));
   region.put("1", new Portfolio(1));
   region.put("2", new Portfolio(2));
   region.put("3", new Portfolio(3));
   QueryService qs = CacheUtils.getQueryService();
   String qStr =
       "SELECT DISTINCT key: key, iD: entry.value.iD, secId: posnVal.secId  FROM /pos.entries entry, entry.value.positions.values posnVal  WHERE entry.value.\"type\" = 'type0' AND posnVal.secId = 'YHOO'";
   Query q = qs.newQuery(qStr);
   SelectResults result = (SelectResults) q.execute();
   StructType type = (StructType) result.getCollectionType().getElementType();
   String names[] = type.getFieldNames();
   List list = result.asList();
   if (list.size() < 1) fail("Test failed as the resultset's size is zero");
   for (int i = 0; i < list.size(); ++i) {
     Struct stc = (Struct) list.get(i);
     if (!stc.get(names[2]).equals("YHOO")) {
       fail("Test failed as the SecID value is not YHOO");
     }
   }
 }
  @Test
  public void testBug37119() throws Exception {
    Region region = CacheUtils.createRegion("portfolios", Portfolio.class);
    region.put("0", new Portfolio(0));
    region.put("1", new Portfolio(1));
    region.put("2", new Portfolio(2));
    region.put("3", new Portfolio(3));
    region.put(Integer.MIN_VALUE + "", new Portfolio(Integer.MIN_VALUE));
    region.put("-1", new Portfolio(-1));
    QueryService qs = CacheUtils.getQueryService();

    String qStr = "Select distinct * from /portfolios pf where pf.getID() = " + Integer.MIN_VALUE;
    Query q = qs.newQuery(qStr);
    SelectResults result = (SelectResults) q.execute();
    assertEquals(result.size(), 1);
    Portfolio pf = (Portfolio) result.iterator().next();
    assertEquals(pf.getID(), Integer.MIN_VALUE);
    qStr = "Select distinct * from /portfolios pf where pf.getID() = -1";
    q = qs.newQuery(qStr);
    result = (SelectResults) q.execute();
    assertEquals(result.size(), 1);
    pf = (Portfolio) result.iterator().next();
    assertEquals(pf.getID(), -1);

    qStr =
        "Select distinct * from /portfolios pf where pf.getID() = 3 and pf.getLongMinValue() = "
            + Long.MIN_VALUE
            + 'l';
    q = qs.newQuery(qStr);
    result = (SelectResults) q.execute();
    assertEquals(result.size(), 1);
    pf = (Portfolio) result.iterator().next();
    assertEquals(pf.getID(), 3);

    qStr =
        "Select distinct * from /portfolios pf where pf.getID() = 3 and pf.getFloatMinValue() = "
            + Float.MIN_VALUE
            + 'f';
    q = qs.newQuery(qStr);
    result = (SelectResults) q.execute();
    assertEquals(result.size(), 1);
    pf = (Portfolio) result.iterator().next();
    assertEquals(pf.getID(), 3);

    qStr =
        "Select distinct * from /portfolios pf where pf.getID() = 3 and pf.getDoubleMinValue() = "
            + Double.MIN_VALUE;
    q = qs.newQuery(qStr);
    result = (SelectResults) q.execute();
    assertEquals(result.size(), 1);
    pf = (Portfolio) result.iterator().next();
    assertEquals(pf.getID(), 3);
  }
  @Test
  public void testScenario1() throws Exception {
    Context ctx = cache.getJNDIContext();
    UserTransaction ta = null;
    // Connection conn = null;
    try {
      qs.createIndex("iIndex", IndexType.FUNCTIONAL, "ID", "/portfolios");
      // PUT 4 objects in region, QUERY, CREATEINDEX
      for (int i = 0; i < 4; i++) {
        currRegion.put("key" + i, new Portfolio(i));
      }
      QueryObserverImpl observer2 = new QueryObserverImpl();
      QueryObserverHolder.setInstance(observer2);
      q = qs.newQuery("select distinct * from /portfolios where ID != 53");
      Object r = q.execute();
      if (!observer2.isIndexesUsed) {
        fail("NO INDEX WAS USED, IT WAS EXPECTED TO BE USED");
      }
      if (((Collection) r).size() != 4) {
        fail("Query result not of expected size");
      }
      // print("Size of query result :"+ ((Collection)r).size());
      // print("Result of query =" + Utils.printResult(r));

      // print("Index IS: " + ((RangeIndex)i2).dump());

      // BEGIN TX PUT new 4 objects in region, QUERY,CREATEINDEX, ROLLBACK
      ta = (UserTransaction) ctx.lookup("java:/UserTransaction");
      ta.begin();
      for (int i = 9; i < 13; i++) {
        currRegion.put("key" + i, new Portfolio(i));
      }
      observer2 = new QueryObserverImpl();
      QueryObserverHolder.setInstance(observer2);
      q = qs.newQuery("select distinct * from /portfolios where ID != 53");
      r = q.execute();
      if (!observer2.isIndexesUsed) {
        fail("NO INDEX WAS USED, IT WAS EXPECTED TO BE USED");
      }
      if (((Collection) r).size() != 4) {
        fail("Query result not of expected size");
      }
      // print("Size of query result :"+ ((Collection)r).size());
      // print("Result of query =" + Utils.printResult(r));
      Index i1 = qs.createIndex("tIndex", IndexType.FUNCTIONAL, "status", "/portfolios");
      // print("Index IS: " + ((RangeIndex)i1).dump());
      observer2 = new QueryObserverImpl();
      QueryObserverHolder.setInstance(observer2);
      q = qs.newQuery("select distinct * from /portfolios where status = 'active'");
      r = q.execute();
      if (!observer2.isIndexesUsed) {
        fail("NO INDEX WAS USED, IT WAS EXPECTED TO BE USED");
      }
      if (((Collection) r).size() != 2) {
        fail("Query result not of expected size");
      }
      observer2 = new QueryObserverImpl();
      QueryObserverHolder.setInstance(observer2);
      q = qs.newQuery("select distinct * from /portfolios where status = 'inactive'");
      r = q.execute();
      if (!observer2.isIndexesUsed) {
        fail("NO INDEX WAS USED, IT WAS EXPECTED TO BE USED");
      }
      if (((Collection) r).size() != 2) {
        fail("Query result not of expected size");
      }
      ta.rollback();
      // PRINT INDEX AFTER ROLLBACK, REMOVEINDEX.
      observer2 = new QueryObserverImpl();
      QueryObserverHolder.setInstance(observer2);
      q = qs.newQuery("select distinct * from /portfolios where status = 'active'");
      r = q.execute();
      if (!observer2.isIndexesUsed) {
        fail("NO INDEX WAS USED, IT WAS EXPECTED TO BE USED");
      }
      if (((Collection) r).size() != 2) {
        fail("Query result not of expected size");
      }
      // print("AfterRollback \n"+currRegion.values());
      // print("Index IS: " + ((RangeIndex)i1).dump());
      qs.removeIndex(i1);
      // BEGIN TX PUT new 4 objects in region,CREATEINDEX, QUERY ,COMMIT
      ta.begin();
      for (int i = 9; i < 13; i++) {
        currRegion.put("key" + i, new Portfolio(i));
      }
      i1 = qs.createIndex("tIndex", IndexType.FUNCTIONAL, "status", "/portfolios");
      // print("Index IS: " + ((RangeIndex)i1).dump());
      observer2 = new QueryObserverImpl();
      QueryObserverHolder.setInstance(observer2);
      q = qs.newQuery("select distinct * from /portfolios where status = 'active'");
      r = q.execute();
      if (!observer2.isIndexesUsed) {
        fail("NO INDEX WAS USED, IT WAS EXPECTED TO BE USED");
      }
      if (((Collection) r).size() != 2) {
        fail("Query result not of expected size");
      }
      for (int i = 9; i < 13; i++) {
        currRegion.put("key" + i, new Portfolio(i));
      }
      observer2 = new QueryObserverImpl();
      QueryObserverHolder.setInstance(observer2);
      q = qs.newQuery("select distinct * from /portfolios where status = 'active'");
      r = q.execute();
      if (!observer2.isIndexesUsed) {
        fail("NO INDEX WAS USED, IT WAS EXPECTED TO BE USED");
      }
      if (((Collection) r).size() != 2) {
        fail("Query result not of expected size");
      }
      // print("Size of query result :"+ ((Collection)r).size());
      // print("Result of query =" + Utils.printResult(r));
      // print("Index on status IS: " + ((RangeIndex)i1).dump());
      // print("Index On ID IS: " + ((RangeIndex)i2).dump());
      ta.commit();
      // WAIT FOR 2 secs DISPLAYINDEX, QUERY

      Thread.sleep(2000);

      // print("Aftercommit \n"+currRegion.values());
      // print("Index IS: " + ((RangeIndex)i1).dump());
      observer2 = new QueryObserverImpl();
      QueryObserverHolder.setInstance(observer2);
      q = qs.newQuery("select distinct * from /portfolios where status = 'active'");
      r = q.execute();
      if (!observer2.isIndexesUsed) {
        fail("NO INDEX WAS USED, IT WAS EXPECTED TO BE USED");
      }
      if (((Collection) r).size() != 4) {
        fail("Query result not of expected size");
      }
      observer2 = new QueryObserverImpl();
      QueryObserverHolder.setInstance(observer2);
      q = qs.newQuery("select distinct * from /portfolios where status = 'inactive'");
      r = q.execute();
      if (!observer2.isIndexesUsed) {
        fail("NO INDEX WAS USED, IT WAS EXPECTED TO BE USED");
      }
      if (((Collection) r).size() != 4) {
        fail("Query result not of expected size");
      }
      observer2 = new QueryObserverImpl();
      QueryObserverHolder.setInstance(observer2);
      q = qs.newQuery("select distinct * from /portfolios where ID != 53");
      r = q.execute();
      if (!observer2.isIndexesUsed) {
        fail("NO INDEX WAS USED, IT WAS EXPECTED TO BE USED");
      }
      if (((Collection) r).size() != 8) {
        fail("Query result not of expected size");
      }
      // print("Size of query result :"+ ((Collection)r).size());
      // print("Result of query =" + Utils.printResult(r));
      // print("Index On ID IS: " + ((RangeIndex)i2).dump());
    } catch (RollbackException e) {
      e.printStackTrace();
    } catch (Exception e) {
      e.printStackTrace();
      ta.rollback();
    }
  }