ResourceHandler { URL getResourceUrl(String path) throws MalformedURLException { final String dddbDirFromProperty = System.getProperty(SMOS_DDDB_DIR_PROPERTY_NAME); if (StringUtils.isNotNullAndNotEmpty(dddbDirFromProperty)) { final File resourceFile = new File(dddbDirFromProperty, path); if (resourceFile.isFile()) { return new URL("file", "", 0, resourceFile.getAbsolutePath()); } } return getClass().getResource(path); }    }
@Test public void testGetResourceURL_fromJar() throws IOException { System.clearProperty(ResourceHandler.SMOS_DDDB_DIR_PROPERTY_NAME); final String resourcePath = "bands/AUX_/DGGFLO/DBL_SM_XXXX_AUX_DGGFLO_0400.csv"; final URL resourceUrl = resourceHandler.getResourceUrl(resourcePath); assertNotNull(resourceUrl); assertThat(resourceUrl.getPath(), containsString(resourcePath)); assertEquals("file", resourceUrl.getProtocol()); }
@Test public void testGetResourceURL_fromDirectory() throws IOException { File targetDirectory = null; try { targetDirectory = createTestDirectory(); System.setProperty(ResourceHandler.SMOS_DDDB_DIR_PROPERTY_NAME, targetDirectory.getAbsolutePath()); final File targetFile = new File(targetDirectory, "wurst"); if (!targetFile.createNewFile()) { fail("unable to create test file"); } final URL resourceUrl = resourceHandler.getResourceUrl("wurst"); assertNotNull(resourceUrl); assertEquals(targetDirectory.getAbsolutePath() + File.separator + "wurst", resourceUrl.getPath()); assertEquals("file", resourceUrl.getProtocol()); } finally { System.clearProperty(ResourceHandler.SMOS_DDDB_DIR_PROPERTY_NAME); if (targetDirectory != null && targetDirectory.isDirectory()) { if (!FileUtils.deleteTree(targetDirectory)) { fail("Unable to delete test directory"); } } } }
@Test public void testGetResourceURL_fromDirectory_resourceFileDoesNotExist() throws IOException { File targetDirectory = null; try { targetDirectory = createTestDirectory(); System.setProperty(ResourceHandler.SMOS_DDDB_DIR_PROPERTY_NAME, targetDirectory.getAbsolutePath()); final URL resourceUrl = resourceHandler.getResourceUrl("wurst"); assertNull(resourceUrl); } finally { System.clearProperty(ResourceHandler.SMOS_DDDB_DIR_PROPERTY_NAME); if (targetDirectory != null && targetDirectory.isDirectory()) { if (!FileUtils.deleteTree(targetDirectory)) { fail("Unable to delete test directory"); } } } }