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

com.gemstone.gemfire.cache.query.QueryServiceTest 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.
 */
/*
 * QueryServiceTest.java
 * JUnit based test
 *
 * Created on March 8, 2005, 4:53 PM
 */

package com.gemstone.gemfire.cache.query;

import junit.framework.*;
import java.util.*;
import com.gemstone.gemfire.cache.*;
import com.gemstone.gemfire.cache.query.data.Portfolio;

/**
 *
 * @author vaibhav
 */
public class QueryServiceTest extends TestCase {
  
  public QueryServiceTest(String testName) {
    super(testName);
  }
  
  protected void setUp() throws java.lang.Exception {
    CacheUtils.startCache();
    Region region = CacheUtils.createRegion("Portfolios", Portfolio.class);
    for(int i=0;i<5;i++){
      region.put(i+"",new Portfolio(i));
    }
  }
  
  protected void tearDown() throws java.lang.Exception {
    CacheUtils.closeCache();
  }
  
  public static junit.framework.Test suite() {
    junit.framework.TestSuite suite = new junit.framework.TestSuite(QueryServiceTest.class);
    
    return suite;
  }
  
  // tests to make sure no exception is thrown when a valid query is created
  // and an invalid query throws an exception
  public void testNewQuery() throws Exception {
    System.out.println("testNewQuery");
    QueryService qs = CacheUtils.getQueryService();
    qs.newQuery("SELECT DISTINCT * FROM /root");
    
    try {
      qs.newQuery("SELET DISTINCT * FROM /root");
      fail("Should have thrown an InvalidQueryException");
    }
    catch (QueryInvalidException e) {
      //pass
    }
  }
  
  public void testCreateIndex() throws Exception {
    System.out.println("testCreateIndex");
    QueryService qs = CacheUtils.getQueryService();
//    DebuggerSupport.waitForJavaDebugger(CacheUtils.getLogger());
    Index index = qs.createIndex("statusIndex", IndexType.FUNCTIONAL,"status","/Portfolios");
    
    try{
      index = qs.createIndex("statusIndex", IndexType.FUNCTIONAL,"status","/Portfolios");
      if(index != null)
        fail("QueryService.createIndex allows duplicate index names");
    }
    catch(IndexNameConflictException e) {
    }
    
    try{
      index = qs.createIndex("statusIndex1", IndexType.FUNCTIONAL,"status","/Portfolios");
      if(index != null)
        fail("QueryService.createIndex allows duplicate indexes");
    }
    catch(IndexExistsException e) {
    }
  }
  
  public void testIndexDefinitions() throws Exception{
    Object[][] testDataFromClauses = {
      {"status", "/Portfolios", Boolean.TRUE},
      {"status", "/Portfolios.entries", Boolean.FALSE},
      {"status", "/Portfolios.values", Boolean.TRUE},
      {"status", "/Portfolios.keys", Boolean.TRUE},
      {"status", "/Portfolios p", Boolean.TRUE},
      {"status", "/Portfolio", Boolean.FALSE},
      {"status", "/Portfolio.positions", Boolean.FALSE},
      {"status", "/Portfolios p, p.positions", Boolean.TRUE},
    };
    
    runCreateIndexTests(testDataFromClauses);
    
    Object[][] testDataIndexExpr = {
      {"positions", "/Portfolios", Boolean.FALSE},
      {"status.length", "/Portfolios", Boolean.TRUE},
      {"p.status", "/Portfolios p", Boolean.TRUE},
      {"p.getStatus()", "/Portfolios p", Boolean.TRUE},
      {"pos.value.secId", "/Portfolios p, p.positions pos", Boolean.TRUE},
      {"pos.getValue().getSecId()", "/Portfolios p, p.positions pos", Boolean.TRUE},
      {"pos.getValue.secId", "/Portfolios p, p.positions pos", Boolean.TRUE},
      {"secId", "/Portfolios p, p.positions", Boolean.FALSE},
      {"is_defined(status)", "/Portfolios", Boolean.FALSE},
      {"is_undefined(status)", "/Portfolios", Boolean.FALSE},
      {"NOT(status = null)", "/Portfolios", Boolean.FALSE},
      {"$1", "/Portfolios", Boolean.FALSE},
    };
    
    runCreateIndexTests(testDataIndexExpr);
  }
  
  private void runCreateIndexTests(Object testData[][]) throws Exception{
    QueryService qs = CacheUtils.getQueryService();
    qs.removeIndexes();
    for(int i=0;i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy