commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Andre Weihe <...@zmt.info>
Subject [configuration] CombinedConfiguration#clone() not cloning changed properties
Date Wed, 05 Dec 2012 15:32:39 GMT
CombinedConfiguration#clone() does not clone properties set/changed on the CombinedConfiguration
that are stored in combinedRoot (which seems NOT to be cloned).

Bug, feature or "You are using it wrong!"?


Demonstration program:
import org.apache.commons.configuration.*;
import org.apache.commons.configuration.tree.OverrideCombiner;
import java.util.Iterator;

public class CommonsConfigurationTester {
    private static final String FOO_KEY = "Test.foo";
    private static final String BAR_KEY = "Test.bar";

    public static void main(String[] args) {
        new CommonsConfigurationTester().ccTest();
    }

    void ccTest() {
        HierarchicalConfiguration defaultConfig = new HierarchicalConfiguration();
        defaultConfig.addProperty(FOO_KEY, Integer.valueOf(1));
        defaultConfig.addProperty(BAR_KEY, Integer.valueOf(2));

        System.out.println("Default config:\n" + configToString(defaultConfig));

        HierarchicalConfiguration individualConfig = new HierarchicalConfiguration();
        individualConfig.addProperty(FOO_KEY, Integer.valueOf(3));
        individualConfig.addProperty(BAR_KEY, Integer.valueOf(4));

        System.out.println("Individual config (has precedence):\n" + configToString(individualConfig));

        CombinedConfiguration combinedConfig = new CombinedConfiguration(new OverrideCombiner());
        combinedConfig.addConfiguration(individualConfig);
        combinedConfig.addConfiguration(defaultConfig);

        System.out.println("Combined config (default + individual):\n" + configToString(combinedConfig));

        combinedConfig.setProperty(FOO_KEY, Integer.valueOf(5));
        combinedConfig.setProperty(BAR_KEY, Integer.valueOf(6));

        System.out.println("Changed config (default + individual):\n" + configToString(combinedConfig));

        CombinedConfiguration combinedConfig2 = (CombinedConfiguration)combinedConfig.clone();

        System.out.println("Cloned changed config (default + individual):\n" + configToString(combinedConfig2));
    }


    /**
     * A little helper method to get all the settings of a configuration into a String
     * @param config the Configuration whose String representation we want
     * @return a String containing the key-value pairs
     */
    static String configToString(Configuration config) {
        String pairs = new String();
        for (Iterator keys = config.getKeys(); keys.hasNext();) {
            String key = (String)keys.next();
            pairs = pairs + key + '=' + config.getProperty(key) + '\n';
        }

        return pairs;
    }
}

-- 
André Weihe                            Zengel Medizintechnik GmbH
Tel.:   +49-6136-9943925               Friedrich-Ebert-Str. 2
Fax.:   +49-6723-999598                65375 Oestrich-Winkel
                                       Geschäftsführer: Grischa Zengel
E-Mail: awe@zmt.info                   Amtsgericht Wiesbaden HRB 23154
                                       Steuer-Nr.:       040 249 57267
                                       USt-IdNr.:          DE255806446




Mime
View raw message