Skip to content

Commit

Permalink
Fixed #95 , #96, #98
Browse files Browse the repository at this point in the history
  • Loading branch information
anujgandharv committed Oct 17, 2013
1 parent 15b60a7 commit bd4518e
Show file tree
Hide file tree
Showing 14 changed files with 305 additions and 111 deletions.
12 changes: 6 additions & 6 deletions src/main/java/org/easetech/easytest/annotation/Format.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";

}
22 changes: 21 additions & 1 deletion src/main/java/org/easetech/easytest/annotation/Param.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -120,6 +122,8 @@
*
*/
static class DataSupplier {

private DateTimeFormat dateTimeFormatToUse;


/**
Expand Down Expand Up @@ -148,10 +152,26 @@ public List<PotentialAssignment> getValueSources(String testMethodName, EasyPara
List<Map<String , Object>> 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;
}



Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -76,17 +78,18 @@ public class CollectionConverter implements BaseConverter<List<Map<String, Objec
* @param signature An instance of {@link EasyParamSignature} that contains information about the signature of the test method.
* @param paramName The name of the parameter for which the data is being converted
* @param convertEmptyToNull Whether empty values should be converted to Null values or not
* @param dateTimeFormat the user specified date time format to use
*/
public CollectionConverter(EasyParamSignature signature , String paramName , Boolean convertEmptyToNull ) {
public CollectionConverter(EasyParamSignature signature , String paramName , Boolean convertEmptyToNull , DateTimeFormat dateTimeFormat) {
this.signature = signature;
this.paramName = paramName;
Class<?> 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));
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -73,24 +75,32 @@ public class ConversionDelegator implements BaseConverter<List<Map<String, Objec
* Constant key for specifying collections converter identified by {@link CollectionConverter}
*/
private static final String COLLECTIONS_CONVERTER = "collectionsConverter";

/**
* Whether empty String should be converted to null or not
*/
private final Boolean convertEmptyToNull;

/**
*
* Construct a new ConversionDelegator
*
* @param signature the {@link EasyParamSignature} instance that contains all the information regarding the parameter whose data is currently being converted.
* @param paramName the optional name of the parameter with which to search for the data.
* @param convertEmptyToNull whether an empty string be converted to Null or not
* @param dateTimeFormat User specified date time format
*/
public ConversionDelegator(EasyParamSignature signature, String paramName , Boolean convertEmptyToNull) {
public ConversionDelegator(EasyParamSignature signature, String paramName , Boolean convertEmptyToNull , DateTimeFormat dateTimeFormat) {
this.signature = signature;
this.paramName = paramName;
this.convertEmptyToNull = convertEmptyToNull;
converters.put(MAP_OBJECT_CONVERTER, new MapConverter(signature.getParameterType()));
converters.put(COLLECTIONS_CONVERTER, new CollectionConverter(signature, paramName , convertEmptyToNull));
converters.put(STANDARD_OBJECT_CONVERTER, new StandardObjectConverter(signature.getParameterType(), paramName , convertEmptyToNull));
converters.put(COLLECTIONS_CONVERTER, new CollectionConverter(signature, paramName , convertEmptyToNull , dateTimeFormat));
converters.put(STANDARD_OBJECT_CONVERTER, new StandardObjectConverter(signature.getParameterType(), paramName , convertEmptyToNull , dateTimeFormat));
converters.put(PROPERTY_EDITOR_CONVERTER, new PropertyEditorConverter(signature.getParameterType(), paramName));
converters.put(USER_DEFINED_CONVERTER, new UserDefinedConverter(signature.getParameterType(), paramName));
converters.put(JSON_DATA_CONVERTER, new JSONDataConverter(signature.getParameterType(), paramName));
converters.put(PARAM_CONSTRUCTOR_CONVERTER, new ParamConstructorConverter(signature.getParameterType(), paramName , null , convertEmptyToNull));
converters.put(PARAM_CONSTRUCTOR_CONVERTER, new ParamConstructorConverter(signature.getParameterType(), paramName , null , convertEmptyToNull , dateTimeFormat));

}

Expand All @@ -101,10 +111,12 @@ public ConversionDelegator(EasyParamSignature signature, String paramName , Bool
* @param signature the {@link EasyParamSignature} instance that contains all the information regarding the parameter whose data is currently being converted.
* @param paramName the optional name of the parameter with which to search for the data.
* @param converters The list of converters that a user can specify.
* @param convertEmptyToNull Whether empty String should be converted to null or not
*/
public ConversionDelegator(EasyParamSignature signature, String paramName, LinkedHashMap<String , BaseConverter<List<Map<String, Object>>, List<PotentialAssignment>>> converters) {
public ConversionDelegator(EasyParamSignature signature, String paramName, LinkedHashMap<String , BaseConverter<List<Map<String, Object>>, List<PotentialAssignment>>> converters ,Boolean convertEmptyToNull ) {
this.signature = signature;
this.paramName = paramName;
this.convertEmptyToNull = convertEmptyToNull;
this.converters.putAll(converters);


Expand All @@ -120,9 +132,24 @@ public List<PotentialAssignment> convert(List<Map<String, Object>> convertFrom)
List<PotentialAssignment> potentialAssignments = null;

if (GeneralUtil.dataAlreadyConverted(signature.getParameterType(), convertFrom, paramName)) {

potentialAssignments = new ArrayList<PotentialAssignment>();
Object value = null;
for (Map<String, Object> 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()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -39,6 +41,11 @@ public class ParamConstructorConverter implements BaseConverter<List<Map<String,
*/
private final Boolean convertEmptyToNull;

/**
* the user specified date time format to use
*/
private final DateTimeFormat dateTimeFormat;

/**
* Logger
*/
Expand All @@ -50,12 +57,15 @@ public class ParamConstructorConverter implements BaseConverter<List<Map<String,
* @param parameterType The type of parameter to convert to
* @param paramName The name of the parameter that is being converted
* @param collection The optional collection instance in case of Collection type parameter
* @param convertEmptyToNull whether empty string be converted to null or not
* @param dateTimeFormat the user specified date time format to use
*/
public ParamConstructorConverter(Class<?> 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;
}

/**
Expand All @@ -69,7 +79,7 @@ public List<PotentialAssignment> convert(List<Map<String, Object>> convertFrom)
List<PotentialAssignment> potentialAssignments = new ArrayList<PotentialAssignment>();
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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -40,19 +42,27 @@ public class StandardObjectCollectionConverter implements BaseConverter<List<Map
*/
private final Boolean convertEmptyToNull;

/**
* User specified date time format to use
*/
private final DateTimeFormat dateTimeFormat;

/**
*
* Construct a new StandardObjectCollectionConverter
* @param collection The collection instance in case of Collection type parameter
* @param signature an instance of {@link EasyParamSignature}
* @param paramName The name of the parameter that is being converted
* @param convertEmptyToNull whether empty values be converted to null or not
* @param dateTimeFormat user specified date time format
*/
public StandardObjectCollectionConverter(Collection collection, EasyParamSignature signature, String paramName , Boolean convertEmptyToNull) {
public StandardObjectCollectionConverter(Collection collection, EasyParamSignature signature, String paramName , Boolean convertEmptyToNull , DateTimeFormat dateTimeFormat) {
super();
this.collection = collection;
this.signature = signature;
this.paramName = paramName;
this.convertEmptyToNull = convertEmptyToNull;
this.dateTimeFormat = dateTimeFormat;
}

/**
Expand Down Expand Up @@ -80,7 +90,7 @@ public List<PotentialAssignment> convert(List<Map<String, Object>> convertFrom)
for (Map<String, Object> 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));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -34,6 +36,11 @@ public class StandardObjectConverter implements BaseConverter<List<Map<String, O
*/
private final Boolean convertEmptyToNull;

/**
* The user specified date time format to use
*/
private final DateTimeFormat dateTimeFormat;

/**
* Logger
*/
Expand All @@ -45,11 +52,13 @@ public class StandardObjectConverter implements BaseConverter<List<Map<String, O
* @param parameterType The type of parameter to convert the raw data to
* @param paramName The name of the parameter that is being converted
* @param convertEmptyToNull Whether empty values should be converted to Null values or not
* @param dateTimeFormat The user specified date time format to use
*/
public StandardObjectConverter(Class<?> 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;
}

/**
Expand All @@ -65,7 +74,7 @@ public List<PotentialAssignment> convert(List<Map<String, Object>> convertFrom)
potentialAssignments = new ArrayList<PotentialAssignment>();
for (Map<String, Object> object : convertFrom) {
potentialAssignments.add(PotentialAssignment.forValue(EMPTY_STRING,
GeneralUtil.convertToTargetType(parameterType, object.get(paramName) , convertEmptyToNull)));
GeneralUtil.convertToTargetType(parameterType, object.get(paramName) , convertEmptyToNull, dateTimeFormat)));
}
}

Expand Down
64 changes: 64 additions & 0 deletions src/main/java/org/easetech/easytest/internal/DateTimeFormat.java
Original file line number Diff line number Diff line change
@@ -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 + "]";
}



}
Loading

0 comments on commit bd4518e

Please sign in to comment.