Uploaded image for project: 'Tamaya'
  1. Tamaya
  2. TAMAYA-235

The get method of the MappedConfiguration class may throw a NullPointerException

    XMLWordPrintableJSON

Details

    Description

      When the parameter "stripKeys" is set true in the section method of the ConfigurationFunctions class, the behavior of the get method MappedConfiguration is incoherent : the section name has to be prepend twice in order to get the value.

      The following code source is based on the "02-custom-property-source" example project.

      Main.java
      public static void main(String[] args) {
          	// keep original section name
          	// testSection(false);
          	// strip section name
          	testSection(true);
          }
          
          private static void testSection(boolean stripKeys){
              Configuration cfg = ConfigurationProvider.getConfiguration().with(ConfigurationFunctions.section("example.", stripKeys));
              System.out.println("*****************************************************");
              System.out.println("stripKeys: " + stripKeys);
              System.out.println("*****************************************************");
              dump(cfg.getProperties(), System.out);
              System.out.println();
              System.out.println("Example Metadata:");
              System.out.println("\texample.type         :  " + cfg.get("example.type"));
              System.out.println("\texample.example.type :  " + cfg.get("example.example.type"));
              System.out.println("\ttype                 :  " + cfg.get("type"));
              dump(cfg.getProperties(), System.out);
          }
      
          private static void dump(Map<String, String> properties, PrintStream stream) {
              stream.println("FULL DUMP:");
              for (Map.Entry<String, String> en : new TreeMap<>(properties).entrySet()) {
                  stream.println(format("\t%s = %s", en.getKey(), en.getValue()));
              }
          }
      

      This is the result of the execution :

      *****************************************************
      stripKeys: true
      *****************************************************
      FULL DUMP:
      	author = anatole@apache.org
      	description = A minimal example using an self written property source.
      	name = simple-propertysource
      	type = standalone
      	version = 1
      
      Example Metadata:
      	example.type         :  null
      	example.example.type :  standalone
      Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -4
      	at java.lang.String.substring(String.java:1931)
      	at org.apache.tamaya.functions.ConfigurationFunctions$5$2.mapKey(ConfigurationFunctions.java:235)
      	at org.apache.tamaya.functions.MappedConfiguration.get(MappedConfiguration.java:79)
      	at org.apache.tamaya.functions.MappedConfiguration.get(MappedConfiguration.java:74)
      	at org.apache.tamaya.functions.MappedConfiguration.get(MappedConfiguration.java:51)
      	at org.apache.tamaya.examples.custompropertysource.Main.testSection(Main.java:84)
      	at org.apache.tamaya.examples.custompropertysource.Main.main(Main.java:71)
      

      In the dump of the MappedConfiguration, the "standalone" value is associated to the "type" key".
      When calling the get method of the MappedConfiguration, the "standalone" value is got by prepending twice the "example." section name in the "type" key.
      Without adding the section name, a NullPointerException is thrown.

      Attachments

        Activity

          People

            anatole Anatole Tresch
            fmessiaen Francois MESSIAEN
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: