private static void assertPropertiesSupported(ConfigurationMetadata<?> metadata, Set<String> propertyNames, boolean allowDeprecatedProperties) { Set<String> supportedProperties = new TreeSet<>(); Set<String> nonDeprecatedProperties = new TreeSet<>(); for (AttributeMetadata attribute : metadata.getAttributes().values()) { if (attribute.getInjectionPoint().getProperty() != null) { nonDeprecatedProperties.add(attribute.getInjectionPoint().getProperty()); supportedProperties.add(attribute.getInjectionPoint().getProperty()); } for (ConfigurationMetadata.InjectionPointMetaData deprecated : attribute.getLegacyInjectionPoints()) { supportedProperties.add(deprecated.getProperty()); } } if (!supportedProperties.containsAll(propertyNames)) { TreeSet<String> unsupportedProperties = new TreeSet<>(propertyNames); unsupportedProperties.removeAll(supportedProperties); fail("Unsupported properties: " + unsupportedProperties); } // check for usage of deprecated properties if (!allowDeprecatedProperties && !nonDeprecatedProperties.containsAll(propertyNames)) { TreeSet<String> deprecatedProperties = new TreeSet<>(propertyNames); deprecatedProperties.removeAll(nonDeprecatedProperties); fail("Deprecated properties: " + deprecatedProperties); } }
private static void assertPropertiesSupported(ConfigurationMetadata<?> metadata, Set<String> propertyNames, boolean allowDeprecatedProperties) { Set<String> supportedProperties = new TreeSet<>(); Set<String> nonDeprecatedProperties = new TreeSet<>(); for (AttributeMetadata attribute : metadata.getAttributes().values()) { if (attribute.getInjectionPoint().getProperty() != null) { nonDeprecatedProperties.add(attribute.getInjectionPoint().getProperty()); supportedProperties.add(attribute.getInjectionPoint().getProperty()); } for (ConfigurationMetadata.InjectionPointMetaData deprecated : attribute.getLegacyInjectionPoints()) { supportedProperties.add(deprecated.getProperty()); } } if (!supportedProperties.containsAll(propertyNames)) { TreeSet<String> unsupportedProperties = new TreeSet<>(propertyNames); unsupportedProperties.removeAll(supportedProperties); Assert.fail("Unsupported properties: " + unsupportedProperties); } // check for usage of deprecated properties if (!allowDeprecatedProperties && !nonDeprecatedProperties.containsAll(propertyNames)) { TreeSet<String> deprecatedProperties = new TreeSet<>(propertyNames); deprecatedProperties.removeAll(nonDeprecatedProperties); Assert.fail("Deprecated properties: " + deprecatedProperties); } }
public void addInjectionPoint(InjectionPointMetaData injectionPointMetaData) { requireNonNull(injectionPointMetaData); if (injectionPointMetaData.isLegacy()) { this.legacyInjectionPoints.add(injectionPointMetaData); return; } if (this.injectionPoint != null) { throw Problems.exceptionFor("Trying to set current property twice: '%s' on method [%s] and '%s' on method [%s]", this.injectionPoint.getProperty(), this.injectionPoint.getSetter().toGenericString(), injectionPointMetaData.getProperty(), injectionPointMetaData.getSetter().toGenericString()); } this.injectionPoint = injectionPointMetaData; }
String operativeValue = null; if (operativeInjectionPoint != null) { operativeName = prefix + operativeInjectionPoint.getProperty(); operativeValue = properties.get(operativeName); String fullName = prefix + injectionPoint.getProperty(); String value = properties.get(fullName); if (value != null) { String replacement = "deprecated."; if (attribute.getInjectionPoint() != null) { replacement = format("replaced. Use '%s' instead.", prefix + attribute.getInjectionPoint().getProperty());
String propertyName = prefix + attribute.getInjectionPoint().getProperty(); Method getter = attribute.getGetter();
private void verifyMetaData(ConfigurationMetadata<?> metadata, Class<?> configClass, String description, boolean securitySensitive, Map<String, Set<String>> attributeProperties) throws Exception { assertEquals(metadata.getConfigClass(), configClass); if (metadata.getConstructor() != null) { assertEquals(metadata.getConstructor(), configClass.getDeclaredConstructor()); } else { try { configClass.getDeclaredConstructor(); fail(String.format("Expected configClass [%s] not to have a constructor", configClass.getName())); } catch (NoSuchMethodException expected) { } } assertEquals(metadata.getAttributes().size(), attributeProperties.keySet().size()); for (String name : attributeProperties.keySet()) { AttributeMetadata attribute = metadata.getAttributes().get(name); assertEquals(attribute.getConfigClass(), configClass); Set<String> namesToTest = new HashSet<>(); namesToTest.add(attribute.getInjectionPoint().getProperty()); for (ConfigurationMetadata.InjectionPointMetaData legacyInjectionPoint : attribute.getLegacyInjectionPoints()) { namesToTest.add(legacyInjectionPoint.getProperty()); } assertEquals(namesToTest, attributeProperties.get(name)); assertEquals(attribute.getDescription(), description); assertEquals(attribute.isSecuritySensitive(), securitySensitive); } }
private void verifyMetaData(ConfigurationMetadata<?> metadata, Class<?> configClass, String description, boolean securitySensitive, Map<String, Set<String>> attributeProperties) throws Exception { assertEquals(metadata.getConfigClass(), configClass); if (metadata.getConstructor() != null) { assertEquals(metadata.getConstructor(), configClass.getDeclaredConstructor()); } else { try { configClass.getDeclaredConstructor(); fail(String.format("Expected configClass [%s] not to have a constructor", configClass.getName())); } catch (NoSuchMethodException expected) { } } assertEquals(metadata.getAttributes().size(), attributeProperties.keySet().size()); for (String name : attributeProperties.keySet()) { AttributeMetadata attribute = metadata.getAttributes().get(name); assertEquals(attribute.getConfigClass(), configClass); Set<String> namesToTest = new HashSet<>(); namesToTest.add(attribute.getInjectionPoint().getProperty()); for (ConfigurationMetadata.InjectionPointMetaData legacyInjectionPoint : attribute.getLegacyInjectionPoints()) { namesToTest.add(legacyInjectionPoint.getProperty()); } assertEquals(namesToTest, attributeProperties.get(name)); assertEquals(attribute.getDescription(), description); assertEquals(attribute.isSecuritySensitive(), securitySensitive); } }
public static <T> void assertFullMapping(Map<String, String> properties, T expected) { Assert.assertNotNull(properties, "properties"); Assert.assertNotNull(expected, "expected"); Class<T> configClass = (Class<T>) expected.getClass(); ConfigurationMetadata<T> metadata = ConfigurationMetadata.getValidConfigurationMetadata(configClass); // verify all supplied properties are supported and not deprecated assertPropertiesSupported(metadata, properties.keySet(), false); // verify that every (non-deprecated) property is tested Set<String> nonDeprecatedProperties = new TreeSet<>(); for (AttributeMetadata attribute : metadata.getAttributes().values()) { if (attribute.getInjectionPoint().getProperty() != null) { nonDeprecatedProperties.add(attribute.getInjectionPoint().getProperty()); } } if (!properties.keySet().equals(nonDeprecatedProperties)) { TreeSet<String> untestedProperties = new TreeSet<>(nonDeprecatedProperties); untestedProperties.removeAll(properties.keySet()); Assert.fail("Untested properties " + untestedProperties); } // verify that none of the values are the same as a default for the configuration T actual = newInstance(configClass, properties); T defaultInstance = newDefaultInstance(configClass); assertAttributesNotEqual(metadata, actual, defaultInstance); // verify that a configuration object created from the properties is equivalent to the expected object assertAttributesEqual(metadata, actual, expected); }
public static <T> void assertFullMapping(Map<String, String> properties, T expected) { assertNotNull(properties, "properties"); assertNotNull(expected, "expected"); Class<T> configClass = (Class<T>) expected.getClass(); ConfigurationMetadata<T> metadata = ConfigurationMetadata.getValidConfigurationMetadata(configClass); // verify all supplied properties are supported and not deprecated assertPropertiesSupported(metadata, properties.keySet(), false); // verify that every (non-deprecated) property is tested Set<String> nonDeprecatedProperties = new TreeSet<>(); for (AttributeMetadata attribute : metadata.getAttributes().values()) { if (attribute.getInjectionPoint().getProperty() != null) { nonDeprecatedProperties.add(attribute.getInjectionPoint().getProperty()); } } if (!properties.keySet().equals(nonDeprecatedProperties)) { TreeSet<String> untestedProperties = new TreeSet<>(nonDeprecatedProperties); untestedProperties.removeAll(properties.keySet()); fail("Untested properties " + untestedProperties); } // verify that none of the values are the same as a default for the configuration T actual = newInstance(configClass, properties); T defaultInstance = newDefaultInstance(configClass); assertAttributesNotEqual(metadata, actual, defaultInstance); // verify that a configuration object created from the properties is equivalent to the expected object assertAttributesEqual(metadata, actual, expected); }
public static <T> void assertFullMapping(Map<String, String> properties, T expected) { assertNotNull(properties, "properties"); assertNotNull(expected, "expected"); Class<T> configClass = (Class<T>) expected.getClass(); ConfigurationMetadata<T> metadata = ConfigurationMetadata.getValidConfigurationMetadata(configClass); // verify all supplied properties are supported and not deprecated assertPropertiesSupported(metadata, properties.keySet(), false); // verify that every (non-deprecated) property is tested Set<String> nonDeprecatedProperties = new TreeSet<>(); for (AttributeMetadata attribute : metadata.getAttributes().values()) { if (attribute.getInjectionPoint().getProperty() != null) { nonDeprecatedProperties.add(attribute.getInjectionPoint().getProperty()); } } if (!properties.keySet().equals(nonDeprecatedProperties)) { TreeSet<String> untestedProperties = new TreeSet<>(nonDeprecatedProperties); untestedProperties.removeAll(properties.keySet()); fail("Untested properties " + untestedProperties); } // verify that none of the values are the same as a default for the configuration T actual = newInstance(configClass, properties); T defaultInstance = newDefaultInstance(configClass); assertAttributesNotEqual(metadata, actual, defaultInstance); // verify that a configuration object created from the properties is equivalent to the expected object assertAttributesEqual(metadata, actual, expected); }
private void verifyMetaData(ConfigurationMetadata<?> metadata, Class<?> configClass, String description, Map<String, Set<String>> attributeProperties) throws Exception { Assert.assertEquals(metadata.getConfigClass(), configClass); if (metadata.getConstructor() != null) { Assert.assertEquals(metadata.getConstructor(), configClass.getDeclaredConstructor()); } else { try { configClass.getDeclaredConstructor(); Assert.fail(String.format("Expected configClass [%s] not to have a constructor", configClass.getName())); } catch (NoSuchMethodException expected) { } } Assert.assertEquals(metadata.getAttributes().size(), attributeProperties.keySet().size()); for (String name : attributeProperties.keySet()) { AttributeMetadata attribute = metadata.getAttributes().get(name); Assert.assertEquals(attribute.getConfigClass(), configClass); Set<String> namesToTest = Sets.newHashSet(); namesToTest.add(attribute.getInjectionPoint().getProperty()); for (ConfigurationMetadata.InjectionPointMetaData legacyInjectionPoint : attribute.getLegacyInjectionPoints()) { namesToTest.add(legacyInjectionPoint.getProperty()); } Assert.assertEquals(namesToTest, attributeProperties.get(name)); Assert.assertEquals(attribute.getDescription(), description); } }
private Object getInjectedValue(AttributeMetadata attribute, ConfigurationMetadata.InjectionPointMetaData injectionPoint, String prefix) throws InvalidConfigurationException { // Get the property value String name = prefix + injectionPoint.getProperty(); String value = properties.get(name); String printableValue = value; if (attribute.isSecuritySensitive()) { printableValue = "[REDACTED]"; } if (value == null) { return null; } // coerce the property value to the final type Class<?> propertyType = injectionPoint.getSetter().getParameterTypes()[0]; Object finalValue = coerce(propertyType, value); if (finalValue == null) { throw new InvalidConfigurationException(format("Could not coerce value '%s' to %s (property '%s') in order to call [%s]", printableValue, propertyType.getName(), name, injectionPoint.getSetter().toGenericString())); } usedProperties.add(name); return finalValue; }
private static void assertPropertiesSupported(ConfigurationMetadata<?> metadata, Set<String> propertyNames, boolean allowDeprecatedProperties) { Set<String> supportedProperties = new TreeSet<>(); Set<String> nonDeprecatedProperties = new TreeSet<>(); for (AttributeMetadata attribute : metadata.getAttributes().values()) { if (attribute.getInjectionPoint().getProperty() != null) { nonDeprecatedProperties.add(attribute.getInjectionPoint().getProperty()); supportedProperties.add(attribute.getInjectionPoint().getProperty()); } for (ConfigurationMetadata.InjectionPointMetaData deprecated : attribute.getLegacyInjectionPoints()) { supportedProperties.add(deprecated.getProperty()); } } if (!supportedProperties.containsAll(propertyNames)) { TreeSet<String> unsupportedProperties = new TreeSet<>(propertyNames); unsupportedProperties.removeAll(supportedProperties); fail("Unsupported properties: " + unsupportedProperties); } // check for usage of deprecated properties if (!allowDeprecatedProperties && !nonDeprecatedProperties.containsAll(propertyNames)) { TreeSet<String> deprecatedProperties = new TreeSet<>(propertyNames); deprecatedProperties.removeAll(nonDeprecatedProperties); fail("Deprecated properties: " + deprecatedProperties); } }
private <T> void setConfigProperty(T instance, AttributeMetadata attribute, String prefix, Problems problems) throws InvalidConfigurationException { // Get property value ConfigurationMetadata.InjectionPointMetaData injectionPoint = findOperativeInjectionPoint(attribute, prefix, problems); // If we did not get an injection point, do not call the setter if (injectionPoint == null) { return; } if (injectionPoint.getSetter().isAnnotationPresent(Deprecated.class)) { problems.addWarning("Configuration property '%s' is deprecated and should not be used", injectionPoint.getProperty()); } Object value = getInjectedValue(injectionPoint, prefix); try { injectionPoint.getSetter().invoke(instance, value); } catch (Throwable e) { if (e instanceof InvocationTargetException && e.getCause() != null) { e = e.getCause(); } throw new InvalidConfigurationException(e, "Error invoking configuration method [%s]", injectionPoint.getSetter().toGenericString()); } }
private Object getInjectedValue(AttributeMetadata attribute, ConfigurationMetadata.InjectionPointMetaData injectionPoint, String prefix) throws InvalidConfigurationException { // Get the property value String name = prefix + injectionPoint.getProperty(); String value = properties.get(name); String printableValue = value; if (attribute.isSecuritySensitive()) { printableValue = "[REDACTED]"; } if (value == null) { return null; } // coerce the property value to the final type Class<?> propertyType = injectionPoint.getSetter().getParameterTypes()[0]; Object finalValue = coerce(propertyType, value); if (finalValue == null) { throw new InvalidConfigurationException(format("Could not coerce value '%s' to %s (property '%s') in order to call [%s]", printableValue, propertyType.getName(), name, injectionPoint.getSetter().toGenericString())); } usedProperties.add(name); return finalValue; }
private <T> void setConfigProperty(T instance, AttributeMetadata attribute, String prefix, Problems problems) throws InvalidConfigurationException { // Get property value ConfigurationMetadata.InjectionPointMetaData injectionPoint = findOperativeInjectionPoint(attribute, prefix, problems); // If we did not get an injection point, do not call the setter if (injectionPoint == null) { return; } if (injectionPoint.getSetter().isAnnotationPresent(Deprecated.class)) { problems.addWarning("Configuration property '%s' is deprecated and should not be used", prefix + injectionPoint.getProperty()); } Object value = getInjectedValue(attribute, injectionPoint, prefix); try { injectionPoint.getSetter().invoke(instance, value); } catch (Throwable e) { if (e instanceof InvocationTargetException && e.getCause() != null) { e = e.getCause(); } throw new InvalidConfigurationException(e, format("Error invoking configuration method [%s]", injectionPoint.getSetter().toGenericString())); } }
private Object getInjectedValue(ConfigurationMetadata.InjectionPointMetaData injectionPoint, String prefix) throws InvalidConfigurationException { // Get the property value String name = prefix + injectionPoint.getProperty(); String value = properties.get(name); if (value == null) { return null; } // coerce the property value to the final type Class<?> propertyType = injectionPoint.getSetter().getParameterTypes()[0]; Object finalValue = coerce(propertyType, value); if (finalValue == null) { throw new InvalidConfigurationException(format("Could not coerce value '%s' to %s (property '%s') in order to call [%s]", value, propertyType.getName(), injectionPoint.getProperty(), injectionPoint.getSetter().toGenericString())); } usedProperties.add(name); return finalValue; }
private <T> void setConfigProperty(T instance, AttributeMetadata attribute, String prefix, Problems problems) throws InvalidConfigurationException { // Get property value ConfigurationMetadata.InjectionPointMetaData injectionPoint = findOperativeInjectionPoint(attribute, prefix, problems); // If we did not get an injection point, do not call the setter if (injectionPoint == null) { return; } if (injectionPoint.getSetter().isAnnotationPresent(Deprecated.class)) { problems.addWarning("Configuration property '%s' is deprecated and should not be used", prefix + injectionPoint.getProperty()); } Object value = getInjectedValue(attribute, injectionPoint, prefix); try { injectionPoint.getSetter().invoke(instance, value); } catch (Throwable e) { if (e instanceof InvocationTargetException && e.getCause() != null) { e = e.getCause(); } throw new InvalidConfigurationException(e, format("Error invoking configuration method [%s]", injectionPoint.getSetter().toGenericString())); } }
public void addInjectionPoint(InjectionPointMetaData injectionPointMetaData) { Preconditions.checkNotNull(injectionPointMetaData); if (injectionPointMetaData.isLegacy()) { this.legacyInjectionPoints.add(injectionPointMetaData); return; } if (this.injectionPoint != null) { throw Problems.exceptionFor("Trying to set current property twice: '%s' on method [%s] and '%s' on method [%s]", this.injectionPoint.getProperty(), this.injectionPoint.getSetter().toGenericString(), injectionPointMetaData.getProperty(), injectionPointMetaData.getSetter().toGenericString()); } this.injectionPoint = injectionPointMetaData; }
public void addInjectionPoint(InjectionPointMetaData injectionPointMetaData) { requireNonNull(injectionPointMetaData); if (injectionPointMetaData.isLegacy()) { this.legacyInjectionPoints.add(injectionPointMetaData); return; } if (this.injectionPoint != null) { throw Problems.exceptionFor("Trying to set current property twice: '%s' on method [%s] and '%s' on method [%s]", this.injectionPoint.getProperty(), this.injectionPoint.getSetter().toGenericString(), injectionPointMetaData.getProperty(), injectionPointMetaData.getSetter().toGenericString()); } this.injectionPoint = injectionPointMetaData; }