Uploaded image for project: 'Commons Configuration'
  1. Commons Configuration
  2. CONFIGURATION-646

autoSave and manual save behave differently

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: 2.1
    • Fix Version/s: 2.1.1
    • Component/s: None
    • Labels:
      None

      Description

      Using builder.autoSave(true) behaves differently than builder.save() to the aspect that builder.autoSave(true) doesn't write newly added properties to the assigned config file.

      I've set up a minimal example using only two dependencies:
      commons-beanutils:commons-beanutils:1.9.3 (runtime)
      org.apache.commons:commons-configuration2:2.1.0 (compiletime)

      public class Main {
          public static void main(String... args) {
              Parameters params = new Parameters();
              File propertiesFile = new File("conf.properties");
      
              FileBasedConfigurationBuilder<PropertiesConfiguration> builder =
                      new FileBasedConfigurationBuilder<>(PropertiesConfiguration.class)
                              .configure(params.fileBased().setFile(propertiesFile));
              builder.setAutoSave(true);
              try {
                  PropertiesConfiguration config = builder.getConfiguration();
                  config.setProperty("foo", "bar");
                  // builder.save();
              } catch (ConfigurationException e) {
                  e.printStackTrace();
              }
          }
      }
      

      This is mainly like the example from the documentation for filebased configurations.

      conf.properties exists and is empty. With the builder.save line commented out, the file remains empty. Uncommenting this line results in the expected behavior.

      I debugged a bit an saw that the property isn't added to the PropertiesConfigurationLayout in the first case.

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              cmrudolph Christian Rudolph
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: