commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Christian Koncilia" <Christian.Konci...@panoratio.de>
Subject RE: AW: Commons Configuration: delete entry
Date Wed, 09 Aug 2006 09:55:36 GMT
Oliver,

this is really strange. I promise that the behaviour of the Configuration package was exactly
as I wrote. As you mentioned, I changed the code

PropertiesConfiguration c = new PropertiesConfiguration(PROPERTIESFILE);
c.clearProperty(entry);
c.save();

into

PropertiesConfiguration c = new PropertiesConfiguration(PROPERTIESFILE);
System.out.println("contains key before del: " + c.containsKey(entry));
c.clearProperty(entry);
System.out.println("contains key after del: " + c.containsKey(entry));
c.save();

The strange thing is, that this code did not only produce the correct output (i.e., "true"
and "false") it also produced the correct result. I really don't know what happened. Anyway,
it works! So, Oliver, thank you very much for your help and support.

Bye
	Christian

-----Original Message-----
From: Oliver Heger [mailto:oliver.heger@oliver-heger.de] 
Sent: Tuesday, August 08, 2006 10:35 PM
To: Jakarta Commons Users List
Subject: Re: AW: Commons Configuration: delete entry

Christian Koncilia wrote:
> Hi Oliver,
> 
> thats what I tried before but it doesn't work neither. Another strange thing, if I use
"config.clear()" to clear the whole properties file, everything except(!) the last thing I
read is deleted...
> 
> This looks to me like some internal bug. Or do you have another idea?
> 
> Bye
>     Christian

Christian,

I added the following test cases to the unit test class of 
PropertiesConfiguration [1]:


     public void testClearProperty() throws ConfigurationException
     {
         if (testSavePropertiesFile.exists())
         {
             assertTrue(testSavePropertiesFile.delete());
         }
         assertTrue("Property not found", 
conf.containsKey("configuration.loaded"));
         conf.clearProperty("configuration.loaded");
         conf.save(testSavePropertiesFile);

         PropertiesConfiguration conf2 = new 
PropertiesConfiguration(testSavePropertiesFile);
         assertFalse("Property still contained", 
conf2.containsKey("configuration.loaded"));
     }

     public void testClear() throws ConfigurationException
     {
         if (testSavePropertiesFile.exists())
         {
             assertTrue(testSavePropertiesFile.delete());
         }
         conf.clear();
         conf.save(testSavePropertiesFile);

         PropertiesConfiguration conf2 = new 
PropertiesConfiguration(testSavePropertiesFile);
         assertTrue("Configuration not empty", conf2.isEmpty());
     }

Here conf is an instance of PropertiesConfiguration that is initialized 
with a test properties file. testSavePropertiesFile is a File object, 
which points to the target file. Both test cases work for me, so I can't 
reproduce your problem.

Which version of Commons Configuration do you use?

After you have removed the property by calling clearProperty() can you 
check what containsKey() says? And does the isEmpty() method return true 
after you have called clear()?

Oliver

[1] 
http://svn.apache.org/repos/asf/jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestPropertiesConfiguration.java

> 
> -----Urspr√ľngliche Nachricht-----
> Von: Oliver Heger [mailto:oliver.heger@oliver-heger.de]
> Gesendet: Mo 07.08.2006 21:53
> An: Jakarta Commons Users List
> Betreff: Re: Commons Configuration: delete entry
>  
> Christian Koncilia wrote:
>> Hello everybody,
>>
>> I have a question regarding the Commons Configuration. Let's assume I
>> have a configuration file "myprops.properties" that looks like this:
>>
>>> -------------------------
>> # written by PropertiesConfiguration
>> # Mon Aug 07 08:29:12 CEST 2006
>>
>> group1.entry1 = value1
>> group1.entry2 = value2
>> group1.entry3 = value3
>>> -------------------------
>> Now I would like to simply delete an entry. For instance, I would like
>> to remove the whole line "group1.entry2 = value2" from my properties
>> file. How do I do this?
>>
>> Using
>>
>>   PropertiesConfiguration config = 
>>             new PropertiesConfiguration("myprops.properties");
>>   config.clearProperty("group1.entry2 = value2");
>>   config.save();
>>
>> doesn't work. 
>>
>> Thanks for your help!
>>
>> Bye
>>     Christian
>>
> You only need to pass in the key of the property you want to delete to 
> clearProperty(), so the line in question should run:
> 
> config.clearProperty("group1.entry2");
> 
> HTH
> Oliver
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
> 
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Mime
View raw message