com.facebook.airlift.configuration.testing.ConfigAssertions Maven / Gradle / Ivy
/*
* Copyright 2010 Proofpoint, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.facebook.airlift.configuration.testing;
import com.facebook.airlift.configuration.ConfigurationFactory;
import com.facebook.airlift.configuration.ConfigurationMetadata;
import com.facebook.airlift.configuration.ConfigurationMetadata.AttributeMetadata;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.MapMaker;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentMap;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotEquals;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.fail;
public final class ConfigAssertions
{
private static final Method GET_RECORDING_CONFIG_METHOD;
static {
try {
GET_RECORDING_CONFIG_METHOD = $$RecordingConfigProxy.class.getMethod("$$getRecordedConfig");
}
catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
}
private ConfigAssertions()
{
}
public static void assertDefaults(Map expectedAttributeValues, Class configClass)
{
ConfigurationMetadata> metadata = ConfigurationMetadata.getValidConfigurationMetadata(configClass);
// verify all supplied attributes are supported
if (!metadata.getAttributes().keySet().containsAll(expectedAttributeValues.keySet())) {
TreeSet unsupportedAttributes = new TreeSet<>(expectedAttributeValues.keySet());
unsupportedAttributes.removeAll(metadata.getAttributes().keySet());
fail("Unsupported attributes: " + unsupportedAttributes);
}
// verify all supplied attributes are supported not deprecated
Set nonDeprecatedAttributes = new TreeSet<>();
for (AttributeMetadata attribute : metadata.getAttributes().values()) {
if (attribute.getInjectionPoint().getProperty() != null) {
nonDeprecatedAttributes.add(attribute.getName());
}
}
if (!nonDeprecatedAttributes.containsAll(expectedAttributeValues.keySet())) {
TreeSet unsupportedAttributes = new TreeSet<>(expectedAttributeValues.keySet());
unsupportedAttributes.removeAll(nonDeprecatedAttributes);
fail("Deprecated attributes: " + unsupportedAttributes);
}
// verify all attributes are tested
if (!expectedAttributeValues.keySet().containsAll(nonDeprecatedAttributes)) {
TreeSet untestedAttributes = new TreeSet<>(nonDeprecatedAttributes);
untestedAttributes.removeAll(expectedAttributeValues.keySet());
fail("Untested attributes: " + untestedAttributes);
}
// create an uninitialized default instance
T actual = newDefaultInstance(configClass);
// verify each attribute is either the supplied default value
for (AttributeMetadata attribute : metadata.getAttributes().values()) {
Method getter = attribute.getGetter();
if (getter == null) {
continue;
}
Object actualAttributeValue = invoke(actual, getter);
Object expectedAttributeValue = expectedAttributeValues.get(attribute.getName());
assertEquals(expectedAttributeValue, actualAttributeValue, attribute.getName());
}
}
public static void assertFullMapping(Map properties, T expected)
{
assertNotNull(properties, "properties");
assertNotNull(expected, "expected");
Class configClass = (Class) expected.getClass();
ConfigurationMetadata 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 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 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);
}
@SafeVarargs
public static void assertDeprecatedEquivalence(Class configClass, Map currentProperties, Map... oldPropertiesList)
{
assertNotNull(configClass, "configClass");
assertNotNull(currentProperties, "currentProperties");
assertNotNull(oldPropertiesList, "oldPropertiesList");
ConfigurationMetadata metadata = ConfigurationMetadata.getValidConfigurationMetadata(configClass);
// verify all current properties are supported and not deprecated
assertPropertiesSupported(metadata, currentProperties.keySet(), false);
// verify all old properties are supported (deprecation allowed)
for (Map evenOlderProperties : oldPropertiesList) {
assertPropertiesSupported(metadata, evenOlderProperties.keySet(), true);
}
// verify that all deprecated properties are tested
Set knownDeprecatedProperties = new TreeSet<>();
for (AttributeMetadata attribute : metadata.getAttributes().values()) {
for (ConfigurationMetadata.InjectionPointMetaData deprecated : attribute.getLegacyInjectionPoints()) {
knownDeprecatedProperties.add(deprecated.getProperty());
}
}
Set suppliedDeprecatedProperties = new TreeSet<>();
for (Map evenOlderProperties : oldPropertiesList) {
suppliedDeprecatedProperties.addAll(evenOlderProperties.keySet());
}
if (!suppliedDeprecatedProperties.containsAll(knownDeprecatedProperties)) {
TreeSet untestedDeprecatedProperties = new TreeSet<>(knownDeprecatedProperties);
untestedDeprecatedProperties.removeAll(suppliedDeprecatedProperties);
fail("Untested deprecated properties: " + untestedDeprecatedProperties);
}
// verify property sets create equivalent configurations
T currentConfiguration = newInstance(configClass, currentProperties);
for (Map evenOlderProperties : oldPropertiesList) {
T evenOlderConfiguration = newInstance(configClass, evenOlderProperties);
assertAttributesEqual(metadata, currentConfiguration, evenOlderConfiguration);
}
}
private static void assertPropertiesSupported(ConfigurationMetadata> metadata, Set propertyNames, boolean allowDeprecatedProperties)
{
Set supportedProperties = new TreeSet<>();
Set 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 unsupportedProperties = new TreeSet<>(propertyNames);
unsupportedProperties.removeAll(supportedProperties);
fail("Unsupported properties: " + unsupportedProperties);
}
// check for usage of deprecated properties
if (!allowDeprecatedProperties && !nonDeprecatedProperties.containsAll(propertyNames)) {
TreeSet deprecatedProperties = new TreeSet<>(propertyNames);
deprecatedProperties.removeAll(nonDeprecatedProperties);
fail("Deprecated properties: " + deprecatedProperties);
}
}
private static void assertAttributesEqual(ConfigurationMetadata metadata, T actual, T expected)
{
for (AttributeMetadata attribute : metadata.getAttributes().values()) {
Method getter = attribute.getGetter();
if (getter == null) {
continue;
}
Object actualAttributeValue = invoke(actual, getter);
Object expectedAttributeValue = invoke(expected, getter);
assertEquals(actualAttributeValue, expectedAttributeValue, attribute.getName());
}
}
private static void assertAttributesNotEqual(ConfigurationMetadata metadata, T actual, T expected)
{
for (AttributeMetadata attribute : metadata.getAttributes().values()) {
Method getter = attribute.getGetter();
if (getter == null) {
continue;
}
Object actualAttributeValue = invoke(actual, getter);
Object expectedAttributeValue = invoke(expected, getter);
assertNotEquals(actualAttributeValue, expectedAttributeValue, attribute.getName());
}
}
public static void assertRecordedDefaults(T recordedConfig)
{
$$RecordedConfigData recordedConfigData = getRecordedConfig(recordedConfig);
Set invokedMethods = recordedConfigData.getInvokedMethods();
T config = recordedConfigData.getInstance();
Class configClass = (Class) config.getClass();
ConfigurationMetadata> metadata = ConfigurationMetadata.getValidConfigurationMetadata(configClass);
// collect information about the attributes that have been set
Map attributeValues = new TreeMap<>();
Set setDeprecatedAttributes = new TreeSet<>();
Set validSetterMethods = new HashSet<>();
for (AttributeMetadata attribute : metadata.getAttributes().values()) {
if (attribute.getInjectionPoint().getProperty() != null) {
validSetterMethods.add(attribute.getInjectionPoint().getSetter());
}
if (invokedMethods.contains(attribute.getInjectionPoint().getSetter())) {
if (attribute.getInjectionPoint().getProperty() != null) {
Object value = invoke(config, attribute.getGetter());
attributeValues.put(attribute.getName(), value);
}
else {
setDeprecatedAttributes.add(attribute.getName());
}
}
}
// verify no deprecated attribute setters have been called
if (!setDeprecatedAttributes.isEmpty()) {
fail("Invoked deprecated attribute setter methods: " + setDeprecatedAttributes);
}
// verify no other methods have been set
if (!validSetterMethods.containsAll(invokedMethods)) {
Set invalidInvocations = new HashSet<>(invokedMethods);
invalidInvocations.removeAll(validSetterMethods);
fail("Invoked non-attribute setter methods: " + invalidInvocations);
}
assertDefaults(attributeValues, configClass);
}
public static T recordDefaults(Class type)
{
final T instance = newDefaultInstance(type);
T proxy = (T) Enhancer.create(type, new Class>[] {$$RecordingConfigProxy.class}, new MethodInterceptor()
{
private final ConcurrentMap invokedMethods = new MapMaker().makeMap();
@Override
public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy)
throws Throwable
{
if (GET_RECORDING_CONFIG_METHOD.equals(method)) {
return new $$RecordedConfigData<>(instance, ImmutableSet.copyOf(invokedMethods.keySet()));
}
invokedMethods.put(method, Boolean.TRUE);
Object result = methodProxy.invoke(instance, args);
if (result == instance) {
return proxy;
}
else {
return result;
}
}
});
return proxy;
}
static $$RecordedConfigData getRecordedConfig(T config)
{
if (!(config instanceof $$RecordingConfigProxy)) {
throw new IllegalArgumentException("Configuration was not created with the recordDefaults method");
}
return (($$RecordingConfigProxy) config).$$getRecordedConfig();
}
@SuppressWarnings("checkstyle:TypeName")
public static class $$RecordedConfigData
{
private final T instance;
private final Set invokedMethods;
public $$RecordedConfigData(T instance, Set invokedMethods)
{
this.instance = instance;
this.invokedMethods = ImmutableSet.copyOf(invokedMethods);
}
public T getInstance()
{
return instance;
}
public Set getInvokedMethods()
{
return invokedMethods;
}
}
@SuppressWarnings("checkstyle:TypeName")
public interface $$RecordingConfigProxy
{
$$RecordedConfigData $$getRecordedConfig();
}
private static T newInstance(Class configClass, Map properties)
{
ConfigurationFactory configurationFactory = new ConfigurationFactory(properties);
return configurationFactory.build(configClass);
}
private static T newDefaultInstance(Class configClass)
{
try {
return configClass.newInstance();
}
catch (Exception e) {
AssertionError error = new AssertionError(String.format("Exception creating default instance of %s", configClass.getName()));
error.initCause(e);
throw error;
}
}
private static Object invoke(T actual, Method getter)
{
try {
return getter.invoke(actual);
}
catch (Exception e) {
AssertionError error = new AssertionError(String.format("Exception invoking %s", getter.toGenericString()));
error.initCause(e);
throw error;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy