fixed broken tests

This commit is contained in:
Andreas Billmann
2016-09-23 11:14:45 +02:00
parent ef376d6c61
commit 5eaf021632
6 changed files with 126 additions and 47 deletions

View File

@@ -17,7 +17,6 @@ repositories {
dependencies { dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12' testCompile group: 'junit', name: 'junit', version: '4.12'
testCompile group: 'org.hamcrest', name: 'hamcrest-library', version: '1.3' testCompile group: 'org.hamcrest', name: 'hamcrest-library', version: '1.3'
testCompile group: 'org.mockito', name: 'mockito-all', version: '1.10.19'
compile group: 'org.codehaus.groovy', name: 'groovy-all', version: '2.4.7' compile group: 'org.codehaus.groovy', name: 'groovy-all', version: '2.4.7'
compile group: 'org.springframework', name:'spring-context', version: '4.3.1.RELEASE' compile group: 'org.springframework', name:'spring-context', version: '4.3.1.RELEASE'
compile group: 'net.sf.supercsv', name: 'super-csv', version: '2.4.0' compile group: 'net.sf.supercsv', name: 'super-csv', version: '2.4.0'

View File

@@ -1,5 +1,7 @@
package ninja.javafx.smartcsv.validation; package ninja.javafx.smartcsv.validation;
import java.text.SimpleDateFormat;
/** /**
* @author abi * @author abi
*/ */
@@ -31,6 +33,13 @@ public class ValidationFormatHelper {
format = format.replace("%j", "DDD"); format = format.replace("%j", "DDD");
format = format.replace("%U", "ww"); format = format.replace("%U", "ww");
return format; return format;
} else {
try {
new SimpleDateFormat(format);
return format;
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
} }
} }
return defaultFormat; return defaultFormat;

View File

@@ -181,12 +181,12 @@ public class Validator {
} }
if (column.getType() == DATE) { if (column.getType() == DATE) {
String format = dateFormat(column.getFormat(), "YYYY-MM-DD"); String format = dateFormat(column.getFormat(), "yyyy-MM-dd");
add(column.getName(), new DateValidation(format)); add(column.getName(), new DateValidation(format));
} }
if (column.getType() == DATETIME) { if (column.getType() == DATETIME) {
String format = dateFormat(column.getFormat(), "YYYY-MM-DDThh:mm:ssZ"); String format = dateFormat(column.getFormat(), "yyyy-MM-ddThh:mm:ssZ");
add(column.getName(), new DateValidation(format)); add(column.getName(), new DateValidation(format));
} }

View File

@@ -82,7 +82,7 @@ public class HeaderValidationTest {
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@Before @Before
public void initialize() { public void initialize() {
sut = new Validator(config); sut = new Validator(config, new TestColumnValueProvider());
} }
@@ -116,7 +116,21 @@ public class HeaderValidationTest {
} }
public static String json(String... headerNames) { public static String json(String... headerNames) {
return "{\"headers\":{\"list\":[" + asList(headerNames).stream().collect(joining(", ")) + "]}}";
String json = "{ \"fields\": [";
for (String headerName: headerNames) {
json += "{\"name\" : \""+headerName+"\" },";
}
if (headerNames != null && headerNames.length > 0) {
json = json.substring(0, json.length() - 1);
}
json += "]}";
return json;
} }
} }

View File

@@ -0,0 +1,18 @@
package ninja.javafx.smartcsv.validation;
import ninja.javafx.smartcsv.fx.table.model.ColumnValueProvider;
/**
* @author abi
*/
public class TestColumnValueProvider implements ColumnValueProvider {
@Override
public String getValue(int row, String column) {
return null;
}
@Override
public int getNumberOfRows() {
return 0;
}
}

View File

@@ -26,8 +26,7 @@ public class ValidatorTest {
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// parameters // parameters
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
private ValidationConfiguration config; private String config;
private String column;
private String value; private String value;
private Boolean expectedResult; private Boolean expectedResult;
private ValidationMessage expectedError; private ValidationMessage expectedError;
@@ -43,14 +42,10 @@ public class ValidatorTest {
// parameterized constructor // parameterized constructor
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public ValidatorTest(String config, public ValidatorTest(String config,
String column,
String value, String value,
Boolean expectedResult, Boolean expectedResult,
ValidationMessage expectedError) { ValidationMessage expectedError) {
System.out.println(config); this.config = config;
Gson gson = new GsonBuilder().create();
this.config = gson.fromJson(config, ValidationConfiguration.class);
this.column = column;
this.value = value; this.value = value;
this.expectedResult = expectedResult; this.expectedResult = expectedResult;
this.expectedError = expectedError; this.expectedError = expectedError;
@@ -62,7 +57,9 @@ public class ValidatorTest {
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@Before @Before
public void initialize() { public void initialize() {
sut = new Validator(config); Gson gson = new GsonBuilder().create();
ValidationConfiguration validationConfiguration = gson.fromJson(config, ValidationConfiguration.class);
sut = new Validator(validationConfiguration, new TestColumnValueProvider());
} }
@@ -71,10 +68,14 @@ public class ValidatorTest {
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@Test @Test
public void validation() { public void validation() {
System.out.println(column + " " + value + " " + expectedResult + " " + expectedError); System.out.println("===================================================");
System.out.println(config);
System.out.println(value + " " + expectedResult + " " + expectedError);
System.out.println("===================================================\n");
// execution // execution
ValidationError result = sut.isValid(0, column, value); ValidationError result = sut.isValid(0, "column", value);
// assertion // assertion
assertThat(result == null, is(expectedResult)); assertThat(result == null, is(expectedResult));
@@ -89,42 +90,80 @@ public class ValidatorTest {
@Parameterized.Parameters @Parameterized.Parameters
public static Collection validationConfigurations() { public static Collection validationConfigurations() {
return asList(new Object[][] { return asList(new Object[][] {
{ json("column", "not empty", true), "column", "value", true, null }, { constraintsJSON("string", "required", true), "value", true, null },
{ json("column", "not empty", true), "column", "", false, new ValidationMessage("validation.message.not.empty") }, { constraintsJSON("string", "required", true), "", false, new ValidationMessage("validation.message.not.empty") },
{ json("column", "not empty", true), "column", null, false, new ValidationMessage("validation.message.not.empty") }, { constraintsJSON("string", "required", true), null, false, new ValidationMessage("validation.message.not.empty") },
{ json("column", "integer", true), "column", "999", true, null }, { constraintsJSON("integer", null, true), "999", true, null },
{ json("column", "integer", true), "column", "a", false, new ValidationMessage("validation.message.integer") }, { constraintsJSON("integer", null, true), "a", false, new ValidationMessage("validation.message.integer") },
{ json("column", "double", true), "column", "999", true, null }, { constraintsJSON("number", null, true), "999", true, null },
{ json("column", "double", true), "column", "999.000", true, null }, { constraintsJSON("number", null, true), "999.000", true, null },
{ json("column", "double", true), "column", "a", false, new ValidationMessage("validation.message.double") }, { constraintsJSON("number", null, true), "a", false, new ValidationMessage("validation.message.double") },
{ json("column", "minlength", 2), "column", "12", true, null }, { constraintsJSON("string", "minLength", 2), "12", true, null },
{ json("column", "minlength", 2), "column", "1", false, new ValidationMessage("validation.message.min.length", "2") }, { constraintsJSON("string", "minLength", 2), "1", false, new ValidationMessage("validation.message.min.length", "2") },
{ json("column", "maxlength", 2), "column", "12", true, null }, { constraintsJSON("string", "maxLength", 2), "12", true, null },
{ json("column", "maxlength", 2), "column", "123", false, new ValidationMessage("validation.message.max.length", "2") }, { constraintsJSON("string", "maxLength", 2), "123", false, new ValidationMessage("validation.message.max.length", "2") },
{ json("column", "date", "yyyyMMdd"), "column", "20151127", true, null }, { constraintsJSON("string", "pattern", "[a-z]*"), "abc", true, null },
{ json("column", "date", "yyyyMMdd"), "column", "27.11.2015", false, new ValidationMessage("validation.message.date.format", "yyyyMMdd") }, { constraintsJSON("string", "pattern", "[a-z]*"), "abcA", false, new ValidationMessage("validation.message.regexp", "[a-z]*") },
{ json("column", "alphanumeric", true), "column", "abcABC123", true, null }, { constraintsJSON("string", "enum", asList("a","b","c","d","e")), "c", true, null },
{ json("column", "alphanumeric", true), "column", "-abcABC123", false, new ValidationMessage("validation.message.alphanumeric") }, { constraintsJSON("string", "enum", asList("a","b","c","d","e")), "f", false, new ValidationMessage("validation.message.value.of", "f", "a, b, c, d, e") },
{ json("column", "regexp", "[a-z]*"), "column", "abc", true, null },
{ json("column", "regexp", "[a-z]*"), "column", "abcA", false, new ValidationMessage("validation.message.regexp", "[a-z]*") }, { formatJSON("string", null), "some string", true, null },
{ json("column", "groovy", "value.contains('a')? 'true' : 'no a inside'"), "column", "abcdef", true, null }, { formatJSON("string", "email"), "test@javafx.ninja", true, null },
{ json("column", "groovy", "value.contains('a')? 'true' : 'no a inside'"), "column", "bcdefg", false, new ValidationMessage("no a inside") }, { formatJSON("string", "email"), "wrong email", false, new ValidationMessage("validation.message.email") },
{ json("column", "value of", asList("a","b","c","d","e")), "column", "c", true, null }, { formatJSON("string", "uri"), "http://www.javafx.ninja", true, null },
{ json("column", "value of", asList("a","b","c","d","e")), "column", "f", false, new ValidationMessage("validation.message.value.of", "f", "a, b, c, d, e") }, { formatJSON("string", "uri"), "!$%&/()", false, new ValidationMessage("validation.message.uri") },
{ formatJSON("string", "binary"), "dGVzdA==", true, null },
{ formatJSON("string", "binary"), "no binary", false, new ValidationMessage("validation.message.binary") },
{ formatJSON("string", "uuid"), "6ba7b810-9dad-11d1-80b4-00c04fd430c8", true, null },
{ formatJSON("string", "uuid"), "no uuid", false, new ValidationMessage("validation.message.uuid") },
{ formatJSON("date", null), "2015-11-27", true, null },
{ formatJSON("date", null), "27.11.2015", false, new ValidationMessage("validation.message.date.format", "yyyy-MM-dd") },
{ formatJSON("date", "yyyyMMdd"), "20151127", true, null },
{ formatJSON("date", "yyyyMMdd"), "27.11.2015", false, new ValidationMessage("validation.message.date.format", "yyyyMMdd") },
{ customAttributeJSON( "groovy", "value.contains('a')? 'true' : 'no a inside'"), "abcdef", true, null },
{ customAttributeJSON( "groovy", "value.contains('a')? 'true' : 'no a inside'"), "bcdefg", false, new ValidationMessage("no a inside") },
}); });
} }
public static String json(String column, String rule, Object value) { public static String constraintsJSON(String type, String constraint, Object value) {
String json = "{\"headers\": { \"list\": [\""+column+"\"]},\"columns\":{\"" + column + "\":{\"" + rule + "\":"; String json = "{\"fields\": [ { \"name\": \"column\", \"type\" : \"" + type +"\"";
if (value instanceof String) {
json += "\""+ value + "\""; if (constraint != null) {
} else if (value instanceof List) { json += ", \"constraints\": { \"" + constraint + "\":";
List<String> list = (List<String>)value; if (value instanceof String) {
json += "[" + list.stream().collect(joining(", ")) + "]"; json += "\"" + value + "\"";
} else { } else if (value instanceof List) {
json += value; List<String> list = (List<String>) value;
json += "[" + list.stream().collect(joining(", ")) + "]";
} else {
json += value;
}
json += "}";
} }
json += "}}}"; json += "}]}";
return json;
}
public static String formatJSON(String type, String format) {
String json = "{\"fields\": [ { \"name\": \"column\", \"type\" : \"" + type +"\"";
if (format != null) {
json += ", \"format\": \"" + format + "\"";
}
json += "}]}";
return json;
}
public static String customAttributeJSON(String attribute, String value) {
String json = "{\"fields\": [ { \"name\": \"column\", \"type\" : \"string\"";
if (attribute != null) {
json += ", \""+attribute+"\": \"" + value + "\"";
}
json += "}]}";
return json; return json;
} }
} }