[#1094] Remove config values set to null, add unit test for such behaviour.

This commit is contained in:
md_5 2014-07-01 13:40:17 +10:00
parent bb69af5cd0
commit 21be93a1b1
2 changed files with 30 additions and 4 deletions

View File

@ -79,7 +79,13 @@ public final class Configuration
Configuration section = getSectionFor( path ); Configuration section = getSectionFor( path );
if ( section == this ) if ( section == this )
{ {
self.put( path, value ); if ( value == null )
{
self.remove( path );
} else
{
self.put( path, value );
}
} else } else
{ {
section.set( getChild( path ), value ); section.set( getChild( path ), value );

View File

@ -1,5 +1,7 @@
package net.md_5.bungee.config; package net.md_5.bungee.config;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.junit.Assert; import org.junit.Assert;
@ -8,7 +10,7 @@ import org.junit.Test;
public class YamlConfigurationTest public class YamlConfigurationTest
{ {
private String docuement = "" private String document = ""
+ "receipt: Oz-Ware Purchase Invoice\n" + "receipt: Oz-Ware Purchase Invoice\n"
+ "date: 2012-08-06\n" + "date: 2012-08-06\n"
+ "customer:\n" + "customer:\n"
@ -43,10 +45,24 @@ public class YamlConfigurationTest
+ " man behind the curtain."; + " man behind the curtain.";
@Test @Test
public void testRead() throws Exception public void testConfig() throws Exception
{ {
Configuration conf = ConfigurationProvider.getProvider( YamlConfiguration.class ).load( docuement ); Configuration conf = ConfigurationProvider.getProvider( YamlConfiguration.class ).load( document );
testSection( conf );
StringWriter sw = new StringWriter();
ConfigurationProvider.getProvider( YamlConfiguration.class ).save( conf, sw );
// Check nulls were saved, see #1094
Assert.assertFalse( "Config contains null", sw.toString().contains( "null" ) );
conf = ConfigurationProvider.getProvider( YamlConfiguration.class ).load( new StringReader( sw.toString() ) );
conf.set( "receipt", "Oz-Ware Purchase Invoice" ); // Add it back
testSection( conf );
}
private void testSection(Configuration conf)
{
Assert.assertEquals( "receipt", "Oz-Ware Purchase Invoice", conf.getString( "receipt" ) ); Assert.assertEquals( "receipt", "Oz-Ware Purchase Invoice", conf.getString( "receipt" ) );
// Assert.assertEquals( "date", "2012-08-06", conf.get( "date" ).toString() ); // Assert.assertEquals( "date", "2012-08-06", conf.get( "date" ).toString() );
@ -57,5 +73,9 @@ public class YamlConfigurationTest
List items = conf.getList( "items" ); List items = conf.getList( "items" );
Map item = (Map) items.get( 0 ); Map item = (Map) items.get( 0 );
Assert.assertEquals( "items[0].part_no", "A4786", item.get( "part_no" ) ); Assert.assertEquals( "items[0].part_no", "A4786", item.get( "part_no" ) );
conf.set( "receipt", null );
Assert.assertEquals( null, conf.get( "receipt" ) );
Assert.assertEquals( "foo", conf.get( "receipt", "foo" ) );
} }
} }