From c6a2b740890a9eab9e86bdc26cce319428c755f4 Mon Sep 17 00:00:00 2001 From: Tako Schotanus Date: Mon, 13 Dec 2021 11:37:47 +0100 Subject: [PATCH] feat: Implemented `remove()` --- .../org/codejive/properties/Properties.java | 35 ++++++++-- .../codejive/properties/TestProperties.java | 67 +++++++++++++++++++ src/test/resources/test-clear.properties | 0 src/test/resources/test-removeall.properties | 4 ++ .../resources/test-removefirst.properties | 15 +++++ src/test/resources/test-removelast.properties | 16 +++++ .../resources/test-removemiddle.properties | 13 ++++ 7 files changed, 145 insertions(+), 5 deletions(-) create mode 100644 src/test/resources/test-clear.properties create mode 100644 src/test/resources/test-removeall.properties create mode 100644 src/test/resources/test-removefirst.properties create mode 100644 src/test/resources/test-removelast.properties create mode 100644 src/test/resources/test-removemiddle.properties diff --git a/src/main/java/org/codejive/properties/Properties.java b/src/main/java/org/codejive/properties/Properties.java index 50b27c5..d21268b 100644 --- a/src/main/java/org/codejive/properties/Properties.java +++ b/src/main/java/org/codejive/properties/Properties.java @@ -210,7 +210,9 @@ public Set> entrySet() { @Override public Iterator> iterator() { return new Iterator>() { - final Iterator> iter = values.entrySet().iterator(); + private final Iterator> iter = + values.entrySet().iterator(); + private Entry currentEntry; @Override public boolean hasNext() { @@ -219,12 +221,14 @@ public boolean hasNext() { @Override public Entry next() { - return iter.next(); + return (currentEntry = iter.next()); } @Override public void remove() { - // TODO handle remove + if (currentEntry != null) { + removeItem(currentEntry.getKey()); + } iter.remove(); } }; @@ -382,8 +386,29 @@ private Cursor addNewKeyValue(String rawKey, String key, String rawValue, String @Override public String remove(Object key) { - // TODO handle remove - return values.remove(key); + String skey = key.toString(); + removeItem(skey); + return values.remove(skey); + } + + private void removeItem(String skey) { + setComment(skey, Collections.emptyList()); + Cursor pos = indexOf(skey); + assert pos.isType(PropertiesParser.Type.KEY); + pos.remove(); + assert pos.isType(PropertiesParser.Type.SEPARATOR); + pos.remove(); + assert pos.isType(PropertiesParser.Type.VALUE); + pos.remove(); + if (pos.isEol()) { + pos.remove(); + } + } + + @Override + public void clear() { + tokens.clear(); + values.clear(); } /** diff --git a/src/test/java/org/codejive/properties/TestProperties.java b/src/test/java/org/codejive/properties/TestProperties.java index 13b4ff8..02317ad 100644 --- a/src/test/java/org/codejive/properties/TestProperties.java +++ b/src/test/java/org/codejive/properties/TestProperties.java @@ -9,6 +9,7 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.util.Collections; +import java.util.Iterator; import org.junit.jupiter.api.Test; public class TestProperties { @@ -314,6 +315,72 @@ void testPutFirstWithHeader() throws IOException, URISyntaxException { } } + @Test + void testRemoveFirst() throws IOException, URISyntaxException { + Properties p = Properties.loadProperties(getResource("/test.properties")); + p.remove("one"); + StringWriter sw = new StringWriter(); + p.store(sw); + assertThat(sw.toString(), equalTo(readAll(getResource("/test-removefirst.properties")))); + } + + @Test + void testRemoveMiddle() throws IOException, URISyntaxException { + Properties p = Properties.loadProperties(getResource("/test.properties")); + p.remove("three"); + StringWriter sw = new StringWriter(); + p.store(sw); + assertThat(sw.toString(), equalTo(readAll(getResource("/test-removemiddle.properties")))); + } + + @Test + void testRemoveLast() throws IOException, URISyntaxException { + Properties p = Properties.loadProperties(getResource("/test.properties")); + p.remove("key.4"); + StringWriter sw = new StringWriter(); + p.store(sw); + assertThat(sw.toString(), equalTo(readAll(getResource("/test-removelast.properties")))); + } + + @Test + void testRemoveAll() throws IOException, URISyntaxException { + Properties p = Properties.loadProperties(getResource("/test.properties")); + p.remove("one"); + p.remove("two"); + p.remove("three"); + p.remove(" with spaces"); + p.remove("altsep"); + p.remove("multiline"); + p.remove("key.4"); + StringWriter sw = new StringWriter(); + p.store(sw); + assertThat(sw.toString(), equalTo(readAll(getResource("/test-removeall.properties")))); + } + + @Test + void testRemoveMiddleIterator() throws IOException, URISyntaxException { + Properties p = Properties.loadProperties(getResource("/test.properties")); + Iterator iter = p.keySet().iterator(); + while (iter.hasNext()) { + if (iter.next().equals("three")) { + iter.remove(); + break; + } + } + StringWriter sw = new StringWriter(); + p.store(sw); + assertThat(sw.toString(), equalTo(readAll(getResource("/test-removemiddle.properties")))); + } + + @Test + void testClear() throws IOException, URISyntaxException { + Properties p = Properties.loadProperties(getResource("/test.properties")); + p.clear(); + StringWriter sw = new StringWriter(); + p.store(sw); + assertThat(sw.toString(), equalTo(readAll(getResource("/test-clear.properties")))); + } + @Test void testRemoveComment() throws IOException, URISyntaxException { Properties p = Properties.loadProperties(getResource("/test.properties")); diff --git a/src/test/resources/test-clear.properties b/src/test/resources/test-clear.properties new file mode 100644 index 0000000..e69de29 diff --git a/src/test/resources/test-removeall.properties b/src/test/resources/test-removeall.properties new file mode 100644 index 0000000..613f145 --- /dev/null +++ b/src/test/resources/test-removeall.properties @@ -0,0 +1,4 @@ +#comment1 +# comment2 + +# final comment diff --git a/src/test/resources/test-removefirst.properties b/src/test/resources/test-removefirst.properties new file mode 100644 index 0000000..88d9407 --- /dev/null +++ b/src/test/resources/test-removefirst.properties @@ -0,0 +1,15 @@ +#comment1 +# comment2 + +two=value containing spaces +# another comment +! and a comment +! block +three=and escapes\n\t\r\f +\ with\ spaces = everywhere +altsep:value +multiline = one \ + two \ + three +key.4 = \u1234 +# final comment diff --git a/src/test/resources/test-removelast.properties b/src/test/resources/test-removelast.properties new file mode 100644 index 0000000..0f4cd11 --- /dev/null +++ b/src/test/resources/test-removelast.properties @@ -0,0 +1,16 @@ +#comment1 +# comment2 + +! comment3 +one=simple +two=value containing spaces +# another comment +! and a comment +! block +three=and escapes\n\t\r\f +\ with\ spaces = everywhere +altsep:value +multiline = one \ + two \ + three +# final comment diff --git a/src/test/resources/test-removemiddle.properties b/src/test/resources/test-removemiddle.properties new file mode 100644 index 0000000..aaa6461 --- /dev/null +++ b/src/test/resources/test-removemiddle.properties @@ -0,0 +1,13 @@ +#comment1 +# comment2 + +! comment3 +one=simple +two=value containing spaces +\ with\ spaces = everywhere +altsep:value +multiline = one \ + two \ + three +key.4 = \u1234 +# final comment