From 748933c5442ff13c9cca12bcdd88f64a209924ff Mon Sep 17 00:00:00 2001 From: Konrad Windszus Date: Sun, 11 Sep 2022 12:50:13 +0200 Subject: [PATCH] Xpp3DomUtils#mergeIntoXpp3Dom() must not override the dominant value in case it is blank (#212) This fixes #212 and closes #213 --- .../plexus/util/xml/Xpp3DomUtils.java | 2 +- .../plexus/util/xml/Xpp3DomUtilsTest.java | 26 ++++++++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/codehaus/plexus/util/xml/Xpp3DomUtils.java b/src/main/java/org/codehaus/plexus/util/xml/Xpp3DomUtils.java index d4909290..8efa7de5 100644 --- a/src/main/java/org/codehaus/plexus/util/xml/Xpp3DomUtils.java +++ b/src/main/java/org/codehaus/plexus/util/xml/Xpp3DomUtils.java @@ -290,6 +290,6 @@ public static boolean isNotEmpty( String str ) @Deprecated public static boolean isEmpty( String str ) { - return ( str == null || str.trim().length() == 0 ); + return ( str == null || str.length() == 0 ); } } diff --git a/src/test/java/org/codehaus/plexus/util/xml/Xpp3DomUtilsTest.java b/src/test/java/org/codehaus/plexus/util/xml/Xpp3DomUtilsTest.java index 548c93e3..9d1e21e6 100644 --- a/src/test/java/org/codehaus/plexus/util/xml/Xpp3DomUtilsTest.java +++ b/src/test/java/org/codehaus/plexus/util/xml/Xpp3DomUtilsTest.java @@ -18,9 +18,11 @@ import static org.junit.Assert.assertEquals; +import java.io.IOException; import java.io.StringReader; import org.codehaus.plexus.util.xml.pull.XmlPullParser; +import org.codehaus.plexus.util.xml.pull.XmlPullParserException; import org.junit.Test; /** @@ -111,7 +113,29 @@ public void testCombineKeys() assertEquals( "RHS", mergeResult.getChildren( "property" )[2].getChild( "value" ).getValue() ); assertEquals( "right", p2.getChild( "value" ).getInputLocation() ); } - + + @Test + public void testOverwriteDominantBlankValue() throws XmlPullParserException, IOException { + String lhs = " "; + + String rhs = "recessive"; + + Xpp3Dom leftDom = Xpp3DomBuilder.build( new StringReader( lhs ), new FixedInputLocationBuilder( "left" ) ); + Xpp3Dom rightDom = Xpp3DomBuilder.build( new StringReader( rhs ), new FixedInputLocationBuilder( "right" ) ); + + Xpp3Dom mergeResult = Xpp3DomUtils.mergeXpp3Dom( leftDom, rightDom, true ); + assertEquals( " ", mergeResult.getValue() ); + } + + @Test + public void testIsNotEmptyNegatesIsEmpty() + { + assertEquals( !Xpp3DomUtils.isEmpty( null ), Xpp3DomUtils.isNotEmpty( null ) ); + assertEquals( !Xpp3DomUtils.isEmpty( "" ), Xpp3DomUtils.isNotEmpty( "" ) ); + assertEquals( !Xpp3DomUtils.isEmpty( " " ), Xpp3DomUtils.isNotEmpty( " " ) ); + assertEquals( !Xpp3DomUtils.isEmpty( "someValue" ), Xpp3DomUtils.isNotEmpty( "someValue" ) ); + } + private static class FixedInputLocationBuilder implements Xpp3DomBuilder.InputLocationBuilder {