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

com.gemstone.gemfire.cache.query.internal.ProjectionAttributeTest 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.
 */
/*
 * Created on Feb 24, 2005
 *
 *
 */
package com.gemstone.gemfire.cache.query.internal;

import com.gemstone.gemfire.cache.*;
import com.gemstone.gemfire.cache.query.CacheUtils;
import com.gemstone.gemfire.cache.query.FunctionDomainException;
import com.gemstone.gemfire.cache.query.IndexType;
import com.gemstone.gemfire.cache.query.NameResolutionException;
import com.gemstone.gemfire.cache.query.data.Portfolio;
import com.gemstone.gemfire.cache.query.data.Quote;
import com.gemstone.gemfire.cache.query.data.Restricted;
import com.gemstone.gemfire.cache.query.internal.types.ObjectTypeImpl;
import com.gemstone.gemfire.cache.query.internal.types.StructTypeImpl;
import com.gemstone.gemfire.cache.query.Query;
import com.gemstone.gemfire.cache.query.QueryException;
import com.gemstone.gemfire.cache.query.QueryInvocationTargetException;
import com.gemstone.gemfire.cache.query.QueryService;
import com.gemstone.gemfire.cache.query.SelectResults;
import com.gemstone.gemfire.cache.query.types.ObjectType;
import com.gemstone.gemfire.cache.query.types.StructType;
import com.gemstone.gemfire.cache.query.TypeMismatchException;
import java.util.List;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
 * @author Asif
 *
 *
 */
public class ProjectionAttributeTest extends TestCase  {
  
  String queries[]={
            "select distinct p from /pos p where p.ID > 0 ",//ResultSet
            "select distinct p.status from /pos p where p.ID > 0 ",//ResultSet
            "select distinct 'a' from /pos p ",//ResultSet
            "select distinct 1 from /pos p ",//ResultSet
            "select distinct p.status,p.ID from /pos p where p.ID > 0 ",
            "select distinct p,p.P1 from /pos p where p.ID > 0 ",
            "select distinct p,p.P1.SecId from /pos p where p.ID > 0 ",
            "select distinct portfolio: p ,p.P1.SecId from /pos p where p.ID > 0 ",
            "select distinct p.status as STATUS, SECID: p.P1.SecId, ID from /pos p where p.ID > 0 ",
            "select distinct p.status as STATUS, SECID: p.P1.SecId, ID from /pos p ",
            "select distinct 'a',1, p from /pos p ",
  };
  
  String miscQueries[]={
    "select distinct * from null ",
            "select distinct 1 from null ",
            "select distinct 'a',1, p from null ",
            "select distinct * from UNDEFINED ",
            "select distinct 1 from UNDEFINED",
            "select distinct 'a',1, p from UNDEFINED",
  };
  
  public ProjectionAttributeTest(String testName) {
    super(testName);
  }
  
  public static Test suite() {
    TestSuite suite = new TestSuite(ProjectionAttributeTest.class);
    return suite;
  }
  
  public void testMisc() throws Exception {
    QueryService qs = CacheUtils.getQueryService();
    for(int i =0;i 3) {
        assertTrue(r.getCollectionType().getElementType().isStructType());
        checkNames(r, qStr);
    }
    }
    
    String qStr =     "select distinct * from /pos p ";
    Query q = qs.newQuery(qStr);
    Object r = q.execute();
    if(r instanceof StructSet)
      fail(q.getQueryString());
  }
  
  private void checkNames(SelectResults results, String query){
    int i1 = query.indexOf(" distinct ");
    int i2 = query.indexOf(" from ");
    if(i1 < 0 || i2 < 0)
      fail(query);
    String projStr = query.substring(i1+" distinct ".length(), i2);
    //System.out.println("projStr = "+projStr);
    QCompiler compiler = new QCompiler(CacheUtils.getLogger().convertToLogWriterI18n());
    List projAttrs = compiler.compileProjectionAttributes(projStr);
    StructType stype = (StructType)results.getCollectionType().getElementType();
    String names[] = stype.getFieldNames();
    for(int i=0;i 1050");
	    q.execute();
    }catch(Exception e) {
      e.printStackTrace();
      fail("Test failed bcoz of exception "+e);
    }
	    
  
  }
  public void testProjectionAttributesWithIndex() throws QueryException {
    QueryService qs = CacheUtils.getQueryService();
    qs.createIndex("PortFolioID", IndexType.FUNCTIONAL,"ID", "/pos");
    testProjectionAttributes();
  }
  
 
  
 
  
  protected void setUp() throws Exception {
    CacheUtils.startCache();
    CacheUtils.getCache();
    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));
  }
  
  protected void tearDown() throws Exception {
    CacheUtils.closeCache();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy