FilesystemPathAccessibleValidation implements Validation { @Override public ValidationResult validate(Config config) { if (!config.hasPath(path)) { return new ValidationResult(this, Validity.VALID, "Configuration '" + path + "' does not exist, so will not check if its value as a filesystem path could be opened"); } String filesystemPath = config.getString(path); InputStream stream; try { FileSystem fs = FileSystem.get(new URI(filesystemPath), new Configuration()); stream = fs.open(new Path(filesystemPath)); stream.close(); } catch (Exception e) { return new ValidationResult(this, Validity.INVALID, "Filesystem path '" + filesystemPath + "' could not be opened.", e); } return new ValidationResult(this, Validity.VALID, "Filesystem path '" + filesystemPath + "' could be opened"); } FilesystemPathAccessibleValidation(String path); @Override ValidationResult validate(Config config); @Override String toString(); @Override Set<String> getKnownPaths();  }
@Test public void testAccessible() throws Exception { File file = new File("test_accessible"); file.createNewFile(); file.deleteOnExit(); Validation v = new FilesystemPathAccessibleValidation("hello"); ValidationResult vr = v.validate(ConfigFactory.parseString("hello = \"test_accessible\"")); assertEquals(vr.getValidity(), Validity.VALID); }
@Test public void testInaccessible() { Validation v = new FilesystemPathAccessibleValidation("hello"); ValidationResult vr = v.validate(ConfigFactory.parseString("hello = \"test_inaccessible\"")); assertEquals(vr.getValidity(), Validity.INVALID); }
@Test public void testConfigPathDoesntExist() { Validation v = new FilesystemPathAccessibleValidation("hello"); ValidationResult vr = v.validate(ConfigFactory.empty()); assertEquals(vr.getValidity(), Validity.VALID); }