OptionalPathValidation implements Validation { @Override public ValidationResult validate(Config config) { if (config.hasPath(path)) { if (type != null) { if (config.getValue(path).valueType().equals(type) || ConfigUtils.canBeCoerced(config, path, type)) { return new ValidationResult(this, Validity.VALID, "Optional configuration was found with required type: " + path); } else { return new ValidationResult(this, Validity.INVALID, "Optional configuration '" + path + "' was found but does not have required type: " + type); } } else { return new ValidationResult(this, Validity.VALID, "Optional configuration was found: " + path); } } else { return new ValidationResult(this, Validity.VALID, "Optional configuration was not found: " + path); } } OptionalPathValidation(String path);  OptionalPathValidation(String path, ConfigValueType type); @Override ValidationResult validate(Config config); @Override String toString(); @Override Set<String> getKnownPaths();  }
@Test public void testPathExistsWithoutSpecifiedType() { Validation v = new OptionalPathValidation("hello"); ValidationResult vr = v.validate(ConfigFactory.parseString("hello = 2")); assertEquals(vr.getValidity(), Validity.VALID); }
@Test public void testPathExistsWithCorrectType() { Validation v = new OptionalPathValidation("hello", ConfigValueType.NUMBER); ValidationResult vr = v.validate(ConfigFactory.parseString("hello = 2")); assertEquals(vr.getValidity(), Validity.VALID); }
@Test public void testPathExistsWithWrongType() { Validation v = new OptionalPathValidation("hello", ConfigValueType.STRING); ValidationResult vr = v.validate(ConfigFactory.parseString("hello = 2")); assertEquals(vr.getValidity(), Validity.INVALID); }
@Test public void testPathDoesntExist() { Validation v = new OptionalPathValidation("world"); ValidationResult vr = v.validate(ConfigFactory.parseString("hello = 2")); assertEquals(vr.getValidity(), Validity.VALID); }