From f41979960b3844215175838de1cb6d215cd1cb47 Mon Sep 17 00:00:00 2001 From: Andreas Billmann Date: Wed, 23 Oct 2019 05:57:06 +0200 Subject: [PATCH] migrated all test to junit 5 --- build.gradle | 5 + .../smartcsv/files/FileStorageTest.java | 3 - .../smartcsv/fx/table/model/CSVModelTest.java | 10 +- .../smartcsv/fx/table/model/CSVRowTest.java | 8 +- .../preferences/CharsetHelperTest.java | 6 +- .../validation/HeaderValidationTest.java | 76 +++------- .../smartcsv/validation/ValidatorTest.java | 138 ++++++------------ 7 files changed, 89 insertions(+), 157 deletions(-) diff --git a/build.gradle b/build.gradle index 8b52c22..8725552 100644 --- a/build.gradle +++ b/build.gradle @@ -18,6 +18,7 @@ javafx { dependencies { // https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.5.2' + testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-params', version: '5.5.2' testCompile group: 'org.hamcrest', name: 'hamcrest-library', version: '2.2' testCompile group: 'org.mockito', name: 'mockito-core', version:'3.1.0' compile group: 'org.codehaus.groovy', name: 'groovy-all', version: '3.0.0-beta-3' @@ -35,6 +36,10 @@ dependencies { compile group: 'org.fxmisc.richtext', name: 'richtextfx', version: '0.10.2' } +test { + useJUnitPlatform() +} + group 'ninja.javafx' version '0.9.3-SNAPSHOT' mainClassName = 'ninja.javafx.smartcsv.Main' diff --git a/src/test/java/ninja/javafx/smartcsv/files/FileStorageTest.java b/src/test/java/ninja/javafx/smartcsv/files/FileStorageTest.java index 815f8f0..ee289f3 100644 --- a/src/test/java/ninja/javafx/smartcsv/files/FileStorageTest.java +++ b/src/test/java/ninja/javafx/smartcsv/files/FileStorageTest.java @@ -2,12 +2,9 @@ package ninja.javafx.smartcsv.files; import ninja.javafx.smartcsv.FileReader; import ninja.javafx.smartcsv.FileWriter; -import org.junit.Before; -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; - import java.io.File; import static org.hamcrest.MatcherAssert.assertThat; diff --git a/src/test/java/ninja/javafx/smartcsv/fx/table/model/CSVModelTest.java b/src/test/java/ninja/javafx/smartcsv/fx/table/model/CSVModelTest.java index 61f8c50..1e493fd 100644 --- a/src/test/java/ninja/javafx/smartcsv/fx/table/model/CSVModelTest.java +++ b/src/test/java/ninja/javafx/smartcsv/fx/table/model/CSVModelTest.java @@ -26,7 +26,7 @@ package ninja.javafx.smartcsv.fx.table.model; -import org.junit.Test; +import org.junit.jupiter.api.Test; import static org.hamcrest.Matchers.*; import static org.junit.Assert.assertThat; @@ -39,14 +39,14 @@ public class CSVModelTest { //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // constants //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - static final String TESTHEADER = "TESTHEADER"; - static final String TESTVALUE = "TESTVALUE"; - static final String FILEPATH = "filepath"; + private static final String TESTHEADER = "TESTHEADER"; + private static final String TESTVALUE = "TESTVALUE"; + private static final String FILEPATH = "filepath"; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // subject under test //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - CSVModel sut = new CSVModel(); + private CSVModel sut = new CSVModel(); //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // tests diff --git a/src/test/java/ninja/javafx/smartcsv/fx/table/model/CSVRowTest.java b/src/test/java/ninja/javafx/smartcsv/fx/table/model/CSVRowTest.java index 7649fe7..1fef7c0 100644 --- a/src/test/java/ninja/javafx/smartcsv/fx/table/model/CSVRowTest.java +++ b/src/test/java/ninja/javafx/smartcsv/fx/table/model/CSVRowTest.java @@ -1,6 +1,6 @@ package ninja.javafx.smartcsv.fx.table.model; -import org.junit.Test; +import org.junit.jupiter.api.Test; import static org.hamcrest.Matchers.hasKey; import static org.hamcrest.Matchers.is; @@ -14,13 +14,13 @@ public class CSVRowTest { //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // constants //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - static final String COLUMN = "COLUMN"; - static final String VALUE = "VALUE"; + private static final String COLUMN = "COLUMN"; + private static final String VALUE = "VALUE"; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // subject under test //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - CSVRow sut = new CSVRow(); + private CSVRow sut = new CSVRow(); //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/test/java/ninja/javafx/smartcsv/preferences/CharsetHelperTest.java b/src/test/java/ninja/javafx/smartcsv/preferences/CharsetHelperTest.java index 8c5ec5a..81623f3 100644 --- a/src/test/java/ninja/javafx/smartcsv/preferences/CharsetHelperTest.java +++ b/src/test/java/ninja/javafx/smartcsv/preferences/CharsetHelperTest.java @@ -28,6 +28,7 @@ package ninja.javafx.smartcsv.preferences; import org.junit.jupiter.api.Test; +import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import static org.junit.jupiter.api.Assertions.*; @@ -38,18 +39,19 @@ public class CharsetHelperTest { public void getCharsetName_known_charset() { String result = CharsetHelper.getCharsetName("UTF-16"); assertEquals(StandardCharsets.UTF_16.name(), result); + assertNotEquals(StandardCharsets.UTF_16.name(), Charset.defaultCharset()); } @Test public void getCharsetName_unknown_charset() { String result = CharsetHelper.getCharsetName("foobar"); - assertEquals(StandardCharsets.UTF_8.name(), result); + assertEquals(Charset.defaultCharset().name(), result); } @Test public void getCharsetName_null_charset() { String result = CharsetHelper.getCharsetName(null); - assertEquals(StandardCharsets.UTF_8.name(), result); + assertEquals(Charset.defaultCharset().name(), result); } } diff --git a/src/test/java/ninja/javafx/smartcsv/validation/HeaderValidationTest.java b/src/test/java/ninja/javafx/smartcsv/validation/HeaderValidationTest.java index 5f29229..b2a8759 100644 --- a/src/test/java/ninja/javafx/smartcsv/validation/HeaderValidationTest.java +++ b/src/test/java/ninja/javafx/smartcsv/validation/HeaderValidationTest.java @@ -29,13 +29,12 @@ package ninja.javafx.smartcsv.validation; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import ninja.javafx.smartcsv.validation.configuration.ValidationConfiguration; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; -import java.util.Collection; import java.util.List; +import java.util.stream.Stream; import static java.util.Arrays.asList; import static java.util.Collections.singletonList; @@ -46,50 +45,22 @@ import static org.junit.Assert.assertTrue; /** * unit test for header validator */ -@RunWith(Parameterized.class) public class HeaderValidationTest { - //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - // parameters - //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - private ValidationConfiguration config; - private Boolean expectedResult; - private List expectedErrors; - private String[] headerNames; - - //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - // subject under test - //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - private Validator sut; - - - //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - // parameterized constructor - //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - public HeaderValidationTest(String configHeaderNames, - String[] headerNames, - Boolean expectedResult, - List expectedErrors) { - Gson gson = new GsonBuilder().create(); - this.config = gson.fromJson(configHeaderNames, ValidationConfiguration.class); - this.headerNames = headerNames; - this.expectedResult = expectedResult; - this.expectedErrors = expectedErrors; - } - - //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - // init - //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - @Before - public void initialize() { - sut = new Validator(config, new TestColumnValueProvider()); - } - //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // tests //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - @Test - public void validation() { + @ParameterizedTest + @MethodSource("validationConfigurations") + public void validation(String configHeaderNames, + String[] headerNames, + Boolean expectedResult, + List expectedErrors) { + // setup + Gson gson = new GsonBuilder().create(); + ValidationConfiguration config = gson.fromJson(configHeaderNames, ValidationConfiguration.class); + Validator sut = new Validator(config, new TestColumnValueProvider()); + // execution ValidationError result = sut.isHeaderValid(headerNames); @@ -103,15 +74,14 @@ public class HeaderValidationTest { //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // parameters for tests //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - @Parameterized.Parameters - public static Collection validationConfigurations() { - return asList(new Object[][] { - { json(), new String[] {}, true, null }, - { json("a"), new String[] {"a"}, true, null }, - { json("a"), new String[] {"b"}, false, singletonList(new ValidationMessage("validation.message.header.match", "0", "a", "b"))}, - { json("a"), new String[] {"a","b"}, false, singletonList(new ValidationMessage("validation.message.header.length", "2", "1"))}, - { json("a", "b"), new String[] {"b", "a"}, false, asList(new ValidationMessage("validation.message.header.match", "0", "a", "b"), new ValidationMessage("validation.message.header.match", "1", "b", "a")) } - }); + public static Stream validationConfigurations() { + return Stream.of( + Arguments.of( json(), new String[] {}, true, null ), + Arguments.of( json("a"), new String[] {"a"}, true, null ), + Arguments.of( json("a"), new String[] {"b"}, false, singletonList(new ValidationMessage("validation.message.header.match", "0", "a", "b"))), + Arguments.of( json("a"), new String[] {"a","b"}, false, singletonList(new ValidationMessage("validation.message.header.length", "2", "1"))), + Arguments.of( json("a", "b"), new String[] {"b", "a"}, false, asList(new ValidationMessage("validation.message.header.match", "0", "a", "b"), new ValidationMessage("validation.message.header.match", "1", "b", "a")) ) + ); } @SuppressWarnings("StringConcatenationInLoop") diff --git a/src/test/java/ninja/javafx/smartcsv/validation/ValidatorTest.java b/src/test/java/ninja/javafx/smartcsv/validation/ValidatorTest.java index 35d7eb0..2ee9605 100644 --- a/src/test/java/ninja/javafx/smartcsv/validation/ValidatorTest.java +++ b/src/test/java/ninja/javafx/smartcsv/validation/ValidatorTest.java @@ -3,13 +3,12 @@ package ninja.javafx.smartcsv.validation; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import ninja.javafx.smartcsv.validation.configuration.ValidationConfiguration; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; -import java.util.Collection; import java.util.List; +import java.util.stream.Stream; import static java.util.Arrays.asList; import static java.util.stream.Collectors.joining; @@ -20,59 +19,21 @@ import static org.junit.Assert.assertThat; /** * unit test for validator */ -@RunWith(Parameterized.class) public class ValidatorTest { - //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - // parameters - //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - private String config; - private String value; - private Boolean expectedResult; - private ValidationMessage expectedError; - - - //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - // subject under test - //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - private Validator sut; - - - //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - // parameterized constructor - //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - public ValidatorTest(String config, - String value, - Boolean expectedResult, - ValidationMessage expectedError) { - this.config = config; - this.value = value; - this.expectedResult = expectedResult; - this.expectedError = expectedError; - } - - - //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - // init - //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - @Before - public void initialize() { - Gson gson = new GsonBuilder().create(); - ValidationConfiguration validationConfiguration = gson.fromJson(config, ValidationConfiguration.class); - sut = new Validator(validationConfiguration, new TestColumnValueProvider()); - } - - //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // tests //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - @Test - public void validation() { - System.out.println("==================================================="); - System.out.println(config); - System.out.println(value + " " + expectedResult + " " + expectedError); - System.out.println("===================================================\n"); - + @ParameterizedTest + @MethodSource("validationConfigurations") + public void validation(String config, + String value, + Boolean expectedResult, + ValidationMessage expectedError) { + // setup + Gson gson = new GsonBuilder().create(); + ValidationConfiguration validationConfiguration = gson.fromJson(config, ValidationConfiguration.class); + Validator sut = new Validator(validationConfiguration, new TestColumnValueProvider()); // execution ValidationError result = sut.isValid(0, "column", value); @@ -87,45 +48,42 @@ public class ValidatorTest { //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // parameters for tests //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - @Parameterized.Parameters - public static Collection validationConfigurations() { - return asList(new Object[][] { - { constraintsJSON("string", "required", true), "value", true, null }, - { constraintsJSON("string", "required", true), "", false, new ValidationMessage("validation.message.not.empty") }, - { constraintsJSON("string", "required", true), null, false, new ValidationMessage("validation.message.not.empty") }, - { constraintsJSON("integer", null, true), "999", true, null }, - { constraintsJSON("integer", null, true), "a", false, new ValidationMessage("validation.message.integer") }, - { constraintsJSON("number", null, true), "999", true, null }, - { constraintsJSON("number", null, true), "999.000", true, null }, - { constraintsJSON("number", null, true), "a", false, new ValidationMessage("validation.message.double") }, - { constraintsJSON("string", "minLength", 2), "12", true, null }, - { constraintsJSON("string", "minLength", 2), "1", false, new ValidationMessage("validation.message.min.length", "2") }, - { constraintsJSON("string", "maxLength", 2), "12", true, null }, - { constraintsJSON("string", "maxLength", 2), "123", false, new ValidationMessage("validation.message.max.length", "2") }, - { constraintsJSON("string", "pattern", "[a-z]*"), "abc", true, null }, - { constraintsJSON("string", "pattern", "[a-z]*"), "abcA", false, new ValidationMessage("validation.message.regexp", "[a-z]*") }, - { constraintsJSON("string", "enum", asList("a","b","c","d","e")), "c", true, null }, - { constraintsJSON("string", "enum", asList("a","b","c","d","e")), "f", false, new ValidationMessage("validation.message.value.of", "f", "a, b, c, d, e") }, + public static Stream validationConfigurations() { + return Stream.of( + Arguments.of( constraintsJSON("string", "required", true), "value", true, null ), + Arguments.of( constraintsJSON("string", "required", true), "", false, new ValidationMessage("validation.message.not.empty") ), + Arguments.of( constraintsJSON("string", "required", true), null, false, new ValidationMessage("validation.message.not.empty") ), + Arguments.of( constraintsJSON("integer", null, true), "999", true, null ), + Arguments.of( constraintsJSON("integer", null, true), "a", false, new ValidationMessage("validation.message.integer") ), + Arguments.of( constraintsJSON("number", null, true), "999", true, null ), + Arguments.of( constraintsJSON("number", null, true), "999.000", true, null ), + Arguments.of( constraintsJSON("number", null, true), "a", false, new ValidationMessage("validation.message.double") ), + Arguments.of( constraintsJSON("string", "minLength", 2), "12", true, null ), + Arguments.of( constraintsJSON("string", "minLength", 2), "1", false, new ValidationMessage("validation.message.min.length", "2") ), + Arguments.of( constraintsJSON("string", "maxLength", 2), "12", true, null ), + Arguments.of( constraintsJSON("string", "maxLength", 2), "123", false, new ValidationMessage("validation.message.max.length", "2") ), + Arguments.of( constraintsJSON("string", "pattern", "[a-z]*"), "abc", true, null ), + Arguments.of( constraintsJSON("string", "pattern", "[a-z]*"), "abcA", false, new ValidationMessage("validation.message.regexp", "[a-z]*") ), + Arguments.of( constraintsJSON("string", "enum", asList("a","b","c","d","e")), "c", true, null ), + Arguments.of( constraintsJSON("string", "enum", asList("a","b","c","d","e")), "f", false, new ValidationMessage("validation.message.value.of", "f", "a, b, c, d, e") ), - { formatJSON("string", null), "some string", true, null }, - { formatJSON("string", "email"), "test@javafx.ninja", true, null }, - { formatJSON("string", "email"), "wrong email", false, new ValidationMessage("validation.message.email") }, - { formatJSON("string", "uri"), "http://www.javafx.ninja", true, null }, - { 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") }, + Arguments.of( formatJSON("string", null), "some string", true, null ), + Arguments.of( formatJSON("string", "email"), "test@javafx.ninja", true, null ), + Arguments.of( formatJSON("string", "email"), "wrong email", false, new ValidationMessage("validation.message.email") ), + Arguments.of( formatJSON("string", "uri"), "http://www.javafx.ninja", true, null ), + Arguments.of( formatJSON("string", "uri"), "!$%&/()", false, new ValidationMessage("validation.message.uri") ), + Arguments.of( formatJSON("string", "binary"), "dGVzdA==", true, null ), + Arguments.of( formatJSON("string", "binary"), "no binary", false, new ValidationMessage("validation.message.binary") ), + Arguments.of( formatJSON("string", "uuid"), "6ba7b810-9dad-11d1-80b4-00c04fd430c8", true, null ), + Arguments.of( formatJSON("string", "uuid"), "no uuid", false, new ValidationMessage("validation.message.uuid") ), + Arguments.of( formatJSON("date", null), "2015-11-27", true, null ), + Arguments.of( formatJSON("date", null), "27.11.2015", false, new ValidationMessage("validation.message.date.format", "yyyy-MM-dd") ), + Arguments.of( formatJSON("date", "yyyyMMdd"), "20151127", true, null ), + Arguments.of( 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") }, - - }); + Arguments.of( customAttributeJSON( "groovy", "value.contains('a')? 'true' : 'no a inside'"), "abcdef", true, null ), + Arguments.of( customAttributeJSON( "groovy", "value.contains('a')? 'true' : 'no a inside'"), "bcdefg", false, new ValidationMessage("no a inside") ) + ); } public static String constraintsJSON(String type, String constraint, Object value) {