Best SeLion code snippet using com.paypal.selion.platform.dataprovider.impl.ExcelDataProviderImpl.fetchMatchingCustomType
Source:ExcelDataProviderImpl.java
...417 }418 logger.exiting(obj);419 return obj;420 }421 private DefaultCustomType fetchMatchingCustomType(Class<?> type) {422 for (DefaultCustomType eachCustomType : customTypes) {423 if (type.equals(eachCustomType.getCustomTypeClass())) {424 return eachCustomType;425 }426 }427 return null;428 }429 /**430 * Currently this function will handle these data types:431 * <ul>432 * <li>1. Primitive data type: int, boolean, double, float, long</li>433 * <li>2. Object data type: String, Integer, Double, Float, Long</li>434 * <li>3. Array of primitive data type: int[], boolean[], double[], float[], long[]</li>435 * <li>4. Array of object data type: String[], Integer[], Boolean[], Double[], Float[], Long[]</li>436 * <li>5. User defined data type.</li>437 * <li>6. Array of user defined data type.</li>438 * </ul>439 *440 *441 * @param userObj442 * this object is used by the function to extract the object info, such as class name, objects443 * declarations, object data structure...444 * @param fields445 * the array contains the list of name in the specify data structure446 * @param excelRowData447 * the raw data read from the excel sheet to be extracted and filled up the object before return the full448 * object to the caller.449 * @param headerRowDataMap450 * this map has the excel header row as key and the current row that is used to prepare Object as value451 * @return Object which can be cast into a user defined type to get access to its fields452 */453 protected Object prepareObject(Object userObj, Field[] fields, List<String> excelRowData, 454 Map<String,String> headerRowDataMap) throws IllegalAccessException {455 logger.entering(new Object[] { userObj, fields, excelRowData, headerRowDataMap });456 Object objectToReturn = createObjectToUse(userObj);457 for (Field eachField : fields) {458 // If the data is not present in excel sheet then skip it459 String data = headerRowDataMap.get(eachField.getName().toLowerCase());460 if (StringUtils.isEmpty(data)) {461 continue;462 }463 Class<?> eachFieldType = eachField.getType();464 if (eachFieldType.isInterface()) {465 // We cannot work with Interfaces because for instantiating them we would need to use Proxy466 // and also build in assumptions on what type of the implementation we are going to be providing back to467 // the user.468 // things get complex if the user supplies us with an interface of which we dont have any idea.469 // so lets just throw an error and bail out.470 throw new IllegalArgumentException(eachField.getName()471 + " is an interface. Interfaces are not supported.");472 }473 eachField.setAccessible(true);474 boolean isArray = eachFieldType.isArray();475 DataMemberInformation memberInfo = new DataMemberInformation(eachField, userObj, objectToReturn, data);476 if (isArray) {477 try {478 setValueForArrayType(memberInfo);479 } catch (ArrayIndexOutOfBoundsException | IllegalArgumentException | InstantiationException e) {480 throw new DataProviderException(e.getMessage(), e);481 }482 } else {483 try {484 setValueForNonArrayType(memberInfo);485 } catch (InstantiationException | IllegalArgumentException | InvocationTargetException486 | NoSuchMethodException | SecurityException e) {487 throw new DataProviderException(e.getMessage(), e);488 }489 }490 }491 logger.exiting(objectToReturn);492 return objectToReturn;493 }494 495 496 /*497 * prepares map of excel header row and the the excel data row498 * 499 * @param header the excel header row500 * 501 * @param rowData row data to be used for preparing the return value502 * 503 * @return map of the header row and data row504 */505 private Map<String, String> prepareHeaderRowDataMap(List<String> header, List<String> rowData) {506 Map<String, String> headerRowDataMap = new HashMap<>();507 if (header.size() == rowData.size()) {508 for (int i = 0; i < header.size(); i++) {509 if (null != header.get(i)) {510 headerRowDataMap.put(header.get(i).toLowerCase(), rowData.get(i));511 }512 }513 } else {514 logger.warning("header and columns are not of same size");515 }516 return headerRowDataMap;517 }518 private Object createObjectToUse(Object userObject) throws IllegalAccessException {519 try {520 // Create a new instance of the data so we can521 // store it here before return everything to the users.522 return userObject.getClass().newInstance();523 } catch (InstantiationException e1) {524 String msg = String.format(525 "Unable to instantiate an object of class %s bcoz it doesn't have a default constructor. ",526 userObject.getClass().getCanonicalName());527 throw new DataProviderException(msg, e1);528 }529 }530 /**531 * A utility method that setups up data members which are arrays.532 *533 * @param memberInfo534 * A {@link DataMemberInformation} object that represents values pertaining to every data member.535 * @throws IllegalAccessException536 * @throws ArrayIndexOutOfBoundsException537 * @throws IllegalArgumentException538 * @throws InstantiationException539 */540 private void setValueForArrayType(DataMemberInformation memberInfo) throws IllegalAccessException,541 ArrayIndexOutOfBoundsException, IllegalArgumentException, InstantiationException {542 logger.entering(memberInfo);543 Field eachField = memberInfo.getField();544 Object objectToSetDataInto = memberInfo.getObjectToSetDataInto();545 String data = memberInfo.getDataToUse();546 Class<?> eachFieldType = eachField.getType();547 // We are dealing with arrays548 String[] arrayData = data.split(",");549 Object arrayObject;550 // Check if its an array of primitive data type551 if (ReflectionUtils.isPrimitiveArray(eachFieldType)) {552 arrayObject = ReflectionUtils.instantiatePrimitiveArray(eachFieldType, arrayData);553 eachField.set(objectToSetDataInto, arrayObject);554 logger.exiting();555 return;556 }557 if (ReflectionUtils.isWrapperArray(eachFieldType)558 || ReflectionUtils.hasOneArgStringConstructor(eachFieldType.getComponentType())) {559 // Check if its an array of either Wrapper classes or classes that have a 1 arg string constructor560 arrayObject = ReflectionUtils.instantiateWrapperArray(eachFieldType, arrayData);561 eachField.set(objectToSetDataInto, arrayObject);562 logger.exiting();563 return;564 }565 DefaultCustomType customType = fetchMatchingCustomType(eachFieldType);566 if (customType != null) {567 // Maybe it belongs to one of the custom types568 arrayObject = ReflectionUtils.instantiateDefaultCustomTypeArray(customType, arrayData);569 eachField.set(objectToSetDataInto, arrayObject);570 logger.exiting();571 return;572 }573 // If we are here then it means that the field is a Pojo class that points to another sheet in the excel sheet574 arrayObject = Array.newInstance(eachFieldType.getComponentType(), arrayData.length);575 for (int counter = 0; counter < arrayData.length; counter++) {576 Array.set(arrayObject, counter,577 getSingleExcelRow(eachFieldType.getComponentType().newInstance(), arrayData[counter].trim(), true));578 }579 eachField.set(objectToSetDataInto, arrayObject);580 logger.exiting();581 }582 /**583 * A utility method that setups up data members which are NOT arrays.584 *585 * @param memberInfo586 * A {@link DataMemberInformation} object that represents values pertaining to every data member.587 *588 * @throws IllegalAccessException589 * @throws InstantiationException590 * @throws IllegalArgumentException591 * @throws InvocationTargetException592 * @throws NoSuchMethodException593 * @throws SecurityException594 */595 private void setValueForNonArrayType(DataMemberInformation memberInfo) throws IllegalAccessException,596 InstantiationException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException,597 SecurityException {598 logger.entering(memberInfo);599 Field eachField = memberInfo.getField();600 Class<?> eachFieldType = eachField.getType();601 Object objectToSetDataInto = memberInfo.getObjectToSetDataInto();602 Object userProvidedObject = memberInfo.getUserProvidedObject();603 String data = memberInfo.getDataToUse();604 boolean isPrimitive = eachFieldType.isPrimitive();605 if (isPrimitive) {606 // We found a primitive data type such as int, float, char etc.,607 eachField.set(objectToSetDataInto,608 ReflectionUtils.instantiatePrimitiveObject(eachFieldType, userProvidedObject, data));609 logger.exiting();610 return;611 }612 if (ClassUtils.isPrimitiveWrapper(eachFieldType)) {613 // We found a wrapper data type such as Float, Integer, Character etc.,614 eachField.set(objectToSetDataInto,615 ReflectionUtils.instantiateWrapperObject(eachFieldType, userProvidedObject, data));616 logger.exiting();617 return;618 }619 if (ReflectionUtils.hasOneArgStringConstructor(eachFieldType)) {620 // We found a class that has a 1 arg constructor. String.class is an example for that.621 Object objToSet = eachFieldType.getConstructor(new Class<?>[] { String.class }).newInstance(data);622 eachField.set(objectToSetDataInto, objToSet);623 logger.exiting();624 return;625 }626 DefaultCustomType customType = fetchMatchingCustomType(eachFieldType);627 if (customType != null) {628 // If we are here then it means that the field is one of the predefined custom types that was given to us.629 eachField.set(objectToSetDataInto, customType.instantiateObject(data));630 logger.exiting();631 return;632 }633 // If eventually we land here, then we have found a pojo class given by the user that points to another634 // sheet in the excel sheet.635 eachField.set(objectToSetDataInto, getSingleExcelRow(eachFieldType.newInstance(), data, true));636 logger.exiting();637 }638 /**639 * Using the specified rowIndex to search for the row from the specified Excel sheet, then return the row contents640 * in a list of string format....
fetchMatchingCustomType
Using AI Code Generation
1ExcelDataProviderImpl excelDataProviderImpl = new ExcelDataProviderImpl();2excelDataProviderImpl.setExcelFile("ExcelFile.xlsx");3excelDataProviderImpl.setSheetName("SheetName");4excelDataProviderImpl.setUniqueColumn("UniqueColumn");5excelDataProviderImpl.setKeyColumn("KeyColumn");6excelDataProviderImpl.fetchMatchingCustomType("UniqueColumnValue", "KeyColumnValue");7excelDataProviderImpl.fetchMatchingCustomType("UniqueColumnValue", "KeyColumnValue", "CustomType");8excelDataProviderImpl.fetchMatchingCustomType("UniqueColumnValue", "KeyColumnValue", "CustomType", "CustomType");9excelDataProviderImpl.fetchMatchingCustomType("UniqueColumnValue", "KeyColumnValue", "CustomType", "CustomType", "CustomType");10excelDataProviderImpl.fetchMatchingCustomType("UniqueColumnValue", "KeyColumnValue", "CustomType", "CustomType", "CustomType", "CustomType");11excelDataProviderImpl.fetchMatchingCustomType("UniqueColumnValue", "KeyColumnValue", "CustomType", "CustomType", "CustomType", "CustomType", "CustomType");12excelDataProviderImpl.fetchMatchingCustomType("UniqueColumnValue", "KeyColumnValue", "CustomType", "CustomType", "CustomType", "CustomType", "CustomType", "CustomType");13excelDataProviderImpl.fetchMatchingCustomType("UniqueColumnValue", "KeyColumnValue", "CustomType", "CustomType", "CustomType", "CustomType", "CustomType", "CustomType", "CustomType");14excelDataProviderImpl.fetchMatchingCustomType("UniqueColumnValue", "KeyColumnValue", "CustomType", "CustomType", "CustomType", "CustomType", "CustomType", "CustomType", "CustomType", "CustomType");
fetchMatchingCustomType
Using AI Code Generation
1ExcelDataProviderImpl excelDataProvider = new ExcelDataProviderImpl();2Map<String, String> data = excelDataProvider.fetchMatchingCustomType("testData", "testdata", "key", "value");3System.out.println(data);4JSONDataProviderImpl jsonDataProvider = new JSONDataProviderImpl();5Map<String, String> data = jsonDataProvider.fetchMatchingCustomType("testData", "testdata", "key", "value");6System.out.println(data);7XMLDataProviderImpl xmlDataProvider = new XMLDataProviderImpl();8Map<String, String> data = xmlDataProvider.fetchMatchingCustomType("testData", "testdata", "key", "value");9System.out.println(data);10YAMLDataProviderImpl yamlDataProvider = new YAMLDataProviderImpl();11Map<String, String> data = yamlDataProvider.fetchMatchingCustomType("testData", "testdata", "key", "value");12System.out.println(data);13CSVDataProviderImpl csvDataProvider = new CSVDataProviderImpl();14Map<String, String> data = csvDataProvider.fetchMatchingCustomType("testData", "testdata", "key", "value");15System.out.println(data);16PropertiesDataProviderImpl propertiesDataProvider = new PropertiesDataProviderImpl();17Map<String, String> data = propertiesDataProvider.fetchMatchingCustomType("testData", "testdata", "key", "value");18System.out.println(data);19SQLDataProviderImpl sqlDataProvider = new SQLDataProviderImpl();20Map<String, String> data = sqlDataProvider.fetchMatchingCustomType("testData", "testdata", "key", "value");21System.out.println(data);22XMLDataProviderImpl xmlDataProvider = new XMLDataProviderImpl();23Map<String, String> data = xmlDataProvider.fetchMatchingCustomType("test
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!