All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gemstone.gemfire.cache.query.QueryTest Maven / Gradle / Ivy

There is a newer version: 2.0-BETA
Show newest version
/*
 * Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you
 * may not use this file except in compliance with the License. You
 * may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License. See accompanying
 * LICENSE file.
 */
/******
* THIS FILE IS ENCODED IN UTF-8 IN ORDER TO TEST UNICODE IN FIELD NAMES.
* THE ENCODING MUST BE SPECIFIED AS UTF-8 WHEN COMPILED
*******/
/*
 * QueryTest.java
 * JUnit based test
 *
 * Created on March 8, 2005, 4:54 PM
 */

package com.gemstone.gemfire.cache.query;

import static org.junit.runners.MethodSorters.*;

import java.util.*;

import org.junit.FixMethodOrder;

import com.gemstone.gemfire.cache.Region;
import com.gemstone.gemfire.cache.query.internal.DefaultQuery;

import junit.framework.*;

import com.gemstone.gemfire.cache.query.data.Portfolio;


/**
 *
 * @author vaibhav
 */
@FixMethodOrder(NAME_ASCENDING)
public class QueryTest extends TestCase {
  
  public static void main(String[] args) {
    junit.textui.TestRunner.run(suite());
  }

  public QueryTest(String testName) {
    super(testName);
  }
  
  protected void setUp() throws java.lang.Exception {
    CacheUtils.startCache();
  }
  
  protected void tearDown() throws java.lang.Exception {
    CacheUtils.closeCache();
  }
  
  public static junit.framework.Test suite() {
    TestSuite suite = new TestSuite(QueryTest.class);
    return suite;
  }
  
  public void test000GetQueryString() {
    System.out.println("testGetQueryString");
    String queryStr = "SELECT DISTINCT * FROM /root";
    Query q = CacheUtils.getQueryService().newQuery(queryStr);
    if(!queryStr.equals(q.getQueryString())){
      fail("Query.getQueryString() returns different query string");
    }
  }
  
  public void test001Execute() {
    System.out.println("testExecute");
    try{
      Region region = CacheUtils.createRegion("Portfolios", Portfolio.class);
      region.put("1",new Portfolio(1));
      region.put("2",new Portfolio(0));
      String queryStr = "SELECT DISTINCT * FROM /Portfolios";
      Query q = CacheUtils.getQueryService().newQuery(queryStr);
      SelectResults results = (SelectResults)q.execute();
      assertEquals(results.size(), 2);
    }catch(Exception e){
      e.printStackTrace();
      fail("Exception during Query.execute");
    }
  }
  
  public void test002UnicodeInQuery() {
    System.out.println("testUnicodeInQuery");
    try{
      Region region = CacheUtils.createRegion("Portfolios", Portfolio.class);
      region.put("1",new Portfolio(1));
      region.put("2",new Portfolio(0));
      String queryStr = "SELECT DISTINCT * FROM /Portfolios WHERE unicodeṤtring = 'ṤẐṶ'";
      Query q = CacheUtils.getQueryService().newQuery(queryStr);
      SelectResults results = (SelectResults)q.execute();
      assertEquals(results.size(), 1);
      Portfolio p = (Portfolio)results.iterator().next();
      assertEquals(p.unicodeṤtring, "ṤẐṶ");
    }catch(Exception e){
      e.printStackTrace();
      fail("Exception during Query.execute");
    }
  }

  public void test003UnicodeInRegionNameAndQuery() {
    System.out.println("testUnicodeInQuery");
    try{
      Region region = CacheUtils.createRegion("中æå«", Portfolio.class);
      Portfolio p = new Portfolio(0);
      p.unicodeṤtring = "中æå«";
      region.put("1",p);
      region.put("2",new Portfolio(1));
      String queryStr = "SELECT DISTINCT * FROM " + region.getFullPath() +" WHERE unicodeṤtring = '中æå«'";
      Query q = CacheUtils.getQueryService().newQuery(queryStr);
      SelectResults results = (SelectResults)q.execute();
      assertEquals(results.size(), 1);
      p = (Portfolio)results.iterator().next();
      assertEquals(p.unicodeṤtring, "中æå«");
    }catch(Exception e){
      e.printStackTrace();
      fail("Exception during Query.execute");
    }
  }

  public void test004UnicodeInRegionNameAndQueryWithIndex() {
    try {
      String unicode = "‰∏≠Êñá±Á´";
      Region region = CacheUtils.createRegion(unicode, Portfolio.class);
      CacheUtils.getQueryService().createIndex("unicodeIndex", "unicodeṤtring",
          "/'" + unicode + "'");
      Portfolio p = new Portfolio(0);
      p.unicodeṤtring = unicode;
      region.put("1", p);
      region.put("2", new Portfolio(1));
      String queryStr = "SELECT DISTINCT * FROM /'" + unicode
          + "' WHERE unicodeṤtring = '" + unicode + "'";
      Query q = CacheUtils.getQueryService().newQuery(queryStr);
      SelectResults results = (SelectResults) q.execute();
      assertEquals(results.size(), 1);
      p = (Portfolio) results.iterator().next();
      assertEquals(p.unicodeṤtring, unicode);

      String unicode2 = "䇱縭斫";
      CacheUtils.createRegion(unicode2, Portfolio.class);
      queryStr = "SELECT DISTINCT * FROM /'" + unicode + "' u1, /'" + unicode2
          + "' u2 WHERE u1.unicodeṤtring = '" + unicode
          + "' order by u1.unicodeṤtring  limit 1";
      System.out.println(queryStr);
      CacheUtils.getQueryService().newQuery(queryStr).execute();

    } catch (Exception e) {
      e.printStackTrace();
      fail("Exception during Query.execute");
    }
  }


  public void test005UnicodeInRegionNameAndQueryWithIndexUsingQuotesAsDelim() {
    try {
      String unicode = "‰∏≠Êñá±*+|=. !@#$%^&*()_+,;:Á´[]{}?";
      Region region = CacheUtils.createRegion(unicode, Portfolio.class);
      CacheUtils.getQueryService().createIndex("unicodeIndex", "unicodeṤtring",
          "/'" + unicode + "'");
      Portfolio p = new Portfolio(0);
      p.unicodeṤtring = unicode;
      region.put("1", p);
      region.put("2", new Portfolio(1));
      String queryStr = "SELECT DISTINCT * FROM /'" + unicode
          + "' WHERE unicodeṤtring = '" + unicode + "'";
      Query q = CacheUtils.getQueryService().newQuery(queryStr);
      SelectResults results = (SelectResults) q.execute();
      assertEquals(results.size(), 1);
      p = (Portfolio) results.iterator().next();
      assertEquals(p.unicodeṤtring, unicode);

      String unicode2 = "!@#$%^&*(|)_?+,;: Á´‰∏≠Êñá±*+<>=.[]{}?";
      CacheUtils.createRegion(unicode2, Portfolio.class);
      queryStr = "SELECT DISTINCT * FROM /'" + unicode + "' u1, /'" + unicode2
          + "' u2 WHERE u1.unicodeṤtring = '" + unicode
          + "' order by u1.unicodeṤtring  limit 1";
      System.out.println(queryStr);
      CacheUtils.getQueryService().newQuery(queryStr).execute();

    } catch (Exception e) {
      e.printStackTrace();
      fail("Exception during Query.execute");
    }
  }

  public void test006Compile() {
    System.out.println("testCompile");
    //fail("The test case is empty.");
  }
  
  public void test007IsCompiled() {
    System.out.println("testIsCompiled");
    String queryStr = "SELECT DISTINCT * FROM /root";
    Query q = CacheUtils.getQueryService().newQuery(queryStr);
    if(q.isCompiled())
      fail("Query.isCompiled() returns true for non-compiled query");
  }
    
  public void test008GetStatistics() {
    System.out.println("testGetStatistics");
    String queryStr = "SELECT DISTINCT * FROM /Portfolios where status='active'";
    Query q = CacheUtils.getQueryService().newQuery(queryStr);
    QueryStatistics qst = q.getStatistics();
    if(qst.getNumExecutions()!=0 && qst.getTotalExecutionTime()!=0){
      fail("QueryStatistics not initialized properly");
    }
    try{
      Region region = CacheUtils.createRegion("Portfolios", Portfolio.class);
      CacheUtils.getQueryService().createIndex("testIndex", IndexType.FUNCTIONAL,
                                         "status", "/Portfolios");
      for(int i=0;i<10000;i++){
        region.put(i+"",new Portfolio(i));
      }
      q.execute();
      qst = q.getStatistics();
      if(qst.getNumExecutions() != 1) { // || qst.getTotalExecutionTime()==0){  // bruce - time based CachePerfStats are disabled by default
        fail("QueryStatistics not updated.");
      }
      
      for (int i = 0; i < 10; i++) {
        q.execute();
      }
      
    }
    catch(Exception e){
      e.printStackTrace();
      fail("Exception during Query.execute");
    }
  }
  
  public void test009GetRegionsInQuery() {
    
    String queryStrs[] = new String[] {
        "SELECT DISTINCT * FROM /Portfolios where status='active'",
        "/Portfolios", "/Portfolios.values","/Portfolios.keys()","/Portfolios.entries(false)",
        "null = null", "select distinct * from /Employees where not (select distinct * from collect).isEmpty",
        "select distinct * from $2 where salary > $1","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'",
        "SELECT DISTINCT * FROM (SELECT DISTINCT * FROM /Portfolios ptf, positions pos) WHERE pos.value.secId = 'IBM'",
        "SELECT DISTINCT * FROM /Portfolios WHERE NOT(SELECT DISTINCT * FROM positions.values p WHERE p.secId = 'IBM').isEmpty",
        "SELECT DISTINCT * FROM /Portfolios where status = ELEMENT(SELECT DISTINCT * FROM /Portfolios p where p.ID = 0).status",
        "Select distinct * from /Portfolios pf, /Portfolios2, /Portfolios3, /Data where pf.status='active'",
        "select distinct * from /portfolios p, p.positions.values myPos, (select distinct * from /Employees x)  where myPos.secId = 'YHOO'",  
        "select distinct * from /portfolios p, p.positions.values myPos, (select distinct * from /Employees x, /portfolios)  where myPos.secId = 'YHOO'",
        "select distinct * from /portfolios p, p.positions.values myPos, (select distinct * from /Employees x, /Portfolios)  where myPos.secId = 'YHOO'",
        "select distinct /Portfolios.size, key FROM /pos.entries",
        "select distinct /Portfolios2.size, key FROM /pos.entries WHERE (Select distinct * from /portfolios4, entries).size = 3",
        
    };
    String regions[][]= new String[][] {
        {"/Portfolios"}, {"/Portfolios"},{"/Portfolios"},{"/Portfolios"},{"/Portfolios"},{}, {"/Employees"},
        {"/Portfolios"}, {"/pos"}, {"/Portfolios"}, {"/Portfolios"}, {"/Portfolios"}, {"/Portfolios","/Portfolios2","/Portfolios3","/Data"},
        {"/portfolios","/Employees"}, {"/portfolios","/Employees"}, {"/portfolios","/Employees","/Portfolios"},
        {"/Portfolios", "/pos"}, {"/Portfolios2", "/pos", "/portfolios4"}
        
    };
    
    Object[] params = new Object[] {"", CacheUtils.createRegion("Portfolios", Portfolio.class) };
    for(int i=0; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy