From 3c7abb5effb3d23dd76d850e786833ad873b743f Mon Sep 17 00:00:00 2001 From: dotasek Date: Tue, 13 Jun 2023 17:29:16 -0400 Subject: [PATCH] Test coverage --- .../validation/cli/utils/AsteriskFilter.java | 6 ++ .../cli/utils/AsteriskFilterTests.java | 76 +++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 org.hl7.fhir.validation/src/test/java/org/hl7/fhir/validation/cli/utils/AsteriskFilterTests.java diff --git a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/utils/AsteriskFilter.java b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/utils/AsteriskFilter.java index 432485cc0..1e203b7fe 100644 --- a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/utils/AsteriskFilter.java +++ b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/utils/AsteriskFilter.java @@ -5,6 +5,8 @@ import java.io.FilenameFilter; import java.io.IOException; public class AsteriskFilter implements FilenameFilter { + + String dir; String regex; @@ -22,6 +24,10 @@ public class AsteriskFilter implements FilenameFilter { else regex = regex + "\\" + expression.charAt(i); } + isDirValid(); + } + + protected void isDirValid() throws IOException { File f = new File(dir); if (!f.exists()) { throw new IOException("Directory " + dir + " does not exist"); diff --git a/org.hl7.fhir.validation/src/test/java/org/hl7/fhir/validation/cli/utils/AsteriskFilterTests.java b/org.hl7.fhir.validation/src/test/java/org/hl7/fhir/validation/cli/utils/AsteriskFilterTests.java new file mode 100644 index 000000000..5c41bc283 --- /dev/null +++ b/org.hl7.fhir.validation/src/test/java/org/hl7/fhir/validation/cli/utils/AsteriskFilterTests.java @@ -0,0 +1,76 @@ +package org.hl7.fhir.validation.cli.utils; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; +import org.junit.jupiter.params.provider.ValueSource; + +import javax.annotation.Nonnull; +import java.io.IOException; +import java.util.stream.Stream; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class AsteriskFilterTests { + + private static Stream provideValidFiltersAndDirsForwardSlash() { + return Stream.of( + Arguments.of("one/*", "one/", ".*"), + Arguments.of("/one/*", "/one/", ".*"), + Arguments.of("/one/*.*", "/one/", ".*\\..*"), + Arguments.of("/one/two.*", "/one/", "two\\..*"), + Arguments.of("/one/*.two", "/one/", ".*\\.two"), + Arguments.of("one/*/*", "one/*/", ".*"), + Arguments.of("one/*/*.*", "one/*/", ".*\\..*"), + Arguments.of("/one/two/*", "/one/two/",".*"), + Arguments.of("one/two/*/*.*", "one/two/*/", ".*\\..*"), + Arguments.of("one/two/*/three.*", "one/two/*/", "three\\..*"), + Arguments.of("one/two/*/*.three", "one/two/*/", ".*\\.three"), + Arguments.of("one/two/*/*.*three", "one/two/*/", ".*\\..*three"), + Arguments.of("/one-two/*", "/one-two/", ".*"), + Arguments.of("/one-two/three/*", "/one-two/three/", ".*") + ); + } + + @ParameterizedTest + @MethodSource("provideValidFiltersAndDirsForwardSlash") + public void testValidFilterDirAndRegex(String filterString, String expectedDir, String expectedRegex) throws IOException { + //Test with forward slash separators + testFilterDirExtraction(filterString, expectedDir, expectedRegex); + //Test with backward slash separators + testFilterDirExtraction(filterString.replace('/','\\'), expectedDir.replace('/','\\'), expectedRegex.replace('/','\\')); + } + + private static void testFilterDirExtraction(String filterString, String expectedDir, String expectedRegex) throws IOException { + AsteriskFilter asteriskFilter = getAsteriskFilterWithoutDirValidityCheck(filterString); + assertEquals(expectedRegex, asteriskFilter.regex); + assertEquals(expectedDir, asteriskFilter.dir); + } + + @ParameterizedTest + @ValueSource( + strings = { + "", + "one/", + "one\\", + "one/two", + "one\\two" + } + ) + public void testInvalidFilterThrowsException(String filterString) { + IOException thrown = Assertions.assertThrows(IOException.class, () -> { + AsteriskFilter asteriskFilter = getAsteriskFilterWithoutDirValidityCheck(filterString); + }, "NumberFormatException was expected"); + } + + @Nonnull + private static AsteriskFilter getAsteriskFilterWithoutDirValidityCheck(String filterString) throws IOException { + return new AsteriskFilter(filterString) { + @Override + protected void isDirValid() throws IOException { + // Dont check for existent directory in this case. + } + }; + } +}