|
7 | 7 |
|
8 | 8 | public class TestResourceFinder |
9 | 9 | { |
| 10 | + private static final String DATA_HOME_PROPERTY = "bc.test.data.home"; |
| 11 | + private static final String DATA_HOME_ENV = "BC_TEST_DATA_HOME"; |
10 | 12 | private static final String dataDirName = "bc-test-data"; |
11 | 13 |
|
12 | 14 | /** |
13 | | - * We search starting at the working directory looking for the bc-test-data directory. |
| 15 | + * Resolve a test fixture from the bc-test-data tree. |
| 16 | + * <p> |
| 17 | + * Resolution order for the bc-test-data root: |
| 18 | + * <ol> |
| 19 | + * <li>The {@code bc.test.data.home} system property, if set.</li> |
| 20 | + * <li>The {@code BC_TEST_DATA_HOME} environment variable, if set.</li> |
| 21 | + * <li>Walk up from the working directory looking for a directory literally named |
| 22 | + * {@code bc-test-data} (the legacy resolution path, for direct test |
| 23 | + * invocations that don't set either).</li> |
| 24 | + * </ol> |
| 25 | + * When the property or environment variable is supplied, the named path is |
| 26 | + * required to exist; a mistyped value fails fast rather than silently falling |
| 27 | + * through to the walk-up. |
14 | 28 | * |
15 | | - * @throws FileNotFoundException |
| 29 | + * @throws FileNotFoundException if no lookup locates the bc-test-data root. |
16 | 30 | */ |
17 | 31 | public static InputStream findTestResource(String homeDir, String fileName) |
18 | 32 | throws FileNotFoundException |
19 | 33 | { |
20 | | - String wrkDirName = System.getProperty("user.dir"); |
21 | 34 | String separator = System.getProperty("file.separator"); |
| 35 | + |
| 36 | + String configured = System.getProperty(DATA_HOME_PROPERTY); |
| 37 | + String configuredSource = "-D" + DATA_HOME_PROPERTY; |
| 38 | + if (configured == null || configured.length() == 0) |
| 39 | + { |
| 40 | + configured = System.getenv(DATA_HOME_ENV); |
| 41 | + configuredSource = "$" + DATA_HOME_ENV; |
| 42 | + } |
| 43 | + if (configured != null && configured.length() > 0) |
| 44 | + { |
| 45 | + File dataDir = new File(configured); |
| 46 | + if (!dataDir.exists()) |
| 47 | + { |
| 48 | + String ln = System.getProperty("line.separator"); |
| 49 | + throw new FileNotFoundException("Test data directory '" + configured |
| 50 | + + "' from " + configuredSource + " not found." + ln |
| 51 | + + "Test data available from: https://github.com/bcgit/bc-test-data.git"); |
| 52 | + } |
| 53 | + return new FileInputStream(new File(dataDir, homeDir + separator + fileName)); |
| 54 | + } |
| 55 | + |
| 56 | + String wrkDirName = System.getProperty("user.dir"); |
22 | 57 | File wrkDir = new File(wrkDirName); |
23 | 58 | File dataDir = new File(wrkDir, dataDirName); |
24 | 59 | while (!dataDir.exists() && wrkDirName.length() > 1) |
|
0 commit comments