diff --git a/src/main/java/org/easetech/easytest/annotation/Format.java b/src/main/java/org/easetech/easytest/annotation/Format.java index bde439c..4d57df3 100644 --- a/src/main/java/org/easetech/easytest/annotation/Format.java +++ b/src/main/java/org/easetech/easytest/annotation/Format.java @@ -16,14 +16,14 @@ public @interface Format { /** - * The date format to be used + * The date formats to be used */ - String date() default "dd/MM/yyyy"; + String[] date() default "dd/MM/yyyy"; - /** The date time format to be used*/ - String dateTime() default "dd/MM/yyyy HH:MM:SS"; + /** The date time formats to be used*/ + String[] dateTime() default "dd/MM/yyyy HH:MM:SS"; - /** The time format to be used*/ - String time() default "HH:MM:SS"; + /** The time formats to be used*/ + String[] time() default "HH:MM:SS"; } diff --git a/src/main/java/org/easetech/easytest/annotation/Param.java b/src/main/java/org/easetech/easytest/annotation/Param.java index d94d087..d9c53eb 100644 --- a/src/main/java/org/easetech/easytest/annotation/Param.java +++ b/src/main/java/org/easetech/easytest/annotation/Param.java @@ -1,6 +1,8 @@ package org.easetech.easytest.annotation; +import org.easetech.easytest.internal.DateTimeFormat; + import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @@ -120,6 +122,8 @@ * */ static class DataSupplier { + + private DateTimeFormat dateTimeFormatToUse; /** @@ -148,10 +152,26 @@ public List getValueSources(String testMethodName, EasyPara List> testData = data.get(testMethodName); String paramName = provider != null ? provider.name() : null; Boolean convertEmptyToNull = provider != null ? provider.convertEmptyToNull() : false; - listOfData = new ConversionDelegator(signature , paramName, convertEmptyToNull).convert(testData); + listOfData = new ConversionDelegator(signature , paramName, convertEmptyToNull , dateTimeFormatToUse).convert(testData); return listOfData; } + + + /** + * @return the dateTimeFormatToUse + */ + public DateTimeFormat getDateTimeFormatToUse() { + return dateTimeFormatToUse; + } + + + /** + * @param dateTimeFormatToUse the dateTimeFormatToUse to set + */ + public void setDateTimeFormatToUse(DateTimeFormat dateTimeFormatToUse) { + this.dateTimeFormatToUse = dateTimeFormatToUse; + } diff --git a/src/main/java/org/easetech/easytest/converter/CollectionConverter.java b/src/main/java/org/easetech/easytest/converter/CollectionConverter.java index 78cba38..3983c96 100644 --- a/src/main/java/org/easetech/easytest/converter/CollectionConverter.java +++ b/src/main/java/org/easetech/easytest/converter/CollectionConverter.java @@ -1,5 +1,7 @@ package org.easetech.easytest.converter; +import org.easetech.easytest.internal.DateTimeFormat; + import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.Collection; @@ -76,17 +78,18 @@ public class CollectionConverter implements BaseConverter genericType = signature.getIsGenericParameter() ? signature.getGenericParameterArgType() : Object.class; collection = getCollectionInstance(signature.getParameterType(), genericType); - converters.put(STND_OBJ_COLLECTION_CONVERTER , new StandardObjectCollectionConverter(collection, signature, paramName , convertEmptyToNull)); + converters.put(STND_OBJ_COLLECTION_CONVERTER , new StandardObjectCollectionConverter(collection, signature, paramName , convertEmptyToNull , dateTimeFormat)); converters.put(PROPERTY_EDITOR_COLLECTION_CONVERTER, new PropertyEditorCollectionConverter(signature, paramName, collection)); converters.put(USER_DEFINED_COLLECTION_CONVERTER, new UserDefinedCollectionConverter(signature, paramName, collection)); - converters.put(PARAM_CONSTRUCTOR_CONVERTER, new ParamConstructorConverter(genericType, paramName, collection , convertEmptyToNull)); + converters.put(PARAM_CONSTRUCTOR_CONVERTER, new ParamConstructorConverter(genericType, paramName, collection , convertEmptyToNull , dateTimeFormat)); } /** diff --git a/src/main/java/org/easetech/easytest/converter/ConversionDelegator.java b/src/main/java/org/easetech/easytest/converter/ConversionDelegator.java index 8b4a986..dedd96e 100644 --- a/src/main/java/org/easetech/easytest/converter/ConversionDelegator.java +++ b/src/main/java/org/easetech/easytest/converter/ConversionDelegator.java @@ -1,6 +1,8 @@ package org.easetech.easytest.converter; +import org.easetech.easytest.internal.DateTimeFormat; + import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; @@ -73,6 +75,11 @@ public class ConversionDelegator implements BaseConverter>, List>> converters) { + public ConversionDelegator(EasyParamSignature signature, String paramName, LinkedHashMap>, List>> converters ,Boolean convertEmptyToNull ) { this.signature = signature; this.paramName = paramName; + this.convertEmptyToNull = convertEmptyToNull; this.converters.putAll(converters); @@ -120,9 +132,24 @@ public List convert(List> convertFrom) List potentialAssignments = null; if (GeneralUtil.dataAlreadyConverted(signature.getParameterType(), convertFrom, paramName)) { + potentialAssignments = new ArrayList(); + Object value = null; for (Map object : convertFrom) { - potentialAssignments.add(PotentialAssignment.forValue(EMPTY_STRING, object.get(paramName))); + if(String.class.isAssignableFrom(signature.getParameterType())){ + if(convertEmptyToNull){ + if(object.get(paramName) != null && "".equals(object.get(paramName).toString())) { + potentialAssignments.add(PotentialAssignment.forValue(EMPTY_STRING, value)); + } else { + potentialAssignments.add(PotentialAssignment.forValue(EMPTY_STRING, object.get(paramName))); + } + } else { + potentialAssignments.add(PotentialAssignment.forValue(EMPTY_STRING, object.get(paramName))); + } + } else { + potentialAssignments.add(PotentialAssignment.forValue(EMPTY_STRING, object.get(paramName))); + } + } } else { for(String key : converters.keySet()) { diff --git a/src/main/java/org/easetech/easytest/converter/ParamConstructorConverter.java b/src/main/java/org/easetech/easytest/converter/ParamConstructorConverter.java index c563cae..b83926b 100644 --- a/src/main/java/org/easetech/easytest/converter/ParamConstructorConverter.java +++ b/src/main/java/org/easetech/easytest/converter/ParamConstructorConverter.java @@ -1,5 +1,7 @@ package org.easetech.easytest.converter; +import org.easetech.easytest.internal.DateTimeFormat; + import java.util.ArrayList; import java.util.Collection; import java.util.List; @@ -39,6 +41,11 @@ public class ParamConstructorConverter implements BaseConverter parameterType, String paramName, Collection collection, Boolean convertEmptyToNull) { + public ParamConstructorConverter(Class parameterType, String paramName, Collection collection, Boolean convertEmptyToNull , DateTimeFormat dateTimeFormat) { this.parameterType = parameterType; this.paramName = paramName; this.collection = collection; this.convertEmptyToNull = convertEmptyToNull; + this.dateTimeFormat = dateTimeFormat; } /** @@ -69,7 +79,7 @@ public List convert(List> convertFrom) List potentialAssignments = new ArrayList(); Boolean populated = false; try { - populated = GeneralUtil.fillDataUsingConstructor(parameterType, convertFrom, potentialAssignments, paramName, collection , convertEmptyToNull); + populated = GeneralUtil.fillDataUsingConstructor(parameterType, convertFrom, potentialAssignments, paramName, collection , convertEmptyToNull , dateTimeFormat); } catch (Exception e) { LOG.debug("Exception occured while trying to populate the data by instantiating the parameter object" , e); potentialAssignments = null; diff --git a/src/main/java/org/easetech/easytest/converter/StandardObjectCollectionConverter.java b/src/main/java/org/easetech/easytest/converter/StandardObjectCollectionConverter.java index 3eb1be6..1fbcb97 100644 --- a/src/main/java/org/easetech/easytest/converter/StandardObjectCollectionConverter.java +++ b/src/main/java/org/easetech/easytest/converter/StandardObjectCollectionConverter.java @@ -1,5 +1,7 @@ package org.easetech.easytest.converter; +import org.easetech.easytest.internal.DateTimeFormat; + import java.util.ArrayList; import java.util.Collection; import java.util.List; @@ -40,19 +42,27 @@ public class StandardObjectCollectionConverter implements BaseConverter convert(List> convertFrom) for (Map object : convertFrom) { String[] strValues = ((String) object.get(paramName)).split(COLON); for (int i = 0; i < strValues.length; i++) { - collection.add(GeneralUtil.convertToTargetType(genericType, strValues[i], convertEmptyToNull)); + collection.add(GeneralUtil.convertToTargetType(genericType, strValues[i], convertEmptyToNull , dateTimeFormat)); } potentialAssignments.add(PotentialAssignment.forValue(EMPTY_STRING, collection)); } diff --git a/src/main/java/org/easetech/easytest/converter/StandardObjectConverter.java b/src/main/java/org/easetech/easytest/converter/StandardObjectConverter.java index 5811bba..70aed5f 100644 --- a/src/main/java/org/easetech/easytest/converter/StandardObjectConverter.java +++ b/src/main/java/org/easetech/easytest/converter/StandardObjectConverter.java @@ -1,6 +1,8 @@ package org.easetech.easytest.converter; +import org.easetech.easytest.internal.DateTimeFormat; + import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -34,6 +36,11 @@ public class StandardObjectConverter implements BaseConverter parameterType, String paramName , Boolean convertEmptyToNull) { + public StandardObjectConverter(Class parameterType, String paramName , Boolean convertEmptyToNull , DateTimeFormat dateTimeFormat) { this.parameterType = parameterType; this.paramName = paramName; this.convertEmptyToNull = convertEmptyToNull; + this.dateTimeFormat = dateTimeFormat; } /** @@ -65,7 +74,7 @@ public List convert(List> convertFrom) potentialAssignments = new ArrayList(); for (Map object : convertFrom) { potentialAssignments.add(PotentialAssignment.forValue(EMPTY_STRING, - GeneralUtil.convertToTargetType(parameterType, object.get(paramName) , convertEmptyToNull))); + GeneralUtil.convertToTargetType(parameterType, object.get(paramName) , convertEmptyToNull, dateTimeFormat))); } } diff --git a/src/main/java/org/easetech/easytest/internal/DateTimeFormat.java b/src/main/java/org/easetech/easytest/internal/DateTimeFormat.java new file mode 100644 index 0000000..0a2cbfa --- /dev/null +++ b/src/main/java/org/easetech/easytest/internal/DateTimeFormat.java @@ -0,0 +1,64 @@ +package org.easetech.easytest.internal; + +public class DateTimeFormat { + + private String[] dateFormat = {"dd/MM/yyyy", "dd-MM-yyyy"}; + + private String[] timeFormat = {"HH:MM:SS"}; + + private String[] dateTimeFormat = {"dd/MM/yyyy HH:MM:SS" , "dd-MM-yyyy HH:MM:SS"}; + + /** + * @return the dateFormat + */ + public String[] getDateFormat() { + return dateFormat; + } + + /** + * @param dateFormat the dateFormat to set + */ + public void setDateFormat(String[] dateFormat) { + this.dateFormat = dateFormat; + } + + /** + * @return the timeFormat + */ + public String[] getTimeFormat() { + return timeFormat; + } + + /** + * @param timeFormat the timeFormat to set + */ + public void setTimeFormat(String[] timeFormat) { + this.timeFormat = timeFormat; + } + + /** + * @return the dateTimeFormat + */ + public String[] getDateTimeFormat() { + return dateTimeFormat; + } + + /** + * @param dateTimeFormat the dateTimeFormat to set + */ + public void setDateTimeFormat(String[] dateTimeFormat) { + this.dateTimeFormat = dateTimeFormat; + } + + /** + * @return + */ + @Override + public String toString() { + return "DateTimeFormat [dateFormat=" + dateFormat + ", timeFormat=" + timeFormat + ", dateTimeFormat=" + + dateTimeFormat + "]"; + } + + + +} diff --git a/src/main/java/org/easetech/easytest/internal/EasyAssignments.java b/src/main/java/org/easetech/easytest/internal/EasyAssignments.java index 08f2cde..8d8157a 100644 --- a/src/main/java/org/easetech/easytest/internal/EasyAssignments.java +++ b/src/main/java/org/easetech/easytest/internal/EasyAssignments.java @@ -6,13 +6,16 @@ import java.util.List; import org.easetech.easytest.annotation.Format; import org.easetech.easytest.annotation.Param; +import org.easetech.easytest.annotation.TestPolicy; +import org.easetech.easytest.loader.DataConverter; +import org.easetech.easytest.runner.EasyFrameworkMethod; import org.junit.experimental.theories.PotentialAssignment; import org.junit.experimental.theories.PotentialAssignment.CouldNotGenerateValueException; import org.junit.runners.model.TestClass; /** * - * A internal util class for working with the parameters of a test method. + * An internal util class for working with the parameters of a test method. * This class provides EasyTest the facility to identify the method arguments, identify the DataSupplier * associated with the Test Framework and more. * @@ -32,7 +35,7 @@ public class EasyAssignments { private final List fUnassigned; /** - * Test Class associated with te tgiven test method + * Test Class associated with the given test method */ private final TestClass fClass; @@ -89,26 +92,36 @@ public Object[] getActualValues(int start, int stop, boolean nullsOk) throws Cou return values; } - public List potentialsForNextUnassigned(String testMethodName) throws InstantiationException, + public List potentialsForNextUnassigned(EasyFrameworkMethod testMethod) throws InstantiationException, IllegalAccessException { + String testMethodName = DataConverter.getFullyQualifiedTestName(testMethod.getMethodNameForTestData(), + fClass.getJavaClass()); EasyParamSignature unassigned = nextUnassigned(); - return getSupplier(unassigned).getValueSources(testMethodName , unassigned); + return getSupplier(testMethod).getValueSources(testMethodName , unassigned); } /** * Get the instance of class that provides the functionality to provide Data. * In our case, its always {@link org.easetech.easytest.annotation.Param.DataSupplier} - * @param unassigned + * @param testMethod the test method associated with the assignment * @return {@link org.easetech.easytest.annotation.Param.DataSupplier} * @throws InstantiationException * @throws IllegalAccessException */ - public Param.DataSupplier getSupplier(EasyParamSignature unassigned) throws InstantiationException, + public Param.DataSupplier getSupplier(EasyFrameworkMethod testMethod) throws InstantiationException, IllegalAccessException { Param.DataSupplier supplier = new Param.DataSupplier(); - -// supplier.setDateFormat(getDateFormat()); -// supplier.setDateFormat(getDateTimeFormat()); + DateTimeFormat dateTimeFormat = new DateTimeFormat(); + if(getDateFormat(testMethod) != null) { + dateTimeFormat.setDateFormat(getDateFormat(testMethod)); + } + if(getDateTimeFormat(testMethod) != null) { + dateTimeFormat.setDateTimeFormat(getDateTimeFormat(testMethod)); + } + if(getTimeFormat(testMethod) != null) { + dateTimeFormat.setTimeFormat(getTimeFormat(testMethod)); + } + supplier.setDateTimeFormatToUse(dateTimeFormat); return supplier; } @@ -140,24 +153,47 @@ public Object[] getArgumentStrings(boolean nullsOk) throws CouldNotGenerateValue return values; } - protected String getDateFormat() { - String dateFormat = null; - Format format = fClass.getJavaClass().getAnnotation(Format.class); - if(format != null) { - dateFormat = format.date(); + protected String[] getDateFormat(EasyFrameworkMethod testMethod) { + String[] dateFormat = null; + Format formatToUse = formatToUse(testMethod); + if(formatToUse != null) { + dateFormat = formatToUse.date(); } return dateFormat; } - protected String getDateTimeFormat() { - String dateTimeFormat = null; - Format format = fClass.getJavaClass().getAnnotation(Format.class); - if(format != null) { - dateTimeFormat = format.dateTime(); + protected String[] getTimeFormat(EasyFrameworkMethod testMethod) { + String[] timeFormat = null; + Format formatToUse = formatToUse(testMethod); + if(formatToUse != null) { + timeFormat = formatToUse.date(); + } + return timeFormat; + + } + + protected String[] getDateTimeFormat(EasyFrameworkMethod testMethod) { + String[] dateTimeFormat = null; + Format formatToUse = formatToUse(testMethod); + if(formatToUse != null) { + dateTimeFormat = formatToUse.dateTime(); } return dateTimeFormat; } + + + private Format formatToUse(EasyFrameworkMethod testMethod) { + Format policyLevelFormat = null; + TestPolicy testPolicy = fClass.getJavaClass().getAnnotation(TestPolicy.class); + if(testPolicy != null) { + policyLevelFormat = testPolicy.value().getAnnotation(Format.class); + } + Format classLevelFormat = fClass.getJavaClass().getAnnotation(Format.class); + Format methodLevelFormat = testMethod.getAnnotation(Format.class); + Format formatToUse = methodLevelFormat != null ? methodLevelFormat : classLevelFormat != null ? classLevelFormat : policyLevelFormat; + return formatToUse; + } } diff --git a/src/main/java/org/easetech/easytest/runner/InternalParameterizedStatement.java b/src/main/java/org/easetech/easytest/runner/InternalParameterizedStatement.java index 54588ad..fa07d5e 100644 --- a/src/main/java/org/easetech/easytest/runner/InternalParameterizedStatement.java +++ b/src/main/java/org/easetech/easytest/runner/InternalParameterizedStatement.java @@ -107,8 +107,7 @@ public void evaluate() throws Throwable { */ protected void runWithAssignment(EasyAssignments parameterAssignment) throws Throwable { while (!parameterAssignment.isComplete()) { - List potentialAssignments = parameterAssignment.potentialsForNextUnassigned(DataConverter.getFullyQualifiedTestName(fTestMethod.getMethodNameForTestData(), - getTestClass().getJavaClass())); + List potentialAssignments = parameterAssignment.potentialsForNextUnassigned(fTestMethod); boolean isFirstSetOfArguments = listOfAssignments.isEmpty(); for (int i = 0; i < potentialAssignments.size(); i++) { if (isFirstSetOfArguments) { diff --git a/src/main/java/org/easetech/easytest/util/GeneralUtil.java b/src/main/java/org/easetech/easytest/util/GeneralUtil.java index a0ce25c..8a66c72 100644 --- a/src/main/java/org/easetech/easytest/util/GeneralUtil.java +++ b/src/main/java/org/easetech/easytest/util/GeneralUtil.java @@ -1,6 +1,12 @@ package org.easetech.easytest.util; +import org.apache.commons.lang.ArrayUtils; + +import java.util.Arrays; + +import org.easetech.easytest.internal.DateTimeFormat; + import java.io.File; import java.io.IOException; import java.lang.reflect.Constructor; @@ -34,6 +40,7 @@ * @author gpcmol * */ +//PENDING: Refactor this class to make it easier to use public class GeneralUtil { private static final Logger LOG = LoggerFactory.getLogger(GeneralUtil.class); @@ -156,20 +163,15 @@ public static String getAbsoluteLocation(String location) { /** * Method to convert object to java.sql.Timestamp type It checks the instance of the object is of different datatype * then it gets the value from the object and casts it to required data type. - * - * @param Object object - * @return java.sql.Timestamp converted value. + * @param object The object to convert to SQL Timestamp + * @param dateTimeFormat the date time format to use + * @return converted timestamp value */ - public static Timestamp convertToSQLTimestamp(Object object) { + public static Timestamp convertToSQLTimestamp(Object object , DateTimeFormat dateTimeFormat) { Timestamp timestamp = null; if (object != null && !object.toString().isEmpty()) { - timestamp = new java.sql.Timestamp(convertToUtilDate(object).getTime()); - /* - * if(object instanceof java.util.Date){ timestamp = new Timestamp(((java.util.Date) object).getTime()); } - * else if(object instanceof Double){ timestamp = new Timestamp(((Double) object).longValue()); } else - * if(object instanceof String){ timestamp = new Timestamp(Long.valueOf((String) object)); } - */ + timestamp = new java.sql.Timestamp(convertToUtilDate(object , dateTimeFormat).getTime()); } return timestamp; @@ -178,12 +180,12 @@ public static Timestamp convertToSQLTimestamp(Object object) { /** * Method to convert object to java.util.Date type It checks the instance of the object is of different datatype * then it gets the value from the object and casts it to required data type. - * - * @param Object object + * @param object the object to convert to date + * @param dateTimeFormat the date and time format to use to convert * @return java.util.Date converted value. */ - public static Date convertToUtilDate(Object object) { + public static Date convertToUtilDate(Object object , DateTimeFormat dateTimeFormat) { Date date = null; if (object != null && !object.toString().isEmpty()) { if (object instanceof java.util.Date) { @@ -192,12 +194,14 @@ public static Date convertToUtilDate(Object object) { date = new Date(((Double) object).longValue()); } else if (object instanceof String) { try { - date = DateUtils.parseDate((String) object, new String[] { "dd/MM/yy", "dd/MM/yyyy", "MM/dd/yy", - "MM/dd/yyyy", "dd-MM-yy", "dd-MM-YYYY", "MM-dd-yy", "MM-dd-yyyy", "dd/MM/yy HH:MM:SS", - "dd/MM/yyyy HH:MM:SS", "MM/dd/yy HH:MM:SS", "MM/dd/yyyy HH:MM:SS", "dd-MM-yy HH:MM:SS", - "dd-MM-YYYY HH:MM:SS", "MM-dd-yy HH:MM:SS", "MM-dd-yyyy HH:MM:SS", "HH:MM:SS" }); + String[] formats = (String[]) ArrayUtils.addAll(dateTimeFormat.getDateTimeFormat(), dateTimeFormat.getDateFormat()); + String[] availableFormats = (String[]) ArrayUtils.addAll(formats, dateTimeFormat.getTimeFormat()); + + date = DateUtils.parseDate((String) object, availableFormats); + } catch (ParseException e) { - date = new Date(Long.valueOf((String) object)); + LOG.error("Parse exception occured while trying to convert {} to java util date using formats {}", object , dateTimeFormat); + throw new RuntimeException(e); } } @@ -209,15 +213,15 @@ public static Date convertToUtilDate(Object object) { /** * Method to convert object to java.sql.Date type It checks the instance of the object is of different datatype then * it gets the value from the object and casts it to required data type. - * - * @param Object object + * @param object the object to convert to SQL Date + * @param dateTimeFormat the user specified date time format * @return java.sql.Date converted value. */ - public static java.sql.Date convertToSQLDate(Object object) { + public static java.sql.Date convertToSQLDate(Object object , DateTimeFormat dateTimeFormat) { java.sql.Date sqlDate = null; if (object != null && !object.toString().isEmpty()) { - sqlDate = new java.sql.Date(convertToUtilDate(object).getTime()); + sqlDate = new java.sql.Date(convertToUtilDate(object , dateTimeFormat).getTime()); } @@ -227,12 +231,12 @@ public static java.sql.Date convertToSQLDate(Object object) { /** * Method to convert object to java.sql.Tim type It checks the instance of the object is of different datatype then * it gets the value from the object and casts it to required data type. - * - * @param Object object + * @param object the object to convert to SQL Time + * @param dateTimeFormat the date time format to use * @return java.sql.Tim converted value. */ - public static java.sql.Time convertToSQLTime(Object object) { + public static java.sql.Time convertToSQLTime(Object object , DateTimeFormat dateTimeFormat) { java.sql.Time time = null; if (object != null && !object.toString().isEmpty()) { @@ -243,12 +247,14 @@ public static java.sql.Time convertToSQLTime(Object object) { } else if (object instanceof String) { Date date; try { - date = DateUtils.parseDate((String) object, new String[] { "HH:MM:SS" }); + date = DateUtils.parseDate((String) object, dateTimeFormat.getTimeFormat()); } catch (ParseException e) { + LOG.debug("Parse Exception occured while trying to convert to SQL TimeStamp. " + + "The object to convert to : {} and the fomat used to convert to SQL Time : {}" , + object, dateTimeFormat.getTimeFormat()); date = new Date(Long.valueOf((String) object)); } time = new java.sql.Time(date.getTime()); - ; } } @@ -547,9 +553,9 @@ public static Boolean dataAlreadyConverted(Class parameterType , List idClass, Object object , Boolean convertEmptyToNull) { + public static Object convertToTargetType(Class idClass, Object object , Boolean convertEmptyToNull , DateTimeFormat dateTimeFormat) { Object returnObj = null; if (object == null || NULL_STR.equals(object.toString())) { return null; @@ -562,13 +568,13 @@ public static Object convertToTargetType(Class idClass, Object object , Boole if (String.class.isAssignableFrom(idClass)) { returnObj = convertToString(object); } else if (Timestamp.class.isAssignableFrom(idClass)) { - returnObj = convertToSQLTimestamp(object); + returnObj = convertToSQLTimestamp(object , dateTimeFormat); } else if (Time.class.isAssignableFrom(idClass)) { - returnObj = GeneralUtil.convertToSQLTime(object); + returnObj = GeneralUtil.convertToSQLTime(object , dateTimeFormat); } else if (java.sql.Date.class.isAssignableFrom(idClass)) { - returnObj = convertToSQLDate(object); + returnObj = convertToSQLDate(object , dateTimeFormat); } else if (Date.class.isAssignableFrom(idClass)) { - returnObj = convertToUtilDate(object); + returnObj = convertToUtilDate(object , dateTimeFormat); } else if (Double.class.isAssignableFrom(idClass) || double.class.isAssignableFrom(idClass)) { returnObj = convertToDouble(object); } else if (Float.class.isAssignableFrom(idClass) || float.class.isAssignableFrom(idClass)) { @@ -670,46 +676,46 @@ public static Boolean isJSON(String expression) { } public static Boolean fillDataUsingConstructor(Class idClass, List> convertFrom, - List finalData, String paramName, Collection collectionInstance , Boolean convertEmptyToNull) + List finalData, String paramName, Collection collectionInstance , Boolean convertEmptyToNull , DateTimeFormat dateTimeFormat) throws IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException { // See if the parameter class has a constructor that we can use. Constructor constructor = getConstructor(idClass, Long.class) == null ? getConstructor(idClass, long.class) : getConstructor(idClass, Long.class); if (constructor != null) { - fill(idClass, paramName, constructor, finalData, convertFrom, Long.class, collectionInstance , convertEmptyToNull); + fill(idClass, paramName, constructor, finalData, convertFrom, Long.class, collectionInstance , convertEmptyToNull , dateTimeFormat); } else if ((constructor = getConstructor(idClass, String.class)) != null) { - fill(idClass, paramName, constructor, finalData, convertFrom, String.class, collectionInstance , convertEmptyToNull); + fill(idClass, paramName, constructor, finalData, convertFrom, String.class, collectionInstance , convertEmptyToNull , dateTimeFormat); } else if ((constructor = getConstructor(idClass, Integer.class) == null ? getConstructor(idClass, int.class) : getConstructor(idClass, Integer.class)) != null) { - fill(idClass, paramName, constructor, finalData, convertFrom, Integer.class, collectionInstance, convertEmptyToNull); + fill(idClass, paramName, constructor, finalData, convertFrom, Integer.class, collectionInstance, convertEmptyToNull , dateTimeFormat); } else if ((constructor = getConstructor(idClass, Byte.class) == null ? getConstructor(idClass, byte.class) : getConstructor(idClass, Byte.class)) != null) { - fill(idClass, paramName, constructor, finalData, convertFrom, Byte.class, collectionInstance, convertEmptyToNull); + fill(idClass, paramName, constructor, finalData, convertFrom, Byte.class, collectionInstance, convertEmptyToNull, dateTimeFormat); } else if ((constructor = getConstructor(idClass, Character.class) == null ? getConstructor(idClass, char.class) : getConstructor(idClass, Character.class)) != null) { - fill(idClass, paramName, constructor, finalData, convertFrom, Character.class, collectionInstance, convertEmptyToNull); + fill(idClass, paramName, constructor, finalData, convertFrom, Character.class, collectionInstance, convertEmptyToNull, dateTimeFormat); } else if ((constructor = getConstructor(idClass, Date.class)) != null) { - fill(idClass, paramName, constructor, finalData, convertFrom, Date.class, collectionInstance, convertEmptyToNull); + fill(idClass, paramName, constructor, finalData, convertFrom, Date.class, collectionInstance, convertEmptyToNull, dateTimeFormat); } else if ((constructor = getConstructor(idClass, java.util.Date.class)) != null) { - fill(idClass, paramName, constructor, finalData, convertFrom, java.util.Date.class, collectionInstance, convertEmptyToNull); + fill(idClass, paramName, constructor, finalData, convertFrom, java.util.Date.class, collectionInstance, convertEmptyToNull, dateTimeFormat); } else if ((constructor = getConstructor(idClass, Timestamp.class)) != null) { - fill(idClass, paramName, constructor, finalData, convertFrom, Timestamp.class, collectionInstance, convertEmptyToNull); + fill(idClass, paramName, constructor, finalData, convertFrom, Timestamp.class, collectionInstance, convertEmptyToNull, dateTimeFormat); } else if ((constructor = getConstructor(idClass, Time.class)) != null) { - fill(idClass, paramName, constructor, finalData, convertFrom, Time.class, collectionInstance, convertEmptyToNull); + fill(idClass, paramName, constructor, finalData, convertFrom, Time.class, collectionInstance, convertEmptyToNull, dateTimeFormat); } else if ((constructor = getConstructor(idClass, Double.class) == null ? getConstructor(idClass, double.class) : getConstructor(idClass, Double.class)) != null) { - fill(idClass, paramName, constructor, finalData, convertFrom, Double.class, collectionInstance, convertEmptyToNull); + fill(idClass, paramName, constructor, finalData, convertFrom, Double.class, collectionInstance, convertEmptyToNull, dateTimeFormat); } else if ((constructor = getConstructor(idClass, Float.class) == null ? getConstructor(idClass, float.class) : getConstructor(idClass, Float.class)) != null) { - fill(idClass, paramName, constructor, finalData, convertFrom, Float.class, collectionInstance, convertEmptyToNull); + fill(idClass, paramName, constructor, finalData, convertFrom, Float.class, collectionInstance, convertEmptyToNull, dateTimeFormat); } else if ((constructor = getConstructor(idClass, Boolean.class) == null ? getConstructor(idClass, boolean.class) : getConstructor(idClass, Boolean.class)) != null) { - fill(idClass, paramName, constructor, finalData, convertFrom, Boolean.class, collectionInstance, convertEmptyToNull); + fill(idClass, paramName, constructor, finalData, convertFrom, Boolean.class, collectionInstance, convertEmptyToNull, dateTimeFormat); } else if ((constructor = getConstructor(idClass, Short.class) == null ? getConstructor(idClass, short.class) : getConstructor(idClass, Boolean.class)) != null) { - fill(idClass, paramName, constructor, finalData, convertFrom, Short.class, collectionInstance, convertEmptyToNull); + fill(idClass, paramName, constructor, finalData, convertFrom, Short.class, collectionInstance, convertEmptyToNull, dateTimeFormat); } else if ((constructor = getConstructor(idClass, Enum.class)) != null) { - fill(idClass, paramName, constructor, finalData, convertFrom, Enum.class, collectionInstance, convertEmptyToNull); + fill(idClass, paramName, constructor, finalData, convertFrom, Enum.class, collectionInstance, convertEmptyToNull, dateTimeFormat); } else { return false; } @@ -720,16 +726,16 @@ public static Boolean fillDataUsingConstructor(Class idClass, List Boolean fill(Class idClass, String paramName, Constructor constructor, List finalData, List> convertFrom, Class argType, - Collection collectionInstance , Boolean convertEmptyToNull) throws IllegalArgumentException, InstantiationException, IllegalAccessException, + Collection collectionInstance , Boolean convertEmptyToNull , DateTimeFormat dateTimeFormat) throws IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException { if (GeneralUtil.isStandardObjectInstance(argType)) { for (Map object : convertFrom) { T target = null; Object result = null; if (collectionInstance != null) { - fillCollectionData(idClass, object, paramName, constructor, finalData, argType, collectionInstance, convertEmptyToNull); + fillCollectionData(idClass, object, paramName, constructor, finalData, argType, collectionInstance, convertEmptyToNull , dateTimeFormat); } else { - fillData(idClass, object, paramName, constructor, finalData, argType, convertEmptyToNull); + fillData(idClass, object, paramName, constructor, finalData, argType, convertEmptyToNull , dateTimeFormat); } } @@ -742,7 +748,7 @@ private static Boolean fill(Class idClass, String paramName, Constructor con @SuppressWarnings("unchecked") private static void fillCollectionData(Class idClass, Map object, String paramName, - Constructor constructor, List finalData, Class argType, Collection collectionInstance, Boolean convertEmptyToNull) + Constructor constructor, List finalData, Class argType, Collection collectionInstance, Boolean convertEmptyToNull , DateTimeFormat dateTimeFormat) throws IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException { Object result = null; T target = null; @@ -750,7 +756,7 @@ private static void fillCollectionData(Class idClass, Map if (paramName != null && !EMPTY_STRING.equals(paramName)) { String[] strValues = ((String) object.get(paramName)).split(COLON); for (int i = 0; i < strValues.length; i++) { - target = (T) GeneralUtil.convertToTargetType(argType, strValues[i], convertEmptyToNull); + target = (T) GeneralUtil.convertToTargetType(argType, strValues[i], convertEmptyToNull , dateTimeFormat); result = constructor.newInstance(target); collectionInstance.add(result); } @@ -758,7 +764,7 @@ private static void fillCollectionData(Class idClass, Map } else { String[] strValues = ((String) object.get(idClass.getSimpleName())).split(COLON); for (int i = 0; i < strValues.length; i++) { - target = (T) GeneralUtil.convertToTargetType(argType, strValues[i], convertEmptyToNull); + target = (T) GeneralUtil.convertToTargetType(argType, strValues[i], convertEmptyToNull, dateTimeFormat); result = constructor.newInstance(target); collectionInstance.add(result); } @@ -768,18 +774,18 @@ private static void fillCollectionData(Class idClass, Map @SuppressWarnings("unchecked") private static void fillData(Class idClass, Map object, String paramName, - Constructor constructor, List finalData, Class argType, Boolean convertEmptyToNull) + Constructor constructor, List finalData, Class argType, Boolean convertEmptyToNull , DateTimeFormat dateTimeFormat) throws IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException { Object result = null; T target = null; if (paramName != null && !EMPTY_STRING.equals(paramName)) { - target = (T) GeneralUtil.convertToTargetType(argType, object.get(paramName), convertEmptyToNull); + target = (T) GeneralUtil.convertToTargetType(argType, object.get(paramName), convertEmptyToNull, dateTimeFormat); result = constructor.newInstance(target); finalData.add(PotentialAssignment.forValue(EMPTY_STRING, result)); } else { result = constructor.newInstance((T) GeneralUtil.convertToTargetType(argType, - object.get(idClass.getSimpleName()), convertEmptyToNull)); + object.get(idClass.getSimpleName()), convertEmptyToNull, dateTimeFormat)); finalData.add(PotentialAssignment.forValue(EMPTY_STRING, result)); } } diff --git a/src/test/java/org/easetech/easytest/example/TestDates.java b/src/test/java/org/easetech/easytest/example/TestDates.java index c2f5b00..f100b8f 100644 --- a/src/test/java/org/easetech/easytest/example/TestDates.java +++ b/src/test/java/org/easetech/easytest/example/TestDates.java @@ -1,28 +1,28 @@ package org.easetech.easytest.example; -import java.util.Date; -import junit.framework.Assert; -import org.easetech.easytest.annotation.DataLoader; +import java.sql.Date; import org.easetech.easytest.annotation.Param; +import org.easetech.easytest.annotation.TestPolicy; import org.easetech.easytest.runner.DataDrivenTestRunner; +import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; @RunWith(DataDrivenTestRunner.class) -@DataLoader(filePaths={"paramTestConditions.csv"}) +@TestPolicy(TestDatesPolicy.class) public class TestDates { - @Test + @Test public void testDatesFormating(@Param(name="date") Date date){ Assert.assertNotNull(date); System.out.println("testDatesFormating : "+date); } @Test - public void testEmptyInteger(@Param(name="day") Integer day , @Param(name="str")String str , @Param(name="long")Long longVal, @Param(name="Double")Double doubleVal){ - System.out.print("day = " + day); - System.out.print(" str = " + str); - System.out.print(" long = " + longVal); - System.out.println(" Double = " + doubleVal); + public void testEmptyInteger(@Param(name="day") Integer day , @Param(name="str", convertEmptyToNull=true)String str , @Param(name="long")Long longVal, @Param(name="Double")Double doubleVal){ + System.out.print("day = " + day + ","); + System.out.print("str = " + str + ","); + System.out.print("long = " + longVal + ","); + System.out.println("Double = " + doubleVal); } } diff --git a/src/test/java/org/easetech/easytest/example/TestDatesPolicy.java b/src/test/java/org/easetech/easytest/example/TestDatesPolicy.java new file mode 100644 index 0000000..138eac9 --- /dev/null +++ b/src/test/java/org/easetech/easytest/example/TestDatesPolicy.java @@ -0,0 +1,10 @@ +package org.easetech.easytest.example; + +import org.easetech.easytest.annotation.DataLoader; +import org.easetech.easytest.annotation.Format; + +@DataLoader(filePaths={"paramTestConditions.csv"} , writeData=false) +@Format(date={"dd-MM-yyyy", "dd/MM/yy"}) +public class TestDatesPolicy { + +} diff --git a/src/test/resources/paramTestConditions.csv b/src/test/resources/paramTestConditions.csv index a99f990..5f64907 100644 --- a/src/test/resources/paramTestConditions.csv +++ b/src/test/resources/paramTestConditions.csv @@ -1 +1 @@ -testNonGenericListInterface,items , 1:2:3 ,4:05:06 testGenericListInterface,items ,11:22:33 ,44:55:66 testGenericListImplementation,items ,111:222:333 ,444:555:666 testNonGenericListImplementation,items ,112:223:334 ,445:556:667 testGenericSetInterface,dates ,10/12/2012:11/12/2013 ,11/12/2012:11/12/2013 ,12/12/2012:11/12/2013 testNonGenericSetInterface,items ,1.2:2.3:3.4 testNonGenericSetImplementation,items ,3.4:4.5:5.6 testGenericSetImplementation,items ,90:100:200 testGenericQueueInterface,items , 1:2:35 testNonGenericQueueImplementation,items ,34:00:09 testGenericQueueImplementation,items , 20:30:40 testNonGenericQueueInterface,items , 1:2:3 testNonGenericBlockingQueueImplementation,items , 45:90:57 testNonGenericDequeImplementation,items , 999:222:1899 testAbstractCollection,items ,0.066319444 testAbstractList,items ,0.066319444 testAbstractQueue,items , 20:30:40 testAbstractSequentialList,items ,12:23:34 testAbstractSet,items ,90:100:200 testArrayBlockingQueue,items ,12:2.3:3.4 testArrayDeque,items ,34:00:09 testArrayList,items , 20:30:40 testAttributeList,items , 999:222:1899 testConcurrentLinkedQueue,items , 45:90:57 testConcurrentSkipListSet,items , 1:2:35 testCopyOnWriteArrayList,items ,34:45:56 testCopyOnWriteArraySet,items , 20:30:40 testEnumSet,items ,Monday:Wednesday:Sunday testLinkedBlockingDeque,items , 999:222:1899 testLinkedBlockingQueue,items , 1:2:3 testLinkedHashSet,items ,34:45:56 testPriorityBlockingQueue,items ,34:45:56 testPriorityQueue,items ,90:100:200 testRoleList,items , 999:222:1899 testRoleUnresolvedList,items , 20:30:40 testStack,items ,34:45:56 testSynchronousQueue,items ,1 testVector,items ,34:45:56 testDatesFormating,date ,20/12/2012 ,31-12-2013 ,1/12/13 testEmptyInteger,"day,str,long,Double" ,",,," ,"null,null,null,null" ,"1,,null,2" ,"null,,," \ No newline at end of file +testNonGenericListInterface,items,,, , 1:2:3,,, ,4:05:06,,, testGenericListInterface,items,,, ,11:22:33,,, ,44:55:66,,, testGenericListImplementation,items,,, ,111:222:333,,, ,444:555:666,,, testNonGenericListImplementation,items,,, ,112:223:334,,, ,445:556:667,,, testGenericSetInterface,dates,,, ,10/12/2012:11/12/2013,,, ,11/12/2012:11/12/2013,,, ,12/12/2012:11/12/2013,,, testNonGenericSetInterface,items,,, ,1.2:2.3:3.4,,, testNonGenericSetImplementation,items,,, ,3.4:4.5:5.6,,, testGenericSetImplementation,items,,, ,90:100:200,,, testGenericQueueInterface,items,,, , 1:2:35,,, testNonGenericQueueImplementation,items,,, ,34:00:09,,, testGenericQueueImplementation,items,,, , 20:30:40,,, testNonGenericQueueInterface,items,,, , 1:2:3,,, testNonGenericBlockingQueueImplementation,items,,, , 45:90:57,,, testNonGenericDequeImplementation,items,,, , 999:222:1899,,, testAbstractCollection,items,,, ,0.066319444,,, testAbstractList,items,,, ,0.066319444,,, testAbstractQueue,items,,, , 20:30:40,,, testAbstractSequentialList,items,,, ,12:23:34,,, testAbstractSet,items,,, ,90:100:200,,, testArrayBlockingQueue,items,,, ,12:2.3:3.4,,, testArrayDeque,items,,, ,34:00:09,,, testArrayList,items,,, , 20:30:40,,, testAttributeList,items,,, , 999:222:1899,,, testConcurrentLinkedQueue,items,,, , 45:90:57,,, testConcurrentSkipListSet,items,,, , 1:2:35,,, testCopyOnWriteArrayList,items,,, ,34:45:56,,, testCopyOnWriteArraySet,items,,, , 20:30:40,,, testEnumSet,items,,, ,Monday:Wednesday:Sunday,,, testLinkedBlockingDeque,items,,, , 999:222:1899,,, testLinkedBlockingQueue,items,,, , 1:2:3,,, testLinkedHashSet,items,,, ,34:45:56,,, testPriorityBlockingQueue,items,,, ,34:45:56,,, testPriorityQueue,items,,, ,90:100:200,,, testRoleList,items,,, , 999:222:1899,,, testRoleUnresolvedList,items,,, , 20:30:40,,, testStack,items,,, ,34:45:56,,, testSynchronousQueue,items,,, ,1,,, testVector,items,,, ,34:45:56,,, testDatesFormating,date,,, ,20/12/2012,,, ,31-12-2013,,, ,1/12/13,,, testEmptyInteger,day,str,long,Double ,,,, ,null,null,null,null ,1,,null,2 ,null,,, \ No newline at end of file