Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* Orignal work: Copyright 2015 www.seleniumtests.com
* Modified work: Copyright 2016 www.infotel.com
*
* 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.
*/
package com.seleniumtests.util.helper;
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.log4j.Logger;
import com.seleniumtests.core.Filter;
import com.seleniumtests.core.SeleniumTestsContextManager;
import com.seleniumtests.customexception.DatasetException;
import com.seleniumtests.util.internal.entity.TestEntity;
import com.seleniumtests.util.logging.SeleniumRobotLogger;
public class SpreadSheetHelper {
private static final Logger logger = SeleniumRobotLogger.getLogger(SpreadSheetHelper.class);
private static final Map, Class>> PRIMITIVE_TYPE_MAP = new HashMap<>();
// Setup primitives map
static {
PRIMITIVE_TYPE_MAP.put(Boolean.TYPE, Boolean.class);
PRIMITIVE_TYPE_MAP.put(Byte.TYPE, Byte.class);
PRIMITIVE_TYPE_MAP.put(Character.TYPE, Character.class);
PRIMITIVE_TYPE_MAP.put(Short.TYPE, Short.class);
PRIMITIVE_TYPE_MAP.put(Integer.TYPE, Integer.class);
PRIMITIVE_TYPE_MAP.put(Long.TYPE, Long.class);
PRIMITIVE_TYPE_MAP.put(Float.TYPE, Float.class);
PRIMITIVE_TYPE_MAP.put(Double.TYPE, Double.class);
}
private SpreadSheetHelper() {
}
/**
* Read field value of an Object :
* if fieldClz is an Array : readFieldValue with the class of Array data
* if fieldClz is a parametric class : readFieldValue with the class of the actual argument
*
* @param fieldClz
* @param type
* @param dataMap
* @param combinedFieldName
* @return
* @throws Exception
*/
private static Object readFieldValueObject(final Class> fieldClz, final Type type,
final Map dataMap, final String combinedFieldName) throws Exception {
Object fieldValue = null;
if (fieldClz.isArray()) {
int size = getArraySize(dataMap, combinedFieldName);
if (size > 0) {
fieldValue = Array.newInstance(fieldClz.getComponentType(), size);
for (int j = 0; j < size; j++) {
Array.set(fieldValue, j,
readFieldValue(fieldClz.getComponentType(), combinedFieldName + "." + j, dataMap));
}
}
} else if (fieldClz.isAssignableFrom(java.util.List.class)) {
java.util.ArrayList list = java.util.ArrayList.class.newInstance();
int size = getArraySize(dataMap, combinedFieldName);
if (size > 0) {
fieldValue = list;
Class> itemClz = getListItemType(type);
for (int j = 0; j < size; j++) {
list.add(readFieldValue(itemClz, combinedFieldName + "." + j, dataMap));
}
}
} else if (fieldClz.isAssignableFrom(java.util.Set.class)) {
java.util.Set list = java.util.LinkedHashSet.class.newInstance();
int size = getArraySize(dataMap, combinedFieldName);
if (size > 0) {
fieldValue = list;
Class> itemClz = getListItemType(type);
for (int j = 0; j < size; j++) {
list.add(readFieldValue(itemClz, combinedFieldName + "." + j, dataMap));
}
}
} else {
fieldValue = readFieldValue(fieldClz, combinedFieldName, dataMap);
}
return fieldValue;
}
/**
* Rewrite DPTags to assert they will be correctly used by getDPFilter
*
* @param rowDataMap
*/
protected static void formatDPTags(final Map rowDataMap) {
if (rowDataMap.get(TestEntity.TEST_DP_TAGS) != null) {
String dpTags = rowDataMap.get(TestEntity.TEST_DP_TAGS).toString();
if (dpTags.trim().length() > 0) {
String[] dpTagArray = dpTags.split(",");
String tempDPTags = "";
for (int idx = 0; dpTagArray.length > 0 && idx < dpTagArray.length; idx++) {
tempDPTags = tempDPTags.concat("[" + dpTagArray[idx].trim() + "]");
if (idx != dpTagArray.length - 1) {
tempDPTags = tempDPTags.concat(",");
}
}
rowDataMap.put(TestEntity.TEST_DP_TAGS, tempDPTags);
}
}
}
/**
* The map key has to be written like this : key.digits (digits : String in hexadecimal or octa)
* Return the integer corresponding to digits.
*
* @param map
* @param key
* @return
*/
public static int getArraySize(final Map map, String key) {
int count = 0;
boolean valueFound = false;
String newKey = key.toLowerCase();
for (Entry entry : map.entrySet()) {
String key2 = entry.getKey();
String value2 = (String) entry.getValue();
if (value2 == null || value2.length() == 0) {
continue;
}
key2 = key2.toLowerCase();
if (key2.startsWith(newKey + ".")) {
valueFound = true;
String subst = key2.substring((newKey + ".").length());
String[] ss = subst.split("\\.");
try {
int value = Integer.parseInt(ss[0]);
count = value > count ? value : count;
} catch (NumberFormatException e) {
logger.error(e);
}
}
}
return valueFound ? count + 1 : count;
}
/**
* Reads data from spreadsheet. If sheetName and sheetNumber both are supplied the sheetName takes precedence. Put
* the excel sheet in the same folder as the test case and specify clazz as this.getClass() .
*/
public static synchronized Iterator