diff --git a/build.gradle b/build.gradle index 1f76b2c..3a477d7 100644 --- a/build.gradle +++ b/build.gradle @@ -25,7 +25,8 @@ dependencies { implementation 'org.apache.commons:commons-csv:1.9.0' implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.13.0' implementation 'com.google.code.gson:gson:2.8.8' - testImplementation 'junit:junit:[4.13.2,)' + testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1' + testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.8.1' testImplementation 'org.mockito:mockito-all:1.10.19' } @@ -52,12 +53,13 @@ shadowJar { } test { + useJUnitPlatform() finalizedBy jacocoTestReport } jacocoTestReport { dependsOn test reports { - xml.enabled true + xml.required = true } } @@ -103,7 +105,7 @@ if (isForSigning && isForOssrhPublishing) { licenses { license { name = 'The Apache License, Version 2.0' - url = 'http://www.apache.org/licenses/LICENSE-2.0.txt' + url = 'https://www.apache.org/licenses/LICENSE-2.0.txt' } } diff --git a/src/test/java/com/sonalake/utah/ExamplesTest.java b/src/test/java/com/sonalake/utah/ExamplesTest.java index 95380ba..0066316 100644 --- a/src/test/java/com/sonalake/utah/ExamplesTest.java +++ b/src/test/java/com/sonalake/utah/ExamplesTest.java @@ -2,8 +2,8 @@ import com.sonalake.utah.config.Config; import com.sonalake.utah.config.ConfigLoader; -import org.junit.Assert; -import org.junit.Test; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; import java.io.IOException; import java.io.InputStreamReader; @@ -346,7 +346,7 @@ public void testGetHeaderNames() throws IOException { String[] expectedHeaderNames = {"remoteIp", "uptime", "activeV4", "receivedV4", "accepted_V4", "activeV6", "receivedV6", "accepted_V6", "activeV4", "receivedV4", "accepted_V4", "activeV6", "receivedV6", "accepted_V6"}; - Assert.assertEquals(Arrays.asList(expectedHeaderNames), config.getHeaderNames()); + Assertions.assertEquals(Arrays.asList(expectedHeaderNames), config.getHeaderNames()); } /** @@ -378,7 +378,7 @@ private void testFileProcessing(String configResource, String fileResource, List } } - Assert.assertEquals(expectedResults, observedValues); + Assertions.assertEquals(expectedResults, observedValues); } } diff --git a/src/test/java/com/sonalake/utah/cli/CliConfigTest.java b/src/test/java/com/sonalake/utah/cli/CliConfigTest.java index d0f2e14..ea0de59 100644 --- a/src/test/java/com/sonalake/utah/cli/CliConfigTest.java +++ b/src/test/java/com/sonalake/utah/cli/CliConfigTest.java @@ -3,7 +3,8 @@ import com.sonalake.utah.Parser; import com.sonalake.utah.config.Config; import org.apache.commons.io.FileUtils; -import org.junit.Test; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; import java.io.File; import java.io.FileNotFoundException; @@ -14,19 +15,16 @@ import java.util.Map; import java.util.UUID; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - public class CliConfigTest { @Test - public void testLoadWorks() throws IOException, IOException { + public void testLoadWorks() throws IOException { File testFile = getConfigFileForCiscoBGPSummary(); CLIConfig cliConfig = new CLIConfig(CLIConfig.Format.CSV, testFile.getAbsolutePath()); Config config = cliConfig.loadConfig(); - assertNotNull("No config created", config); + Assertions.assertNotNull(config, "No config created"); } private File getConfigFileForCiscoBGPSummary() throws IOException { @@ -37,10 +35,12 @@ private File getConfigFileForCiscoBGPSummary() throws IOException { } - @Test(expected = FileNotFoundException.class) - public void testLoadHandlesErrors() throws IOException { - CLIConfig cliConfig = new CLIConfig(CLIConfig.Format.CSV, UUID.randomUUID().toString()); - cliConfig.loadConfig(); + @Test + public void testLoadHandlesErrors() { + Assertions.assertThrows(FileNotFoundException.class, () -> { + CLIConfig cliConfig = new CLIConfig(CLIConfig.Format.CSV, UUID.randomUUID().toString()); + cliConfig.loadConfig(); + }); } @Test @@ -58,12 +58,13 @@ public void testParser() throws IOException { Parser parser = cli.parseInput(cliConfig, reader); Map result = parser.next(); - assertNotNull("Expected results from parser", result); - assertEquals("routerId record field incorrect", "192.0.2.70", result.get("routerId")); - assertEquals("localAS record field incorrect", "65550", result.get("localAS")); - assertEquals("remoteIp record field incorrect", "192.0.2.77", result.get("remoteIp")); - assertEquals("remoteAS record field incorrect", "65551", result.get("remoteAS")); - assertEquals("uptime record field incorrect", "5w4d", result.get("uptime")); - assertEquals("status record field incorrect", "1", result.get("status")); + Assertions.assertAll( + () -> Assertions.assertNotNull(result, "Expected results from parser"), + () -> Assertions.assertEquals( "192.0.2.70", result.get("routerId"), "routerId record field incorrect"), + () -> Assertions.assertEquals( "65550", result.get("localAS"), "localAS record field incorrect"), + () -> Assertions.assertEquals( "192.0.2.77", result.get("remoteIp"), "remoteIp record field incorrect"), + () -> Assertions.assertEquals( "65551", result.get("remoteAS"), "remoteAS record field incorrect"), + () -> Assertions.assertEquals( "5w4d", result.get("uptime"), "uptime record field incorrect"), + () -> Assertions.assertEquals( "1", result.get("status"), "status record field incorrect")); } } diff --git a/src/test/java/com/sonalake/utah/cli/CommandLineInterfaceTest.java b/src/test/java/com/sonalake/utah/cli/CommandLineInterfaceTest.java index 7f74959..15db594 100644 --- a/src/test/java/com/sonalake/utah/cli/CommandLineInterfaceTest.java +++ b/src/test/java/com/sonalake/utah/cli/CommandLineInterfaceTest.java @@ -8,13 +8,13 @@ import org.apache.commons.csv.CSVParser; import org.apache.commons.csv.CSVRecord; import org.apache.commons.lang3.StringUtils; -import org.junit.Test; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; import java.io.*; import java.nio.charset.StandardCharsets; import java.util.*; -import static org.junit.Assert.*; import static org.mockito.Matchers.any; import static org.mockito.Mockito.*; @@ -28,12 +28,8 @@ public void testLogHelp() { @Test public void testRequiredArgsMissing() { - try { - generateCommandline(""); - fail("This should have failed, no args were supplied"); - } catch (ParseException expected) { - assertTrue(expected.getMessage().contains("No configuration file supplied")); - } + Throwable exception = Assertions.assertThrows(ParseException.class, () -> generateCommandline("")); + Assertions.assertTrue(exception.getMessage().contains("No configuration file supplied")); } /* @@ -43,9 +39,9 @@ public void testRequiredArgsMissing() { public void testArgCsv() throws ParseException { CLIConfig config = generateCommandline(" -o csv -f config.xml"); - assertEquals(CLIConfig.Format.CSV, config.getFormat()); - assertEquals("config.xml", config.getPathToConfig()); - + Assertions.assertAll( + () -> Assertions.assertEquals(CLIConfig.Format.CSV, config.getFormat()), + () -> Assertions.assertEquals("config.xml", config.getPathToConfig())); } /* @@ -55,9 +51,9 @@ public void testArgCsv() throws ParseException { public void testArgJson() throws ParseException { CLIConfig config = generateCommandline(" -o json -f config.xml"); - assertEquals(CLIConfig.Format.JSON, config.getFormat()); - assertEquals("config.xml", config.getPathToConfig()); - + Assertions.assertAll( + () -> Assertions.assertEquals(CLIConfig.Format.JSON, config.getFormat()), + () -> Assertions.assertEquals("config.xml", config.getPathToConfig())); } /* @@ -67,9 +63,9 @@ public void testArgJson() throws ParseException { public void testArgDefaultToCSV() throws ParseException { CLIConfig config = generateCommandline(" -f config.xml"); - assertEquals(CLIConfig.Format.CSV, config.getFormat()); - assertEquals("config.xml", config.getPathToConfig()); - + Assertions.assertAll( + () -> Assertions.assertEquals(CLIConfig.Format.CSV, config.getFormat()), + () -> Assertions.assertEquals("config.xml", config.getPathToConfig())); } /* @@ -77,12 +73,8 @@ public void testArgDefaultToCSV() throws ParseException { */ @Test public void testArgBadFormat() { - try { - generateCommandline(" -o monkey -f config.xml"); - fail("This should have failed, monkey is not a valid format"); - } catch (ParseException expected) { - assertTrue(expected.getMessage().contains("monkey is not a valid format")); - } + Throwable exception = Assertions.assertThrows(ParseException.class, () -> generateCommandline(" -o monkey -f config.xml")); + Assertions.assertTrue(exception.getMessage().contains("monkey is not a valid format")); } /* @@ -96,7 +88,7 @@ public void testPrintOutputJSON() { map.put("name", "23"); map.put("gender", "23"); mapList.add(map); - assertTrue(CommandLineInterface.mapListToJSON(mapList).contains("\"age\": \"23\",")); + Assertions.assertTrue(CommandLineInterface.mapListToJSON(mapList).contains("\"age\": \"23\",")); } /* @@ -124,7 +116,7 @@ public void testJsonFormat() { String expected = new GsonBuilder().setPrettyPrinting().create().toJson(parsedRecords); String observed =helper.getOutputAsString(); - assertEquals(expected, observed); + Assertions.assertEquals(expected, observed); } private class OutputHelper { @@ -180,9 +172,8 @@ public void testHelpOutputForNoArgs() { new StringReader("some input file \n with another line"), helper.target); - assertTrue(String.format("Wrong message: %s", helper.getOutputAsString()), - helper.getOutputAsString().contains("usage: ")); - + Assertions.assertTrue(helper.getOutputAsString().contains("usage: "), + String.format("Wrong message: %s", helper.getOutputAsString())); } /** @@ -196,8 +187,8 @@ public void testBranchingForCsv() throws ParseException, IOException { OutputHelper helper = processMockOutput(format); - assertTrue(String.format("Wrong message:\n%s", helper.getOutputAsString()), - helper.getOutputAsString().contains("xyz")); + Assertions.assertTrue(helper.getOutputAsString().contains("xyz"), + String.format("Wrong message:\n%s", helper.getOutputAsString())); } @Test @@ -206,8 +197,8 @@ public void testBranchingForWrongFormat() throws ParseException, IOException { OutputHelper helper = processMockOutput(format); - assertTrue(String.format("Wrong message:\n%s", helper.getOutputAsString()), - helper.getOutputAsString().contains("xyz")); + Assertions.assertTrue(helper.getOutputAsString().contains("xyz"), + String.format("Wrong message:\n%s", helper.getOutputAsString())); } /** @@ -221,8 +212,8 @@ public void testBranchingForJson() throws ParseException, IOException { OutputHelper helper = processMockOutput(format); - assertTrue(String.format("Wrong message:\n %s", helper.getOutputAsString()), - helper.getOutputAsString().contains("\"a\": \"xyz\"")); + Assertions.assertTrue(helper.getOutputAsString().contains("\"a\": \"xyz\""), + String.format("Wrong message:\n %s", helper.getOutputAsString())); } /** @@ -258,7 +249,7 @@ private void assertCsvContent(String content, List> expected) throw } observed.add(lineValues); } - assertEquals(expected, observed); + Assertions.assertEquals(expected, observed); } private CLIConfig generateCommandline(String cli) throws ParseException { diff --git a/src/test/java/com/sonalake/utah/config/ConfigTests.java b/src/test/java/com/sonalake/utah/config/ConfigTests.java index 5026e1a..a4a770c 100644 --- a/src/test/java/com/sonalake/utah/config/ConfigTests.java +++ b/src/test/java/com/sonalake/utah/config/ConfigTests.java @@ -1,8 +1,8 @@ package com.sonalake.utah.config; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; @@ -20,10 +20,6 @@ import java.io.StringWriter; import java.util.Map; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - /** * A test of the configuration classes */ @@ -35,7 +31,7 @@ public class ConfigTests { */ private Document document; - @Before + @BeforeEach public void setup() throws ParserConfigurationException { document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); } @@ -43,35 +39,40 @@ public void setup() throws ParserConfigurationException { /** * If the config has no delimiter, then fail out */ - @Test(expected = IllegalArgumentException.class) - public void testConfigHasNoDelimiter() throws TransformerException, IOException { - createEmptyDocument(); - new ConfigLoader().loadConfig(buildDocReader()); + @Test + public void testConfigHasNoDelimiter() { + Assertions.assertThrows(IllegalArgumentException.class, () -> { + createEmptyDocument(); + new ConfigLoader().loadConfig(buildDocReader()); + }); } /** * If the config has values with no groups, then fail out */ - @Test(expected = IllegalArgumentException.class) - public void testConfigMappingHasNoGroup() throws TransformerException, IOException { - createEmptyDocument(); - addDelimiter("DELIM"); - addSearch("number", "\\d*"); - addValue("value", "{number}"); - new ConfigLoader().loadConfig(buildDocReader()); - + @Test + public void testConfigMappingHasNoGroup() { + Assertions.assertThrows(IllegalArgumentException.class, () -> { + createEmptyDocument(); + addDelimiter("DELIM"); + addSearch("number", "\\d*"); + addValue("value", "{number}"); + new ConfigLoader().loadConfig(buildDocReader()); + }); } /** * If the config has values with no groups, then fail out */ - @Test(expected = IllegalArgumentException.class) - public void testConfigMappingWhenInvalidRegex() throws TransformerException, IOException { - createEmptyDocument(); - addDelimiter("DELIM"); - addSearch("number", "(\\d*)"); - addValue("value", "{number}("); - new ConfigLoader().loadConfig(buildDocReader()); + @Test + public void testConfigMappingWhenInvalidRegex() { + Assertions.assertThrows(IllegalArgumentException.class, () -> { + createEmptyDocument(); + addDelimiter("DELIM"); + addSearch("number", "(\\d*)"); + addValue("value", "{number}("); + new ConfigLoader().loadConfig(buildDocReader()); + }); } /** @@ -86,7 +87,7 @@ public void testValidValueConfigOk() throws TransformerException, IOException { Config config = new ConfigLoader().loadConfig(buildDocReader()); Map record = config.buildRecord("this is a value 123 hello"); - assertEquals("123", record.get("value")); + Assertions.assertEquals("123", record.get("value")); } /** @@ -101,7 +102,7 @@ public void testValidHeaderValueConfigOk() throws TransformerException, IOExcept Config config = new ConfigLoader().loadConfig(buildDocReader()); Map record = config.buildHeader("this is a value 123 hello"); - assertEquals("123", record.get("value")); + Assertions.assertEquals("123", record.get("value")); } /** @@ -113,9 +114,9 @@ public void testHeaderDelimiterValueConfigOk() throws TransformerException, IOEx addDelimiter("DELIM"); addHeaderDelimiter("HEADER-DELIM"); Config config = new ConfigLoader().loadConfig(buildDocReader()); - assertTrue(config.matchesHeaderDelim("HEADER-DELIM")); - assertFalse(config.matchesHeaderDelim("something else")); - + Assertions.assertAll( + () -> Assertions.assertTrue(config.matchesHeaderDelim("HEADER-DELIM")), + () -> Assertions.assertFalse(config.matchesHeaderDelim("something else"))); } @Test @@ -125,10 +126,11 @@ public void testPerLine() throws TransformerException, IOException { Config config = new ConfigLoader().loadConfig(buildDocReader()); Delimiter delimiter = config.delimiters.get(0); - Assert.assertNotNull(delimiter); - assertTrue(delimiter.isPerLine); - assertTrue(delimiter.matches("a line")); - assertFalse(delimiter.matches("")); + Assertions.assertAll( + () -> Assertions.assertNotNull(delimiter), + () -> Assertions.assertTrue(delimiter.isPerLine), + () -> Assertions.assertTrue(delimiter.matches("a line")), + () -> Assertions.assertFalse(delimiter.matches(""))); } @Test @@ -139,9 +141,10 @@ public void testRetainDelim() throws TransformerException, IOException { Config config = new ConfigLoader().loadConfig(buildDocReader()); Delimiter delimiter = config.delimiters.get(0); - Assert.assertNotNull(delimiter); - assertTrue(delimiter.isRetainDelim()); - assertTrue(delimiter.isRetainDelim); + Assertions.assertAll( + () -> Assertions.assertNotNull(delimiter), + () -> Assertions.assertTrue(delimiter.isRetainDelim()), + () -> Assertions.assertTrue(delimiter.isRetainDelim)); } @Test @@ -152,14 +155,14 @@ public void testDelimAtStart() throws TransformerException, IOException { Config config = new ConfigLoader().loadConfig(buildDocReader()); Delimiter delimiter = config.delimiters.get(0); - Assert.assertNotNull(delimiter); - assertTrue(delimiter.isDelimAtStartOfRecord()); - - // here we check if the retain delim field is false - // but the start of record is true, that the retain delim - // operation will still return true - assertTrue(delimiter.isRetainDelim()); - assertFalse(delimiter.isRetainDelim); + Assertions.assertAll( + () -> Assertions.assertNotNull(delimiter), + () -> Assertions.assertTrue(delimiter.isDelimAtStartOfRecord()), + // here we check if the retain delim field is false + // but the start of record is true, that the retain delim + // operation will still return true + () -> Assertions.assertTrue(delimiter.isRetainDelim()), + () -> Assertions.assertFalse(delimiter.isRetainDelim)); } /** @@ -174,7 +177,7 @@ public void testValidHeaderConfigOk() throws TransformerException, IOException { Config config = new ConfigLoader().loadConfig(buildDocReader()); Map header = config.buildHeader("this is a header 999y hello"); - assertEquals("999", header.get("header")); + Assertions.assertEquals("999", header.get("header")); } /** diff --git a/src/test/java/com/sonalake/utah/config/ParserTest.java b/src/test/java/com/sonalake/utah/config/ParserTest.java index 6970dfd..61de172 100644 --- a/src/test/java/com/sonalake/utah/config/ParserTest.java +++ b/src/test/java/com/sonalake/utah/config/ParserTest.java @@ -1,23 +1,22 @@ package com.sonalake.utah.config; import com.sonalake.utah.Parser; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.TreeMap; -import static org.junit.Assert.assertEquals; - /** * A test of the parser */ public class ParserTest { private Config config; - @Before + @BeforeEach public void setup() { config = new Config(); config.delimiters = new ArrayList(); @@ -53,7 +52,7 @@ public void testPerRecordParser() { }}); }}; - assertEquals(expectedValues, results); + Assertions.assertEquals(expectedValues, results); } /** @@ -84,7 +83,7 @@ public void testPerLineParser() { }}); }}; - assertEquals(expectedValues, results); + Assertions.assertEquals(expectedValues, results); } /** @@ -122,7 +121,7 @@ public void testPerLineParserWithHeader() { }}); }}; - assertEquals(expectedValues, results); + Assertions.assertEquals(expectedValues, results); } /** @@ -161,7 +160,7 @@ public void testMultipleDelimiters() { }}); }}; - assertEquals(expectedValues, results); + Assertions.assertEquals(expectedValues, results); } private void addHeaderDelimiter(String s) {