From feabaa70a9b2e48ce7e72539b9b5294ab333ac7f Mon Sep 17 00:00:00 2001 From: Andreas Billmann Date: Fri, 4 Dec 2015 16:22:21 +0100 Subject: [PATCH] first steps for header validation --- .../smartcsv/fx/table/model/CSVModel.java | 5 + .../javafx/smartcsv/validation/Validator.java | 69 +++++++++-- .../smartcsv/validation/ConfigMock.java | 58 +++++++++ .../validation/HeaderValidationTest.java | 111 ++++++++++++++++++ .../smartcsv/validation/ValidatorTest.java | 28 +---- 5 files changed, 232 insertions(+), 39 deletions(-) create mode 100644 src/test/java/ninja/javafx/smartcsv/validation/ConfigMock.java create mode 100644 src/test/java/ninja/javafx/smartcsv/validation/HeaderValidationTest.java diff --git a/src/main/java/ninja/javafx/smartcsv/fx/table/model/CSVModel.java b/src/main/java/ninja/javafx/smartcsv/fx/table/model/CSVModel.java index 7a2f31e..0cc3de0 100644 --- a/src/main/java/ninja/javafx/smartcsv/fx/table/model/CSVModel.java +++ b/src/main/java/ninja/javafx/smartcsv/fx/table/model/CSVModel.java @@ -89,6 +89,7 @@ public class CSVModel { */ public void setHeader(String[] header) { this.header = header; + revalidate(); } /** @@ -104,6 +105,10 @@ public class CSVModel { * walks through the data and validates each value */ private void revalidate() { + if (header != null) { + validator.isHeaderValid(header); + } + for (CSVRow row: rows) { row.setValidator(validator); for (String column: row.getColumns().keySet()) { diff --git a/src/main/java/ninja/javafx/smartcsv/validation/Validator.java b/src/main/java/ninja/javafx/smartcsv/validation/Validator.java index 5b5614e..324f9d7 100644 --- a/src/main/java/ninja/javafx/smartcsv/validation/Validator.java +++ b/src/main/java/ninja/javafx/smartcsv/validation/Validator.java @@ -33,6 +33,7 @@ import groovy.lang.Script; import org.codehaus.groovy.control.CompilationFailedException; import java.util.HashMap; +import java.util.List; import java.util.Map; import static org.apache.commons.validator.GenericValidator.*; @@ -78,17 +79,20 @@ public class Validator { public ValidationState isValid(String column, String value) { ValidationState result = new ValidationState(); if (validationConfig != null) { - Config columnConfig = getColumnConfig(column); - if (columnConfig != null) { - checkBlankOrNull(columnConfig, value, result); - if (value != null) { - checkRegularExpression(columnConfig, value, result); - checkAlphaNumeric(columnConfig, value, result); - checkDate(columnConfig, value, result); - checkMaxLength(columnConfig, value, result); - checkMinLength(columnConfig, value, result); - checkInteger(columnConfig, value, result); - checkGroovy(column, columnConfig, value, result); + Config columnSectionConfig = getColumnSectionConfig(); + if (columnSectionConfig != null) { + Config columnConfig = getColumnConfig(columnSectionConfig, column); + if (columnConfig != null) { + checkBlankOrNull(columnConfig, value, result); + if (value != null) { + checkRegularExpression(columnConfig, value, result); + checkAlphaNumeric(columnConfig, value, result); + checkDate(columnConfig, value, result); + checkMaxLength(columnConfig, value, result); + checkMinLength(columnConfig, value, result); + checkInteger(columnConfig, value, result); + checkGroovy(column, columnConfig, value, result); + } } } } @@ -196,10 +200,15 @@ public class Validator { } } - private Config getColumnConfig(String column) { - return validationConfig.hasPath(column) ? validationConfig.getConfig(column) : null; + private Config getColumnSectionConfig() { + return validationConfig.hasPath("columns") ? validationConfig.getConfig("columns") : null; } + private Config getColumnConfig(Config columnSectionConfig, String column) { + return columnSectionConfig.hasPath(column) ? columnSectionConfig.getConfig(column) : null; + } + + private String getString(Config columnConfig, String path) { return columnConfig.hasPath(path) ? columnConfig.getString(path) : null; } @@ -213,4 +222,38 @@ public class Validator { return columnConfig.hasPath(path) && columnConfig.getBoolean(path); } + public ValidationState isHeaderValid(String[] headerNames) { + ValidationState result = new ValidationState(); + if (validationConfig != null) { + if (validationConfig.hasPath("headers")) { + Config headerSectionConfig = validationConfig.getConfig("headers"); + if (headerSectionConfig.hasPath("list")) { + List headerConfig = headerSectionConfig.getStringList("list"); + if (headerConfig != null) { + if (headerNames.length != headerConfig.size()) { + result.invalidate("number of headers is not correct! there are " + + headerNames.length + + " but there should be " + + headerConfig.size()); + return result; + } + + for(int i=0; i + + 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 com.typesafe.config.Config; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +import java.util.Collection; + +import static java.util.Arrays.asList; +import static ninja.javafx.smartcsv.validation.ConfigMock.headerSectionConfig; +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; + +/** + * unit test for header validator + */ +@RunWith(Parameterized.class) +public class HeaderValidationTest { + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + // parameters + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + private Config config; + private Boolean expectedResult; + private String expectedError; + private String[] headerNames; + + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + // subject under test + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + private Validator sut; + + + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + // parameterized constructor + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + public HeaderValidationTest(String[] configHeaderNames, + String[] headerNames, + Boolean expectedResult, + String expectedError) { + this.config = headerSectionConfig(configHeaderNames); + this.headerNames = headerNames; + this.expectedResult = expectedResult; + this.expectedError = expectedError; + } + + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + // init + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + @Before + public void initialize() { + sut = new Validator(config); + } + + + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + // tests + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + @Test + public void validation() { + // execution + ValidationState result = sut.isHeaderValid(headerNames); + + // assertion + assertThat(result.isValid(), is(expectedResult)); + if (!expectedResult) { + assertThat(result.error(), is(expectedError)); + } + } + + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + // parameters for tests + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + @Parameterized.Parameters + public static Collection validationConfigurations() { + 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, "header number 0 does not match \"a\" should be \"b\"\n" }, + { new String[] {"a"}, new String[] {"a","b"}, false, "number of headers is not correct! there are 2 but there should be 1\n" }, + { new String[] {"a", "b"}, new String[] {"b", "a"}, false, "header number 0 does not match \"a\" should be \"b\"\nheader number 1 does not match \"b\" should be \"a\"\n" } + }); + } +} diff --git a/src/test/java/ninja/javafx/smartcsv/validation/ValidatorTest.java b/src/test/java/ninja/javafx/smartcsv/validation/ValidatorTest.java index d2ba076..4682158 100644 --- a/src/test/java/ninja/javafx/smartcsv/validation/ValidatorTest.java +++ b/src/test/java/ninja/javafx/smartcsv/validation/ValidatorTest.java @@ -9,10 +9,9 @@ import org.junit.runners.Parameterized; import java.util.Collection; import static java.util.Arrays.asList; +import static ninja.javafx.smartcsv.validation.ConfigMock.columnSectionConfig; import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; /** * unit test for validator @@ -46,7 +45,7 @@ public class ValidatorTest { String value, Boolean expectedResult, String expectedError) { - this.config = config(configcolumn, configValidation, configValue); + this.config = columnSectionConfig(configcolumn, configValidation, configValue); this.column = column; this.value = value; this.expectedResult = expectedResult; @@ -78,29 +77,6 @@ public class ValidatorTest { } } - //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - // mocks - //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - private Config config(String column, String validation, Object value) { - - Config columnConfig = mock(Config.class); - Config validatorConfig = mock(Config.class); - - when(columnConfig.hasPath(column)).thenReturn(true); - when(columnConfig.getConfig(column)).thenReturn(validatorConfig); - - when(validatorConfig.hasPath(validation)).thenReturn(true); - if (value instanceof Boolean) { - when(validatorConfig.getBoolean(validation)).thenReturn((Boolean) value); - } else if (value instanceof String) { - when(validatorConfig.getString(validation)).thenReturn((String) value); - } else if (value instanceof Integer) { - when(validatorConfig.getInt(validation)).thenReturn((Integer)value); - } - - return columnConfig; - } - //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // parameters for tests ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////