com.gemstone.gemfire.cache.query.functional.IndexOperatorTest 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.
*/
/*
* CompileIndexOperatorTest.java
*
* Created on March 23, 2005, 4:52 PM
*/
package com.gemstone.gemfire.cache.query.functional;
import com.gemstone.gemfire.cache.Region;
import java.util.ArrayList;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import com.gemstone.gemfire.cache.query.*;
import com.gemstone.gemfire.cache.query.data.Portfolio;
import java.util.HashMap;
/**
*
* @author vikramj
*/
public class IndexOperatorTest extends TestCase {
public IndexOperatorTest(String testName) {
super(testName);
}
protected void setUp() throws Exception {
CacheUtils.startCache();
}
protected void tearDown() throws Exception {
CacheUtils.closeCache();
}
public static Test suite(){
TestSuite suite = new TestSuite(IndexOperatorTest.class);
return suite;
}
public void testWithString() throws Exception {
String str = "xyz";
Character c = (Character)runQuery(str, 0);
if(c.charValue() != 'x')
fail();
Character d = (Character)runQuery(str, 2);
if(d.charValue() != 'z')
fail();
}
public void testWithArray() throws Exception {
Object result = null;
int index = 1;
String stringArray[] = {"a","b"};
result = runQuery(stringArray, index);
if(result == null || !stringArray[index].equals(result))
fail("failed for String array");
int intArray[] = {1,2};
result = runQuery(intArray, index);
if(result == null || intArray[index] != ((Integer)result).intValue())
fail("failed for int array");
Object objectArray[] = {"a","b"};
result = runQuery(objectArray, index);
if(result == null || !objectArray[index].equals(result))
fail("failed for String array");
}
public void testWithList() throws Exception {
ArrayList list = new ArrayList();
list.add("aa");
list.add("bb");
Object result = null;
int index = 1;
result = runQuery(list, index);
if(result == null || !list.get(index).equals(result))
fail("failed for List");
}
public void testWithMap() throws Exception {
HashMap map = new HashMap();
map.put("0",new Integer(11));
map.put("1",new Integer(12));
Object result = null;
Object index = "1";
result = runQuery(map, index);
if(result == null || !map.get(index).equals(result))
fail("failed for Map");
}
public void testWithRegion() throws Exception {
Region region = CacheUtils.createRegion("Portfolio", Portfolio.class);
for(int i=0;i<5;i++){
region.put(""+i, new Portfolio(i));
}
Object result = null;
Object index="2";
result=runQuery(region, index);
if(result == null || !region.get(index).equals(result))
fail("failed for Region");
}
public void testIndexOfIndex() throws Exception{
String array[] = { "abc", "def"};
Query q = CacheUtils.getQueryService().newQuery("$1[0][0]");
Object params[] = {array, new Integer(0)};
Character result = (Character)q.execute(params);
System.out.println(Utils.printResult(result));
if(result == null || result.charValue() != 'a')
fail();
}
public void testWithNULL() throws Exception{
runQuery(null, 0);
runQuery(null, null);
Object objectArray[] = {"a","b"};
try{
runQuery(objectArray, null);
fail();
}catch(TypeMismatchException e){
}
HashMap map = new HashMap();
map.put("0",new Integer(11));
map.put("1",new Integer(12));
Object result = runQuery(map, null);
if(result != null)
fail();
}
public void testWithUNDEFINED() throws Exception{
try{
runQuery(QueryService.UNDEFINED, 0);
}catch(TypeMismatchException e){
fail();
}
try{
runQuery(QueryService.UNDEFINED, QueryService.UNDEFINED);
}catch(TypeMismatchException e){
fail();
}
Object objectArray[] = {"a","b"};
try{
runQuery(objectArray, QueryService.UNDEFINED);
fail();
}catch(TypeMismatchException e){
}
HashMap map = new HashMap();
map.put("0",new Integer(11));
map.put("1",new Integer(12));
Object result = runQuery(map, QueryService.UNDEFINED);
if(result != null)
fail();
}
public void testWithUnsupportedArgs() throws Exception{
try{
runQuery("a","a");
fail();
}catch(TypeMismatchException e){
}
try{
runQuery(new Object(), 0);
fail();
}catch(TypeMismatchException e){
}
try{
Object objectArray[] = {"a","b"};
runQuery(objectArray, new Object());
fail();
}catch(TypeMismatchException e){
}
}
public Object runQuery(Object array, Object index) throws Exception{
Query q = CacheUtils.getQueryService().newQuery("$1[$2]");
Object params[] = {array, index};
Object result = q.execute(params);
System.out.println(Utils.printResult(result));
return result;
}
public Object runQuery(Object array, int index) throws Exception{
Query q = CacheUtils.getQueryService().newQuery("$1[$2]");
Object params[] = {array, new Integer(index)};
Object result = q.execute(params);
System.out.println(Utils.printResult(result));
return result;
}
}