prepare the usage of dynamic validation messages

This commit is contained in:
Andreas Billmann
2015-12-17 21:15:37 +01:00
parent a67f793670
commit 5121b998c5
6 changed files with 124 additions and 49 deletions

View File

@@ -26,6 +26,8 @@
package ninja.javafx.smartcsv.fx.util; package ninja.javafx.smartcsv.fx.util;
import ninja.javafx.smartcsv.validation.ValidationMessage;
import java.io.StringWriter; import java.io.StringWriter;
import java.util.List; import java.util.List;
import java.util.ResourceBundle; import java.util.ResourceBundle;
@@ -35,14 +37,14 @@ import java.util.ResourceBundle;
*/ */
public class I18nValidationUtil { public class I18nValidationUtil {
public static String getI18nValidatioMessage(ResourceBundle resourceBundle, List<String> messages) { public static String getI18nValidatioMessage(ResourceBundle resourceBundle, List<ValidationMessage> validationMessages) {
StringWriter message = new StringWriter(); StringWriter message = new StringWriter();
for (String key: messages) { for (ValidationMessage validationMessage: validationMessages) {
if (resourceBundle.containsKey(key)) { if (resourceBundle.containsKey(validationMessage.getKey())) {
message.append(resourceBundle.getString(key)).append("\n"); message.append(resourceBundle.getString(validationMessage.getKey())).append("\n");
} else { } else {
message.append(key).append("\n"); message.append(validationMessage.getKey()).append("\n");
} }
} }

View File

@@ -33,14 +33,14 @@ import java.util.List;
*/ */
public class ValidationError { public class ValidationError {
private List<String> messages; private List<ValidationMessage> messages;
private Integer lineNumber; private Integer lineNumber;
public ValidationError(List<String> messages) { public ValidationError(List<ValidationMessage> messages) {
this(messages, -1); this(messages, -1);
} }
public ValidationError(List<String> messages, Integer lineNumber) { public ValidationError(List<ValidationMessage> messages, Integer lineNumber) {
this.messages = messages; this.messages = messages;
this.lineNumber = lineNumber; this.lineNumber = lineNumber;
} }
@@ -49,7 +49,7 @@ public class ValidationError {
return lineNumber; return lineNumber;
} }
public List<String> getMessages() { public List<ValidationMessage> getMessages() {
return messages; return messages;
} }

View File

@@ -0,0 +1,71 @@
/*
The MIT License (MIT)
-----------------------------------------------------------------------------
Copyright (c) 2015 javafx.ninja <info@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;
}
}

View File

@@ -86,7 +86,7 @@ public class Validator {
Config columnConfig = getColumnConfig(columnSectionConfig, column); Config columnConfig = getColumnConfig(columnSectionConfig, column);
if (columnConfig != null) { if (columnConfig != null) {
List<String> errorMessages = new ArrayList<>(); List<ValidationMessage> errorMessages = new ArrayList<>();
checkBlankOrNull(columnConfig, value, errorMessages); checkBlankOrNull(columnConfig, value, errorMessages);
if (value != null) { if (value != null) {
checkRegularExpression(columnConfig, value, errorMessages); checkRegularExpression(columnConfig, value, errorMessages);
@@ -112,7 +112,7 @@ public class Validator {
// private methods // private methods
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
private void checkGroovy(String column, Config columnConfig, String value, List<String> result) { private void checkGroovy(String column, Config columnConfig, String value, List<ValidationMessage> result) {
String groovyScript = getString(columnConfig, "groovy"); String groovyScript = getString(columnConfig, "groovy");
if (groovyScript != null) { if (groovyScript != null) {
@@ -130,15 +130,15 @@ public class Validator {
try { try {
groovyResult = script.run(); groovyResult = script.run();
} catch (CompilationFailedException e) { } 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(); e.printStackTrace();
} }
if (groovyResult == null) { if (groovyResult == null) {
result.add("groovy script '"+groovyScript+"' returns null"); result.add(new ValidationMessage("groovy script '"+groovyScript+"' returns null"));
} }
if (!isScriptResultTrue(groovyResult)) { 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"); return groovyResult.equals(true) || groovyResult.toString().trim().toLowerCase().equals("true");
} }
private void checkBlankOrNull(Config columnConfig, String value, List<String> result) { private void checkBlankOrNull(Config columnConfig, String value, List<ValidationMessage> result) {
if (getBoolean(columnConfig, "not empty")) { if (getBoolean(columnConfig, "not empty")) {
if (isBlankOrNull(value)) { 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<String> result) { private void checkInteger(Config columnConfig, String value, List<ValidationMessage> result) {
if (getBoolean(columnConfig, "integer")) { if (getBoolean(columnConfig, "integer")) {
if (!isInt(value)) { if (!isInt(value)) {
result.add("validation.message.integer"); result.add(new ValidationMessage("validation.message.integer"));
} }
} }
} }
private void checkMinLength(Config columnConfig, String value, List<String> result) { private void checkMinLength(Config columnConfig, String value, List<ValidationMessage> result) {
Integer minLength = getInteger(columnConfig, "minlength"); Integer minLength = getInteger(columnConfig, "minlength");
if (minLength != null) { if (minLength != null) {
if (!minLength(value, minLength)) { 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<String> result) { private void checkMaxLength(Config columnConfig, String value, List<ValidationMessage> result) {
Integer maxLength = getInteger(columnConfig, "maxlength"); Integer maxLength = getInteger(columnConfig, "maxlength");
if (maxLength != null) { if (maxLength != null) {
if (!maxLength(value, maxLength)) { 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<String> result) { private void checkDate(Config columnConfig, String value, List<ValidationMessage> result) {
String dateformat = getString(columnConfig, "date"); String dateformat = getString(columnConfig, "date");
if (dateformat != null && !dateformat.trim().isEmpty()) { if (dateformat != null && !dateformat.trim().isEmpty()) {
if (!isDate(value, dateformat, true)) { 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<String> result) { private void checkAlphaNumeric(Config columnConfig, String value, List<ValidationMessage> result) {
if (getBoolean(columnConfig, "alphanumeric")) { if (getBoolean(columnConfig, "alphanumeric")) {
if (!matchRegexp(value, "[0-9a-zA-Z]*")) { 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<String> result) { private void checkRegularExpression(Config columnConfig, String value, List<ValidationMessage> result) {
String regexp = getString(columnConfig, "regexp"); String regexp = getString(columnConfig, "regexp");
if (regexp != null && !regexp.trim().isEmpty()) { if (regexp != null && !regexp.trim().isEmpty()) {
if (!matchRegexp(value, regexp)) { 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<String> headerConfig = headerSectionConfig.getStringList("list"); List<String> headerConfig = headerSectionConfig.getStringList("list");
if (headerConfig != null) { if (headerConfig != null) {
if (headerNames.length != headerConfig.size()) { 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 + headerNames.length +
" but there should be " + " but there should be " +
headerConfig.size())); headerConfig.size())));
return result; return result;
} }
List<String> errorMessages = new ArrayList<>(); List<ValidationMessage> errorMessages = new ArrayList<>();
for(int i=0; i<headerConfig.size(); i++) { for(int i=0; i<headerConfig.size(); i++) {
String header = headerConfig.get(i); String header = headerConfig.get(i);
if (!header.equals(headerNames[i])) { if (!header.equals(headerNames[i])) {
errorMessages.add("header number " + errorMessages.add(new ValidationMessage("header number " +
i + i +
" does not match \"" + " does not match \"" +
header + header +
"\" should be \"" + "\" should be \"" +
headerNames[i] + headerNames[i] +
"\""); "\""));
} }
} }
if (!errorMessages.isEmpty()) { if (!errorMessages.isEmpty()) {

View File

@@ -34,9 +34,11 @@ import org.junit.runners.Parameterized;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Collections;
import java.util.List; import java.util.List;
import static java.util.Arrays.asList; import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
import static ninja.javafx.smartcsv.validation.ConfigMock.headerSectionConfig; import static ninja.javafx.smartcsv.validation.ConfigMock.headerSectionConfig;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
@@ -52,7 +54,7 @@ public class HeaderValidationTest {
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
private Config config; private Config config;
private Boolean expectedResult; private Boolean expectedResult;
private List<String> expectedErrors; private List<ValidationMessage> expectedErrors;
private String[] headerNames; private String[] headerNames;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -67,7 +69,7 @@ public class HeaderValidationTest {
public HeaderValidationTest(String[] configHeaderNames, public HeaderValidationTest(String[] configHeaderNames,
String[] headerNames, String[] headerNames,
Boolean expectedResult, Boolean expectedResult,
List<String> expectedErrors) { List<ValidationMessage> expectedErrors) {
this.config = headerSectionConfig(configHeaderNames); this.config = headerSectionConfig(configHeaderNames);
this.headerNames = headerNames; this.headerNames = headerNames;
this.expectedResult = expectedResult; this.expectedResult = expectedResult;
@@ -106,9 +108,9 @@ public class HeaderValidationTest {
return asList(new Object[][] { return asList(new Object[][] {
{ new String[] {}, new String[] {}, true, null }, { new String[] {}, new String[] {}, true, null },
{ new String[] {"a"}, new String[] {"a"}, 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[] {"b"}, false, singletonList(new ValidationMessage("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"}, 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, 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", "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\"")) }
}); });
} }
} }

View File

@@ -27,7 +27,7 @@ public class ValidatorTest {
private String column; private String column;
private String value; private String value;
private Boolean expectedResult; private Boolean expectedResult;
private String expectedError; private ValidationMessage expectedError;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -45,7 +45,7 @@ public class ValidatorTest {
String column, String column,
String value, String value,
Boolean expectedResult, Boolean expectedResult,
String expectedError) { ValidationMessage expectedError) {
this.config = columnSectionConfig(configcolumn, configValidation, configValue); this.config = columnSectionConfig(configcolumn, configValidation, configValue);
this.column = column; this.column = column;
this.value = value; this.value = value;
@@ -85,22 +85,22 @@ public class ValidatorTest {
public static Collection validationConfigurations() { public static Collection validationConfigurations() {
return asList(new Object[][] { return asList(new Object[][] {
{ "column", "not empty", true, "column", "value", true, null }, { "column", "not empty", true, "column", "value", true, null },
{ "column", "not empty", true, "column", "", false, "validation.message.not.empty" }, { "column", "not empty", true, "column", "", false, new ValidationMessage("validation.message.not.empty") },
{ "column", "not empty", true, "column", null, false, "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", "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", "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", "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", "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", 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", "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", "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") },
}); });
} }