Retrieves resources used for test purposes from the file system
| 12 | * |
| 13 | */ |
| 14 | public class TestResource { |
| 15 | /* Set up a map of resource files that can be generically used by all tests |
| 16 | * and some test specific property files that override the default behaviour |
| 17 | * from the standard config.xml file such as for tests using 2 AS2 server instances. |
| 18 | */ |
| 19 | public static final Map<String, String[]> resources = Map.of( |
| 20 | "config", new String[]{"config", "config.xml"}, |
| 21 | "partnerships", new String[]{"config", "partnerships.xml"}, |
| 22 | "server1-props", new String[]{"custom", "server1.properties"}, |
| 23 | "server1-partnerships", new String[]{"custom", "server1_partnerships.xml"}, |
| 24 | "server2-props", new String[]{"custom", "server2.properties"}, |
| 25 | "server2-partnerships", new String[]{"custom", "server2_partnerships.xml"}, |
| 26 | "api-server-props", new String[]{"custom", "api-server.properties"} |
| 27 | ); |
| 28 | |
| 29 | static String pathPrefix = Paths.get("src","test","resources").toAbsolutePath().toString(); |
| 30 | |
| 31 | |
| 32 | public static String getResource(String resourceIdentifier) throws FileNotFoundException { |
| 33 | /** |
| 34 | * Get absolute path to a file identified by the resource identifier passed in. |
| 35 | * |
| 36 | * @param resourceIdentifier - an identifier matching one of the keys in this classes resources map |
| 37 | * @return a file |
| 38 | */ |
| 39 | String[] resourceAttributes = resources.get(resourceIdentifier); |
| 40 | String filePath = get(resourceAttributes); |
| 41 | |
| 42 | return filePath; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Get the absolute path to a file or directory within {@link #resourceBaseFolder} |
| 47 | * |
| 48 | * @param foldersAndFile - a list of optional folders in path with the actual file name as the last in the list |
| 49 | * @return a file |
| 50 | */ |
| 51 | public static String get(String... foldersAndFile) throws FileNotFoundException { |
| 52 | String filePath = pathPrefix + File.separator + StringUtils.join(foldersAndFile, File.separator); |
| 53 | File file = new File(filePath); |
| 54 | if (!file.exists()) { |
| 55 | throw new FileNotFoundException(filePath); |
| 56 | } |
| 57 | return filePath; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Get a File object for a file or directory within {@link #resourceBaseFolder} |
| 62 | * |
| 63 | * @param foldersAndFile - a list of optional folders in path with the actual file name as the last in the list |
| 64 | * @return a File object |
| 65 | */ |
| 66 | public static File getFile(String... foldersAndFile) throws FileNotFoundException { |
| 67 | return new File(get(foldersAndFile)); |
| 68 | } |
| 69 | } |