com.gemstone.gemfire.cache.query.functional.IUMRCompositeIteratorTest Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gemfire-junit Show documentation
Show all versions of gemfire-junit Show documentation
SnappyData store based off Pivotal GemFireXD
/*
* 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.
*/
/*
* IUMRCompositeIteratorTest.java
*
* Created on October 20, 2005, 4:52 PM
*/
package com.gemstone.gemfire.cache.query.functional;
import com.gemstone.gemfire.cache.Region;
import com.gemstone.gemfire.cache.query.CacheUtils;
import com.gemstone.gemfire.cache.query.SelectResults;
import com.gemstone.gemfire.cache.query.Index;
import com.gemstone.gemfire.cache.query.IndexType;
import com.gemstone.gemfire.cache.query.Query;
import com.gemstone.gemfire.cache.query.QueryService;
import com.gemstone.gemfire.cache.query.data.Address;
import com.gemstone.gemfire.cache.query.data.Employee;
import com.gemstone.gemfire.cache.query.data.PhoneNo;
import com.gemstone.gemfire.cache.query.data.Street;
import com.gemstone.gemfire.cache.query.data.Country;
import com.gemstone.gemfire.cache.query.internal.QueryObserverAdapter;
import com.gemstone.gemfire.cache.query.internal.QueryObserverHolder;
import com.gemstone.gemfire.cache.query.Utils;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import junit.framework.*;
/**
*
* @author vjadhav
*/
public class IUMRCompositeIteratorTest extends TestCase {
/** Creates a new instance of IUMRCompositeIteratorTest */
public IUMRCompositeIteratorTest(String testName) {
super(testName);
}
protected void setUp() throws java.lang.Exception {
CacheUtils.startCache();
Region r1 = CacheUtils.createRegion("countries", Country.class);
Set CountrySet1 = new HashSet();
CountrySet1.add(new String("India"));
CountrySet1.add(new String("China"));
CountrySet1.add(new String("USA"));
CountrySet1.add(new String("UK"));
for (int i=0; i<4 ; i++){
r1.put(new Integer(i), new Country(i,CountrySet1));
}
Set add1 = new HashSet();
add1.add(new Address("411045", "Baner"));
add1.add(new Address("411001", "DholePatilRd"));
Region r2 = CacheUtils.createRegion("employees", Employee.class);
for(int i=0;i<4;i++){
r2.put(i+"", new Employee("empName",(20+i),i,"Mr.",(5000+i),add1));
}
Region r3 = CacheUtils.createRegion("address", Address.class);
Set ph = new HashSet();
Set str = new HashSet();
ph.add(new PhoneNo(111, 222, 333, 444));
str.add(new Street("DPRoad","lane5"));
str.add(new Street("DPStreet1","lane5"));
for(int i=0;i<4;i++){
r3.put(new Integer(i), new Address("411001","Pune",str,ph));
}
}
protected void tearDown() throws java.lang.Exception {
CacheUtils.closeCache();
}
public static Test suite(){
TestSuite suite = new TestSuite(IUMRCompositeIteratorTest.class);
return suite;
}
public void testQueryWithCompositeIter1() throws Exception {
QueryService qs;
qs = CacheUtils.getQueryService();
String queries[] = {
//Test Case No. IUMR
"Select distinct * from /employees e, /address a, e.getPhoneNo(a.zipCode) ea where e.name ='empName'",
// "Select distinct * from /employees e, /address a, e.getPh(e.empId) where e.name ='empName'",
};
SelectResults r[][] = new SelectResults[queries.length][2];
//Execute Query without Indexes
for (int i = 0; i < queries.length; i++) {
Query q = null;
try {
q = CacheUtils.getQueryService().newQuery(queries[i]);
CacheUtils.getLogger().info("Executing query: " + queries[i]);
r[i][0] = (SelectResults)q.execute();
} catch (Exception e) {
e.printStackTrace();
fail(q.getQueryString());
}
}
//Create Indexes and Execute Queries
qs.createIndex("nameIndex", IndexType.FUNCTIONAL,"e.name","/employees e");
for (int i = 0; i < queries.length; i++) {
Query q = null;
try {
q = CacheUtils.getQueryService().newQuery(queries[i]);
CacheUtils.getLogger().info("Executing query: " + queries[i]);
QueryObserverImpl observer = new QueryObserverImpl();
QueryObserverHolder.setInstance(observer);
r[i][1] = (SelectResults)q.execute();
if(!observer.isIndexesUsed){
fail("Index is NOT uesd");
}
int indxs = observer.indexesUsed.size();
System.out.println("***********Indexes Used :::: "+indxs+" IndexName::"+observer.IndexName);
if(indxs!=1){
fail("FAILED: The Index should be used. Presently only "+indxs+" Index(es) is used");
}
Iterator itr = observer.indexesUsed.iterator();
String temp;
while(itr.hasNext()){
temp = itr.next().toString();
if(temp.equals("nameIndex")){
break;
}else{
fail("indices used do not match with the which is expected to be used" +
" was expected but found " +itr.next());
}
}
} catch (Exception e) {
e.printStackTrace();
fail(q.getQueryString());
}
}
//Verifying the query results
// StructSetOrResultsSet ssORrs = new StructSetOrResultsSet();
CacheUtils.compareResultsOfWithAndWithoutIndex(r,this );
}
public void _testQueryWithCompositeIter2() throws Exception {
QueryService qs;
qs = CacheUtils.getQueryService();
String queries[] = {
//Test Case No. IUMR
"Select distinct * from /countries c, /employees e, c.citizens[e.empId].arr where e.name='empName'",
};
SelectResults r[][] = new SelectResults[queries.length][2];
//Execute Query without Indexes
for (int i = 0; i < queries.length; i++) {
Query q = null;
try {
q = CacheUtils.getQueryService().newQuery(queries[i]);
CacheUtils.getLogger().info("Executing query: " + queries[i]);
r[i][0] = (SelectResults)q.execute();
System.out.println(Utils.printResult(r[i][0]));
} catch (Exception e) {
e.printStackTrace();
fail(q.getQueryString());
}
}
//Create Indexes and Execute Queries
qs.createIndex("nameIndex", IndexType.FUNCTIONAL,"e.name","/employees e");
for (int i = 0; i < queries.length; i++) {
Query q = null;
try {
q = CacheUtils.getQueryService().newQuery(queries[i]);
CacheUtils.getLogger().info("Executing query: " + queries[i]);
QueryObserverImpl observer = new QueryObserverImpl();
QueryObserverHolder.setInstance(observer);
r[i][1] = (SelectResults)q.execute();
if(!observer.isIndexesUsed){
fail("Index is NOT uesd");
}
int indxs = observer.indexesUsed.size();
System.out.println("***********Indexes Used :::: "+indxs+" IndexName::"+observer.IndexName);
if(indxs!=1){
fail("FAILED: The Index should be used. Presently only "+indxs+" Index(es) is used");
}
Iterator itr = observer.indexesUsed.iterator();
String temp;
while(itr.hasNext()){
temp = itr.next().toString();
if(temp.equals("nameIndex")){
break;
}else{
fail("indices used do not match with the which is expected to be used" +
" was expected but found " +itr.next());
}
}
} catch (Exception e) {
e.printStackTrace();
fail(q.getQueryString());
}
}
//Verifying the query results
CacheUtils.compareResultsOfWithAndWithoutIndex(r,this);
}
class QueryObserverImpl extends QueryObserverAdapter{
boolean isIndexesUsed = false;
ArrayList indexesUsed = new ArrayList();
String IndexName;
public void beforeIndexLookup(Index index, int oper, Object key) {
IndexName = index.getName();
indexesUsed.add(index.getName());
}
public void afterIndexLookup(Collection results) {
if(results != null){
isIndexesUsed = true;
}
}
}
}// end of the class