/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at legal-notices/CDDLv1_0.txt.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information:
* Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*
*
* Copyright 2013-2015 ForgeRock AS.
*/
/**
* Provides Mockito mocks for Configuration objects with default values
* corresponding to those defined in xml configuration files.
* <p>
* These mocks can be used like any other mocks, e.g, you can define stubs using
* {@code when} method or verify calls using {@code verify} method.
* <p>
* Example:
*
* <pre>
* LDAPConnectionHandlerCfg mockCfg = mockCfg(LDAPConnectionHandlerCfg.class);
* assertThat(mockCfg.getMaxRequestSize()).isEqualTo(5 * 1000 * 1000);
* </pre>
*/
public final class ConfigurationMock {
/**
* Returns a mock for the provided configuration class.
* <p>
* If a setting has a default value, the mock automatically returns the
* default value when the getter is called on the setting.
* <p>
* It is possible to override this default behavior with the usual methods
* calls with Mockito (e.g, {@code when} method).
*
* @param <T>
* The type of configuration.
* @param configClass
* The configuration class.
* @return a mock
*/
}
/**
* Returns a mock for the provided configuration class.
* <p>
* If a setting has a default value, the mock automatically returns the
* default value when the getter is called on the setting.
* <p>
* It is possible to override this default behavior with the usual methods
* calls with Mockito (e.g, {@code when} method).
*
* @param <T>
* The type of configuration.
* @param configClass
* The configuration class.
* @return a mock
*/
public static <T extends org.opends.server.admin.Configuration> T legacyMockCfg(Class<T> configClass) {
}
/**
* A stubbed answer for Configuration objects, allowing to return default
* value for settings when available.
*/
/** {@inheritDoc} */
try {
ManagedObjectDefinition<?, ?> definition =
if (!isGetterMethod(invokedMethodName)) {
}
if (defaultValue == null) {
}
return defaultValue;
} catch (Exception e) {
}
}
return super.answer(invocation);
}
}
private static Method getPropertyDefinitionMethod(Class<?> definitionClass, String invokedMethodName)
throws SecurityException, NoSuchMethodException {
// Methods for boolean starts with "is" in Cfg class but with "get" in CfgDefn class.
return definitionClass.getMethod(invokedMethodName.replaceAll("^is", "get") + "PropertyDefinition");
}
/**
* Returns the type of values returned by the property.
*/
}
/**
* Retrieve class name of definition from class name of configuration.
* <p>
* Convert class name "[package].server.FooCfg" to "[package].meta.FooCfgDef"
*/
}
/**
* Returns the default value corresponding to the provided property
* definition getter method from the provided managed object definition.
*
* @param <T>
* The data type of values provided by the property
* definition.
* @param definition
* The definition of the managed object.
* @param getPropertyDefMethod
* The method to retrieve the property definition from the
* definition.
* @param propertyReturnClass
* The class of values provided by the property definition.
* @return the default value of property definition, or
* {@code null} if there is no default value.
* @throws Exception
* If an error occurs.
*/
@SuppressWarnings("unchecked")
PropertyDefinition<T> propertyDefinition = (PropertyDefinition<T>) getPropertyDefMethod.invoke(definition);
DefaultBehaviorProvider<T> defaultBehaviorProvider = (DefaultBehaviorProvider<T>) propertyDefinition
// No default behavior defined
return null;
return values;
} else {
// Single value returned
}
}
}
/**
* A stubbed answer for Configuration objects, allowing to return default
* value for settings when available.
*/
/** {@inheritDoc} */
try {
(org.opends.server.admin.ManagedObjectDefinition<?, ?>) definitionClass.getMethod("getInstance").invoke(null);
if (!isGetterMethod(invokedMethodName)) {
}
if (defaultValue == null) {
}
return defaultValue;
} catch (Exception e) {
}
}
return super.answer(invocation);
}
}
private static Method getPropertyDefinitionMethod(Class<?> definitionClass, String invokedMethodName)
throws SecurityException, NoSuchMethodException {
// Methods for boolean starts with "is" in Cfg class but with "get" in CfgDefn class.
return definitionClass.getMethod(invokedMethodName.replaceAll("^is", "get") + "PropertyDefinition");
}
/**
* Returns the type of values returned by the property.
*/
return ((ParameterizedType) returnClass.getGenericSuperclass()).getActualTypeArguments()[0].getClass();
}
/**
* Retrieve class name of definition from class name of configuration.
* <p>
* Convert class name "[package].server.FooCfg" to "[package].meta.FooCfgDef"
*/
return configClassName.substring(0, finalDot - 6) + "meta." + configClassName.substring(finalDot + 1)
+ "Defn";
}
/**
* Returns the default value corresponding to the provided property
* definition getter method from the provided managed object definition.
*
* @param <T>
* The data type of values provided by the property
* definition.
* @param definition
* The definition of the managed object.
* @param getPropertyDefMethod
* The method to retrieve the property definition from the
* definition.
* @param propertyReturnClass
* The class of values provided by the property definition.
* @return the default value of property definition, or
* {@code null} if there is no default value.
* @throws Exception
* If an error occurs.
*/
@SuppressWarnings("unchecked")
private static <T> Object getDefaultValue(org.opends.server.admin.ManagedObjectDefinition<?, ?> definition,
org.opends.server.admin.PropertyDefinition<T> propertyDefinition = (org.opends.server.admin.PropertyDefinition<T>) getPropertyDefMethod.invoke(definition);
org.opends.server.admin.DefaultBehaviorProvider<T> defaultBehaviorProvider = (org.opends.server.admin.DefaultBehaviorProvider<T>) propertyDefinition
// No default behavior defined
return null;
return values;
} else {
// Single value returned
}
}
}
/** Visitor used to retrieve the default value. */
private static class MockProviderVisitor<T> implements DefaultBehaviorProviderVisitor<T, Collection<T>, Void> {
/** The property definition used to decode the values. */
this.propertyDef = propertyDef;
}
/** {@inheritDoc} */
public Collection<T> visitAbsoluteInherited(AbsoluteInheritedDefaultBehaviorProvider<T> provider, Void p) {
// not handled
return null;
}
/** {@inheritDoc} */
// not handled
return null;
}
/**
* Returns the default value for the property as a collection.
*/
}
return values;
}
/** {@inheritDoc} */
public Collection<T> visitRelativeInherited(RelativeInheritedDefaultBehaviorProvider<T> d, Void p) {
// not handled
return null;
}
/** {@inheritDoc} */
// not handled
return null;
}
}
/** Visitor used to retrieve the default value. */
private static class LegacyMockProviderVisitor<T> implements org.opends.server.admin.DefaultBehaviorProviderVisitor<T, Collection<T>, Void> {
/** The property definition used to decode the values. */
this.propertyDef = propertyDef;
}
/** {@inheritDoc} */
public Collection<T> visitAbsoluteInherited(org.opends.server.admin.AbsoluteInheritedDefaultBehaviorProvider<T> provider, Void p) {
// not handled
return null;
}
/** {@inheritDoc} */
public Collection<T> visitAlias(org.opends.server.admin.AliasDefaultBehaviorProvider<T> provider, Void p) {
// not handled
return null;
}
/**
* Returns the default value for the property as a collection.
*/
public Collection<T> visitDefined(org.opends.server.admin.DefinedDefaultBehaviorProvider<T> provider, Void p) {
}
return values;
}
/** {@inheritDoc} */
public Collection<T> visitRelativeInherited(org.opends.server.admin.RelativeInheritedDefaultBehaviorProvider<T> d, Void p) {
// not handled
return null;
}
/** {@inheritDoc} */
public Collection<T> visitUndefined(org.opends.server.admin.UndefinedDefaultBehaviorProvider<T> d, Void p) {
// not handled
return null;
}
}
}