@Test public void shouldPassValidationForValidConfig() { Properties source = new Properties(); source.setProperty(TEST_KEY, String.valueOf(VALID_TEST_VALUE)); ValidatingConfigurationProvider provider = new ValidatingConfigurationProvider( new PropertiesConfigurationSource(source), new JdkProxyStaticConfigurationFactory()); TestConfig config = provider.getConfiguration(TestConfig.class); assertEquals(VALID_TEST_VALUE, config.getTestProperty()); }
@Override public <T> T getConfiguration(Class<T> configurationType) { requireNonNull(configurationType, "configurationType cannot be null"); T instance = factory.createConfiguration(configurationType, source); Set<ConstraintViolation<T>> errors = validator.validate(instance); if (!errors.isEmpty()) { Set<String> errorDetails = errors.stream() .map(v -> v.getPropertyPath() + " " + v.getMessage()) .collect(toSet()); throw new ConfigurationException( format("The configuration value(s) for %s violate constraint(s): %s", configurationType.getSimpleName(), String.join(";", errorDetails)), errorDetails); } return instance; }
ValidatingConfigurationProvider implements ConfigurationProvider { @Override public <T> T getConfiguration(Class<T> configurationType) { requireNonNull(configurationType, "configurationType cannot be null"); T instance = factory.createConfiguration(configurationType, source); Set<ConstraintViolation<T>> errors = validator.validate(instance); if (!errors.isEmpty()) { Set<String> errorDetails = errors.stream() .map(v -> v.getPropertyPath() + " " + v.getMessage()) .collect(toSet()); throw new ConfigurationException( format("The configuration value(s) for %s violate constraint(s): %s", configurationType.getSimpleName(), String.join(";", errorDetails)), errorDetails); } return instance; } }
ValidatingConfigurationProvider implements ConfigurationProvider { @Override public <T> T getConfiguration(Class<T> configurationType) { requireNonNull(configurationType, "configurationType cannot be null"); T instance = factory.createConfiguration(configurationType, source); Set<ConstraintViolation<T>> errors = validator.validate(instance); if (!errors.isEmpty()) { Set<String> errorDetails = errors.stream() .map(v -> v.getPropertyPath() + " " + v.getMessage()) .collect(toSet()); throw new ConfigurationException( format("The configuration value(s) for %s violate constraint(s): %s", configurationType.getSimpleName(), String.join(";", errorDetails)), errorDetails); } return instance; } ValidatingConfigurationProvider(ConfigurationSource source, ConfigurationFactory factory); }
ValidatingConfigurationProvider implements ConfigurationProvider { @Override public <T> T getConfiguration(Class<T> configurationType) { requireNonNull(configurationType, "configurationType cannot be null"); T instance = factory.createConfiguration(configurationType, source); Set<ConstraintViolation<T>> errors = validator.validate(instance); if (!errors.isEmpty()) { Set<String> errorDetails = errors.stream() .map(v -> v.getPropertyPath() + " " + v.getMessage()) .collect(toSet()); throw new ConfigurationException( format("The configuration value(s) for %s violate constraint(s): %s", configurationType.getSimpleName(), String.join(";", errorDetails)), errorDetails); } return instance; } ValidatingConfigurationProvider(ConfigurationSource source, ConfigurationFactory factory); @Override T getConfiguration(Class<T> configurationType); }
ValidatingConfigurationProvider implements ConfigurationProvider { @Override public <T> T getConfiguration(Class<T> configurationType) { requireNonNull(configurationType, "configurationType cannot be null"); T instance = factory.createConfiguration(configurationType, source); Set<ConstraintViolation<T>> errors = validator.validate(instance); if (!errors.isEmpty()) { Set<String> errorDetails = errors.stream() .map(v -> v.getPropertyPath() + " " + v.getMessage()) .collect(toSet()); throw new ConfigurationException( format("The configuration value(s) for %s violate constraint(s): %s", configurationType.getSimpleName(), String.join(";", errorDetails)), errorDetails); } return instance; } ValidatingConfigurationProvider(ConfigurationSource source, ConfigurationFactory factory); @Override T getConfiguration(Class<T> configurationType);  }