blob: 715986329777b94b8e6ef4ef399c84efe4fb4bbb [file] [log] [blame]
/*=========================================================================
* Copyright (c) 2010-2014 Pivotal Software, Inc. All Rights Reserved.
* This product is protected by U.S. and international copyright
* and intellectual property laws. Pivotal products are covered by
* one or more patents listed at http://www.pivotal.io/patents.
*=========================================================================
*/
/*
* Utils.java
*
* Created on March 8, 2005, 4:16 PM
*/
package com.gemstone.gemfire.cache.query;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;
import com.gemstone.gemfire.LogWriter;
import com.gemstone.gemfire.cache.AttributesFactory;
import com.gemstone.gemfire.cache.Cache;
import com.gemstone.gemfire.cache.CacheFactory;
import com.gemstone.gemfire.cache.CacheTransactionManager;
import com.gemstone.gemfire.cache.Region;
import com.gemstone.gemfire.cache.RegionAttributes;
import com.gemstone.gemfire.cache.Scope;
import com.gemstone.gemfire.cache.query.types.CollectionType;
import com.gemstone.gemfire.cache.query.types.ObjectType;
import com.gemstone.gemfire.distributed.DistributedSystem;
import com.gemstone.gemfire.internal.cache.GemFireCacheImpl;
/**
*
* @author vaibhav
* @author asif
*/
public class CacheUtils {
static Properties props = new Properties();
static DistributedSystem ds;
static volatile Cache cache;
static QueryService qs;
static {
try {
init();
}
catch (Exception e) {
e.printStackTrace();
}
}
static void init() throws Exception {
if (GemFireCacheImpl.getInstance() == null) {
props.setProperty("mcast-port", "0");
cache = new CacheFactory(props).create();
} else {
cache = GemFireCacheImpl.getInstance();
}
ds = cache.getDistributedSystem();
qs = cache.getQueryService();
}
public static Cache getCache() {
return cache;
}
public static void startCache() {
try {
if (cache.isClosed()) {
cache = new CacheFactory(props).create();
ds = cache.getDistributedSystem();
qs = cache.getQueryService();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
public static void closeCache() {
try {
if (!cache.isClosed()) {
cache.close();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
public static void restartCache() {
try {
if (!cache.isClosed()) {
cache.close();
}
cache = new CacheFactory(props).create();
ds = cache.getDistributedSystem();
qs = cache.getQueryService();
}
catch (Exception e) {
e.printStackTrace();
}
}
public static Region createRegion(String regionName, Class valueConstraint, Scope scope) {
try {
AttributesFactory attributesFactory = new AttributesFactory();
attributesFactory.setValueConstraint(valueConstraint);
if( scope != null) {
attributesFactory.setScope( scope);
}
RegionAttributes regionAttributes = attributesFactory
.create();
Region region = cache.createRegion(regionName, regionAttributes);
return region;
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static Region createRegion(String regionName,
RegionAttributes regionAttributes, boolean flag) {
try {
Region region = cache.createRegion(regionName, regionAttributes);
return region;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static Region createRegion(String regionName, Class valueConstraint) {
return createRegion(regionName, valueConstraint, null);
}
public static Region createRegion(String regionName, Class valueConstraint,
boolean indexMaintenanceSynchronous) {
try {
AttributesFactory attributesFactory = new AttributesFactory();
attributesFactory.setValueConstraint(valueConstraint);
attributesFactory
.setIndexMaintenanceSynchronous(indexMaintenanceSynchronous);
RegionAttributes regionAttributes = attributesFactory
.create();
Region region = cache.createRegion(regionName, regionAttributes);
return region;
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static Region createRegion(Region parentRegion, String regionName,
Class valueConstraint) {
try {
AttributesFactory attributesFactory = new AttributesFactory();
if (valueConstraint != null)
attributesFactory.setValueConstraint(valueConstraint);
RegionAttributes regionAttributes = attributesFactory
.create();
Region region = parentRegion
.createSubregion(regionName, regionAttributes);
return region;
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static Region getRegion(String regionPath) {
return cache.getRegion(regionPath);
}
public static QueryService getQueryService() {
if (cache.isClosed()) startCache();
return cache.getQueryService();
}
public static LogWriter getLogger() {
if (cache == null) {
return null;
}
return cache.getLogger();
}
public static void log(Object message) {
Cache cache = GemFireCacheImpl.getInstance();
if (cache != null) {
cache.getLogger().fine(message.toString());
}
}
public static CacheTransactionManager getCacheTranxnMgr() {
return cache.getCacheTransactionManager();
}
public static void compareResultsOfWithAndWithoutIndex(SelectResults[][] r,
Object test) {
Set set1 = null;
Set set2 = null;
Iterator itert1 = null;
Iterator itert2 = null;
ObjectType type1, type2;
for (int j = 0; j < r.length; j++) {
CollectionType collType1 = r[j][0].getCollectionType();
CollectionType collType2 = r[j][1].getCollectionType();
type1 = collType1.getElementType();
type2 = collType2.getElementType();
if (collType1.getSimpleClassName().equals(collType2.getSimpleClassName())) {
log("Both SelectResults are of the same Type i.e.--> "
+ collType1);
}
else {
log("Collection type are : " + collType1 + "and "
+ collType2);
fail("FAILED:Select results Collection Type is different in both the cases. CollectionType1="+collType1 + " CollectionType2="+collType2);
}
if (type1.equals(type2)) {
log("Both SelectResults have same element Type i.e.--> "
+ type1);
}
else {
log("Classes are : type1=" + type1.getSimpleClassName() + " type2= "
+ type2.getSimpleClassName());
fail("FAILED:SelectResult Element Type is different in both the cases. Type1="+ type1 + " Type2="+ type2);
}
if (collType1.equals(collType2)) {
log("Both SelectResults are of the same Type i.e.--> "
+ collType1);
}
else {
log("Collections are : " + collType1 + " "
+ collType2);
fail("FAILED:SelectResults Collection Type is different in both the cases. CollType1="+ collType1 + " CollType2="+ collType2);
}
if (r[j][0].size() == r[j][1].size()) {
log("Both SelectResults are of Same Size i.e. Size= "
+ r[j][1].size());
}
else {
fail("FAILED:SelectResults size is different in both the cases. Size1="
+ r[j][0].size() + " Size2 = " + r[j][1].size());
}
set2 = ((r[j][1]).asSet());
set1 = ((r[j][0]).asSet());
// boolean pass = true;
itert1 = set1.iterator();
while (itert1.hasNext()) {
Object p1 = itert1.next();
itert2 = set2.iterator();
boolean exactMatch = false;
while (itert2.hasNext()) {
Object p2 = itert2.next();
if (p1 instanceof Struct) {
Object[] values1 = ((Struct)p1).getFieldValues();
Object[] values2 = ((Struct)p2).getFieldValues();
assertEquals(values1.length, values2.length);
boolean elementEqual = true;
for (int i = 0; i < values1.length; ++i) {
elementEqual = elementEqual
&& ((values1[i] == values2[i]) || values1[i]
.equals(values2[i]));
}
exactMatch = elementEqual;
}
else {
exactMatch = (p2 == p1) || p2.equals(p1);
}
if (exactMatch) {
break;
}
}
if (!exactMatch) {
fail("Atleast one element in the pair of SelectResults supposedly identical, is not equal ");
}
}
}
}
public static boolean compareResultsOfWithAndWithoutIndex(SelectResults[][] r ) {
boolean ok = true;
Set set1 = null;
Set set2 = null;
Iterator itert1 = null;
Iterator itert2 = null;
ObjectType type1, type2;
outer: for (int j = 0; j < r.length; j++) {
CollectionType collType1 = r[j][0].getCollectionType();
CollectionType collType2 = r[j][1].getCollectionType();
type1 = collType1.getElementType();
type2 = collType2.getElementType();
if(collType1.getSimpleClassName().equals(collType2.getSimpleClassName())) {
log("Both SelectResults are of the same Type i.e.--> " +
collType1);
} else {
log("Collection type are : " + collType1 + "and "
+ collType2);
//test.fail("FAILED:Select results Collection Type is different in both the cases. CollectionType1="+collType1 + " CollectionType2="+collType2);
ok = false;
break;
}
if (type1.equals(type2)) {
log("Both SelectResults have same element Type i.e.--> "
+ type1);
} else {
log("Classes are : type1=" + type1.getSimpleClassName() +
" type2= " + type2.getSimpleClassName());
//test.fail("FAILED:SelectResult Element Type is different in both the cases. Type1="+ type1 + " Type2="+ type2);
ok = false;
break;
}
if (collType1.equals(collType2)) {
log("Both SelectResults are of the same Type i.e.--> "
+ collType1);
}
else {
log("Collections are : " + collType1 + " "
+ collType2);
//test.fail("FAILED:SelectResults Collection Type is different in both the cases. CollType1="+ collType1 + " CollType2="+ collType2);
ok = false;
break;
}
if (r[j][0].size() == r[j][1].size()) {
log("Both SelectResults are of Same Size i.e. Size= "
+ r[j][1].size());
}
else {
//test.fail("FAILED:SelectResults size is different in both the cases. Size1=" + r[j][0].size() + " Size2 = " + r[j][1].size());
ok = false;
break;
}
set2 = (((SelectResults)r[j][1]).asSet());
set1 = (((SelectResults)r[j][0]).asSet());
boolean pass = true;
itert1 = set1.iterator();
while (itert1.hasNext()) {
Object p1 = itert1.next();
itert2 = set2.iterator();
boolean exactMatch = false;
while (itert2.hasNext()) {
Object p2 = itert2.next();
if (p1 instanceof Struct) {
Object[] values1 = ((Struct)p1).getFieldValues();
Object[] values2 = ((Struct)p2).getFieldValues();
//test.assertEquals(values1.length, values2.length);
if(values1.length != values2.length) {
ok = false;
break outer;
}
boolean elementEqual = true;
for (int i = 0; i < values1.length; ++i) {
if(values1[i] != null){
elementEqual = elementEqual && ((values1[i] == values2[i]) || values1[i].equals(values2[i]));
} else{
elementEqual = elementEqual && ((values1[i] == values2[i]));
}
}
exactMatch = elementEqual;
}
else {
exactMatch = (p2 == p1) || p2.equals(p1);
}
if (exactMatch) {
break;
}
}
if (!exactMatch) {
//test.fail("Atleast one element in the pair of SelectResults supposedly identical, is not equal ");
ok = false;
break outer;
}
}
}
return ok;
}
}