001package org.hl7.fhir.utilities.tests; 002 003import java.io.File; 004import java.io.FileInputStream; 005import java.io.IOException; 006import java.io.InputStream; 007import java.nio.file.Path; 008 009import org.apache.commons.io.IOUtils; 010import org.hl7.fhir.utilities.FileUtilities; 011import org.hl7.fhir.utilities.Utilities; 012import org.hl7.fhir.utilities.filesystem.CSFile; 013import org.hl7.fhir.utilities.filesystem.ManagedFileAccess; 014import org.hl7.fhir.utilities.settings.FhirSettings; 015 016public class BaseTestingUtilities { 017 018 static public boolean silent; 019 020 021 public static String loadTestResource(String... paths) throws IOException { 022 /** 023 * This 'if' condition checks to see if the fhir-test-cases project (https://github.com/FHIR/fhir-test-cases) is 024 * installed locally at the same directory level as the core library project is. If so, the test case data is read 025 * directly from that project, instead of the imported maven dependency jar. It is important, that if you want to 026 * test against the dependency imported from sonatype nexus, instead of your local copy, you need to either change 027 * the name of the project directory to something other than 'fhir-test-cases', or move it to another location, not 028 * at the same directory level as the core project. 029 */ 030 031 String dir = TestConfig.getInstance().getFhirTestCasesDirectory(); 032 if (dir == null && FhirSettings.hasFhirTestCasesPath()) { 033 dir = FhirSettings.getFhirTestCasesPath(); 034 } 035 if (dir != null && ManagedFileAccess.csfile(dir).exists()) { 036 String n = Utilities.path(dir, Utilities.path(paths)); 037 // ok, we'll resolve this locally 038 return FileUtilities.fileToString(ManagedFileAccess.csfile(n)); 039 } else { 040 // resolve from the package 041 String contents; 042 String classpath = ("/org/hl7/fhir/testcases/" + Utilities.pathURL(paths)); 043 try (InputStream inputStream = BaseTestingUtilities.class.getResourceAsStream(classpath)) { 044 if (inputStream == null) { 045 throw new IOException("Can't find file on classpath: " + classpath); 046 } 047 contents = IOUtils.toString(inputStream, java.nio.charset.StandardCharsets.UTF_8); 048 } 049 return contents; 050 } 051 } 052 053 054 public static InputStream loadTestResourceStream(String... paths) throws IOException { 055 String dir = TestConfig.getInstance().getFhirTestCasesDirectory(); 056 if (dir == null && FhirSettings.hasFhirTestCasesPath()) { 057 dir = FhirSettings.getFhirTestCasesPath(); 058 } 059 if (dir != null && ManagedFileAccess.file(dir).exists()) { 060 String n = Utilities.path(dir, Utilities.path(paths)); 061 return ManagedFileAccess.inStream(n); 062 } else { 063 String classpath = ("/org/hl7/fhir/testcases/" + Utilities.pathURL(paths)); 064 InputStream s = BaseTestingUtilities.class.getResourceAsStream(classpath); 065 if (s == null) { 066 throw new Error("unable to find resource " + classpath); 067 } 068 return s; 069 } 070 } 071 072 public static byte[] loadTestResourceBytes(String... paths) throws IOException { 073 String dir = TestConfig.getInstance().getFhirTestCasesDirectory(); 074 if (dir == null && FhirSettings.hasFhirTestCasesPath()) { 075 dir = FhirSettings.getFhirTestCasesPath(); 076 } 077 if (dir != null && ManagedFileAccess.file(dir).exists()) { 078 String n = Utilities.path(dir, Utilities.path(paths)); 079 return FileUtilities.fileToBytes(n); 080 } else { 081 String classpath = ("/org/hl7/fhir/testcases/" + Utilities.pathURL(paths)); 082 InputStream s = BaseTestingUtilities.class.getResourceAsStream(classpath); 083 if (s == null) { 084 throw new Error("unable to find resource " + classpath); 085 } 086 return FileUtilities.streamToBytes(s); 087 } 088 } 089 090 public static boolean findTestResource(String... paths) throws IOException { 091 String dir = TestConfig.getInstance().getFhirTestCasesDirectory(); 092 if (dir == null && FhirSettings.hasFhirTestCasesPath()) { 093 dir = FhirSettings.getFhirTestCasesPath(); 094 } 095 if (dir != null && ManagedFileAccess.file(dir).exists()) { 096 String n = Utilities.path(dir, Utilities.path(paths)); 097 return ManagedFileAccess.file(n).exists(); 098 } else { 099 String classpath = ("/org/hl7/fhir/testcases/" + Utilities.pathURL(paths)); 100 try { 101 InputStream inputStream = BaseTestingUtilities.class.getResourceAsStream(classpath); 102 return inputStream != null; 103 } catch (Throwable t) { 104 return false; 105 } 106 } 107 } 108 109 public static String tempFile(String folder, String name) throws IOException { 110 String tmp = tempFolder(folder); 111 return Utilities.path(tmp, name); 112 } 113 114 public static String tempFolder(String name) throws IOException { 115 String path = Utilities.path(FhirSettings.hasTempPath() ? FhirSettings.getTempPath() : "[tmp]", name); 116 FileUtilities.createDirectory(path); 117 return path; 118 } 119 120 public static void setFhirTestCasesDirectory(String s) { 121 } 122 123 public static void createParentDirIfNotExists(Path target) throws IOException { 124 Path parent = target.getParent(); 125 if (!ManagedFileAccess.fromPath(parent).exists()) { 126 ManagedFileAccess.fromPath(parent).mkdirs(); 127 } 128 } 129}