Search in sources :

Example 1 with Config

use of org.hyperledger.fabric.sdk.helper.Config in project fabric-sdk-java by hyperledger.

the class TestUtils method setConfigProperty.

/**
 * Sets a Config property value
 * <p>
 * The Config instance is initialized once on startup which means that
 * its properties don't change throughout its lifetime.
 * This method allows a Config property to be changed temporarily for testing purposes
 *
 * @param key   The key of the property (eg Config.LOGGERLEVEL)
 * @param value The new value
 * @return The previous value
 */
public static String setConfigProperty(String key, String value) throws Exception {
    String oldVal = null;
    try {
        Config config = Config.getConfig();
        final Field sdkPropertiesInstance = config.getClass().getDeclaredField("sdkProperties");
        sdkPropertiesInstance.setAccessible(true);
        final Properties sdkProperties = (Properties) sdkPropertiesInstance.get(config);
        oldVal = sdkProperties.getProperty(key);
        sdkProperties.put(key, value);
    } catch (Exception e) {
        throw new RuntimeException("Failed to set Config property " + key, e);
    }
    return oldVal;
}
Also used : Field(java.lang.reflect.Field) Config(org.hyperledger.fabric.sdk.helper.Config) Properties(java.util.Properties) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with Config

use of org.hyperledger.fabric.sdk.helper.Config in project fabric-sdk-java by hyperledger.

the class TestConfigHelper method clearConfig.

/**
 * clearConfig "resets" Config so that the Config testcases can run without interference from other test suites.
 * Depending on what order JUnit decides to run the tests, Config could have been instantiated earlier and could
 * contain values that make the tests here fail.
 *
 * @throws SecurityException
 * @throws NoSuchFieldException
 * @throws IllegalAccessException
 * @throws IllegalArgumentException
 */
public void clearConfig() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
    Config config = Config.getConfig();
    // Set the private static variable Config.config = null
    java.lang.reflect.Field configInstance = config.getClass().getDeclaredField("config");
    configInstance.setAccessible(true);
    configInstance.set(null, null);
    // Clear the sdkProperties map - Config.sdkProperties.clear()
    java.lang.reflect.Field sdkPropInstance = config.getClass().getDeclaredField("sdkProperties");
    sdkPropInstance.setAccessible(true);
    Properties sdkProperties = (Properties) sdkPropInstance.get(config);
    sdkProperties.clear();
}
Also used : Config(org.hyperledger.fabric.sdk.helper.Config) Properties(java.util.Properties)

Aggregations

Properties (java.util.Properties)2 Config (org.hyperledger.fabric.sdk.helper.Config)2 Field (java.lang.reflect.Field)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1