From 696c16740e5ea4c6a894cf287289c49591a594aa Mon Sep 17 00:00:00 2001 From: Grahame Grieve Date: Tue, 4 May 2021 13:04:06 +1000 Subject: [PATCH] Add tests for package name case insensitivity and handle illegal characters in package file names --- .../java/org/hl7/fhir/utilities/Utilities.java | 17 +++++++++++++++++ .../npm/FilesystemPackageCacheManager.java | 2 +- .../tests/CachingPackageClientTests.java | 8 ++++++++ .../fhir/utilities/tests/PackageCacheTests.java | 9 +++++++++ 4 files changed, 35 insertions(+), 1 deletion(-) diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/Utilities.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/Utilities.java index 6eb0ff9bb..15296debb 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/Utilities.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/Utilities.java @@ -1459,6 +1459,23 @@ public class Utilities { return normalizePath; } + final static int[] illegalChars = {34, 60, 62, 124, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 58, 42, 63, 92, 47}; + + static { + Arrays.sort(illegalChars); + } + + public static String cleanFileName(String badFileName) { + StringBuilder cleanName = new StringBuilder(); + int len = badFileName.codePointCount(0, badFileName.length()); + for (int i=0; i fe : e.getValue().getContent().entrySet()) { - String fn = Utilities.path(dir, fe.getKey()); + String fn = Utilities.path(dir, Utilities.cleanFileName(fe.getKey())); byte[] cnt = fe.getValue(); TextFile.bytesToFile(cnt, fn); size = size + cnt.length; diff --git a/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/tests/CachingPackageClientTests.java b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/tests/CachingPackageClientTests.java index 37d39d1c9..fceb206ea 100644 --- a/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/tests/CachingPackageClientTests.java +++ b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/tests/CachingPackageClientTests.java @@ -15,9 +15,17 @@ public class CachingPackageClientTests { CachingPackageClient client = new CachingPackageClient("http://packages.fhir.org"); Assertions.assertTrue(client.exists("hl7.fhir.r4.core", "4.0.1")); Assertions.assertTrue(!client.exists("hl7.fhir.r4.core", "1.0.2")); + Assertions.assertTrue(client.exists("HL7.fhir.r4.core", "4.0.1")); Assertions.assertTrue(!client.exists("hl7.fhir.nothing", "1.0.1")); } + @Test + public void testCase() throws IOException { + CachingPackageClient client = new CachingPackageClient("http://packages.fhir.org"); + Assertions.assertTrue(client.exists("kbv.basis", "1.1.3")); + Assertions.assertTrue(client.exists("KBV.Basis", "1.1.3")); + } + @Test public void testSearch() throws IOException { CachingPackageClient client = new CachingPackageClient("http://packages.fhir.org"); diff --git a/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/tests/PackageCacheTests.java b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/tests/PackageCacheTests.java index 05d38d4d8..97a6c70a1 100644 --- a/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/tests/PackageCacheTests.java +++ b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/tests/PackageCacheTests.java @@ -47,4 +47,13 @@ public class PackageCacheTests { Assertions.assertEquals(cache.loadPackage("hl7.fhir.us.core", "3.1.x").version(), "3.1.1"); Assertions.assertEquals(cache.loadPackage("hl7.fhir.us.core", "3.0.x").version(), "3.0.1"); } + + @Test + public void testNotCaseSensitive() throws IOException { + FilesystemPackageCacheManager cache = new FilesystemPackageCacheManager(true, ToolsVersion.TOOLS_VERSION); + cache.clear(); + Assertions.assertEquals(cache.loadPackage("KBV.Basis", "1.1.3").version(), "1.1.3"); + Assertions.assertEquals(cache.loadPackage("kbv.basis", "1.1.3").version(), "1.1.3"); + + } } \ No newline at end of file