diff --git a/src/main/java/ninja/javafx/smartcsv/fx/util/I18nValidationUtil.java b/src/main/java/ninja/javafx/smartcsv/fx/util/I18nValidationUtil.java index dae9823..581a510 100644 --- a/src/main/java/ninja/javafx/smartcsv/fx/util/I18nValidationUtil.java +++ b/src/main/java/ninja/javafx/smartcsv/fx/util/I18nValidationUtil.java @@ -26,6 +26,8 @@ package ninja.javafx.smartcsv.fx.util; +import ninja.javafx.smartcsv.validation.ValidationMessage; + import java.io.StringWriter; import java.util.List; import java.util.ResourceBundle; @@ -35,14 +37,14 @@ import java.util.ResourceBundle; */ public class I18nValidationUtil { - public static String getI18nValidatioMessage(ResourceBundle resourceBundle, List messages) { + public static String getI18nValidatioMessage(ResourceBundle resourceBundle, List validationMessages) { StringWriter message = new StringWriter(); - for (String key: messages) { - if (resourceBundle.containsKey(key)) { - message.append(resourceBundle.getString(key)).append("\n"); + for (ValidationMessage validationMessage: validationMessages) { + if (resourceBundle.containsKey(validationMessage.getKey())) { + message.append(resourceBundle.getString(validationMessage.getKey())).append("\n"); } else { - message.append(key).append("\n"); + message.append(validationMessage.getKey()).append("\n"); } } diff --git a/src/main/java/ninja/javafx/smartcsv/validation/ValidationError.java b/src/main/java/ninja/javafx/smartcsv/validation/ValidationError.java index 5380a5e..129e404 100644 --- a/src/main/java/ninja/javafx/smartcsv/validation/ValidationError.java +++ b/src/main/java/ninja/javafx/smartcsv/validation/ValidationError.java @@ -33,14 +33,14 @@ import java.util.List; */ public class ValidationError { - private List messages; + private List messages; private Integer lineNumber; - public ValidationError(List messages) { + public ValidationError(List messages) { this(messages, -1); } - public ValidationError(List messages, Integer lineNumber) { + public ValidationError(List messages, Integer lineNumber) { this.messages = messages; this.lineNumber = lineNumber; } @@ -49,7 +49,7 @@ public class ValidationError { return lineNumber; } - public List getMessages() { + public List getMessages() { return messages; } diff --git a/src/main/java/ninja/javafx/smartcsv/validation/ValidationMessage.java b/src/main/java/ninja/javafx/smartcsv/validation/ValidationMessage.java new file mode 100644 index 0000000..c57a5b2 --- /dev/null +++ b/src/main/java/ninja/javafx/smartcsv/validation/ValidationMessage.java @@ -0,0 +1,71 @@ +/* + The MIT License (MIT) + ----------------------------------------------------------------------------- + + Copyright (c) 2015 javafx.ninja + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + +*/ + +package ninja.javafx.smartcsv.validation; + +import java.util.Arrays; + +/** + * TODO: DESCRIPTION!!! + */ +public class ValidationMessage { + + private String key; + private String[] parameters; + + public ValidationMessage(String key, String... parameters) { + this.key = key; + this.parameters = parameters; + } + + public String getKey() { + return key; + } + + public String[] getParameters() { + return parameters; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + ValidationMessage that = (ValidationMessage) o; + + if (key != null ? !key.equals(that.key) : that.key != null) return false; + // Probably incorrect - comparing Object[] arrays with Arrays.equals + return Arrays.equals(parameters, that.parameters); + + } + + @Override + public int hashCode() { + int result = key != null ? key.hashCode() : 0; + result = 31 * result + Arrays.hashCode(parameters); + return result; + } +} diff --git a/src/main/java/ninja/javafx/smartcsv/validation/Validator.java b/src/main/java/ninja/javafx/smartcsv/validation/Validator.java index 66182e9..4acd27d 100644 --- a/src/main/java/ninja/javafx/smartcsv/validation/Validator.java +++ b/src/main/java/ninja/javafx/smartcsv/validation/Validator.java @@ -86,7 +86,7 @@ public class Validator { Config columnConfig = getColumnConfig(columnSectionConfig, column); if (columnConfig != null) { - List errorMessages = new ArrayList<>(); + List errorMessages = new ArrayList<>(); checkBlankOrNull(columnConfig, value, errorMessages); if (value != null) { checkRegularExpression(columnConfig, value, errorMessages); @@ -112,7 +112,7 @@ public class Validator { // private methods //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - private void checkGroovy(String column, Config columnConfig, String value, List result) { + private void checkGroovy(String column, Config columnConfig, String value, List result) { String groovyScript = getString(columnConfig, "groovy"); if (groovyScript != null) { @@ -130,15 +130,15 @@ public class Validator { try { groovyResult = script.run(); } catch (CompilationFailedException e) { - result.add("groovy script '"+groovyScript+"' throws exception: "+e.getMessage()); + result.add(new ValidationMessage("groovy script '"+groovyScript+"' throws exception: "+e.getMessage())); e.printStackTrace(); } if (groovyResult == null) { - result.add("groovy script '"+groovyScript+"' returns null"); + result.add(new ValidationMessage("groovy script '"+groovyScript+"' returns null")); } if (!isScriptResultTrue(groovyResult)) { - result.add(groovyResult.toString()); + result.add(new ValidationMessage(groovyResult.toString())); } } @@ -148,62 +148,62 @@ public class Validator { return groovyResult.equals(true) || groovyResult.toString().trim().toLowerCase().equals("true"); } - private void checkBlankOrNull(Config columnConfig, String value, List result) { + private void checkBlankOrNull(Config columnConfig, String value, List result) { if (getBoolean(columnConfig, "not empty")) { if (isBlankOrNull(value)) { - result.add("validation.message.not.empty"); + result.add(new ValidationMessage("validation.message.not.empty")); } } } - private void checkInteger(Config columnConfig, String value, List result) { + private void checkInteger(Config columnConfig, String value, List result) { if (getBoolean(columnConfig, "integer")) { if (!isInt(value)) { - result.add("validation.message.integer"); + result.add(new ValidationMessage("validation.message.integer")); } } } - private void checkMinLength(Config columnConfig, String value, List result) { + private void checkMinLength(Config columnConfig, String value, List result) { Integer minLength = getInteger(columnConfig, "minlength"); if (minLength != null) { if (!minLength(value, minLength)) { - result.add("has not min length of " + minLength); + result.add(new ValidationMessage("has not min length of " + minLength)); } } } - private void checkMaxLength(Config columnConfig, String value, List result) { + private void checkMaxLength(Config columnConfig, String value, List result) { Integer maxLength = getInteger(columnConfig, "maxlength"); if (maxLength != null) { if (!maxLength(value, maxLength)) { - result.add("has not max length of " + maxLength); + result.add(new ValidationMessage("has not max length of " + maxLength)); } } } - private void checkDate(Config columnConfig, String value, List result) { + private void checkDate(Config columnConfig, String value, List result) { String dateformat = getString(columnConfig, "date"); if (dateformat != null && !dateformat.trim().isEmpty()) { if (!isDate(value, dateformat, true)) { - result.add("is not a date of format " + dateformat); + result.add(new ValidationMessage("is not a date of format " + dateformat)); } } } - private void checkAlphaNumeric(Config columnConfig, String value, List result) { + private void checkAlphaNumeric(Config columnConfig, String value, List result) { if (getBoolean(columnConfig, "alphanumeric")) { if (!matchRegexp(value, "[0-9a-zA-Z]*")) { - result.add("validation.message.alphanumeric"); + result.add(new ValidationMessage("validation.message.alphanumeric")); } } } - private void checkRegularExpression(Config columnConfig, String value, List result) { + private void checkRegularExpression(Config columnConfig, String value, List result) { String regexp = getString(columnConfig, "regexp"); if (regexp != null && !regexp.trim().isEmpty()) { if (!matchRegexp(value, regexp)) { - result.add("does not match " + regexp); + result.add(new ValidationMessage("does not match " + regexp)); } } } @@ -239,25 +239,25 @@ public class Validator { List headerConfig = headerSectionConfig.getStringList("list"); if (headerConfig != null) { if (headerNames.length != headerConfig.size()) { - result = new ValidationError(singletonList("number of headers is not correct! there are " + + result = new ValidationError(singletonList(new ValidationMessage("number of headers is not correct! there are " + headerNames.length + " but there should be " + - headerConfig.size())); + headerConfig.size()))); return result; } - List errorMessages = new ArrayList<>(); + List errorMessages = new ArrayList<>(); for(int i=0; i expectedErrors; + private List expectedErrors; private String[] headerNames; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -67,7 +69,7 @@ public class HeaderValidationTest { public HeaderValidationTest(String[] configHeaderNames, String[] headerNames, Boolean expectedResult, - List expectedErrors) { + List expectedErrors) { this.config = headerSectionConfig(configHeaderNames); this.headerNames = headerNames; this.expectedResult = expectedResult; @@ -106,9 +108,9 @@ public class HeaderValidationTest { return asList(new Object[][] { { new String[] {}, new String[] {}, true, null }, { new String[] {"a"}, new String[] {"a"}, true, null }, - { new String[] {"a"}, new String[] {"b"}, false, Arrays.asList("header number 0 does not match \"a\" should be \"b\"") }, - { new String[] {"a"}, new String[] {"a","b"}, false, Arrays.asList("number of headers is not correct! there are 2 but there should be 1") }, - { new String[] {"a", "b"}, new String[] {"b", "a"}, false, Arrays.asList("header number 0 does not match \"a\" should be \"b\"", "header number 1 does not match \"b\" should be \"a\"") } + { new String[] {"a"}, new String[] {"b"}, false, singletonList(new ValidationMessage("header number 0 does not match \"a\" should be \"b\""))}, + { new String[] {"a"}, new String[] {"a","b"}, false, singletonList(new ValidationMessage("number of headers is not correct! there are 2 but there should be 1"))}, + { new String[] {"a", "b"}, new String[] {"b", "a"}, false, asList(new ValidationMessage("header number 0 does not match \"a\" should be \"b\""), new ValidationMessage("header number 1 does not match \"b\" should be \"a\"")) } }); } } diff --git a/src/test/java/ninja/javafx/smartcsv/validation/ValidatorTest.java b/src/test/java/ninja/javafx/smartcsv/validation/ValidatorTest.java index f7a14d0..ac426f8 100644 --- a/src/test/java/ninja/javafx/smartcsv/validation/ValidatorTest.java +++ b/src/test/java/ninja/javafx/smartcsv/validation/ValidatorTest.java @@ -27,7 +27,7 @@ public class ValidatorTest { private String column; private String value; private Boolean expectedResult; - private String expectedError; + private ValidationMessage expectedError; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -45,7 +45,7 @@ public class ValidatorTest { String column, String value, Boolean expectedResult, - String expectedError) { + ValidationMessage expectedError) { this.config = columnSectionConfig(configcolumn, configValidation, configValue); this.column = column; this.value = value; @@ -85,22 +85,22 @@ public class ValidatorTest { public static Collection validationConfigurations() { return asList(new Object[][] { { "column", "not empty", true, "column", "value", true, null }, - { "column", "not empty", true, "column", "", false, "validation.message.not.empty" }, - { "column", "not empty", true, "column", null, false, "validation.message.not.empty" }, + { "column", "not empty", true, "column", "", false, new ValidationMessage("validation.message.not.empty") }, + { "column", "not empty", true, "column", null, false, new ValidationMessage("validation.message.not.empty") }, { "column", "integer", true, "column", "999", true, null }, - { "column", "integer", true, "column", "a", false, "validation.message.integer" }, + { "column", "integer", true, "column", "a", false, new ValidationMessage("validation.message.integer") }, { "column", "minlength", 2, "column", "12", true, null }, - { "column", "minlength", 2, "column", "1", false, "has not min length of 2" }, + { "column", "minlength", 2, "column", "1", false, new ValidationMessage("has not min length of 2") }, { "column", "maxlength", 2, "column", "12", true, null }, - { "column", "maxlength", 2, "column", "123", false, "has not max length of 2" }, + { "column", "maxlength", 2, "column", "123", false, new ValidationMessage("has not max length of 2") }, { "column", "date", "yyyyMMdd", "column", "20151127", true, null }, - { "column", "date", "yyyyMMdd", "column", "27.11.2015", false, "is not a date of format yyyyMMdd" }, + { "column", "date", "yyyyMMdd", "column", "27.11.2015", false, new ValidationMessage("is not a date of format yyyyMMdd") }, { "column", "alphanumeric", true, "column", "abcABC123", true, null }, - { "column", "alphanumeric", true, "column", "-abcABC123", false, "validation.message.alphanumeric" }, + { "column", "alphanumeric", true, "column", "-abcABC123", false, new ValidationMessage("validation.message.alphanumeric") }, { "column", "regexp", "[a-z]*", "column", "abc", true, null }, - { "column", "regexp", "[a-z]*", "column", "abcA", false, "does not match [a-z]*" }, + { "column", "regexp", "[a-z]*", "column", "abcA", false, new ValidationMessage("does not match [a-z]*") }, { "column", "groovy", "value.contains('a')? 'true' : 'no a inside'", "column", "abcdef", true, null }, - { "column", "groovy", "value.contains('a')? 'true' : 'no a inside'", "column", "bcdefg", false, "no a inside" }, + { "column", "groovy", "value.contains('a')? 'true' : 'no a inside'", "column", "bcdefg", false, new ValidationMessage("no a inside") }, }); }