diff --git a/README.md b/README.md index 9435106b2..d0e5545a7 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,7 @@ This project was the following modules: * Added new methods `EmailAddress.createOnDemand` * `URLHelper.urlEncode` and `URLHelper.urlDecode` now uses `URLCodec` * Deprecated `ICommonsIterable.forEach` in favour of `findAll` + * Fixed a bug in the cloning of `MapBasedNamespaceContext` (see [issue #17](https://github.com/phax/ph-commons/issues/17)) * v9.3.9 - 2019-12-11 * Made `ClassLoaderHelper.getResource` more robust * Updated "mime-type-info.xml" list with shared-mime-info-spec 1.15 diff --git a/ph-xml/src/main/java/com/helger/xml/namespace/MapBasedNamespaceContext.java b/ph-xml/src/main/java/com/helger/xml/namespace/MapBasedNamespaceContext.java index 1fc7fb696..a5acddead 100644 --- a/ph-xml/src/main/java/com/helger/xml/namespace/MapBasedNamespaceContext.java +++ b/ph-xml/src/main/java/com/helger/xml/namespace/MapBasedNamespaceContext.java @@ -70,7 +70,10 @@ public MapBasedNamespaceContext (@Nullable final MapBasedNamespaceContext aOther { m_sDefaultNamespaceURI = aOther.m_sDefaultNamespaceURI; m_aPrefix2NS.putAll (aOther.m_aPrefix2NS); - m_aNS2Prefix.putAll (aOther.m_aNS2Prefix); + + // putAll is not enough here + for (final Map.Entry > aEntry : aOther.m_aNS2Prefix.entrySet ()) + m_aNS2Prefix.put (aEntry.getKey (), aEntry.getValue ().getClone ()); } } diff --git a/ph-xml/src/test/java/com/helger/xml/namespace/MapBasedNamespaceContextTest.java b/ph-xml/src/test/java/com/helger/xml/namespace/MapBasedNamespaceContextTest.java index 2d717ea53..fc2646af0 100644 --- a/ph-xml/src/test/java/com/helger/xml/namespace/MapBasedNamespaceContextTest.java +++ b/ph-xml/src/test/java/com/helger/xml/namespace/MapBasedNamespaceContextTest.java @@ -20,6 +20,7 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; import javax.xml.XMLConstants; @@ -27,6 +28,7 @@ import org.junit.Test; import com.helger.commons.mock.CommonsTestHelper; +import com.helger.commons.state.EChange; /** * Test class for class {@link MapBasedNamespaceContext}. @@ -91,4 +93,19 @@ public void testAll () CommonsTestHelper.testToStringImplementation (c); CommonsTestHelper.testDefaultSerialization (c); } + + @Test + public void testCloneIssue17 () + { + final MapBasedNamespaceContext aNSCtx = new MapBasedNamespaceContext (); + aNSCtx.addMapping ("p1", "urn:example1"); + aNSCtx.addMapping ("p2", "urn:example2"); + final MapBasedNamespaceContext aNSCtx2 = aNSCtx.getClone (); + // Remove from original + assertSame (EChange.CHANGED, aNSCtx.removeMapping ("p1")); + assertSame (EChange.UNCHANGED, aNSCtx.removeMapping ("p1")); + // Remove from clone + assertSame (EChange.CHANGED, aNSCtx2.removeMapping ("p1")); + assertSame (EChange.UNCHANGED, aNSCtx2.removeMapping ("p1")); + } }