From 21be93a1b192eadc6e878f5c698f6204e79e9594 Mon Sep 17 00:00:00 2001 From: md_5 Date: Tue, 1 Jul 2014 13:40:17 +1000 Subject: [PATCH] [#1094] Remove config values set to null, add unit test for such behaviour. --- .../net/md_5/bungee/config/Configuration.java | 8 +++++- .../bungee/config/YamlConfigurationTest.java | 26 ++++++++++++++++--- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/config/src/main/java/net/md_5/bungee/config/Configuration.java b/config/src/main/java/net/md_5/bungee/config/Configuration.java index 810cb1a53..5b1f3184c 100644 --- a/config/src/main/java/net/md_5/bungee/config/Configuration.java +++ b/config/src/main/java/net/md_5/bungee/config/Configuration.java @@ -79,7 +79,13 @@ public final class Configuration Configuration section = getSectionFor( path ); if ( section == this ) { - self.put( path, value ); + if ( value == null ) + { + self.remove( path ); + } else + { + self.put( path, value ); + } } else { section.set( getChild( path ), value ); diff --git a/config/src/test/java/net/md_5/bungee/config/YamlConfigurationTest.java b/config/src/test/java/net/md_5/bungee/config/YamlConfigurationTest.java index 7a9de2be1..8f15168ff 100644 --- a/config/src/test/java/net/md_5/bungee/config/YamlConfigurationTest.java +++ b/config/src/test/java/net/md_5/bungee/config/YamlConfigurationTest.java @@ -1,5 +1,7 @@ package net.md_5.bungee.config; +import java.io.StringReader; +import java.io.StringWriter; import java.util.List; import java.util.Map; import org.junit.Assert; @@ -8,7 +10,7 @@ import org.junit.Test; public class YamlConfigurationTest { - private String docuement = "" + private String document = "" + "receipt: Oz-Ware Purchase Invoice\n" + "date: 2012-08-06\n" + "customer:\n" @@ -43,10 +45,24 @@ public class YamlConfigurationTest + " man behind the curtain."; @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( "date", "2012-08-06", conf.get( "date" ).toString() ); @@ -57,5 +73,9 @@ public class YamlConfigurationTest List items = conf.getList( "items" ); Map item = (Map) items.get( 0 ); 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" ) ); } }