@Test public void testValidate() { System.out.println("validate"); IncomingMessageFormParameter param = new IncomingMessageFormParameterImpl(); param.setValue("2010.12.10"); param.setMessageFormParamStatus(IncMessageFormParameterStatus.NEW); param.setIncomingMsgFormParamDefinition(new IncomingMessageFormParameterDefinitionImpl()); param.getIncomingMsgFormParamDefinition().setParamType("DATE"); ParamExpressionValidator instance = new ParamExpressionValidator(); instance.setExpression("(0[1-9]|[12][0-9]|3[01])[\\-/. ]?(0[1-9]|1[012])[\\-/. ]?(19|20)?\\d\\d"); instance.setDateFormat("dd/MM/yyyy"); DateFormatRegexMap rfrm = new DateFormatRegexMap(); rfrm.setDateFormatRegexMap(new HashMap<String, String>()); rfrm.getDateFormatRegexMap().put("(0[1-9]|[12][0-9]|3[01])/(0[1-9]|1[012])/(19|20)?\\d\\d", "dd/MM/yy"); rfrm.getDateFormatRegexMap().put("\\d+", "ddMMyy"); rfrm.getDateFormatRegexMap().put("(0[1-9]|[12][0-9]|3[01])-(0[1-9]|1[012])-(19|20)?\\d\\d", "dd-MM-yy"); rfrm.getDateFormatRegexMap().put("(0[1-9]|[12][0-9]|3[01]).(0[1-9]|1[012]).(19|20)?\\d\\d", "dd.MM.yy"); rfrm.getDateFormatRegexMap().put("(0[1-9]|[12][0-9]|3[01]) (0[1-9]|1[012]) (19|20)?\\d\\d", "dd MM yy"); rfrm.getDateFormatRegexMap().put("(0[1-9]|[12][0-9]|3[01])-(0[1-9]|1[012])-(19|20)\\d\\d (0[0-9]|1[0-2]|2[0-4]):([0-5][0-9]):([0-5][0-9]) [A|P]M$", "yyyy-MM-dd hh:mm:ss a"); instance.setDateFormateExpMap(rfrm); instance.setDefaultDateFormat("yyyy-MM-dd hh:mm:ss a"); boolean expResult = false; boolean result = instance.validate(param); assertEquals(expResult, result); assertEquals(param.getMessageFormParamStatus(), IncMessageFormParameterStatus.INVALID); assertEquals(param.getErrCode(), 1); assertEquals(param.getErrText(), "wrong format"); param.setMessageFormParamStatus(IncMessageFormParameterStatus.NEW); param.setValue("10|02|1998"); expResult = false; result = instance.validate(param); assertEquals(expResult, result); assertEquals(param.getMessageFormParamStatus(), IncMessageFormParameterStatus.INVALID); assertEquals(param.getErrCode(), 1); assertEquals(param.getErrText(), "wrong format"); param.setMessageFormParamStatus(IncMessageFormParameterStatus.NEW); param.setValue("10.12.2010"); expResult = true; result = instance.validate(param); assertEquals(expResult, result); assertEquals(param.getMessageFormParamStatus(), IncMessageFormParameterStatus.VALID); param.setMessageFormParamStatus(IncMessageFormParameterStatus.NEW); param.setValue("10-01-10"); expResult = true; result = instance.validate(param); assertEquals(expResult, result); assertEquals(param.getMessageFormParamStatus(), IncMessageFormParameterStatus.VALID); param.setMessageFormParamStatus(IncMessageFormParameterStatus.NEW); param.setValue("10012010"); expResult = true; result = instance.validate(param); assertEquals(expResult, result); assertEquals(param.getMessageFormParamStatus(), IncMessageFormParameterStatus.VALID); param.setValue("2010-12-10 15:04:00"); param.setMessageFormParamStatus(IncMessageFormParameterStatus.NEW); param.getIncomingMsgFormParamDefinition().setParamType("DATETIME"); instance.setDateFormat("y-M-d H:m:s"); expResult = true; result = instance.validate(param); assertEquals(expResult, result); assertEquals(param.getMessageFormParamStatus(), IncMessageFormParameterStatus.VALID); }
public boolean validate(IncomingMessageFormParameter param) { if(param.getMessageFormParamStatus() == IncMessageFormParameterStatus.INVALID) return false; String paramType = param.getIncomingMsgFormParamDefinition().getParamType().toUpperCase(); if(paramType.indexOf("TIME") >= 0){ try{ SimpleDateFormat dFormat = new SimpleDateFormat(dateFormat); dFormat.setLenient(true); Date val = dFormat.parse(param.getValue()); dFormat.applyPattern(defaultDateFormat); param.setValue(dFormat.format(val)); param.setMessageFormParamStatus(IncMessageFormParameterStatus.VALID); }catch (ParseException ex) { logger.error("Invalid datetime format - " + param.getValue(), ex); param.setErrCode(1); param.setErrText("wrong format"); param.setMessageFormParamStatus(IncMessageFormParameterStatus.INVALID); } }else if (!Pattern.matches(expression, param.getValue().trim())) { param.setErrCode(1); param.setErrText("wrong format"); param.setMessageFormParamStatus(IncMessageFormParameterStatus.INVALID); } else { param.setMessageFormParamStatus(IncMessageFormParameterStatus.VALID); if (paramType.indexOf("DATE") >= 0) { try { String dateInputFormat = ""; for (String regex : dateFormateExpMap.getDateFormatRegexMap().keySet()){ boolean match = Pattern.matches(regex, param.getValue()); if (match){ dateInputFormat = dateFormateExpMap.getDateFormatRegexMap().get(regex); break; } } SimpleDateFormat dFormat = new SimpleDateFormat(dateInputFormat); dFormat.setLenient(true); Date val = dFormat.parse(param.getValue()); if (paramType.equalsIgnoreCase("DATE") && val.after(new Date())) { param.setErrCode(1); param.setErrText("invalid date"); param.setMessageFormParamStatus(IncMessageFormParameterStatus.INVALID); } else if (paramType.equalsIgnoreCase("DELIVERYDATE")) { long timeDiff = val.getTime() - new Date().getTime(); long dayDiff = timeDiff / (1000 * 60 * 60 * 24); if(val.before(new Date()) || (dayDiff > 280)){ param.setErrCode(1); param.setErrText("invalid date"); param.setMessageFormParamStatus(IncMessageFormParameterStatus.INVALID); } } dFormat.applyPattern(defaultDateFormat); param.setValue(dFormat.format(val)); } catch (ParseException ex) { logger.error("Invalid date format - " + param.getValue(), ex); param.setErrCode(1); param.setErrText("wrong format"); param.setMessageFormParamStatus(IncMessageFormParameterStatus.INVALID); } } } param.setLastModified(new Date()); return param.getMessageFormParamStatus().equals(IncMessageFormParameterStatus.VALID); }
ParamExpressionValidator implements IncomingMessageFormParameterValidator { public boolean validate(IncomingMessageFormParameter param) { if(param.getMessageFormParamStatus() == IncMessageFormParameterStatus.INVALID) return false; String paramType = param.getIncomingMsgFormParamDefinition().getParamType().toUpperCase(); if(paramType.indexOf("TIME") >= 0){ try{ SimpleDateFormat dFormat = new SimpleDateFormat(dateFormat); dFormat.setLenient(true); Date val = dFormat.parse(param.getValue()); dFormat.applyPattern(defaultDateFormat); param.setValue(dFormat.format(val)); param.setMessageFormParamStatus(IncMessageFormParameterStatus.VALID); }catch (ParseException ex) { logger.error("Invalid datetime format - " + param.getValue(), ex); param.setErrCode(1); param.setErrText("wrong format"); param.setMessageFormParamStatus(IncMessageFormParameterStatus.INVALID); } }else if (!Pattern.matches(expression, param.getValue().trim())) { param.setErrCode(1); param.setErrText("wrong format"); param.setMessageFormParamStatus(IncMessageFormParameterStatus.INVALID); } else { param.setMessageFormParamStatus(IncMessageFormParameterStatus.VALID); if (paramType.indexOf("DATE") >= 0) { try { String dateInputFormat = ""; for (String regex : dateFormateExpMap.getDateFormatRegexMap().keySet()){ boolean match = Pattern.matches(regex, param.getValue()); if (match){ dateInputFormat = dateFormateExpMap.getDateFormatRegexMap().get(regex); break; } } SimpleDateFormat dFormat = new SimpleDateFormat(dateInputFormat); dFormat.setLenient(true); Date val = dFormat.parse(param.getValue()); if (paramType.equalsIgnoreCase("DATE") && val.after(new Date())) { param.setErrCode(1); param.setErrText("invalid date"); param.setMessageFormParamStatus(IncMessageFormParameterStatus.INVALID); } else if (paramType.equalsIgnoreCase("DELIVERYDATE")) { long timeDiff = val.getTime() - new Date().getTime(); long dayDiff = timeDiff / (1000 * 60 * 60 * 24); if(val.before(new Date()) || (dayDiff > 280)){ param.setErrCode(1); param.setErrText("invalid date"); param.setMessageFormParamStatus(IncMessageFormParameterStatus.INVALID); } } dFormat.applyPattern(defaultDateFormat); param.setValue(dFormat.format(val)); } catch (ParseException ex) { logger.error("Invalid date format - " + param.getValue(), ex); param.setErrCode(1); param.setErrText("wrong format"); param.setMessageFormParamStatus(IncMessageFormParameterStatus.INVALID); } } } param.setLastModified(new Date()); return param.getMessageFormParamStatus().equals(IncMessageFormParameterStatus.VALID); } }
ParamExpressionValidator implements IncomingMessageFormParameterValidator { public boolean validate(IncomingMessageFormParameter param) { if(param.getMessageFormParamStatus() == IncMessageFormParameterStatus.INVALID) return false; String paramType = param.getIncomingMsgFormParamDefinition().getParamType().toUpperCase(); if(paramType.indexOf("TIME") >= 0){ try{ SimpleDateFormat dFormat = new SimpleDateFormat(dateFormat); dFormat.setLenient(true); Date val = dFormat.parse(param.getValue()); dFormat.applyPattern(defaultDateFormat); param.setValue(dFormat.format(val)); param.setMessageFormParamStatus(IncMessageFormParameterStatus.VALID); }catch (ParseException ex) { logger.error("Invalid datetime format - " + param.getValue(), ex); param.setErrCode(1); param.setErrText("wrong format"); param.setMessageFormParamStatus(IncMessageFormParameterStatus.INVALID); } }else if (!Pattern.matches(expression, param.getValue().trim())) { param.setErrCode(1); param.setErrText("wrong format"); param.setMessageFormParamStatus(IncMessageFormParameterStatus.INVALID); } else { param.setMessageFormParamStatus(IncMessageFormParameterStatus.VALID); if (paramType.indexOf("DATE") >= 0) { try { String dateInputFormat = ""; for (String regex : dateFormateExpMap.getDateFormatRegexMap().keySet()){ boolean match = Pattern.matches(regex, param.getValue()); if (match){ dateInputFormat = dateFormateExpMap.getDateFormatRegexMap().get(regex); break; } } SimpleDateFormat dFormat = new SimpleDateFormat(dateInputFormat); dFormat.setLenient(true); Date val = dFormat.parse(param.getValue()); if (paramType.equalsIgnoreCase("DATE") && val.after(new Date())) { param.setErrCode(1); param.setErrText("invalid date"); param.setMessageFormParamStatus(IncMessageFormParameterStatus.INVALID); } else if (paramType.equalsIgnoreCase("DELIVERYDATE")) { long timeDiff = val.getTime() - new Date().getTime(); long dayDiff = timeDiff / (1000 * 60 * 60 * 24); if(val.before(new Date()) || (dayDiff > 280)){ param.setErrCode(1); param.setErrText("invalid date"); param.setMessageFormParamStatus(IncMessageFormParameterStatus.INVALID); } } dFormat.applyPattern(defaultDateFormat); param.setValue(dFormat.format(val)); } catch (ParseException ex) { logger.error("Invalid date format - " + param.getValue(), ex); param.setErrCode(1); param.setErrText("wrong format"); param.setMessageFormParamStatus(IncMessageFormParameterStatus.INVALID); } } } param.setLastModified(new Date()); return param.getMessageFormParamStatus().equals(IncMessageFormParameterStatus.VALID); }  }
ParamExpressionValidator implements IncomingMessageFormParameterValidator { public boolean validate(IncomingMessageFormParameter param) { if(param.getMessageFormParamStatus() == IncMessageFormParameterStatus.INVALID) return false; String paramType = param.getIncomingMsgFormParamDefinition().getParamType().toUpperCase(); if(paramType.indexOf("TIME") >= 0){ try{ SimpleDateFormat dFormat = new SimpleDateFormat(dateFormat); dFormat.setLenient(true); Date val = dFormat.parse(param.getValue()); dFormat.applyPattern(defaultDateFormat); param.setValue(dFormat.format(val)); param.setMessageFormParamStatus(IncMessageFormParameterStatus.VALID); }catch (ParseException ex) { logger.error("Invalid datetime format - " + param.getValue(), ex); param.setErrCode(1); param.setErrText("wrong format"); param.setMessageFormParamStatus(IncMessageFormParameterStatus.INVALID); } }else if (!Pattern.matches(expression, param.getValue().trim())) { param.setErrCode(1); param.setErrText("wrong format"); param.setMessageFormParamStatus(IncMessageFormParameterStatus.INVALID); } else { param.setMessageFormParamStatus(IncMessageFormParameterStatus.VALID); if (paramType.indexOf("DATE") >= 0) { try { String dateInputFormat = ""; for (String regex : dateFormateExpMap.getDateFormatRegexMap().keySet()){ boolean match = Pattern.matches(regex, param.getValue()); if (match){ dateInputFormat = dateFormateExpMap.getDateFormatRegexMap().get(regex); break; } } SimpleDateFormat dFormat = new SimpleDateFormat(dateInputFormat); dFormat.setLenient(true); Date val = dFormat.parse(param.getValue()); if (paramType.equalsIgnoreCase("DATE") && val.after(new Date())) { param.setErrCode(1); param.setErrText("invalid date"); param.setMessageFormParamStatus(IncMessageFormParameterStatus.INVALID); } else if (paramType.equalsIgnoreCase("DELIVERYDATE")) { long timeDiff = val.getTime() - new Date().getTime(); long dayDiff = timeDiff / (1000 * 60 * 60 * 24); if(val.before(new Date()) || (dayDiff > 280)){ param.setErrCode(1); param.setErrText("invalid date"); param.setMessageFormParamStatus(IncMessageFormParameterStatus.INVALID); } } dFormat.applyPattern(defaultDateFormat); param.setValue(dFormat.format(val)); } catch (ParseException ex) { logger.error("Invalid date format - " + param.getValue(), ex); param.setErrCode(1); param.setErrText("wrong format"); param.setMessageFormParamStatus(IncMessageFormParameterStatus.INVALID); } } } param.setLastModified(new Date()); return param.getMessageFormParamStatus().equals(IncMessageFormParameterStatus.VALID); }  boolean validate(IncomingMessageFormParameter param); void setExpression(String expression); void setDateFormat(String dateFormat); DateFormatRegexMap getDateFormateExpMap(); void setDateFormateExpMap(DateFormatRegexMap dateFormateExpMap); String getDefaultDateFormat(); void setDefaultDateFormat(String defaultDateFormat); }
ParamExpressionValidator implements IncomingMessageFormParameterValidator { public boolean validate(IncomingMessageFormParameter param) { if(param.getMessageFormParamStatus() == IncMessageFormParameterStatus.INVALID) return false; String paramType = param.getIncomingMsgFormParamDefinition().getParamType().toUpperCase(); if(paramType.indexOf("TIME") >= 0){ try{ SimpleDateFormat dFormat = new SimpleDateFormat(dateFormat); dFormat.setLenient(true); Date val = dFormat.parse(param.getValue()); dFormat.applyPattern(defaultDateFormat); param.setValue(dFormat.format(val)); param.setMessageFormParamStatus(IncMessageFormParameterStatus.VALID); }catch (ParseException ex) { logger.error("Invalid datetime format - " + param.getValue(), ex); param.setErrCode(1); param.setErrText("wrong format"); param.setMessageFormParamStatus(IncMessageFormParameterStatus.INVALID); } }else if (!Pattern.matches(expression, param.getValue().trim())) { param.setErrCode(1); param.setErrText("wrong format"); param.setMessageFormParamStatus(IncMessageFormParameterStatus.INVALID); } else { param.setMessageFormParamStatus(IncMessageFormParameterStatus.VALID); if (paramType.indexOf("DATE") >= 0) { try { String dateInputFormat = ""; for (String regex : dateFormateExpMap.getDateFormatRegexMap().keySet()){ boolean match = Pattern.matches(regex, param.getValue()); if (match){ dateInputFormat = dateFormateExpMap.getDateFormatRegexMap().get(regex); break; } } SimpleDateFormat dFormat = new SimpleDateFormat(dateInputFormat); dFormat.setLenient(true); Date val = dFormat.parse(param.getValue()); if (paramType.equalsIgnoreCase("DATE") && val.after(new Date())) { param.setErrCode(1); param.setErrText("invalid date"); param.setMessageFormParamStatus(IncMessageFormParameterStatus.INVALID); } else if (paramType.equalsIgnoreCase("DELIVERYDATE")) { long timeDiff = val.getTime() - new Date().getTime(); long dayDiff = timeDiff / (1000 * 60 * 60 * 24); if(val.before(new Date()) || (dayDiff > 280)){ param.setErrCode(1); param.setErrText("invalid date"); param.setMessageFormParamStatus(IncMessageFormParameterStatus.INVALID); } } dFormat.applyPattern(defaultDateFormat); param.setValue(dFormat.format(val)); } catch (ParseException ex) { logger.error("Invalid date format - " + param.getValue(), ex); param.setErrCode(1); param.setErrText("wrong format"); param.setMessageFormParamStatus(IncMessageFormParameterStatus.INVALID); } } } param.setLastModified(new Date()); return param.getMessageFormParamStatus().equals(IncMessageFormParameterStatus.VALID); }  boolean validate(IncomingMessageFormParameter param); void setExpression(String expression); void setDateFormat(String dateFormat); DateFormatRegexMap getDateFormateExpMap(); void setDateFormateExpMap(DateFormatRegexMap dateFormateExpMap); String getDefaultDateFormat(); void setDefaultDateFormat(String defaultDateFormat);  }