From f59447dd4ff348b0d232934aef88dfb86951ca83 Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Thu, 13 Jul 2023 09:46:50 -0400 Subject: [PATCH] Must override toString() for Java 17 --- .../properties/OrderedProperties.java | 25 +++++++++++++++++++ .../properties/OrderedPropertiesTest.java | 1 - 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/apache/commons/collections4/properties/OrderedProperties.java b/src/main/java/org/apache/commons/collections4/properties/OrderedProperties.java index 4cf0b9c6c7..ffa9570bdc 100644 --- a/src/main/java/org/apache/commons/collections4/properties/OrderedProperties.java +++ b/src/main/java/org/apache/commons/collections4/properties/OrderedProperties.java @@ -19,6 +19,7 @@ import java.util.AbstractMap.SimpleEntry; import java.util.Collections; import java.util.Enumeration; +import java.util.Iterator; import java.util.LinkedHashSet; import java.util.Map; import java.util.Objects; @@ -145,4 +146,28 @@ public synchronized boolean remove(final Object key, final Object value) { } return remove; } + + @Override + public synchronized String toString() { + // Must override for Java 17 to maintain order since the implementation is based on a map + final int max = size() - 1; + if (max == -1) { + return "{}"; + } + final StringBuilder sb = new StringBuilder(); + final Iterator> it = entrySet().iterator(); + sb.append('{'); + for (int i = 0;; i++) { + final Map.Entry e = it.next(); + final Object key = e.getKey(); + final Object value = e.getValue(); + sb.append(key == this ? "(this Map)" : key.toString()); + sb.append('='); + sb.append(value == this ? "(this Map)" : value.toString()); + if (i == max) { + return sb.append('}').toString(); + } + sb.append(", "); + } + } } diff --git a/src/test/java/org/apache/commons/collections4/properties/OrderedPropertiesTest.java b/src/test/java/org/apache/commons/collections4/properties/OrderedPropertiesTest.java index f0bb17875b..127b3caaaa 100644 --- a/src/test/java/org/apache/commons/collections4/properties/OrderedPropertiesTest.java +++ b/src/test/java/org/apache/commons/collections4/properties/OrderedPropertiesTest.java @@ -288,7 +288,6 @@ public void testRemoveKeyValue() throws FileNotFoundException, IOException { assertFalse(Collections.list(props.propertyNames()).contains(k)); } - @Disabled("Fails on GHA?") @Test public void testToString() { final OrderedProperties orderedProperties = new OrderedProperties();