001package org.hl7.fhir.utilities.tests; 002 003import java.io.IOException; 004import java.io.InputStream; 005import java.nio.file.Path; 006import java.nio.file.StandardCopyOption; 007 008public interface ResourceLoaderTests { 009 010 static final String PATH_DELIMITER = "/"; 011 012 public static InputStream getResourceAsInputStream(Class<?> clazz, String... resourcePath) { 013 return clazz.getClassLoader().getResourceAsStream(String.join(PATH_DELIMITER, resourcePath)); 014 } 015 public default InputStream getResourceAsInputStream(String ... resourcePath) { 016 return getResourceAsInputStream(this.getClass(), resourcePath); 017 } 018 019 public default void copyResourceToFile(Path target, String ... resourcePath) throws IOException { 020 copyResourceToFile(this.getClass(), target, resourcePath); 021 } 022 023 public static void copyResourceToFile(Class<?> clazz, Path target, String ... resourcePath) throws IOException { 024 InputStream initialStream = getResourceAsInputStream(clazz, resourcePath); 025 BaseTestingUtilities.createParentDirIfNotExists(target); 026 java.nio.file.Files.copy( 027 initialStream, 028 target, 029 StandardCopyOption.REPLACE_EXISTING); 030 031 initialStream.close(); 032 } 033 034}